Created
April 18, 2018 12:42
-
-
Save lzakharov/5ff829160b4ec1064082aa3c2f144bc0 to your computer and use it in GitHub Desktop.
JWT token authorization middleware for Django Channels 2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from rest_framework_jwt.serializers import VerifyJSONWebTokenSerializer | |
| class JwtTokenAuthMiddleware: | |
| """ | |
| JWT token authorization middleware for Django Channels 2 | |
| """ | |
| def __init__(self, inner): | |
| self.inner = inner | |
| def __call__(self, scope): | |
| try: | |
| token_header = dict(scope['headers'])[b'authorization'].decode().split() | |
| data = {'token': token_header[1]} | |
| valid_data = VerifyJSONWebTokenSerializer().validate(data) | |
| user = valid_data['user'] | |
| scope['user'] = user | |
| except: | |
| pass | |
| return self.inner(scope) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found it very simple solution but for some reason it won't work for me. The issue is that inside the try block, when I try to VerifyJSONWebToken.... , it jumps out of the block without any exception or error in the console. I wonder why is it so?