Skip to content

Commit 7805987

Browse files
authored
Merge pull request #97 from PROCOLLAB-github/dev
ws now using authentication via query string
2 parents cd48b13 + 247134d commit 7805987

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

chats/middleware.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from urllib.parse import parse_qs
2+
13
import jwt
24
from channels.db import database_sync_to_async
35
from django.conf import settings
@@ -103,24 +105,23 @@ def __init__(self, app):
103105
self.app = app
104106

105107
async def __call__(self, scope, receive, send):
106-
# Look up user from query string (you should also do things like
107-
# checking if it is a valid user ID, or if scope["user"] is already
108-
# populated).
109-
headers = scope["headers"]
110-
try:
111-
token = None
112-
for name, value in headers:
113-
if name == b"authorization":
114-
token = value.decode()
115-
break
108+
# Look up user from query string
116109

110+
# TODO: (you should also do things like
111+
# checking if it is a valid user ID, or if scope["user"] is already
112+
# populated).
113+
114+
query_string = scope["query_string"].decode()
115+
query_dict = parse_qs(query_string)
116+
try:
117+
token = query_dict["token"][0]
117118
if token is None:
118119
raise ValueError("Token is missing from headers")
119120

120121
scope["token"] = token
121122
scope["user"] = await get_user(scope)
122-
except ValueError:
123-
# Token is missing from headers
123+
except (ValueError, KeyError, IndexError):
124+
# Token is missing from query string
124125
from django.contrib.auth.models import AnonymousUser
125126

126127
scope["user"] = AnonymousUser()

0 commit comments

Comments
 (0)