This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2020-09-26 18:11:04 +00:00
|
|
|
"""Channels base classes"""
|
|
|
|
from channels.generic.websocket import JsonWebsocketConsumer
|
|
|
|
from structlog import get_logger
|
|
|
|
|
2020-10-18 12:34:22 +00:00
|
|
|
from passbook.api.auth import token_from_header
|
|
|
|
from passbook.core.models import User
|
2020-09-26 18:11:04 +00:00
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
class AuthJsonConsumer(JsonWebsocketConsumer):
|
|
|
|
"""Authorize a client with a token"""
|
|
|
|
|
|
|
|
user: User
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
headers = dict(self.scope["headers"])
|
|
|
|
if b"authorization" not in headers:
|
|
|
|
LOGGER.warning("WS Request without authorization header")
|
|
|
|
self.close()
|
2020-10-14 08:44:17 +00:00
|
|
|
return False
|
2020-09-26 18:11:04 +00:00
|
|
|
|
2020-10-18 12:34:22 +00:00
|
|
|
raw_header = headers[b"authorization"]
|
|
|
|
|
|
|
|
token = token_from_header(raw_header)
|
|
|
|
if not token:
|
|
|
|
LOGGER.warning("Failed to authenticate")
|
2020-09-26 18:11:04 +00:00
|
|
|
self.close()
|
|
|
|
return False
|
2020-10-18 12:34:22 +00:00
|
|
|
|
|
|
|
self.user = token.user
|
2020-09-26 18:11:04 +00:00
|
|
|
return True
|