Browse Source

Revert "Fix HTTP Basic realm handling (RFC 7617)"

This reverts commit e8c08fc843.
pull/14703/head
Yurii Motov 6 months ago
parent
commit
cb83133c4e
  1. 11
      fastapi/security/http.py
  2. 18
      tests/test_security_http_base.py

11
fastapi/security/http.py

@ -152,7 +152,14 @@ class HTTPBasic(HTTPBase):
""" """
), ),
] = None, ] = None,
realm: Annotated[str, Doc("HTTP Basic authentication realm.")] = "fastapi", realm: Annotated[
Optional[str],
Doc(
"""
HTTP Basic authentication realm.
"""
),
] = None,
description: Annotated[ description: Annotated[
Optional[str], Optional[str],
Doc( Doc(
@ -190,7 +197,9 @@ class HTTPBasic(HTTPBase):
self.auto_error = auto_error self.auto_error = auto_error
def make_authenticate_headers(self) -> dict[str, str]: def make_authenticate_headers(self) -> dict[str, str]:
if self.realm:
return {"WWW-Authenticate": f'Basic realm="{self.realm}"'} return {"WWW-Authenticate": f'Basic realm="{self.realm}"'}
return {"WWW-Authenticate": "Basic"}
async def __call__( # type: ignore async def __call__( # type: ignore
self, request: Request self, request: Request

18
tests/test_security_http_base.py

@ -1,5 +1,4 @@
from fastapi import Depends, FastAPI, Security from fastapi import FastAPI, Security
from fastapi.security import HTTPBasic
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
@ -54,18 +53,3 @@ def test_openapi_schema():
"securitySchemes": {"HTTPBase": {"type": "http", "scheme": "Other"}} "securitySchemes": {"HTTPBase": {"type": "http", "scheme": "Other"}}
}, },
} }
def test_http_basic_includes_realm():
app = FastAPI()
security = HTTPBasic(realm="MyRealm")
@app.get("/")
def read_root(credentials=Depends(security)):
return {"ok": True}
client = TestClient(app)
response = client.get("/")
assert response.status_code == 401
assert response.headers["WWW-Authenticate"] == 'Basic realm="MyRealm"'

Loading…
Cancel
Save