mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix HTTP Bearer security auto-error (#282)
This commit is contained in:
parent
d61f5e4b55
commit
d262f6e929
|
|
@ -112,10 +112,13 @@ class HTTPBearer(HTTPBase):
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
if scheme.lower() != "bearer":
|
if scheme.lower() != "bearer":
|
||||||
raise HTTPException(
|
if self.auto_error:
|
||||||
status_code=HTTP_403_FORBIDDEN,
|
raise HTTPException(
|
||||||
detail="Invalid authentication credentials",
|
status_code=HTTP_403_FORBIDDEN,
|
||||||
)
|
detail="Invalid authentication credentials",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials)
|
return HTTPAuthorizationCredentials(scheme=scheme, credentials=credentials)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,5 +64,5 @@ def test_security_http_bearer_no_credentials():
|
||||||
|
|
||||||
def test_security_http_bearer_incorrect_scheme_credentials():
|
def test_security_http_bearer_incorrect_scheme_credentials():
|
||||||
response = client.get("/users/me", headers={"Authorization": "Basic notreally"})
|
response = client.get("/users/me", headers={"Authorization": "Basic notreally"})
|
||||||
assert response.status_code == 403
|
assert response.status_code == 200
|
||||||
assert response.json() == {"detail": "Invalid authentication credentials"}
|
assert response.json() == {"msg": "Create an account first"}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue