Repository navigation
fix(session): return INVALID_JWT when JWKS lookup fails in authenticate - #785
Open
pcbeingused33 wants to merge 1 commit into
Open
pcbeingused33 wants to merge 1 commit into
pcbeingused33 wants to merge 1 commit into
Conversation
Session.authenticate() and AsyncSession.authenticate() only caught jwt.exceptions.InvalidTokenError, so a PyJWKClientError raised by the JWKS lookup escaped to the caller. refresh() already maps that error to INVALID_JWT; authenticate() now does the same. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Contributor
[Medium impact] This PR appears safe to merge. SummaryBoth
Reviews (1) · Last reviewed commit: "fix(session): return INVALID_JWT when JW..." · Reviewed by Greptile |
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
Session.authenticate()andAsyncSession.authenticate()raisejwt.exceptions.PyJWKClientErrorwhen the JWKS lookup fails (for example, no signing key matches the token'skid, or the JWKS endpoint cannot be reached), instead of returning anAuthenticateWithSessionCookieErrorResponse.Root cause: both methods wrap
get_signing_key_from_jwt()andjwt.decode()inexcept jwt.exceptions.InvalidTokenError.PyJWKClientErrorderives fromPyJWTError, not fromInvalidTokenError, so it is not caught.refresh()(sync and async) already catches both and maps them toINVALID_JWT(added in #673);authenticate()was left out.Fix: catch
jwt.exceptions.PyJWKClientErroralongsideInvalidTokenErrorin bothauthenticate()methods, returningreason=INVALID_JWT, the same mappingrefresh()uses. Onlysrc/workos/session.py(hand-maintained,@oagen-ignore-file) is touched.Tests: added
test_session_authenticate_jwks_error_returns_invalid_jwtandtest_async_session_authenticate_jwks_error_returns_invalid_jwt, mirroring the existing refresh test. Both fail onmainwithPyJWKClientError: Unable to find a signing keyand pass with this change;tests/test_session.pypasses in full (67 tests).Documentation
Does this require changes to the WorkOS Docs? E.g. the API Reference or code snippets need updates.
This PR was prepared with the help of an AI coding assistant (Claude Code).