Skip to content
Open
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
10 changes: 5 additions & 5 deletions fastapi/security/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ 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
Expand Down Expand Up @@ -306,14 +306,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 Expand Up @@ -408,14 +408,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() != "digest":
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN,
status_code=HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_security_http_base_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_security_http_base():

def test_security_http_base_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"}


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_digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def test_security_http_digest():

def test_security_http_digest_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_digest_incorrect_scheme_credentials():
response = client.get(
"/users/me", headers={"Authorization": "Other invalidauthorization"}
)
assert response.status_code == 403, response.text
assert response.status_code == 401, response.text
assert response.json() == {"detail": "Invalid authentication credentials"}


Expand Down