This repository has been archived on 2023-02-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-my-data/web/token_auth.py
2018-07-23 20:19:47 +02:00

24 lines
671 B
Python

import re
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from web.models import Token
class TokenAuth(BaseAuthentication):
def authenticate(self, request):
auth_hash = request.META.get('HTTP_AUTHORIZATION')
if auth_hash is None:
return None
try:
auth_hash = re.search('Bearer (.*)', auth_hash).group(1)
except AttributeError:
return None
try:
token = Token.objects.get(hash=auth_hash)
except (Token.DoesNotExist):
raise exceptions.AuthenticationFailed('invalid token')
return token.user, token