fix(auth): Use Optional syntax for Pydantic 1.x compatibility

The require_jwt dependency used Python 3.10+ union syntax
(HTTPAuthorizationCredentials | None) with Annotated, which
causes FastAPI 0.92/Pydantic 1.10 to incorrectly require a
request body on GET endpoints.

Changed to Optional[HTTPAuthorizationCredentials] = Depends(_bearer)
which is compatible with older FastAPI/Pydantic versions.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
CyberMind-FR 2026-04-29 07:24:20 +02:00
parent 1b7669124d
commit 6f72146a4f

View File

@ -7,7 +7,7 @@ secubox_core.auth — JWT HS256 authentication
"""
from __future__ import annotations
import os, time
from typing import Annotated
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
@ -19,6 +19,7 @@ from .logger import get_logger
log = get_logger("auth")
# auto_error=False allows us to handle missing token gracefully
_bearer = HTTPBearer(auto_error=False)
# ── JWT helpers ────────────────────────────────────────────────────
@ -60,7 +61,7 @@ def _decode_token(token: str) -> dict:
async def require_jwt(
creds: Annotated[HTTPAuthorizationCredentials | None, Depends(_bearer)]
creds: Optional[HTTPAuthorizationCredentials] = Depends(_bearer)
) -> dict:
"""Dependency FastAPI — injecter dans tous les endpoints protégés."""
if creds is None: