Files
trade/app/auth.py
T

29 строки
1.0 KiB
Python

import secrets
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from app.core import get_settings
security = HTTPBasic(auto_error=False)
def require_auth(credentials: HTTPBasicCredentials | None = Depends(security)) -> str:
settings = get_settings()
if not settings.auth_enabled:
return 'auth-disabled'
if credentials is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail='Authentication required',
headers={'WWW-Authenticate': 'Basic'},
)
username_ok = secrets.compare_digest(credentials.username, settings.admin_username)
password_ok = secrets.compare_digest(credentials.password, settings.admin_password)
if not username_ok or not password_ok:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail='Invalid username or password',
headers={'WWW-Authenticate': 'Basic'},
)
return credentials.username