You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
570 B

from fastapi import FastAPI, HTTPException, Request
from fastapi.staticfiles import AuthStaticFiles
async def verify_token(request: Request) -> None:
"""Check for a valid Bearer token in the Authorization header."""
token = request.headers.get("Authorization")
if token != "Bearer mysecrettoken":
raise HTTPException(status_code=401, detail="Not authenticated")
app = FastAPI()
# Private files - requires a valid token to access
app.mount(
"/private",
AuthStaticFiles(directory="private_files", auth=verify_token),
name="private",
)