Repository navigation
feat(keycardai-oauth): refresh with a credential - #431
Conversation
5bd553f to
4e5f6c9
Compare
Release previewkeycardai-oauth 0.35.2 → 0.36.0 (minor)
No other package releases. This comment was automatically generated by the release preview workflow. |
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. It changes how the refresh-token grant authenticates the client at the token endpoint, so a human should still look at it.
What was reviewed:
- The
credential=path inAsyncClient.refresh_token_grant: it rejectsclient_id, swaps inNoneAuth, and the credential's headers and form fields are merged inbuild_refresh_token_http_request. Combined withNoneAuth, nothing from the client's own auth strategy gets into the request. - The removed pre-request
token_endpointcheck inrefresh_authorization: discovery inAsyncClientstill raisesAuthorizationServerDiscoveryErrorwhentoken_endpointis missing, which matches the docstring. RefreshGrantError.status_codeis passed in throughfunctools.partial, and 429/5xx are now checked beforeinvalid_grantinretryable.
Extended reasoning...
The change adds credential-based client authentication (ClientSecret, WebIdentity, WorkloadIdentity) to the refresh-token grant in keycardai-oauth, in client.py, operations/_authorize.py and pkce/web.py. It also changes how refresh errors are classified as retryable, and adds about 260 lines of tests. The security-sensitive part is the token-endpoint client authentication: the credential's headers and form fields replace the client's auth strategy. Because it changes an auth path and the API (credential is async-only, unlike the sync Client), I'm deferring to a human instead of approving, even though no bugs were found.
Adds the credential option to the refresh step in Authorization Code with PKCE (spec-version 5). AsyncClient.refresh_token_grant and refresh_authorization take credential=. The credential's client authentication is called with the token endpoint, and its headers and form fields go on the request in place of the client's auth strategy and the body client_id. credential together with client_id (or client_secret on refresh_authorization), or a credential without client authentication, raises ConfigError before any request. refresh_authorization's client_id is optional and required only without credential. The sync Client does not take a credential, because credentials are async only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…esses the token endpoint - RefreshGrantError keeps the response's status_code. A 429 or 5xx is retryable whatever the code, as base.md Retryability requires. Otherwise invalid_grant is not retryable, and every other code follows OAuthProtocolError. - refresh_authorization with metadata whose token_endpoint is absent or empty raises ConfigError before any request. An empty value no longer falls through to a convention-derived endpoint. It raised ValueError for an absent value before. - The discovered-metadata check that discovery already enforces is removed, and the docstrings name the errors raised: discovery failures, and ConfigError for an unknown ClientSecret zone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Authorization-code exchange and refresh now classify a token endpoint response the way the device grant does. A 2xx is a success and must have access_token, so a 2xx with an error member is invalid_response. Any other status is an error: the grant's OAuth error class when the body is an OAuth error (RFC 6749 Section 5.2), OAuthHttpError otherwise. A 3xx was parsed as a success before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4e5f6c9 to
3663350
Compare
Larry-Osakwe
left a comment
There was a problem hiding this comment.
Classifier looks right. Two things inline: one compat break and one README gap I think people will hit.
| if metadata.token_endpoint is None: | ||
| raise ValueError( | ||
| "Authorization server metadata is missing token_endpoint" | ||
| if not metadata.token_endpoint: |
There was a problem hiding this comment.
This was a ValueError in 0.33.0 and ConfigError isn't one, so a caller doing except ValueError stops catching it under a plain feat. Could it subclass ValueError the way WebIdentityConfigurationError does?
| The same grant is available on `AsyncClient` and `Client` as | ||
| `refresh_token_grant(refresh_token=..., client_id=..., resources=..., scopes=...)`. | ||
|
|
||
| To refresh with an application credential (`ClientSecret`, `WebIdentity`, or |
There was a problem hiding this comment.
Existing WebIdentity setups don't pass client_id= because they relied on resource_client_id from the auth context. The first refresh with one of those raises ConfigError. A line here saying refresh needs client_id= would save a support round trip.
Adds
credential=to the refresh step in Authorization Code with PKCE (spec-version 5), onAsyncClient.refresh_token_grantandrefresh_authorization. A device uses it to get a token for one of the resources the device grant issued. Linear: ECO-486.The credential's
client_authenticationis called with the token endpoint. Its headers and form fields replace the client's auth strategy and the bodyclient_id. The issuer passed to the credential has no trailing slash in every entry mode, so a multi-zoneClientSecretfinds its zone.Errors
credentialwithclient_id, or (onrefresh_authorization) withclient_secretConfigError, before discovery or any requestWebIdentitywithoutclient_idConfigError, before discovery or any requestcredentialnor a non-emptyclient_idonrefresh_authorizationConfigError, before any requestmetadataonrefresh_authorizationwithouttoken_endpoint, or with an empty oneConfigError, before any request. It wasValueError, and an empty value fell through to a convention-derived endpoint.RefreshGrantErrorwith the response'sstatus_code. A429or5xxis retryable whatever the code (base spec, Retryability). Otherwiseinvalid_grantis not retryable.test_status_decides_retryable_first(operations/test_authorize.py) andtest_refresh_rejects_metadata_without_token_endpoint(pkce/test_web.py) cover the last two rows.The sync
Clientdoes not take a credential, because Python credentials are async only.Spec unit test rows covered
Authorization Code with PKCE row 16, in
tests/keycardai/oauth/test_refresh_with_credential.py. Each test runs onAsyncClientand onrefresh_authorizationwithissuer=,metadata=, andwww_authenticate_header=, with discovery on.test_row_16_refresh_with_credential(issuer, endpoint)test_row_16_client_secret_sends_basic_and_no_client_idtest_unknown_zone_sends_no_token_requestConfigError, no POSTtest_misconfigured_refresh_sends_no_requestStack
Replaces #427. Merge in order:
The PR workflow runs only on pull requests to
main, so #430 to #433 show no CI until their base merges.just check,just typecheck,just test-coverage,just test-release-tooling, andjust validate-commitspass locally on each branch.🤖 Generated with Claude Code