FastAPI / services /security_service.py
ravi19's picture
Deploy FastAPI to HF Space
b36cb8b
raw
history blame
736 Bytes
"""
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