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. 13
      fastapi/security/http.py
  2. 18
      tests/test_security_http_base.py

13
fastapi/security/http.py

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

18
tests/test_security_http_base.py

@ -1,5 +1,4 @@
from fastapi import Depends, FastAPI, Security
from fastapi.security import HTTPBasic
from fastapi import FastAPI, Security
from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBase
from fastapi.testclient import TestClient
@ -54,18 +53,3 @@ def test_openapi_schema():
"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