导包问题优化

pull/93/head
李强 2023-04-08 20:29:23 +08:00
parent 3a89182874
commit e98032200e
1 changed files with 7 additions and 7 deletions

View File

@ -13,27 +13,23 @@ import jwt
from channels.db import database_sync_to_async from channels.db import database_sync_to_async
from channels.middleware import BaseMiddleware from channels.middleware import BaseMiddleware
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.core.asgi import get_asgi_application from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter from channels.routing import ProtocolTypeRouter, URLRouter
from django.db import close_old_connections from django.db import close_old_connections
from rest_framework_simplejwt.authentication import AUTH_HEADER_TYPE_BYTES
from rest_framework_simplejwt.exceptions import InvalidToken, TokenError
from rest_framework_simplejwt.tokens import UntypedToken
from dvadmin.system.models import Users
from application import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
@database_sync_to_async @database_sync_to_async
def get_user(validated_token): def get_user(validated_token):
from dvadmin.system.models import Users
try: try:
user = get_user_model().objects.get(id=validated_token["user_id"]) user = get_user_model().objects.get(id=validated_token["user_id"])
# return get_user_model().objects.get(id=toke_id) # return get_user_model().objects.get(id=toke_id)
return user return user
except Users.DoesNotExist: except Users.DoesNotExist:
from django.contrib.auth.models import AnonymousUser
return AnonymousUser() return AnonymousUser()
@ -43,6 +39,9 @@ class JwtAuthMiddleware(BaseMiddleware):
async def __call__(self, scope, receive, send): async def __call__(self, scope, receive, send):
# Close old database connections to prevent usage of timed out connections # Close old database connections to prevent usage of timed out connections
from rest_framework_simplejwt.authentication import AUTH_HEADER_TYPE_BYTES
from rest_framework_simplejwt.exceptions import InvalidToken, TokenError
from rest_framework_simplejwt.tokens import UntypedToken
close_old_connections() close_old_connections()
parts = dict(scope['headers']).get(b'authorization', b'').split() parts = dict(scope['headers']).get(b'authorization', b'').split()
print("parts",scope) print("parts",scope)
@ -69,6 +68,7 @@ class JwtAuthMiddleware(BaseMiddleware):
return None return None
else: else:
# Then token is valid, decode it # Then token is valid, decode it
from application import settings
decoded_data = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"]) decoded_data = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
print(decoded_data) print(decoded_data)
# Will return a dictionary like - # Will return a dictionary like -