keycardai-oauth 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45d2df1885f97c90426bf39a853f4d5143710e7cf281095c72b8bd65cddd78fa
4
- data.tar.gz: 17331a897f01eb3815c8af159421bd7a15ddcc3814281abb0ed9ab1d9d4fa6cb
3
+ metadata.gz: 1ffd084e2ae559bd7d4cb10acd883e13b81ccaf301348ff89a7b412aaedaf540
4
+ data.tar.gz: 7e8cc6b15aecffd015acf9ded6727abb83e2aa87cd35522c2e7694bc8414b82d
5
5
  SHA512:
6
- metadata.gz: 122d103e7bcf1e174f79115464742ccf5ff1ea31b7985e5e363d7fd1a30f6aada593d13eba5b5bd45076d462964e6eff6cce189a9dd0bf4afe7569db7f656d48
7
- data.tar.gz: af2c2e30f97e265f2e61514e0a8a0813a75326d0963f90cd959329ee39d5c9e4f4115a95f8b6c6fc62dc7a0d35a8fc71d9035187fc4a7ff61b712d8608629eee
6
+ metadata.gz: 10d0e36beca8ca688172e01ecdf7fdb1131ecd44acfad1e6634c2f71f77458f9d31f7aeeec5f19f25f9dce03198662f111c023e5c8e76eb11728d4054d9f0a2c
7
+ data.tar.gz: c663311fbc07ac0a8880f8197c3371483e177c61a70d531ddbe9c125e51eb3e6c2ebca7807267250207c93e35b068f2e225f31549711f6989052751dba81368d
data/CHANGELOG.md CHANGED
@@ -15,6 +15,26 @@ the loopback flow (RFC 8252), JWT signing and verification with a caching JWKS
15
15
  keyring, the three application credentials (ClientSecret with multi-zone,
16
16
  WebIdentity, WorkloadIdentity with pluggable token sources), and AccessContext.
17
17
 
18
+ ## 0.7.1-keycardai-oauth (2026-11-23)
19
+
20
+
21
+ - fix(keycardai-oauth): WorkloadIdentity requires client_id before an interactive grant (#66)
22
+ - * fix(keycardai-oauth): WorkloadIdentity requires client_id before an interactive grant
23
+ - The refresh and device grants need a v2 token credential, which the zone
24
+ resolves by client_id. WorkloadIdentity gets check_client_authentication,
25
+ and ClientAuth.validate calls it when a credential responds to it, so
26
+ refresh_authorization and DeviceAuthorizationClient.new raise
27
+ ConfigurationError before any request, discovery included, when client_id
28
+ is not set. Token exchange does not check.
29
+ - Workload Identity spec-version 4 (rows 4d, 4e), Authorization Code with
30
+ PKCE spec-version 8 (row 34), Device Authorization spec-version 5 (row 34).
31
+ - Refs SDK-122
32
+ - * fix(keycardai-oauth): check the credential before the challenge's resource metadata fetch
33
+ - * docs(keycardai-oauth): name the WorkloadIdentity client_id requirement on every interactive grant
34
+ - ---------
35
+ - Co-authored-by: Larry Osakwe <23560354+Larry-Osakwe@users.noreply.github.com>
36
+ Co-authored-by: Larry Osakwe <larry@keycard.ai>
37
+
18
38
  ## 0.7.0-keycardai-oauth (2026-11-19)
19
39
 
20
40
 
data/README.md CHANGED
@@ -127,7 +127,10 @@ stored_refresh_token = token.refresh_token if token.refresh_token
127
127
 
128
128
  A client with a credential passes `credential:` in place of `client_id` and
129
129
  `client_secret`. `ClientSecret`, `WebIdentity`, and `WorkloadIdentity` all work
130
- here. A credential of your own must respond to `client_authentication`.
130
+ here. A `WorkloadIdentity` needs `client_id:` (the public ID of a v2 token
131
+ credential) for refresh, the code exchange, `authenticate` and device
132
+ authorization; without it these raise `ConfigurationError` before any request.
133
+ A credential of your own must respond to `client_authentication`.
131
134
 
132
135
  ### Sign in a device with no browser
133
136
 
@@ -25,6 +25,9 @@ module Keycardai
25
25
  # @param port [Integer] loopback port; 0 binds an ephemeral port
26
26
  # @param callback_timeout [Numeric] seconds to wait for the redirect
27
27
  # @param client_secret [String, nil] confidential clients only
28
+ # @param credential [#client_authentication, nil] confidential clients
29
+ # only, in place of client_secret: client_id goes on the authorize URL
30
+ # only, and the credential authenticates the code exchange
28
31
  # @param verifier_length [Integer] PKCE verifier length, 43 to 128
29
32
  # @param http_client [#get, #post_form] pluggable transport
30
33
  # @param browser_opener [#call, nil] receives the authorize URL; defaults
@@ -34,10 +37,13 @@ module Keycardai
34
37
  # @raise [InteractionTimeoutError] the user did not complete the redirect
35
38
  # @raise [OAuthError] the authorization server denied the request
36
39
  # @raise [ProtocolError] the redirect's state did not match (code state_mismatch)
40
+ # @raise [ConfigurationError] credential combined with client_secret,
41
+ # without client_authentication, or a WorkloadIdentity without client_id
37
42
  def self.authenticate(issuer:, client_id:, scope: nil, resources: [], port: DEFAULT_CALLBACK_PORT,
38
- callback_timeout: DEFAULT_CALLBACK_TIMEOUT, client_secret: nil,
43
+ callback_timeout: DEFAULT_CALLBACK_TIMEOUT, client_secret: nil, credential: nil,
39
44
  verifier_length: PKCE::DEFAULT_VERIFIER_LENGTH,
40
45
  http_client: HTTP::NetHTTPClient.new, browser_opener: nil, timeout: nil)
46
+ ClientAuth.validate(credential: credential, client_id: nil, client_secret: client_secret) if credential
41
47
  authorization_endpoint = authorization_endpoint_for(issuer, http_client, timeout)
42
48
  pair = PKCE.generate_pair(length: verifier_length)
43
49
  state = SecureRandom.urlsafe_base64(24)
@@ -47,12 +53,11 @@ module Keycardai
47
53
  client_id: client_id, scope: scope, resources: resources,
48
54
  browser_opener: browser_opener)
49
55
  code = server.wait_for_code(state: state, timeout: callback_timeout)
50
- exchange_authorization_code(
51
- issuer,
52
- code: code, code_verifier: pair.code_verifier, redirect_uri: server.redirect_uri,
53
- client_id: client_id, client_secret: client_secret,
54
- http_client: http_client, timeout: timeout
55
- )
56
+ # With a credential, client_id goes on the authorize URL only.
57
+ exchange_authorization_code(issuer, code: code, code_verifier: pair.code_verifier,
58
+ redirect_uri: server.redirect_uri,
59
+ client_id: credential ? nil : client_id, client_secret: client_secret,
60
+ credential: credential, http_client: http_client, timeout: timeout)
56
61
  end
57
62
  end
58
63
 
@@ -101,6 +106,10 @@ module Keycardai
101
106
  # @param options [Hash] forwarded to authenticate
102
107
  # @return [TokenResponse]
103
108
  def self.authenticate_from_challenge(www_authenticate, http_client: HTTP::NetHTTPClient.new, **options)
109
+ # Credential errors surface before the resource metadata fetch.
110
+ if options[:credential]
111
+ ClientAuth.validate(credential: options[:credential], client_id: nil, client_secret: options[:client_secret])
112
+ end
104
113
  issuer = resolve_issuer_from_challenge(www_authenticate, http_client: http_client,
105
114
  timeout: options[:timeout])
106
115
  authenticate(issuer: issuer, http_client: http_client, **options)
@@ -38,8 +38,12 @@ module Keycardai
38
38
  end
39
39
 
40
40
  # Exchange an authorization code for a token (RFC 6749 §4.1.3). A public
41
- # client sends its client_id in the body; a confidential client
42
- # authenticates with HTTP Basic and omits client_id from the body.
41
+ # client sends its client_id in the body. A confidential client
42
+ # authenticates with HTTP Basic and omits client_id from the body. A
43
+ # client with a credential adds the headers and form fields of the
44
+ # credential's client_authentication. The code is bound to the authorize
45
+ # URL's client_id, so a credential must authenticate as that client; the
46
+ # zone rejects a mismatch with invalid_grant.
43
47
  #
44
48
  # @param issuer [String] the zone's issuer URL; supplies the token endpoint
45
49
  # @param code [String] the authorization code from the redirect
@@ -47,33 +51,38 @@ module Keycardai
47
51
  # @param redirect_uri [String] must match the authorization request
48
52
  # @param client_id [String, nil] public-client identifier
49
53
  # @param client_secret [String, nil] confidential-client secret; requires client_id
54
+ # @param credential [#client_authentication, nil] authenticates the
55
+ # exchange in place of client_id and client_secret
50
56
  # @param http_client [#get, #post_form] pluggable transport
51
57
  # @param timeout [Numeric, nil]
52
58
  # @return [TokenResponse]
59
+ # @raise [ConfigurationError] before any request: client_secret without
60
+ # client_id, credential set together with client_id or client_secret, a
61
+ # credential without client_authentication, or a WorkloadIdentity
62
+ # without client_id
53
63
  # @raise [OAuthError] an RFC 6749 §5.2 error response (invalid_grant, ...)
