|
| 1 | +from urllib.parse import parse_qs |
| 2 | + |
1 | 3 | import jwt |
2 | 4 | from channels.db import database_sync_to_async |
3 | 5 | from django.conf import settings |
@@ -103,24 +105,23 @@ def __init__(self, app): |
103 | 105 | self.app = app |
104 | 106 |
|
105 | 107 | 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 |
116 | 109 |
|
| 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] |
117 | 118 | if token is None: |
118 | 119 | raise ValueError("Token is missing from headers") |
119 | 120 |
|
120 | 121 | scope["token"] = token |
121 | 122 | 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 |
124 | 125 | from django.contrib.auth.models import AnonymousUser |
125 | 126 |
|
126 | 127 | scope["user"] = AnonymousUser() |
|
0 commit comments