Files
dilib_operations/monitoring/auth.py
T
ms 2c1c48df0c
PostgreSQL integration / tests (push) Has been cancelled
init
2026-11-09 13:15:48 +01:00

17 lines
929 B
Python

import hashlib
from django.utils import timezone
from rest_framework.authentication import BaseAuthentication, get_authorization_header
from rest_framework.exceptions import AuthenticationFailed
from .models import AccessToken
def digest(token): return hashlib.sha256(token.encode()).hexdigest()
class BearerAuthentication(BaseAuthentication):
def authenticate(self,request):
parts=get_authorization_header(request).split()
if not parts or parts[0].lower()!=b'bearer': return None
if len(parts)!=2: raise AuthenticationFailed('Ungültiges Token')
try: token=AccessToken.objects.select_related('user').get(digest=digest(parts[1].decode()),expires_at__gt=timezone.now(),user__is_active=True)
except (AccessToken.DoesNotExist,UnicodeDecodeError): raise AuthenticationFailed('Sitzung abgelaufen')
return token.user,token
def authenticate_header(self,request): return 'Bearer'