mirror of https://github.com/tiangolo/fastapi.git
Merge 8cdf7dbebe into 272204c0c7
This commit is contained in:
commit
1bd85c1e60
|
|
@ -3,7 +3,7 @@ from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
security = HTTPBasic()
|
security = HTTPBasic(realm="simple")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/users/me")
|
@app.get("/users/me")
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ from typing_extensions import Annotated
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
security = HTTPBasic()
|
security = HTTPBasic(realm="simple")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/users/me")
|
@app.get("/users/me")
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
security = HTTPBasic()
|
security = HTTPBasic(realm="simple")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/users/me")
|
@app.get("/users/me")
|
||||||
|
|
|
||||||
|
|
@ -154,13 +154,13 @@ class HTTPBasic(HTTPBase):
|
||||||
),
|
),
|
||||||
] = None,
|
] = None,
|
||||||
realm: Annotated[
|
realm: Annotated[
|
||||||
Optional[str],
|
str,
|
||||||
Doc(
|
Doc(
|
||||||
"""
|
"""
|
||||||
HTTP Basic authentication realm.
|
HTTP Basic authentication realm.
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
] = None,
|
],
|
||||||
description: Annotated[
|
description: Annotated[
|
||||||
Optional[str],
|
Optional[str],
|
||||||
Doc(
|
Doc(
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
from fastapi import Depends, FastAPI
|
||||||
|
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
security = HTTPBasic(realm="")
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/users/me")
|
||||||
|
def read_current_user(credentials: HTTPBasicCredentials = Depends(security)):
|
||||||
|
return {"username": credentials.username, "password": credentials.password}
|
||||||
|
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_security_http_basic_empty_realm():
|
||||||
|
response = client.get("/users/me", auth=("john", "secret"))
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert response.json() == {"username": "john", "password": "secret"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_security_http_basic_invalid_credentials_empty_realm():
|
||||||
|
response = client.get(
|
||||||
|
"/users/me", headers={"Authorization": "Basic notabase64token"}
|
||||||
|
)
|
||||||
|
assert response.status_code == 401, response.text
|
||||||
|
assert response.headers["WWW-Authenticate"] == "Basic"
|
||||||
|
assert response.json() == {"detail": "Invalid authentication credentials"}
|
||||||
|
|
@ -7,7 +7,7 @@ from fastapi.testclient import TestClient
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
security = HTTPBasic(auto_error=False)
|
security = HTTPBasic(realm="simple", auto_error=False)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/users/me")
|
@app.get("/users/me")
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ def test_security_http_basic_no_credentials(client: TestClient):
|
||||||
response = client.get("/users/me")
|
response = client.get("/users/me")
|
||||||
assert response.json() == {"detail": "Not authenticated"}
|
assert response.json() == {"detail": "Not authenticated"}
|
||||||
assert response.status_code == 401, response.text
|
assert response.status_code == 401, response.text
|
||||||
assert response.headers["WWW-Authenticate"] == "Basic"
|
assert response.headers["WWW-Authenticate"] == 'Basic realm="simple"'
|
||||||
|
|
||||||
|
|
||||||
def test_security_http_basic_invalid_credentials(client: TestClient):
|
def test_security_http_basic_invalid_credentials(client: TestClient):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue