Repository navigation
feat(keycardai-oauth): code exchange with a credential - #438
Conversation
| redirect_uri=redirect_uri, | ||
| code_verifier=pkce.code_verifier, | ||
| client_id=client_id, | ||
| client_id=None if authenticator is not None else client_id, |
There was a problem hiding this comment.
🟡 (optional) CLI users who pass client_id plus a WorkloadIdentity with no client_id finish the browser login, then the token exchange fails. At line 195, client_id=None if authenticator is not None else client_id drops the caller's id. That WorkloadIdentity sends only client_assertion, so the zone gets no client_id. It resolves the request as a v1 credential, which cannot do the authorization-code grant. Fix: catch this before the browser opens. Raise ConfigError when the credential has no client_id or a different one (getattr(credential, "client_id", None)), or send the caller's client_id when the credential's form fields leave it out.
Why this was flagged
Trigger: authenticate(client_id="acr_123", issuer=..., credential=WorkloadIdentity(FileTokenSource())), or a credential from discover_credential() when KEYCARD_CLIENT_ID is unset (credentials.py:901). require_client_authenticator (client_auth.py) accepts it because WorkloadIdentity has no check_client_authentication. The flow builds the authorize URL with acr_123, opens the browser and waits for the code (pkce/client.py:171-187). Then line 195 passes client_id=None. WorkloadIdentity.client_authentication (credentials.py:695-701) adds client_id only when self.client_id is set. The zone rejects the exchange only after the user has finished the interactive login. On the base branch there is no credential path, so this is new behaviour. No guard prevents it: the only pre-flight check is credential+client_secret exclusivity at lines 135-141.
Verification: Trigger: a caller passes client_id together with a WorkloadIdentity that has no client_id of its own. require_client_authenticator (client_auth.py:65-72) accepts the credential. pkce/client.py:195 sends client_id=None if authenticator is not None else client_id, so the caller's id is dropped. WorkloadIdentity.client_authentication (credentials.py:695-701) adds client_id only if self.client_id.
Larry-Osakwe
left a comment
There was a problem hiding this comment.
Matches rows 17 to 22, and the pkce tests ran green for me. One thing before merge: the title needs the full package scope, feat(keycardai-oauth):, or cz won't cut a release. release-preview says nothing to release right now. Python already sends client_id on client credentials, so something like "feat(keycardai-oauth): code exchange with a credential" fits what's here.
adds the
credentialoption to the authorization-code exchange (AuthorizationCode with PKCE, spec-version 6), shaped like refresh with a credential (#431).
A confidential client can authenticate the code exchange with a credential's
client authentication (#430) in place of a client secret, such as a keyless
WorkloadIdentitywhoseclient_idnames a v2 token credential.Changes
AsyncClient.exchange_authorization_code(credential=...)calls thecredential's client authentication with the token endpoint. Its headers and
form fields go on the request in place of the client's auth strategy and the
body
client_id.complete_authorization(credential=...):client_idis optional andrequired only without
credential.authenticate(credential=...):client_idgoes on the authorize URL only;the code exchange authenticates with the credential.
credentialtogether withclient_id(orclient_secretoncomplete_authorizationandauthenticate), or a credential without clientauthentication, raises
ConfigErrorbefore any request. A credential's ownerror propagates as is, with no token request sent.
client_idon the authorize URL.The SDK does not compare them; the zone rejects a mismatch with
invalid_grant.WorkloadIdentityanddiscover_credentialdocstrings describe v1 and v2token credentials: v2 is resolved by
client_idand requires it, v1(deprecated) is resolved by the assertion's issuer and subject and rejects
it. Interactive grants need v2.
The sync
Clientdoes not take a credential, because credentials are async only.Testing
Unit rows 17 to 22 of the authorization-code-pkce spec, in
pkce/test_web.py,run through a real
AsyncClientand check the request on the wire:headers are the credential's; no
subject_token_typecredentialwithclient_idorclient_secret, or a credential withoutclient authentication, is a
ConfigErrorbefore any requestWorkloadIdentity'sclient_id, then complete withthat credential; the authorize URL has the
client_idand no assertion, andthe token request has the same
client_idwith the assertion and no Basicheader
WebIdentityassertion hasaudset to the token endpointpkce/test_client.pycoversauthenticatewith a credential and itsConfigErrorcases.