django-channels-jwt-auth-middleware
Custom AuthMiddlewareStack to get users from JWT token for Django Channels.
Installation
Simply,
$ pip install django-channels-jwt-auth-middleware
And that it.
Usage
All you have to do is wrap your URLRouter.
from django_channels_jwt_auth_middleware.auth import JWTAuthMiddlewareStack
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": JWTAuthMiddlewareStack(
URLRouter(
websocket_urlpatterns,
)
),
})
AuthMiddlewareStack is already in JWTAuthMiddlewareStack. If you want to change this, simply do;
from django_channels_jwt_auth_middleware.auth import JWTAuthMiddleware
from . import YourCustomMiddlewareStack
def CustomJWTAuthMiddlewareStack(app):
return JWTAuthMiddleware(YourCustomMiddlewareStack(app))
Simply as that :)
Testing
Testing is done by two methods.
- automated testing using django's test system.
- manual testing by hand.
I have tried testing the middleware through ChannelsLiveServerTestCase, but currently this does not run due to pickle error in multiprocessing package from python.
For this reason no-token test cases were tested using django's test system using a test project, and test cases with jwt token is tested by hand.
Hand testing chrome extension called Browser WebSocket Client was used. B
low is a simple evidence from the hand testing.
If you go to testproject/pages/tests.py you will see the consumer that is used for testing.
class TestConsumer(WebsocketConsumer):
def connect(self):
self.accept()
# def disconnect(self, code):
# self.disconnect()
def receive(self, text_data, bytes_data=None):
context = {
'message': 'testing consumer',
}
user = self.scope['user']
if (user_id := user.id):
context.update({'user': str(user_id)})
else:
context.update({'user': None})
self.send(text_data=json.dumps(context))
simply, is a data is passed in to the websocket it will return a user id of None for AnonymousUser.
1.0.0 (2021-01-20)
- First release on PyPI.
Metadata
Release files for django-channels-jwt-auth-middleware 1.0.0
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 | |
|---|---|---|---|---|
| django-channels-jwt-auth-middleware-1.0.0.tar.gz | 4.8 kB | Without provenance | Details |
Release files / django-channels-jwt-auth-middleware-1.0.0.tar.gz
| Download URL | django-channels-jwt-auth-middleware-1.0.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7fdaaf419ef7f800d0da990d8e0ca53f7d120a66de7fb4446f77607ea3b3dd76
|
|
BLAKE2b-256 checksum How to use checksums |
a738e0ebc367cded21b4ef7d7c484e139aebd6583e8d2dd25d668a908bf42761
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5
|
Security for django-channels-jwt-auth-middleware 1.0.0
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.