Skip to content

Commit

Permalink
Convert fastapi built-in 403 to 401 response (#13)
Browse files Browse the repository at this point in the history
* Convert fastapi built-in 403 to 401 response

See fastapi/fastapi#9130

* Fix test
  • Loading branch information
rjgildea authored Jun 8, 2023
1 parent 29deacc commit 075ce48
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/dials_rest/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ def __init__(self, auto_error: bool = True):
super().__init__(auto_error=auto_error)

async def __call__(self, request: Request) -> UserToken:
token: HTTPAuthorizationCredentials = await super().__call__(request)
try:
token: HTTPAuthorizationCredentials = await super().__call__(request)
except HTTPException as exception:
# Convert HTTP_403_FORBIDDEN to HTTP_401_UNAUTHORIZED
# See https://github.com/tiangolo/fastapi/discussions/9130
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail=exception.detail
)
if token:
if not token.scheme == "Bearer":
raise HTTPException(
Expand Down
4 changes: 2 additions & 2 deletions tests/routers/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from PIL import Image


def test_export_bitmap_without_jwt_responds_403(client):
def test_export_bitmap_without_jwt_responds_401(client):
response = client.post("export_bitmap")
assert response.status_code == status.HTTP_403_FORBIDDEN
assert response.status_code == status.HTTP_401_UNAUTHORIZED


def test_export_bitmap_file_not_found_responds_404(client, authentication_headers):
Expand Down

0 comments on commit 075ce48

Please sign in to comment.