togethere.cloud/api/auth.py

23 lines
806 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Uwierzytelnianie przez klucz API przesyłany w nagłówku X-API-Key.
Klucz jest wspólny dla PHP i Pythona przechowuj go w .env.
"""
from __future__ import annotations
from fastapi import Security, HTTPException, status
from fastapi.security.api_key import APIKeyHeader
from .config import settings
_api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
async def require_api_key(api_key: str | None = Security(_api_key_header)) -> str:
"""Dependency: weryfikuje klucz API. Rzuca 403 jeśli klucz jest nieprawidłowy."""
if not api_key or api_key != settings.api_key:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Nieprawidłowy lub brakujący klucz API (X-API-Key)",
)
return api_key