Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fastapi/security/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ async def __call__(
if not (authorization and scheme and credentials):
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
status_code=HTTP_401_UNAUTHORIZED, detail="Not authenticated"
)
else:
return None
if scheme.lower() != "bearer":
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN,
status_code=HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
)
else:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_security_http_bearer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def test_security_http_bearer():

def test_security_http_bearer_no_credentials():
response = client.get("/users/me")
assert response.status_code == 403, response.text
assert response.status_code == 401, response.text
assert response.json() == {"detail": "Not authenticated"}


def test_security_http_bearer_incorrect_scheme_credentials():
response = client.get("/users/me", headers={"Authorization": "Basic notreally"})
assert response.status_code == 403, response.text
assert response.status_code == 401, response.text
assert response.json() == {"detail": "Invalid authentication credentials"}


Expand Down
4 changes: 2 additions & 2 deletions tests/test_security_http_bearer_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def test_security_http_bearer():

def test_security_http_bearer_no_credentials():
response = client.get("/users/me")
assert response.status_code == 403, response.text
assert response.status_code == 401, response.text
assert response.json() == {"detail": "Not authenticated"}


def test_security_http_bearer_incorrect_scheme_credentials():
response = client.get("/users/me", headers={"Authorization": "Basic notreally"})
assert response.status_code == 403, response.text
assert response.status_code == 401, response.text
assert response.json() == {"detail": "Invalid authentication credentials"}


Expand Down