-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
40 lines (31 loc) · 1006 Bytes
/
Copy pathauth.py
File metadata and controls
40 lines (31 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
API Key authentication for KCB API
"""
from fastapi import HTTPException, Security, status
from fastapi.security import APIKeyHeader
from config import settings
# API Key header scheme
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
async def verify_api_key(api_key: str = Security(api_key_header)) -> str:
"""
Verify API key from request header
Args:
api_key: API key from X-API-Key header
Returns:
The validated API key
Raises:
HTTPException: If API key is missing or invalid
"""
if api_key is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing API Key",
headers={"WWW-Authenticate": "ApiKey"},
)
if api_key != settings.API_KEY:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid API Key",
headers={"WWW-Authenticate": "ApiKey"},
)
return api_key