File size: 736 Bytes
b36cb8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
"""
Security service for API authentication and authorization
"""
from fastapi import Depends, HTTPException
from fastapi.security import APIKeyHeader
class SecurityService:
"""Class for handling API security"""
def __init__(self, api_key: str):
self.api_key = api_key
self.api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
async def verify_api_key(self, api_key: str = Depends(APIKeyHeader(name="X-API-Key", auto_error=False))):
"""Verify API key dependency"""
if self.api_key != "your-api-key-here" and api_key != self.api_key: # Skip check if using default key
raise HTTPException(status_code=401, detail="Invalid API key")
return api_key
|