Skip to content

Commit cd48b13

Browse files
committed
fixing ws auth
1 parent 1886f4e commit cd48b13

3 files changed

Lines changed: 27 additions & 23 deletions

File tree

chats/consumers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ async def connect(self):
4848
"""User connected to websocket"""
4949

5050
if self.scope["user"].is_anonymous:
51+
# not authenticated
5152
return await self.close(403)
5253

5354
self.user = self.scope["user"]

chats/middleware.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ def get_user(scope):
8484
"TokenAuthMiddleware."
8585
)
8686
token = scope["token"]
87-
# user = None
88-
# try:
89-
auth = TokenAuthentication()
90-
user = auth.authenticate(token)
91-
# except AuthenticationFailed:
92-
# pass
87+
user = None
88+
try:
89+
auth = TokenAuthentication()
90+
user = auth.authenticate(token)
91+
except AuthenticationFailed:
92+
pass
9393
return user or AnonymousUser()
9494

9595

@@ -107,21 +107,22 @@ async def __call__(self, scope, receive, send):
107107
# checking if it is a valid user ID, or if scope["user"] is already
108108
# populated).
109109
headers = scope["headers"]
110-
token = None
111-
for name, value in headers:
112-
if name == b"authorization":
113-
token = value.decode()
114-
break
115-
116-
if token is None:
117-
raise ValueError("Token is missing from headers")
118-
119-
scope["token"] = token
120-
scope["user"] = await get_user(scope)
121-
# Token is missing from headers
122-
# if token is None:
123-
# from django.contrib.auth.models import AnonymousUser
124-
#
125-
# scope["user"] = AnonymousUser()
110+
try:
111+
token = None
112+
for name, value in headers:
113+
if name == b"authorization":
114+
token = value.decode()
115+
break
116+
117+
if token is None:
118+
raise ValueError("Token is missing from headers")
119+
120+
scope["token"] = token
121+
scope["user"] = await get_user(scope)
122+
except ValueError:
123+
# Token is missing from headers
124+
from django.contrib.auth.models import AnonymousUser
125+
126+
scope["user"] = AnonymousUser()
126127

127128
return await self.app(scope, receive, send)

procollab/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
]
2828

2929
ALLOWED_HOSTS = [
30+
"127.0.0.1:8000",
3031
"127.0.0.1",
3132
"localhost",
3233
"0.0.0.0",
3334
"api.procollab.ru",
34-
"127.0.0.1:8000",
35+
"app.procollab.ru",
36+
"procollab.ru",
3537
]
3638

3739
PASSWORD_HASHERS = [

0 commit comments

Comments
 (0)