37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""Custom authentication based on JWTAuthentication class"""
|
|
from rest_framework import HTTP_HEADER_ENCODING
|
|
from rest_framework_simplejwt.authentication import JWTAuthentication
|
|
from rest_framework_simplejwt.settings import api_settings
|
|
|
|
from utils.methods import get_token_from_cookies
|
|
|
|
AUTH_HEADER_TYPES = api_settings.AUTH_HEADER_TYPES
|
|
|
|
if not isinstance(api_settings.AUTH_HEADER_TYPES, (list, tuple)):
|
|
AUTH_HEADER_TYPES = (AUTH_HEADER_TYPES,)
|
|
|
|
AUTH_HEADER_TYPE_BYTES = set(
|
|
h.encode(HTTP_HEADER_ENCODING)
|
|
for h in AUTH_HEADER_TYPES
|
|
)
|
|
|
|
|
|
class GMJWTAuthentication(JWTAuthentication):
|
|
"""
|
|
An authentication plugin that authenticates requests through a JSON web
|
|
token provided in a request cookies.
|
|
"""
|
|
|
|
def authenticate(self, request):
|
|
token = get_token_from_cookies(request)
|
|
if token is None:
|
|
return None
|
|
|
|
raw_token = self.get_raw_token(token)
|
|
if raw_token is None:
|
|
return None
|
|
|
|
validated_token = self.get_validated_token(raw_token)
|
|
|
|
return self.get_user(validated_token), None
|