From 94da6f6809b84539ba48d5cb7c96a244a3cea37e Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2026 08:27:21 -0400 Subject: [PATCH] fix(session): return INVALID_JWT when JWKS lookup fails in authenticate 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 --- src/workos/session.py | 10 ++++++++-- tests/test_session.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/workos/session.py b/src/workos/session.py index 41dd7143..e2be9e7a 100644 --- a/src/workos/session.py +++ b/src/workos/session.py @@ -305,7 +305,10 @@ def authenticate( leeway=self._client._jwt_leeway, issuer=self._client._jwt_issuer, ) - except jwt.exceptions.InvalidTokenError: + except ( + jwt.exceptions.InvalidTokenError, + jwt.exceptions.PyJWKClientError, + ): return AuthenticateWithSessionCookieErrorResponse( authenticated=False, reason=AuthenticateWithSessionCookieFailureReason.INVALID_JWT, @@ -521,7 +524,10 @@ def authenticate( leeway=self._client._jwt_leeway, issuer=self._client._jwt_issuer, ) - except jwt.exceptions.InvalidTokenError: + except ( + jwt.exceptions.InvalidTokenError, + jwt.exceptions.PyJWKClientError, + ): return AuthenticateWithSessionCookieErrorResponse( authenticated=False, reason=AuthenticateWithSessionCookieFailureReason.INVALID_JWT, diff --git a/tests/test_session.py b/tests/test_session.py index 0dc0c811..1a639bf0 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -243,6 +243,22 @@ def test_session_authenticate_expired_jwt(self): assert isinstance(result, AuthenticateWithSessionCookieErrorResponse) assert result.reason == AuthenticateWithSessionCookieFailureReason.INVALID_JWT + def test_session_authenticate_jwks_error_returns_invalid_jwt(self): + """A JWKS lookup failure maps to INVALID_JWT instead of raising.""" + token = _make_jwt(self.private_key) + sealed = self._make_sealed_session(access_token=token) + session = Session( + client=self.workos, session_data=sealed, cookie_password=COOKIE_PASSWORD + ) + mock_jwks = MagicMock() + mock_jwks.get_signing_key_from_jwt.side_effect = ( + pyjwt.exceptions.PyJWKClientError("Unable to find a signing key") + ) + session.jwks = mock_jwks + result = session.authenticate() + assert isinstance(result, AuthenticateWithSessionCookieErrorResponse) + assert result.reason == AuthenticateWithSessionCookieFailureReason.INVALID_JWT + def _authenticate_with_client(self, client, claims=None): token = _make_jwt(self.private_key, claims=claims) sealed = self._make_sealed_session(access_token=token) @@ -790,6 +806,28 @@ async def test_async_session_authenticate_rejects_agent_access_token( assert isinstance(result, AuthenticateWithSessionCookieErrorResponse) assert result.reason == AuthenticateWithSessionCookieFailureReason.INVALID_JWT + async def test_async_session_authenticate_jwks_error_returns_invalid_jwt( + self, async_workos + ): + """A JWKS lookup failure maps to INVALID_JWT instead of raising.""" + private_key, _ = _generate_rsa_key_pair() + token = _make_jwt(private_key) + sealed = seal_data( + {"access_token": token, "refresh_token": "rt", "user": {"id": "u1"}}, + COOKIE_PASSWORD, + ) + session = AsyncSession( + client=async_workos, session_data=sealed, cookie_password=COOKIE_PASSWORD + ) + mock_jwks = MagicMock() + mock_jwks.get_signing_key_from_jwt.side_effect = ( + pyjwt.exceptions.PyJWKClientError("Unable to find a signing key") + ) + session.jwks = mock_jwks + result = session.authenticate() + assert isinstance(result, AuthenticateWithSessionCookieErrorResponse) + assert result.reason == AuthenticateWithSessionCookieFailureReason.INVALID_JWT + async def test_async_session_authenticate_rejects_mismatched_issuer(self): private_key, public_key = _generate_rsa_key_pair() token = _make_jwt(private_key, claims={"iss": "https://other.example.com"})