):
self.model = HTTPBaseModel(scheme="basic", description=description)
self.scheme_name = scheme_name or self.__class__.__name__
- self.realm = realm
+ self.realm = realm or ""
self.auto_error = auto_error
async def __call__( # type: ignore
) -> Optional[HTTPBasicCredentials]:
authorization = request.headers.get("Authorization")
scheme, param = get_authorization_scheme_param(authorization)
- if self.realm:
- unauthorized_headers = {"WWW-Authenticate": f'Basic realm="{self.realm}"'}
- else:
- unauthorized_headers = {"WWW-Authenticate": "Basic"}
+ # The "realm" is required, as per https://datatracker.ietf.org/doc/html/rfc7617#section-2.
+ unauthorized_headers = {"WWW-Authenticate": f'Basic realm="{self.realm}"'}
if not authorization or scheme.lower() != "basic":
if self.auto_error:
raise HTTPException(
"/users/me", headers={"Authorization": "Basic notabase64token"}
)
assert response.status_code == 401, response.text
- assert response.headers["WWW-Authenticate"] == "Basic"
+ assert response.headers["WWW-Authenticate"] == 'Basic realm=""'
assert response.json() == {"detail": "Invalid authentication credentials"}
auth_header = f"Basic {payload}"
response = client.get("/users/me", headers={"Authorization": auth_header})
assert response.status_code == 401, response.text
- assert response.headers["WWW-Authenticate"] == "Basic"
+ assert response.headers["WWW-Authenticate"] == 'Basic realm=""'
assert response.json() == {"detail": "Invalid authentication credentials"}
response = client.get("/users/me")
assert response.json() == {"detail": "Not authenticated"}
assert response.status_code == 401, response.text
- assert response.headers["WWW-Authenticate"] == "Basic"
+ assert response.headers["WWW-Authenticate"] == 'Basic realm=""'
def test_security_http_basic_invalid_credentials(client: TestClient):
"/users/me", headers={"Authorization": "Basic notabase64token"}
)
assert response.status_code == 401, response.text
- assert response.headers["WWW-Authenticate"] == "Basic"
+ assert response.headers["WWW-Authenticate"] == 'Basic realm=""'
assert response.json() == {"detail": "Invalid authentication credentials"}
auth_header = f"Basic {payload}"
response = client.get("/users/me", headers={"Authorization": auth_header})
assert response.status_code == 401, response.text
- assert response.headers["WWW-Authenticate"] == "Basic"
+ assert response.headers["WWW-Authenticate"] == 'Basic realm=""'
assert response.json() == {"detail": "Invalid authentication credentials"}