Repository navigation
Conversation
The authorizer turned every failed step into a Deny policy, which API Gateway returns as 403. Clients usually refresh their access token on 401 only, so an expired token was never refreshed. The JWT step now raises TokenExpiredError for an expired token, and the handler raises "Unauthorized", which API Gateway maps to 401. The DEFAULT_4XX gateway response already adds the CORS headers. All other failures still return a Deny policy (403).
RHeynsZa
commented
Nov 17, 2026
Comment on lines
+93
to
+95
| except TokenExpiredError: | ||
| self.__metrics.add_metric(name="UserUnauthenticated", unit=MetricUnit.Count, value=1) | ||
| raise |
Contributor
Author
There was a problem hiding this comment.
Let me know if this metric is overkill @raphaho
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.
What does this change do?
When an access token has expired, the request authorizer returns a Deny policy, and API Gateway turns that into a 403. Most OAuth clients refresh their token only on a 401, so a client that holds an expired token never refreshes it and just keeps getting 403s. We hit this from a CLI and a portal that call the user API with Cognito tokens.
With this change, an expired token gets a 401:
JWTAuthorizerraises a newTokenExpiredErrorwhenjwt.decodefails withExpiredSignatureError, instead of returningFalse. Logging and theCognitoInvalidTokenmetric don't change.Authorizer.authorizepasses that error up instead of turning it into a Deny, and records aUserUnauthenticatedmetric.Exception("Unauthorized"). API Gateway answers 401 only when a Lambda authorizer raises exactly that message (docs).The
DEFAULT_4XXgateway response already adds the CORS headers, so browsers can read the 401.Everything else still returns a Deny (403): an invalid signature, a token that can't be decoded, the wrong client id or
token_use, a failed UserInfo call, and Verified Permissions and project-role denials. Refreshing the token doesn't fix any of these, so I left them alone to keep the change small.Type of change
How was this tested?
cd backend && uv run pytest app/authorization: 66 passed.test_jwt_authorizer_when_expired_should_raise_token_expired(replaces..._should_deny)test_authorize_when_token_expired_should_raise_instead_of_denytest_authorizer_lambda_handler_expired_token_returns_401: runs the handler end to end with an expired JWT and checks that it raisesUnauthorizedI haven't deployed this yet, so the 401 hasn't been checked against a live API Gateway.
Checklist
CONTRIBUTING.mdNotes for reviewers
authorizerResultTtlInSeconds: 1800for each token, path and method, so the 403 for an expired token could stick around. If I read the docs right, anUnauthorizedresult isn't cached that way.Acknowledgment
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.