Repository navigation
fix(api): return 401 and a null auth check instead of a generic 500 when the token is missing - #1445
Open
davidberenstein1957 wants to merge 4 commits into
Open
davidberenstein1957 wants to merge 4 commits into
davidberenstein1957 wants to merge 4 commits into
Conversation
…en is missing or invalid
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Two small changes in the API's authentication path so a missing or rejected token never ends in the catch-all
{"detail": "Generic error"}500:GET /auth/checkwith no token used to callcheck_jwt_user(None, create=True), which doesNone["sub"]and raisesTypeError. It now returns{"user": null}straight away, which is what its docstring already promised.HTTPException(401, "Unauthorized"). It now raises the project'sAuthenticationError, so the response is 401 withdetail: "Missing or invalid access token"and aWWW-Authenticate: Bearerheader, the same shape as an invalid or expired token.The generic 500 handler is unchanged. The webapp already redirects to login on a 401 (
webapp/src/api/errors.ts), so no frontend change is needed.Related Issue
Fixes #692
Motivation and Context
With an expired or removed token, the dashboard showed a blank page with
{"detail": "Generic error"}instead of sending the user to the login screen. The/auth/checkcall is the first thing the UI makes, so theTypeErrorthere turned every expired session into that page.How Has This Been Tested?
New tests in
carbonserver/tests/api/routers/test_authenticate.py, each written before its fix and failing against the previous code:test_auth_check_without_token_returns_null_user: 500 before, 200{"user": null}after. The sign-up service is mocked to raiseTypeErroronNone, mirroring the real service.test_protected_endpoint_without_token_returns_401:detailwas"Unauthorized", now"Missing or invalid access token"with theWWW-Authenticateheader.test_protected_endpoint_with_invalid_token_returns_401: regression guard, passed before and after.uv run task test-api-unit: 43, 65, 6 and 1 passed, 1 skipped, 1 xpassed.pre-commitpasses on the touched files. Not run: the integration tests against Postgres, and the realSignUpServicewith aNonetoken (covered by the mock only).Screenshots (if appropriate):
N/A
Types of changes
AI Usage Disclosure
Please refer to docs/how-to/ai-policy.md for detailed guidelines on how to disclose AI usage in your PR. Accurately completing this section is mandatory.
Checklist:
Commits