mirror of
https://github.com/fusion44/blitz_api.git
synced 2026-08-13 11:52:45 +02:00
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from fastapi import HTTPException, Request, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
|
|
from .auth_handler import decodeJWT
|
|
|
|
# https://testdriven.io/blog/fastapi-jwt-auth/
|
|
|
|
|
|
class JWTBearer(HTTPBearer):
|
|
def __init__(self, auto_error: bool = True):
|
|
super(JWTBearer, self).__init__(auto_error=auto_error)
|
|
|
|
async def __call__(self, request: Request):
|
|
credentials: HTTPAuthorizationCredentials = await super(
|
|
JWTBearer, self
|
|
).__call__(request)
|
|
if credentials:
|
|
if not credentials.scheme == "Bearer":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid authentication scheme.",
|
|
)
|
|
if not self.verify_jwt(credentials.credentials):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid token or expired token.",
|
|
)
|
|
return credentials.credentials
|
|
else:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid authorization code.",
|
|
)
|
|
|
|
def verify_jwt(self, jwtoken: str) -> bool:
|
|
isTokenValid: bool = False
|
|
|
|
try:
|
|
payload = decodeJWT(jwtoken)
|
|
except: # noqa: E722
|
|
payload = None
|
|
|
|
if payload:
|
|
isTokenValid = True
|
|
|
|
return isTokenValid
|