diff --git a/fastapi/security/http.py b/fastapi/security/http.py index b4c3bc6d8..42d39072c 100644 --- a/fastapi/security/http.py +++ b/fastapi/security/http.py @@ -298,10 +298,17 @@ class HTTPBearer(HTTPBase): ), ] = True, ): - self.model = HTTPBearerModel(bearerFormat=bearerFormat, description=description) + if realm is None: + raise ValueError( + "HTTP Basic authentication requires a realm as per RFC 7617" + ) + + self.model = HTTPBaseModel(scheme="basic", description=description) self.scheme_name = scheme_name or self.__class__.__name__ + self.realm = realm self.auto_error = auto_error + async def __call__( self, request: Request ) -> Optional[HTTPAuthorizationCredentials]: diff --git a/tests/test_security_http_basic_optional.py b/tests/test_security_http_basic_optional.py index 7071f381a..d625721e5 100644 --- a/tests/test_security_http_basic_optional.py +++ b/tests/test_security_http_basic_optional.py @@ -75,3 +75,5 @@ def test_openapi_schema(): "securitySchemes": {"HTTPBasic": {"type": "http", "scheme": "basic"}} }, } + +