forked from madisvain/vatcomply
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
26 lines (18 loc) · 948 Bytes
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from starlette.authentication import AuthenticationBackend, AuthCredentials, AuthenticationError, SimpleUser
from settings import TESTING
class TokenAuthenticationBackend(AuthenticationBackend):
""" https://github.com/encode/django-rest-framework/blob/master/rest_framework/authentication.py#L144
"""
keyword = "Token"
async def authenticate(self, request):
if "authorization" not in request.headers:
return
auth = request.headers["authorization"].split()
if not auth or auth[0].lower() != self.keyword.lower():
return
if len(auth) == 1:
raise AuthenticationError("Invalid token header. No credentials provided.")
elif len(auth) > 2:
raise AuthenticationError("Invalid token header. Token string should not contain spaces.")
# TODO: Implement validation
return AuthCredentials(["authenticated"]), SimpleUser("username")