Django Channels JWT
Django Channels JWT Middleware is a secure way to handle authentication for Django Channels WebSocket connections without directly exposing JWT tokens. Instead of sending the token itself in the query parameter, this middleware uses UUID-based authentication and cache-based user retrieval for enhanced security.
Security Benefits
-
Enhanced Security: JWT tokens can potentially be intercepted if sent as query parameters. This middleware avoids sending tokens directly, minimizing the risk of token leakage.
-
UUID-based Authentication: This middleware generates UUIDs as tokens for WebSocket connections. These UUIDs are short-lived and act as temporary access keys. When a user connects, they provide the UUID, which is used to retrieve the authenticated user.
-
Cache-based User Retrieval: Upon connection, the middleware validates the UUID, retrieves the corresponding user ID from the cache, and fetches the user asynchronously. This ensures that the WebSocket connection is only established for authenticated users.
Risks of Sending Tokens as Query Parameters
Sending tokens as query parameters can expose security vulnerabilities:
- Token Exposure: Tokens in query parameters can be captured in logs, browser history, or server logs, increasing the risk of unauthorized access.
- Caching: Some proxies or servers may cache URLs, which could lead to tokens being stored in shared caches.
Installation
Install the package using pip:
pip install django-channels-jwt
Configuration
-
Wrap your URLRouter
from django_channels_jwt.middlware import JwtAuthMiddlewareStack application = ProtocolTypeRouter({ "http": get_asgi_application(), "websocket": JwtAuthMiddlewareStack( URLRouter( websocket_urlpatterns, ) ), })
-
Include the provided URL for ticket generation in your project's
urls.py:
from django.urls import path, include
url_patterns = [
# ... your other URL patterns
path("api/auth/", include('django_channels_jwt.urls')
]
or if you want to set customized route
from django.urls import path, include
from django_channels_jwt.views import AsgiValidateTokenView
url_patterns = [
# ... your other URL patterns
path("auth_for_ws_connection/", AsgiValidateTokenView.as_view())
]
Usage
- Ensure your Django app's models are configured correctly.
- Use the included
AsgiValidateTokenViewto generate a ticket (UUID) for WebSocket connections. - Connect to your WebSocket with the generated UUID to authenticate the connection without exposing the token.
ws://localhost:8001/ws/chat/?uuid=
1.0.0 (2023-08-11)
- First release on PyPI.
Metadata
Release files for django-channels-jwt 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-channels-jwt-0.0.3.tar.gz | 5.4 kB | Without provenance | Details for django-channels-jwt-0.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_channels_jwt-0.0.3-py3-none-any.whl | Python 3 | none | any | Details for django_channels_jwt-0.0.3-py3-none-any.whl |
Total release size: 11.0 kB
Release files / django-channels-jwt-0.0.3.tar.gz
| Download URL | django-channels-jwt-0.0.3.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2f924633c900e1fced52312f53e3cfd5a578a0876a102cbf0131fb0f80ffdd6c
|
|
BLAKE2b-256 checksum How to use checksums |
fb6d557f9da267e43eb1cd9589b555f1a282f5a0671b960df8a503197e6d77a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.0
|
Release files / django_channels_jwt-0.0.3-py3-none-any.whl
| Download URL | django_channels_jwt-0.0.3-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ed11c1e188e69b32631418c23919dc7f69496939b82060783dd935ecd881f931
|
|
BLAKE2b-256 checksum How to use checksums |
369a3d9b8b5a50b65f5c5bbb65bb551d7f9defef83e41c1fac3af75264f0956d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.0
|
Security for django-channels-jwt 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.