Browse Source

Fix OIDC response code

Using a 401 instead of 403 aligns with the HTTP standard when authentication is missing and with the existing OAuth2 dependency.
pull/5332/head
Pieter Ennes 3 years ago
committed by Pieter Ennes
parent
commit
f98ebdae1c
  1. 6
      fastapi/security/open_id_connect_url.py
  2. 2
      tests/test_security_openid_connect.py
  3. 2
      tests/test_security_openid_connect_description.py

6
fastapi/security/open_id_connect_url.py

@ -4,7 +4,7 @@ from fastapi.openapi.models import OpenIdConnect as OpenIdConnectModel
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
class OpenIdConnect(SecurityBase):
@ -27,7 +27,9 @@ class OpenIdConnect(SecurityBase):
if not authorization:
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
status_code=HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"},
)
else:
return None

2
tests/test_security_openid_connect.py

@ -70,5 +70,5 @@ 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.status_code == 401, response.text
assert response.json() == {"detail": "Not authenticated"}

2
tests/test_security_openid_connect_description.py

@ -76,5 +76,5 @@ 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.status_code == 401, response.text
assert response.json() == {"detail": "Not authenticated"}

Loading…
Cancel
Save