This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 0.1.2 instead.
Reason given by maintainers: this version has a bug that cause authenticated users return AttributeError
django-channels-jwt-stateless
Stateless JWT authentication middleware for Django Channels. Zero database queries on WebSocket handshake.
Existing Django Channels JWT packages call User.objects.get() on every
WebSocket connect. Under burst traffic this saturates the connection pool
and causes multi-second stalls. This package verifies the JWT signature
and builds a lightweight HandshakeUser from the token claims instead.
Install
pip install django-channels-jwt-stateless
Quick Start
# asgi.py
from channels.routing import ProtocolTypeRouter, URLRouter
from django_channels_jwt_stateless import JWTAuthMiddlewareStack
application = ProtocolTypeRouter({
"http": django_application,
"websocket": JWTAuthMiddlewareStack(URLRouter(websocket_urlpatterns)),
})
Works out of the box with djangorestframework-simplejwt defaults
(HS256, SECRET_KEY, user_id claim). Pass the token as ?token=<jwt>
in the WebSocket URL.
scope["user"]
On a valid token, scope["user"] is a HandshakeUser instance:
| Attribute | Value |
|---|---|
.id / .pk |
From the JWT user_id claim |
.is_anonymous |
False |
.is_authenticated |
True |
On an invalid, expired, or missing token it is AnonymousUser.
Configuration
Optional. Add to settings.py:
CHANNELS_JWT_STATELESS = {
"ALGORITHM": "HS256", # default
"SIGNING_KEY": None, # defaults to SECRET_KEY
"USER_ID_CLAIM": "user_id", # default
"TOKEN_QUERY_PARAM": "token", # default
"IS_BLACKLISTED": None, # dotted path to (jti) -> bool
"TOKEN_DECODER": None, # dotted path to (token) -> dict
"SUBPROTOCOL_PREFIX": None, # e.g. "access_token"
}
TOKEN_DECODER and IS_BLACKLISTED accept both sync and async callables.
Token Blacklisting
# myapp/auth.py
from django.core.cache import cache
async def is_blacklisted(jti: str) -> bool:
return await cache.aget(f"blacklist:{jti}") is not None
# settings.py
CHANNELS_JWT_STATELESS = {
"IS_BLACKLISTED": "myapp.auth.is_blacklisted",
}
Subprotocol Auth
To avoid leaking tokens in query strings:
CHANNELS_JWT_STATELESS = {"SUBPROTOCOL_PREFIX": "access_token"}
Client sends Sec-WebSocket-Protocol: access_token.<jwt>.
Compatibility
Python 3.10+, Django 4.2+, Channels 4+, PyJWT 2+.
License
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_channels_jwt_stateless-0.1.0.tar.gz.
File metadata
- Download URL: django_channels_jwt_stateless-0.1.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3be7c6a588a67435e5e7218c58b6f732ffd650f9f154b73899125e96f7fef84a
|
|
| MD5 |
a417d9bb1639121d0bfa871cb10f7473
|
|
| BLAKE2b-256 |
5313526b957d35067601ab40c81e8fb920b0e1a273a7e8821e73857faa922c72
|
Provenance
The following attestation bundles were made for django_channels_jwt_stateless-0.1.0.tar.gz:
Publisher:
publish.yml on zxzinn/django-channels-jwt-stateless
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_channels_jwt_stateless-0.1.0.tar.gz -
Subject digest:
3be7c6a588a67435e5e7218c58b6f732ffd650f9f154b73899125e96f7fef84a - Sigstore transparency entry: 2434421017
- Sigstore integration time:
-
Permalink:
zxzinn/django-channels-jwt-stateless@f76a63bb56466b07aa719ad4649007021f7e137e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/zxzinn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f76a63bb56466b07aa719ad4649007021f7e137e -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_channels_jwt_stateless-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_channels_jwt_stateless-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
687049fdd4dc6b967531a6bee1cf037c96b173700b13751c83f73011b018753d
|
|
| MD5 |
4a4445dc01e1cb9852efb3bb65da36c1
|
|
| BLAKE2b-256 |
6fd35ce17750b07825d45a7995dc960de1d6e4708f58039e7f198843663fc58b
|
Provenance
The following attestation bundles were made for django_channels_jwt_stateless-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on zxzinn/django-channels-jwt-stateless
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_channels_jwt_stateless-0.1.0-py3-none-any.whl -
Subject digest:
687049fdd4dc6b967531a6bee1cf037c96b173700b13751c83f73011b018753d - Sigstore transparency entry: 2434421152
- Sigstore integration time:
-
Permalink:
zxzinn/django-channels-jwt-stateless@f76a63bb56466b07aa719ad4649007021f7e137e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/zxzinn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f76a63bb56466b07aa719ad4649007021f7e137e -
Trigger Event:
push
-
Statement type: