diff --git a/src/dials_rest/auth.py b/src/dials_rest/auth.py index 52083d2..50d0a81 100644 --- a/src/dials_rest/auth.py +++ b/src/dials_rest/auth.py @@ -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( diff --git a/tests/routers/test_image.py b/tests/routers/test_image.py index 501f74a..1ff6d93 100644 --- a/tests/routers/test_image.py +++ b/tests/routers/test_image.py @@ -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):