aboutsummaryrefslogtreecommitdiffstats
path: root/backend/authentication
diff options
context:
space:
mode:
Diffstat (limited to 'backend/authentication')
-rw-r--r--backend/authentication/backend.py11
-rw-r--r--backend/authentication/user.py5
2 files changed, 7 insertions, 9 deletions
diff --git a/backend/authentication/backend.py b/backend/authentication/backend.py
index 38668eb..f1d2ece 100644
--- a/backend/authentication/backend.py
+++ b/backend/authentication/backend.py
@@ -1,6 +1,5 @@
import jwt
import typing as t
-from abc import ABC
from starlette import authentication
from starlette.requests import Request
@@ -10,11 +9,11 @@ from backend import constants
from .user import User
-class JWTAuthenticationBackend(authentication.AuthenticationBackend, ABC):
+class JWTAuthenticationBackend(authentication.AuthenticationBackend):
"""Custom Starlette authentication backend for JWT."""
@staticmethod
- def get_token_from_header(header: str) -> t.Optional[str]:
+ def get_token_from_header(header: str) -> str:
"""Parse JWT token from header value."""
try:
prefix, token = header.split()
@@ -32,10 +31,10 @@ class JWTAuthenticationBackend(authentication.AuthenticationBackend, ABC):
async def authenticate(
self, request: Request
- ) -> t.Optional[t.Tuple[authentication.AuthCredentials, authentication.BaseUser]]:
+ ) -> t.Optional[tuple[authentication.AuthCredentials, authentication.BaseUser]]:
"""Handles JWT authentication process."""
if "Authorization" not in request.headers:
- return
+ return None
auth = request.headers["Authorization"]
token = self.get_token_from_header(auth)
@@ -47,7 +46,7 @@ class JWTAuthenticationBackend(authentication.AuthenticationBackend, ABC):
scopes = ["authenticated"]
- if payload.get("admin", False) is True:
+ if payload.get("admin") is True:
scopes.append("admin")
return authentication.AuthCredentials(scopes), User(token, payload)
diff --git a/backend/authentication/user.py b/backend/authentication/user.py
index afa243f..722c348 100644
--- a/backend/authentication/user.py
+++ b/backend/authentication/user.py
@@ -1,13 +1,12 @@
import typing as t
-from abc import ABC
from starlette.authentication import BaseUser
-class User(BaseUser, ABC):
+class User(BaseUser):
"""Starlette BaseUser implementation for JWT authentication."""
- def __init__(self, token: str, payload: t.Dict) -> None:
+ def __init__(self, token: str, payload: dict[str, t.Any]) -> None:
self.token = token
self.payload = payload