keycardai-oauth 0.7.0 → 0.8.0

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: dfd08b9f86490b46112e042f2685724041ae5ee0ee15a224d6be7066cfe792a0
4
+ data.tar.gz: 30270904aa434912c3c3b81ed1b3f0de92170da146bd0a12f509563f721958c4
5
5
  SHA512:
6
- metadata.gz: 122d103e7bcf1e174f79115464742ccf5ff1ea31b7985e5e363d7fd1a30f6aada593d13eba5b5bd45076d462964e6eff6cce189a9dd0bf4afe7569db7f656d48
7
- data.tar.gz: af2c2e30f97e265f2e61514e0a8a0813a75326d0963f90cd959329ee39d5c9e4f4115a95f8b6c6fc62dc7a0d35a8fc71d9035187fc4a7ff61b712d8608629eee
6
+ metadata.gz: 5bc9f3ea131eaaadd12274401536b6ca7c7cf93fafe1c18e88d459339d22cbdc4602116d0b03c37628c8962218af6e25079d744be6f6b084eea872f97323a234
7
+ data.tar.gz: f2c8b190958fcba1e20c73736445432feec7a9bb92209d0be82e243e366a03768f45fb537d71b6aab43372e2f00f95da8cda5ece2da39a605fd80d0f088983fa
data/CHANGELOG.md CHANGED
@@ -15,6 +15,46 @@ 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.8.0-keycardai-oauth (2026-11-23)
19
+
20
+
21
+ - fix(keycardai-oauth)!: require an issuer allow-list before sending client auth to a challenge-resolved issuer (#63)
22
+ - * fix(keycardai-oauth): require allowed_issuers before sending client auth to a challenge-resolved issuer
23
+ - authenticate_from_challenge takes the issuer from the protected resource's
24
+ metadata. With client_secret set it now requires allowed_issuers and
25
+ checks the resolved issuer against it before any request to that
26
+ issuer. SDK-120.
27
+ - * fix(keycardai-oauth): use a multi-zone ClientSecret's zone issuers as the challenge allow-list
28
+ - When allowed_issuers is unset and the credential is a multi-zone
29
+ ClientSecret, authenticate_from_challenge checks the resolved issuer
30
+ against the credential's zone issuers. An explicit allowed_issuers wins.
31
+ SDK-120.
32
+ - * fix(keycardai-oauth): validate challenge issuer allow-list entries
33
+ - Co-Authored-By: Larry Osakwe <larry@keycard.ai>
34
+ - ---------
35
+ - Co-authored-by: Larry Osakwe <larry@keycard.ai>
36
+ Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
37
+
38
+ ## 0.7.1-keycardai-oauth (2026-11-23)
39
+
40
+
41
+ - fix(keycardai-oauth): WorkloadIdentity requires client_id before an interactive grant (#66)
42
+ - * fix(keycardai-oauth): WorkloadIdentity requires client_id before an interactive grant
43
+ - The refresh and device grants need a v2 token credential, which the zone
44
+ resolves by client_id. WorkloadIdentity gets check_client_authentication,
45
+ and ClientAuth.validate calls it when a credential responds to it, so
46
+ refresh_authorization and DeviceAuthorizationClient.new raise
47
+ ConfigurationError before any request, discovery included, when client_id
48
+ is not set. Token exchange does not check.
49
+ - Workload Identity spec-version 4 (rows 4d, 4e), Authorization Code with
50
+ PKCE spec-version 8 (row 34), Device Authorization spec-version 5 (row 34).
51
+ - Refs SDK-122
52
+ - * fix(keycardai-oauth): check the credential before the challenge's resource metadata fetch
53
+ - * docs(keycardai-oauth): name the WorkloadIdentity client_id requirement on every interactive grant
54
+ - ---------
55
+ - Co-authored-by: Larry Osakwe <23560354+Larry-Osakwe@users.noreply.github.com>
56
+ Co-authored-by: Larry Osakwe <larry@keycard.ai>
57
+
18
58
  ## 0.7.0-keycardai-oauth (2026-11-19)
19
59
 
20
60
 
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
 
@@ -97,15 +102,68 @@ module Keycardai
97
102
  # Challenge-driven entry to the login flow: resolve the issuer from a
98
103
  # WWW-Authenticate challenge, then run authenticate against it.
99
104
  #
105
+ # The protected resource names the issuer, so client authentication
106
+ # (client_secret or credential) requires an allow-list. A multi-zone
107
+ # ClientSecret credential counts as the pin: with allowed_issuers unset
108
+ # or empty, its zone issuers are the allow-list.
109
+ # The resolved issuer must equal one of them, compared without a
110
+ # trailing slash. A public client may omit the list; when it sets one,
111
+ # it is enforced the same way.
112
+ #
100
113
  # @param www_authenticate [String] the WWW-Authenticate header value
114
+ # @param allowed_issuers [Array<String>, nil] the issuers the caller
115
+ # trusts; overrides a multi-zone ClientSecret's zone issuers
101
116
  # @param options [Hash] forwarded to authenticate
102
117
  # @return [TokenResponse]
103
- def self.authenticate_from_challenge(www_authenticate, http_client: HTTP::NetHTTPClient.new, **options)
118
+ # @raise [ConfigurationError] invalid credential, an allowed_issuers entry
119
+ # that is not a non-empty String, or client authentication without an
120
+ # allow-list, before any request; or a resolved issuer outside the
121
+ # allow-list, after the resource metadata fetch and before any request
122
+ # to that issuer
123
+ def self.authenticate_from_challenge(www_authenticate, http_client: HTTP::NetHTTPClient.new,
124
+ allowed_issuers: nil, **options)
125
+ # Credential errors surface before the resource metadata fetch.
126
+ if options[:credential]
127
+ ClientAuth.validate(credential: options[:credential], client_id: nil, client_secret: options[:client_secret])
128
+ end
129
+ allowed, source = challenge_allow_list(allowed_issuers, options[:credential])
130
+ client_auth = !options[:client_secret].to_s.empty? || !options[:credential].nil?
131
+ if client_auth && allowed.empty?
132
+ raise ConfigurationError,
133
+ "pass allowed_issuers to send client_secret or credential to an issuer resolved from a challenge"
134
+ end
135
+
104
136
  issuer = resolve_issuer_from_challenge(www_authenticate, http_client: http_client,
105
137
  timeout: options[:timeout])
138
+ check_challenge_issuer(issuer, allowed, source)
106
139
  authenticate(issuer: issuer, http_client: http_client, **options)
107
140
  end
108
141
 
142
+ # Raises unless issuer is in a non-empty allowed, compared without a
143
+ # trailing slash. An empty list allows any issuer (a public client).
144
+ def self.check_challenge_issuer(issuer, allowed, source)
145
+ return if allowed.empty? || allowed.any? { |a| a.chomp("/") == issuer.chomp("/") }
146
+
147
+ raise ConfigurationError, "the challenge resolved issuer #{issuer}, which is not in #{source}"
148
+ end
149
+ private_class_method :check_challenge_issuer
150
+
151
+ # The allow-list for a challenge entry and how to name it in an error:
152
+ # allowed_issuers when set, else a multi-zone ClientSecret's zone issuers.
153
+ def self.challenge_allow_list(allowed_issuers, credential)
154
+ allowed = Array(allowed_issuers)
155
+ unless allowed.all? { |entry| entry.is_a?(String) && !entry.empty? }
156
+ raise ConfigurationError, "allowed_issuers entries must be non-empty strings"
157
+ end
158
+
159
+ return [allowed, "allowed_issuers"] unless allowed.empty?
160
+ return [credential.issuers, "the multi-zone ClientSecret's zone issuers"] if
161
+ credential.is_a?(ClientSecret) && credential.multi_zone?
162
+
163
+ [[], "allowed_issuers"]
164
+ end
165
+ private_class_method :challenge_allow_list
166
+
109
167
  # Internals of the loopback flow. Not public API.
110
168
  module Loopback
111
169
  SUCCESS_PAGE = "<html><body><p>Authentication complete. You can close this window.</p></body></html>"
@@ -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
@@ -47,7 +47,11 @@ module Keycardai
47
47
  !@pairs.nil?
48
48
  end
49
49
 
50
- # @return [Array<String>] the issuer URLs this credential can serve
50
+ # The issuer URLs this credential can serve, without a trailing slash.
51
+ # authenticate_from_challenge uses a multi-zone credential's issuers as
52
+ # its allow-list when allowed_issuers is unset.
53
+ #
54
+ # @return [Array<String>] the zone issuers; empty for a single zone
51
55
  def issuers
52
56
  multi_zone? ? @pairs.keys : []
53
57
  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.8.0"
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.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keycard