Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/workos/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"})
Expand Down
Loading