54
- # @raise [HTTPError, ProtocolError, NetworkError, ConfigurationError]
64
+ # @raise [HTTPError, ProtocolError, NetworkError]
55
65
  def self.exchange_authorization_code(issuer, code:, code_verifier:, redirect_uri:, client_id: nil,
56
- client_secret: nil,
66
+ client_secret: nil, credential: nil,
57
67
  http_client: HTTP::NetHTTPClient.new, timeout: nil)
58
68
  raise ConfigurationError, "client_secret requires client_id" if client_secret && client_id.nil?
59
69
 
70
+ ClientAuth.validate(credential: credential, client_id: client_id, client_secret: client_secret) if credential
60
71
  metadata = fetch_authorization_server_metadata(issuer, http_client: http_client, timeout: timeout)
61
72
  endpoint = metadata.token_endpoint ||
62
73
  raise(ProtocolError.new("metadata for #{issuer} has no token_endpoint", code: "invalid_metadata"))
63
74
 
64
- # A confidential client authenticates with Basic and omits client_id
65
- # from the body; a public client carries client_id in the body.
66
- params = {
67
- "grant_type" => GrantType::AUTHORIZATION_CODE,
68
- "code" => code,
69
- "code_verifier" => code_verifier,
70
- "redirect_uri" => redirect_uri,
71
- "client_id" => client_secret ? nil : client_id
72
- }.compact
75
+ params = [["grant_type", GrantType::AUTHORIZATION_CODE], ["code", code], ["code_verifier", code_verifier],
76
+ ["redirect_uri", redirect_uri]]
73
77
  headers = { "Accept" => "application/json" }
74
- headers["Authorization"] = HTTP.basic_authorization(client_id, client_secret) if client_secret
78
+ # With neither a client_id nor a credential, the exchange carries no client authentication.
79
+ if credential || client_id
80
+ params, headers = ClientAuth.apply(params, headers, issuer: issuer, endpoint: endpoint, credential: credential,
81
+ client_id: client_id, client_secret: client_secret)
82
+ end
75
83
 
76
- TokenRequests.parse_response(http_client.post_form(endpoint, params, headers: headers, timeout: timeout))
84
+ # No key repeats in a code exchange, so the pairs become a hash.
85
+ TokenRequests.parse_response(http_client.post_form(endpoint, params.to_h, headers: headers, timeout: timeout))
77
86
  end
78
87
 
79
88
  # Redeem a refresh token for a new access token (RFC 6749 §6). Client
@@ -100,7 +109,8 @@ module Keycardai
100
109
  # @return [TokenResponse]
101
110
  # @raise [ConfigurationError] before any request: credential set together
102
111
  # with client_id or client_secret, a credential without
103
- # client_authentication, or neither credential nor client_id
112
+ # client_authentication, a WorkloadIdentity without client_id, or
113
+ # neither credential nor client_id
104
114
  # @raise [OAuthError] an RFC 6749 §5.2 error response; error == "invalid_grant"
105
115
  # means the refresh token is expired, revoked, or unknown and the user must
106
116
  # authorize again
@@ -41,16 +41,21 @@ module Keycardai
41
41
  # Raise ConfigurationError when credential is set with client_id or
42
42
  # client_secret, when credential does not respond to
43
43
  # client_authentication, or when neither credential nor a non-empty
44
- # client_id is set. Call it before any request.
44
+ # client_id is set. A credential that responds to
45
+ # check_client_authentication is checked with it, so a WorkloadIdentity
46
+ # without client_id raises here. Call it before any request.
45
47
  #
46
48
  # @raise [ConfigurationError]
47
49
  def validate(credential:, client_id:, client_secret: nil)
48
50
  if credential
49
51
  given = { "client_id" => client_id, "client_secret" => client_secret }.compact.keys
50
52
  raise ConfigurationError, "pass a credential or #{given.join(" and ")}, not both" unless given.empty?
51
- return if credential.respond_to?(:client_authentication)
53
+ unless credential.respond_to?(:client_authentication)
54
+ raise ConfigurationError, "#{credential.class} does not provide client authentication"
55
+ end
52
56
 
53
- raise ConfigurationError, "#{credential.class} does not provide client authentication"
57
+ credential.check_client_authentication if credential.respond_to?(:check_client_authentication)
58
+ return
54
59
  end
55
60
  return unless client_id.nil? || client_id.empty?
56
61
 
@@ -41,21 +41,30 @@ module Keycardai
41
41
  # @param client_assertion [String, nil] client-authentication assertion,
42
42
  # form-encoded in the body
43
43
  # @param client_assertion_type [String, nil]
44
+ # @param client_id [String, nil] the application credential the
45
+ # assertion authenticates as, form-encoded beside client_assertion;
46
+ # never sent with HTTP Basic
44
47
  # @return [TokenResponse]
