File size: 893 Bytes
a31f556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import secrets as _secrets

from fastapi import HTTPException, Security
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials

from backend.dependencies.auth import get_or_create_master_token

security = HTTPBearer()

async def verify_token(credentials: HTTPAuthorizationCredentials = Security(security)):
    # S4: unified with the middleware token (JARVIS_CLOUD_TOKEN on the Space,
    # persisted file token locally). The old hardcoded fallback lived in a public
    # repo, which made it no auth at all — resolved lazily so env set at boot wins.
    if credentials.scheme != "Bearer":
        raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
    if not _secrets.compare_digest(credentials.credentials, get_or_create_master_token()):
        raise HTTPException(status_code=401, detail="Invalid API Token.")
    return credentials.credentials