fix(keycardai-starlette): return 401/503 (not 500) for JWKS errors - #196
Merged
Conversation
KeycardAuthBackend.authenticate() caught only InvalidTokenError. JWKS errors are a separate tree (JWKSError/JWKSKeyNotFoundError extend OAuthError(Exception); JWKSDiscoveryError/JWKSUriValidationError extend OAuthServerError(Exception)), none of which is InvalidTokenError or a Starlette AuthenticationError. They escaped AuthenticationMiddleware to ServerErrorMiddleware, yielding HTTP 500 with no WWW-Authenticate. So a token whose kid is absent from the JWKS (forged, or rotated out), or an unreachable/non-2xx JWKS or discovery endpoint, returned 500 instead of a 401 (client should re-auth) or 503 (transient). This is the Python-side of the same bug reported against the TypeScript SDK (#119). Map the JWKS error classes at the auth boundary: - JWKSKeyNotFoundError -> KeycardAuthError invalid_token (401 + challenge) - JWKSError / JWKSDiscoveryError / JWKSUriValidationError -> 503 _build_unauthorized_response now emits a bare 503 body with no WWW-Authenticate header for 5xx, since that challenge is not one a client can satisfy by re-authenticating. Refs keycardai/typescript-sdk#119
Contributor
📦 Release PreviewThis analysis shows the expected release impact: 📈 Expected Version Changes📋 Package Details[
{
"package_name": "keycardai-starlette",
"package_dir": "packages/starlette",
"has_changes": true,
"current_version": "0.10.0",
"next_version": "0.11.0",
"increment": "MINOR"
}
]📝 Changelog PreviewThis comment was automatically generated by the release preview workflow. |
The 503 except clause lists the named JWKS discovery/fetch classes rather than the OAuthServerError base on purpose: config and cache faults (VerifierConfigError, CacheError) are not per-request failures and must stay on the unexpected-error path (500), not be advertised as a transient 503. Comment only, no behavior change.
Contributor
📦 Release PreviewThis analysis shows the expected release impact: 📈 Expected Version Changes📋 Package Details[
{
"package_name": "keycardai-starlette",
"package_dir": "packages/starlette",
"has_changes": true,
"current_version": "0.10.0",
"next_version": "0.11.0",
"increment": "MINOR"
}
]📝 Changelog PreviewThis comment was automatically generated by the release preview workflow. |
Larry-Osakwe
marked this pull request as ready for review
July 20, 2026 03:28
mnoble
approved these changes
Jul 20, 2026
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.
Python-side of keycardai/typescript-sdk#119.
Problem
KeycardAuthBackend.authenticate()(packages/starlette/src/keycardai/starlette/middleware/bearer.py) caught onlyInvalidTokenError. JWKS errors are a separate tree —JWKSError/JWKSKeyNotFoundErrorextendOAuthError(Exception);JWKSDiscoveryError/JWKSUriValidationErrorextendOAuthServerError(Exception)— none of which isInvalidTokenErroror a StarletteAuthenticationError.So they escaped
AuthenticationMiddleware→ServerErrorMiddleware→ HTTP 500 with noWWW-Authenticate. A token whosekidis absent from the JWKS (forged, or rotated out), or an unreachable/non-2xx JWKS or discovery endpoint, all returned 500 instead of a clean 401/503.Fix
Map the JWKS error classes at the auth boundary:
JWKSKeyNotFoundErrorinvalid_token+WWW-Authenticatechallenge (client re-runs authorization)JWKSError/JWKSDiscoveryError/JWKSUriValidationError_build_unauthorized_responsenow emits a bare 503 body with noWWW-Authenticateheader for 5xx — that challenge isn't one a client can satisfy by re-authenticating.Errors outside these classes are unchanged: they remain genuine unexpected errors and are not swallowed.
Tests
Added to
test_provider.py:JWKSKeyNotFoundError→ 401 challenge;JWKSFetchError/JWKSDiscoveryError(parametrized) → 503 with no challenge header. 46/46 green.Parity
Companion PR fixes the TypeScript SDK (keycardai/typescript-sdk#123). The Go SDK is not affected (its verifier funnels all key-resolution failures into
InvalidTokenErrorand the middleware has a catch-all default).