feat: 优化ws 认证

This commit is contained in:
caoqianming 2023-09-01 09:11:43 +08:00
parent 16490c5907
commit d5386ccd84
1 changed files with 6 additions and 3 deletions

View File

@ -15,6 +15,9 @@ class TokenAuthMiddleware:
# checking if it is a valid user ID, or if scope["user"] is already
# populated).
from urllib.parse import parse_qs
token = parse_qs(str(scope["query_string"], 'UTF-8'))['token'][0]
scope['user'] = await _get_user(token)
return await self.app(scope, receive, send)
token = parse_qs(str(scope["query_string"], 'UTF-8')).get('token', [None])[0]
if token:
user = await _get_user(token)
if user:
scope['user'] = user
return await self.app(scope, receive, send)