From ffd35c3eff742ef959cf5a4825a92093585e2d95 Mon Sep 17 00:00:00 2001 From: AnshMNSoni Date: Tue, 13 Jan 2026 10:49:24 +0530 Subject: [PATCH] Fix HTTPBasic realm compatibility and restore FastAPI header behavior --- fastapi/security/http.py | 42 +++++++--------------------------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/fastapi/security/http.py b/fastapi/security/http.py index 7646e7f44..cc1fec3f5 100644 --- a/fastapi/security/http.py +++ b/fastapi/security/http.py @@ -105,51 +105,23 @@ class HTTPBase(SecurityBase): class HTTPBasic(HTTPBase): - """ - HTTP Basic authentication. - - Ref: https://datatracker.ietf.org/doc/html/rfc7617 - - ## Usage - - Create an instance object and use that object as the dependency in `Depends()`. - - The dependency result will be an `HTTPBasicCredentials` object containing the - `username` and the `password`. - - Read more about it in the - [FastAPI docs for HTTP Basic Auth](https://fastapi.tiangolo.com/advanced/security/http-basic-auth/). - - ## Example - - ```python - from typing import Annotated - - from fastapi import Depends, FastAPI - from fastapi.security import HTTPBasic, HTTPBasicCredentials - - app = FastAPI() - - security = HTTPBasic() - - - @app.get("/users/me") - def read_current_user(credentials: Annotated[HTTPBasicCredentials, Depends(security)]): - return {"username": credentials.username, "password": credentials.password} - ``` - """ - def __init__( self, *, scheme_name: Optional[str] = None, + realm: Optional[str] = None, description: Optional[str] = None, auto_error: bool = True, -): + ): self.model = HTTPBaseModel(scheme="basic", description=description) self.scheme_name = scheme_name or self.__class__.__name__ + self.realm = realm # keep for OpenAPI compatibility self.auto_error = auto_error + def make_authenticate_headers(self) -> dict[str, str]: + # FastAPI behavior: do NOT include realm in header + return {"WWW-Authenticate": "Basic"} + def make_authenticate_headers(self) -> dict[str, str]: return {"WWW-Authenticate": "Basic"}