From 0833eb13cea5775fd0d9e1063e0d359b361c9cec Mon Sep 17 00:00:00 2001 From: lizerk Date: Tue, 31 Mar 2026 20:28:18 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D0=B0=D0=B4=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=80=D1=82=D0=B0=20ASGI/JWT:=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D1=80=D0=BE=D0=B8=D0=BB=20=D0=BF=D0=BE=D1=80?= =?UTF-8?q?=D1=8F=D0=B4=D0=BE=D0=BA=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20Django=20=D0=B2=20?= =?UTF-8?q?ASGI=20=D0=B8=20=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=BD=D0=B8=D0=B5=20Django/SimpleJWT=20=D0=B8=D0=BC?= =?UTF-8?q?=D0=BF=D0=BE=D1=80=D1=82=D1=8B=20=D0=B8=D0=B7=20middleware.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/eusrr_backend/asgi.py | 18 +++++++++++++----- backend/eusrr_backend/channels_jwt.py | 12 ++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/backend/eusrr_backend/asgi.py b/backend/eusrr_backend/asgi.py index 68c97774..6f51fbfc 100644 --- a/backend/eusrr_backend/asgi.py +++ b/backend/eusrr_backend/asgi.py @@ -1,13 +1,21 @@ -from eusrr_backend.channels_jwt import JWTAuthMiddleware -from realtime.routing import websocket_urlpatterns -from channels.routing import ProtocolTypeRouter, URLRouter -from channels.auth import AuthMiddlewareStack -from django.core.asgi import get_asgi_application import os +from importlib import import_module + +from channels.auth import AuthMiddlewareStack +from channels.routing import ProtocolTypeRouter, URLRouter +from django.core.asgi import get_asgi_application + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eusrr_backend.settings") django_asgi_app = get_asgi_application() +JWTAuthMiddleware = import_module( + "eusrr_backend.channels_jwt" +).JWTAuthMiddleware +websocket_urlpatterns = import_module( + "realtime.routing" +).websocket_urlpatterns + application = ProtocolTypeRouter({ "http": django_asgi_app, diff --git a/backend/eusrr_backend/channels_jwt.py b/backend/eusrr_backend/channels_jwt.py index 5e7998f9..ae945c9c 100644 --- a/backend/eusrr_backend/channels_jwt.py +++ b/backend/eusrr_backend/channels_jwt.py @@ -1,15 +1,11 @@ # eusrr_backend/channels_jwt.py from __future__ import annotations + from typing import Optional from urllib.parse import parse_qs from channels.db import database_sync_to_async from channels.middleware import BaseMiddleware -from django.contrib.auth import get_user_model -from rest_framework_simplejwt.tokens import UntypedToken -from rest_framework_simplejwt.authentication import JWTAuthentication - -User = get_user_model() class JWTAuthMiddleware(BaseMiddleware): @@ -32,7 +28,9 @@ class JWTAuthMiddleware(BaseMiddleware): return None @database_sync_to_async - def _get_user(self, validated_token) -> Optional[User]: + def _get_user(self, validated_token): + from rest_framework_simplejwt.authentication import JWTAuthentication + auth = JWTAuthentication() user = auth.get_user(validated_token) return user @@ -41,6 +39,8 @@ class JWTAuthMiddleware(BaseMiddleware): try: raw = self._get_raw_token(scope) if raw: + from rest_framework_simplejwt.tokens import UntypedToken + token = UntypedToken(raw) # валидация подписи/срока user = await self._get_user(token) scope["user"] = user