45
48
  # @raise [OAuthError] an RFC 6749 §5.2 error response (invalid_client,
46
49
  # invalid_scope, ...)
47
50
  # @raise [HTTPError, ProtocolError, NetworkError]
48
51
  def request_token(scope: nil, resource: nil, client_assertion: nil, client_assertion_type: nil,
49
- issuer: nil)
52
+ client_id: nil, issuer: nil)
53
+ target = issuer || @issuer
54
+ if client_id && @credential&.authorization_header(issuer: target)
55
+ raise ConfigurationError, "client_id is sent beside client_assertion, not with HTTP Basic"
56
+ end
57
+
50
58
  post_token_request(
51
59
  {
52
60
  "grant_type" => GrantType::CLIENT_CREDENTIALS,
53
61
  "scope" => scope,
54
62
  "resource" => resource,
55
63
  "client_assertion" => client_assertion,
56
- "client_assertion_type" => client_assertion_type
64
+ "client_assertion_type" => client_assertion_type,
65
+ "client_id" => client_id
57
66
  },
58
- issuer: issuer || @issuer
67
+ issuer: target
59
68
  )
60
69
  end
61
70
  end
@@ -48,6 +48,8 @@ module Keycardai
48
48
  # @param client_assertion [String, nil] client-authentication assertion,
49
49
  # form-encoded in the body
50
50
  # @param client_assertion_type [String, nil]
51
+ # @param client_id [String, nil] the application credential the
52
+ # assertion authenticates as, form-encoded beside client_assertion
51
53
  # @param issuer [String, nil] per-call zone selection: resolves the zone's
52
54
  # token endpoint and, for a multi-zone credential, that zone's client
53
55
  # authentication. Defaults to the client's issuer.
@@ -58,7 +60,7 @@ module Keycardai
58
60
  def exchange_token(subject_token:, subject_token_type: nil, resource: nil,
59
61
  audience: nil, scope: nil, requested_token_type: nil, actor_token: nil,
60
62
  actor_token_type: nil, client_assertion: nil, client_assertion_type: nil,
61
- issuer: nil)
63
+ client_id: nil, issuer: nil)
62
64
  raise ArgumentError, "actor_token_type is required when actor_token is set" if actor_token && !actor_token_type
63
65
 
64
66
  target = issuer || @issuer
@@ -71,7 +73,8 @@ module Keycardai
71
73
  "actor_token" => actor_token,
72
74
  "actor_token_type" => actor_token_type,
73
75
  "client_assertion" => client_assertion,
74
- "client_assertion_type" => client_assertion_type
76
+ "client_assertion_type" => client_assertion_type,
77
+ "client_id" => client_id
75
78
  }.compact
76
79
  post_token_request(base_exchange_params(subject_token, target).merge(overrides), issuer: target)
77
80
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Keycardai
4
4
  module OAuth
5
- VERSION = "0.7.0"
5
+ VERSION = "0.7.1"
6
6
  end
7
7
  end
@@ -13,9 +13,10 @@ module Keycardai
13
13
  class WorkloadIdentity
14
14
  # @param source [#identity_token, #call] supplies the platform-signed
15
15
  # OIDC token; a bare callable is accepted
16
- # @param client_id [String, nil] the Keycard application credential this
17
- # workload authenticates as; sent as the client_id form parameter when
18
- # set (token-federation credentials are resolved by it)
16
+ # @param client_id [String, nil] public ID of the Keycard application
17
+ # credential this workload authenticates as; sent as the client_id form
18
+ # parameter on every token request and client authentication when set.
19
+ # A v2 token credential requires it; a v1 credential rejects it
19
20
  # @raise [ConfigurationError] when the source is missing or has neither
20
21
  # an identity_token method nor call
21
22
  def initialize(source:, client_id: nil)
@@ -71,6 +72,21 @@ module Keycardai
71
72
  }.compact)
72
73
  end
73
74
 
75
+ # Raise unless client_id is set. The interactive grants (the code
76
+ # exchange, authenticate, authenticate_from_challenge, refresh and
77
+ # device authorization) need a v2 token credential, which the zone
78
+ # resolves by client_id, so they call this before any request.
79
+ #
80
+ # @return [void]
81
+ # @raise [ConfigurationError] client_id is nil or empty
82
+ def check_client_authentication
83
+ return unless @client_id.nil? || @client_id.empty?
84
+
85
+ raise ConfigurationError,
86
+ "WorkloadIdentity: client_id is required for interactive grants " \
87
+ "(the public ID of a v2 token credential)"
88
+ end
89
+
74
90
  private
75
91
 
76
92
  def normalize_source(source)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keycardai-oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keycard