24 lines
1.0 KiB
Python
24 lines
1.0 KiB
Python
import hmac
|
|
from fastapi import HTTPException, Security, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from app.core.config import settings
|
|
|
|
security = HTTPBearer(auto_error=False)
|
|
|
|
def verify_admin_token(credentials: HTTPAuthorizationCredentials = Security(security)):
|
|
"""Enforces Bearer authentication on sensitive trading and execution endpoints."""
|
|
if not credentials or not credentials.credentials:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Restricted Endpoint: Valid Master Bearer Token required to execute orders.",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
# Constant-time comparison to prevent timing attacks
|
|
if not hmac.compare_digest(credentials.credentials, settings.ADMIN_TOKEN):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Forbidden: Invalid Master Token. Unauthorized access attempt logged.",
|
|
)
|
|
return credentials.credentials
|