openreplay/api/auth/auth_apikey.py
Kraiem Taha Yassine 437fe9533b
Api v1.15.0 (#1580)
* fix(chalice): fixed public API
refactor(chalice): changed path logging
refactor(chalice): refactored public API
2023-10-27 14:35:16 +02:00

31 lines
1.1 KiB
Python

import logging
from typing import Optional
from fastapi import Request
from fastapi.security import APIKeyHeader
from starlette import status
from starlette.exceptions import HTTPException
from chalicelib.core import authorizers
from schemas import CurrentAPIContext
logger = logging.getLogger(__name__)
class APIKeyAuth(APIKeyHeader):
def __init__(self, auto_error: bool = True):
super(APIKeyAuth, self).__init__(name="Authorization", auto_error=auto_error)
async def __call__(self, request: Request) -> Optional[CurrentAPIContext]:
api_key: Optional[str] = await super(APIKeyAuth, self).__call__(request)
r = authorizers.api_key_authorizer(api_key)
if r is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid API Key",
)
r["authorizer_identity"] = "api_key"
logger.debug(r)
request.state.authorizer_identity = "api_key"
request.state.currentContext = CurrentAPIContext(tenantId=r["tenantId"])
return request.state.currentContext