Skip to content

Instantly share code, notes, and snippets.

View alex-pobeditel-2004's full-sized avatar

alex-pobeditel-2004

  • Mother Russia
View GitHub Profile
@dmwyatt
dmwyatt / channels_middleware.py
Last active August 7, 2021 16:03
[django-channels subprotocol auth middleware] Retrieves JWT from the websocket's subprotocol and retreives the appropriate user. #jwt #auth #django #websocket
import logging
from typing import Awaitable, Final, List, TYPE_CHECKING, TypedDict
from channels.auth import AuthMiddlewareStack
from channels.db import database_sync_to_async
from django.contrib.auth.models import AnonymousUser
from rest_framework.exceptions import AuthenticationFailed
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework_jwt.blacklist.exceptions import MissingToken