21 lines
649 B
Python
21 lines
649 B
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Optional
|
|
|
|
from fastapi import Header, HTTPException
|
|
|
|
|
|
def get_api_token() -> str:
|
|
return os.getenv("BOT_API_TOKEN", "dev-token-change-me")
|
|
|
|
|
|
def require_bearer(authorization: Optional[str] = Header(default=None)) -> str:
|
|
expected = get_api_token()
|
|
if not authorization or not authorization.lower().startswith("bearer "):
|
|
raise HTTPException(status_code=401, detail="Authorization Bearer requerido")
|
|
token = authorization.split(" ", 1)[1].strip()
|
|
if token != expected:
|
|
raise HTTPException(status_code=401, detail="Token inválido")
|
|
return token
|