JWTAuth: KeyError
Я хочу внедрить JWTAuth в мой проект чата с помощью сокетов и получаю эту ошибку, когда перехожу на страницу чата, сам чат не работает после того, как я изменил auth на мой персонализированный JWT. Подскажите, пожалуйста, что не так?
Ошибки:
HTTP GET /chat/lobby/ 200 [0.01, 127.0.0.1:60197]
WebSocket HANDSHAKING /ws/chat/lobby/ [127.0.0.1:55592]
Exception inside application: 'token'
Traceback (most recent call last):
File "C:\Users\ikoli\PycharmProjects\mychat\venv\lib\site-packages\channels\staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "C:\Users\ikoli\PycharmProjects\mychat\venv\lib\site-packages\channels\routing.py", line 71, in __call__
return await application(scope, receive, send)
File "C:\Users\ikoli\AppData\Local\Programs\Python\Python39\lib\site-packages\asgiref\compatibility.py", line 34, in new_application
instance = application(scope)
File "C:\Users\ikoli\PycharmProjects\mychat\mychat\mychat\channelsmiddleware.py", line 25, in __call__
token = parse_qs(scope["query_string"].decode("utf8"))["token"][0]
KeyError: 'token'
WebSocket DISCONNECT /ws/chat/lobby/ [127.0.0.1:55592]
HTTP GET /chat/lobby/ 200 [0.00, 127.0.0.1:52491]
WebSocket HANDSHAKING /ws/chat/lobby/ [127.0.0.1:63145]
Exception inside application: 'token'
Traceback (most recent call last):
File "C:\Users\ikoli\PycharmProjects\mychat\venv\lib\site-packages\channels\staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "C:\Users\ikoli\PycharmProjects\mychat\venv\lib\site-packages\channels\routing.py", line 71, in __call__
return await application(scope, receive, send)
File "C:\Users\ikoli\AppData\Local\Programs\Python\Python39\lib\site-packages\asgiref\compatibility.py", line 34, in new_application
instance = application(scope)
File "C:\Users\ikoli\PycharmProjects\mychat\mychat\mychat\channelsmiddleware.py", line 25, in __call__
token = parse_qs(scope["query_string"].decode("utf8"))["token"][0]
KeyError: 'token'
WebSocket DISCONNECT /ws/chat/lobby/ [127.0.0.1:63145]
consumers.py:
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.user = self.scope["user"]
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name,
self.user
)
await self.accept()
async def disconnect(self, close_code):
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
async def chat_message(self, event):
message = event['message']
await self.send(text_data=json.dumps({
'message': message
}))
my TokenAuthMiddleware в channelsmiddlleware.py:
class TokenAuthMiddleware:
def __init__(self, inner):
self.inner = inner
def __call__(self, scope):
close_old_connections()
token = parse_qs(scope["query_string"].decode("utf8"))["token"][0]
try:
UntypedToken(token)
except (InvalidToken, TokenError) as e:
print(e)
return None
else:
user = get_user_model().objects.get(id=decoded_data["user_id"])
return self.inner(dict(scope, user=user))
мое приложение в routing.py
application = ProtocolTypeRouter({
'websocket': TokenAuthMiddleware(
URLRouter(
websocket_urlpatterns
)
)
})