__init__
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
"""Shared FastAPI dependencies for accessing and protecting UVM services."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hmac
|
||||
from typing import cast
|
||||
|
||||
from fastapi import Header, HTTPException, Request, status
|
||||
|
||||
from ..app import Application
|
||||
|
||||
|
||||
def get_application(request: Request) -> Application:
|
||||
return cast(Application, request.app.state.uvm_application)
|
||||
|
||||
|
||||
def get_authorized_application(
|
||||
request: Request,
|
||||
x_uvm_token: str | None = Header(default=None),
|
||||
) -> Application:
|
||||
application = get_application(request)
|
||||
expected_token = application.settings.api_token
|
||||
if not expected_token:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="UVM_API_TOKEN is not configured",
|
||||
)
|
||||
if not hmac.compare_digest(x_uvm_token or "", expected_token):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="missing or invalid X-UVM-Token",
|
||||
)
|
||||
return application
|
||||
Reference in New Issue
Block a user