Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: give correct unauthorized response on OpenID Connect Auth #9312

Closed
wants to merge 5 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions fastapi/security/open_id_connect_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi.security.base import SecurityBase
from starlette.exceptions import HTTPException
from starlette.requests import Request
from starlette.status import HTTP_403_FORBIDDEN
from starlette.status import HTTP_401_UNAUTHORIZED
from typing_extensions import Annotated, Doc # type: ignore [attr-defined]


Expand Down Expand Up @@ -77,7 +77,7 @@ async def __call__(self, request: Request) -> Optional[str]:
if not authorization:
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
status_code=HTTP_401_UNAUTHORIZED, detail="Unauthorized"
)
else:
return None
Expand Down
4 changes: 2 additions & 2 deletions tests/test_security_openid_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def test_security_oauth2_password_other_header():

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


def test_openapi_schema():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_security_openid_connect_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_security_oauth2_password_other_header():

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


def test_openapi_schema():
Expand Down