Coverage for auth/auth_apikey.py: 75%
22 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:56 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:56 +0000
1import logging
2from typing import Optional
4from fastapi import Request
5from fastapi.security import APIKeyHeader
6from starlette import status
7from starlette.exceptions import HTTPException
9from chalicelib.core import authorizers
10from schemas import CurrentAPIContext
12logger = logging.getLogger(__name__)
15class APIKeyAuth(APIKeyHeader):
16 def __init__(self, auto_error: bool = True):
17 super(APIKeyAuth, self).__init__(name="Authorization", auto_error=auto_error)
19 async def __call__(self, request: Request) -> Optional[CurrentAPIContext]:
20 api_key: Optional[str] = await super(APIKeyAuth, self).__call__(request)
21 r = authorizers.api_key_authorizer(api_key)
22 if r is None: 22 ↛ 27line 22 didn't jump to line 27 because the condition on line 22 was always true
23 raise HTTPException(
24 status_code=status.HTTP_401_UNAUTHORIZED,
25 detail="Invalid API Key",
26 )
27 r["authorizer_identity"] = "api_key"
28 logger.debug(r)
29 request.state.authorizer_identity = "api_key"
30 request.state.currentContext = CurrentAPIContext(tenantId=r["tenantId"])
31 return request.state.currentContext