Skip to content

Instantly share code, notes, and snippets.

@lzakharov
Created April 18, 2018 12:42
Show Gist options
  • Select an option

  • Save lzakharov/5ff829160b4ec1064082aa3c2f144bc0 to your computer and use it in GitHub Desktop.

Select an option

Save lzakharov/5ff829160b4ec1064082aa3c2f144bc0 to your computer and use it in GitHub Desktop.
JWT token authorization middleware for Django Channels 2.
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)
@Noman1023
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment