keycardai-oauth 0.13.1 → 0.15.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: bea3543557872871f3bf07497a4726dd80c93a7952bfc287de9c839549aafa86
4
- data.tar.gz: 7387d40704ed417795864ad8130d709c9242c65724193ddab8a0be1a0da424d1
3
+ metadata.gz: 2270af78d15b4eaffa12e3f66d5f7ad750705a4b9e09c38a0473a49b358f7271
4
+ data.tar.gz: d036adb9c1c73b34e7ad1a30c03603781a2a55ea32cdc2606dd6c059b407d6d9
5
5
  SHA512:
6
- metadata.gz: e4a24f1daa6c4e6d845fe6a9d0914f4b2f59bd73f1ccffa78609b617b231d49c1d648d6b457c797e2cb6df87d7c2428e4bea9396471fa6e39969692504fb4f06
7
- data.tar.gz: dcd880b7d247b572d9e7b390b850f2058b9e0241358e8920d1c79bd5c704cbc043096cd71c2e464c33377099d1e44305df04644f23d8982d510b8b0686a7ecae
6
+ metadata.gz: 9f3c50b35c045bbada1c713da736e9746d1b4d720b0cbb9ba3c8c498952617e1b1b6d91d7aca4710d0f58fd2fb7b1c83142eb46203ed09000a6c59886fd0a240
7
+ data.tar.gz: b023b385d517a4a4155763e656b0cb6614e307ce6d0df3836704a33b1dced3f3a2dd5cfe63fab14a52fd52499b960ddf29583e8aa900321d44d289ae7caaaac3
data/CHANGELOG.md CHANGED
@@ -15,6 +15,29 @@ 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.15.0-keycardai-oauth (2026-11-26)
19
+
20
+
21
+ - feat(keycardai-oauth): bind credentials to the client credentials grant (#103)
22
+ - * feat(keycardai-oauth): bind credentials to the client credentials grant
23
+ - Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
24
+ - * test(keycardai-oauth): cover row 4d without a request client_id
25
+ - * fix(keycardai-oauth): reject request client fields beside a configured assertion credential and resolve ClientSecret before discovery
26
+ - ---------
27
+ - Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
28
+ Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
29
+ Co-authored-by: Larry Osakwe <23560354+Larry-Osakwe@users.noreply.github.com>
30
+
31
+ ## 0.14.0-keycardai-oauth (2026-11-26)
32
+
33
+
34
+ - feat(keycardai-oauth): actor token source on the token exchange client (SDK-99) (#101)
35
+ - * feat(keycardai-oauth): actor token source on the token exchange client (SDK-99)
36
+ - * fix(keycardai-oauth): mark transport-shaped token source failures retryable
37
+ - * fix(keycardai-oauth): mark only transient Fly socket errnos retryable
38
+ - ---------
39
+ - Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
40
+
18
41
  ## 0.13.1-keycardai-oauth (2026-11-26)
19
42
 
20
43
 
data/README.md CHANGED
@@ -14,7 +14,7 @@ bundle add keycardai-oauth
14
14
  Capabilities:
15
15
 
16
16
  - Token exchange and impersonation (RFC 8693)
17
- - Client credentials grant (RFC 6749 §4.4)
17
+ - Client credentials grant (RFC 6749 §4.4), with an application credential bound on `ClientCredentialsClient`
18
18
  - Authorization code + PKCE, including the challenge-driven loopback flow (RFC 8252)
19
19
  - Device authorization grant (RFC 8628)
20
20
  - Token revocation (RFC 7009)
@@ -14,9 +14,8 @@ module Keycardai
14
14
  include TokenRequests
15
15
 
16
16
  # @param issuer [String] the zone's issuer URL
17
- # @param credential [Object, nil] a Basic-auth credential (ClientSecret);
18
- # exclusive with client_id/client_secret. Assertion-based credentials
19
- # supply their assertion through the request fields instead.
17
+ # @param credential [Object, nil] provides client authentication;
18
+ # exclusive with client_id/client_secret
20
19
  # @param client_id [String, nil] shared-secret client id (HTTP Basic)
21
20
  # @param client_secret [String, nil] shared-secret client secret;
22
21
  # provide both or neither
@@ -39,7 +38,7 @@ module Keycardai
39
38
  # @param scope [String, nil] space-separated scopes for the token
40
39
  # @param resource [String, nil] RFC 8707 resource indicator
41
40
  # @param client_assertion [String, nil] client-authentication assertion,
42
- # form-encoded in the body
41
+ # form-encoded in the body in place of configured client authentication
43
42
  # @param client_assertion_type [String, nil]
44
43
  # @param client_id [String, nil] the application credential the
45
44
  # assertion authenticates as, form-encoded beside client_assertion;
@@ -50,12 +49,7 @@ module Keycardai
50
49
  # @raise [HTTPError, ProtocolError, NetworkError]
51
50
  def request_token(scope: nil, resource: nil, client_assertion: nil, client_assertion_type: nil,
52
51
  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
-
58
- post_token_request(
52
+ post_credentials(
59
53
  {
60
54
  "grant_type" => GrantType::CLIENT_CREDENTIALS,
61
55
  "scope" => scope,
@@ -64,9 +58,52 @@ module Keycardai
64
58
  "client_assertion_type" => client_assertion_type,
65
59
  "client_id" => client_id
66
60
  },
67
- issuer: target
61
+ issuer || @issuer
68
62
  )
69
63
  end
64
+
65
+ private
66
+
67
+ def post_credentials(params, issuer)
68
+ if params["client_assertion"] || !@credential
69
+ return post_token_request(params, issuer: issuer, authorization: nil)
70
+ end
71
+ # A ClientSecret resolves its Basic header without the token endpoint,
72
+ # so an unconfigured zone fails before any discovery request.
73
+ if @credential.is_a?(ClientSecret) || !@credential.respond_to?(:client_authentication)
74
+ return post_with_header(params, issuer)
75
+ end
76
+
77
+ post_authenticated_credentials(params, issuer)
78
+ end
79
+
80
+ def post_authenticated_credentials(params, issuer)
81
+ reject_request_client_fields(params)
82
+ endpoint = token_endpoint(issuer)
83
+ auth = @credential.client_authentication(issuer: issuer, endpoint: endpoint)
84
+ form = params.merge(auth.params)
85
+ post_token_form(endpoint, form, { "Accept" => "application/json" }.merge(auth.headers))
86
+ end
87
+
88
+ def post_with_header(params, issuer)
89
+ authorization = @credential.authorization_header(issuer: issuer)
90
+ reject_body_client_id(params) if authorization
91
+ post_token_request(params, issuer: issuer, authorization: authorization)
92
+ end
93
+
94
+ def reject_body_client_id(params)
95
+ return unless params["client_id"]
96
+
97
+ raise ConfigurationError, "client_id is sent beside client_assertion, not with HTTP Basic"
98
+ end
99
+
100
+ # The configured credential supplies client_id and client_assertion_type,
101
+ # so a request value would be dropped or overridden: fail instead.
102
+ def reject_request_client_fields(params)
103
+ return unless params["client_id"] || params["client_assertion_type"]
104
+
105
+ raise ConfigurationError, "client_id and client_assertion_type are sent only beside a request client_assertion"
106
+ end
70
107
  end
71
108
  end
72
109
  end
@@ -217,6 +217,11 @@ module Keycardai
217
217
  super(message)
218
218
  @source = source
219
219
  end
220
+
221
+ # @return [Boolean] configuration errors are not retryable
222
+ def retryable?
223
+ false
224
+ end
220
225
  end
221
226
 
222
227
  # A workload-identity source failed at request time: file unreadable or
@@ -226,9 +231,15 @@ module Keycardai
226
231
  # @return [String]
227
232
  attr_reader :source
228
233
 
229
- def initialize(message, source:)
234
+ def initialize(message, source:, retryable: false)
230
235
  super(message)
231
236
  @source = source
237
+ @retryable = retryable
238
+ end
239
+
240
+ # @return [Boolean] whether repeating the fetch unchanged could succeed
241
+ def retryable?
242
+ @retryable
232
243
  end
233
244
  end
234
245
  end
@@ -20,18 +20,24 @@ module Keycardai
20
20
  # @param client_id [String, nil] shared-secret client id (HTTP Basic)
21
21
  # @param client_secret [String, nil] shared-secret client secret;
22
22
  # provide both or neither
23
+ # @param actor [#identity_token, #call, nil] supplies the acting
24
+ # workload's identity token, sent as actor_token with type JWT on
25
+ # every exchange unless the request sets its own; never sent on
26
+ # impersonate
23
27
  # @param http_client [#get, #post_form] pluggable transport
24
28
  # @param timeout [Numeric, nil] request timeout in seconds
25
29
  # @param discovery_ttl [Numeric] token-endpoint cache lifetime in seconds
26
30
  # @param clock [#call] returns the current Time; override in tests
27
31
  # @raise [ConfigurationError] when only one of client_id/client_secret is
28
- # given, or a credential is combined with a raw pair
29
- def initialize(issuer:, credential: nil, client_id: nil, client_secret: nil,
32
+ # given, a credential is combined with a raw pair, or actor responds
33
+ # to neither identity_token nor call
34
+ def initialize(issuer:, credential: nil, client_id: nil, client_secret: nil, actor: nil,
30
35
  http_client: HTTP::NetHTTPClient.new, timeout: nil,
31
36
  discovery_ttl: TokenRequests::DEFAULT_DISCOVERY_TTL, clock: MetadataRetry::WALL_CLOCK)
32
37
  initialize_token_client(issuer: issuer, credential: credential, client_id: client_id,
33
38
  client_secret: client_secret, http_client: http_client, timeout: timeout,
34
39
  discovery_ttl: discovery_ttl, clock: clock)
40
+ @actor = actor ? WorkloadIdentity.normalize_source(actor) : nil
35
41
  end
36
42
 
37
43
  # Exchange a subject token (RFC 8693).
@@ -43,7 +49,8 @@ module Keycardai
43
49
  # @param scope [String, nil] space-separated scopes for the issued token
44
50
  # @param requested_token_type [String, nil]
45
51
  # @param actor_token [String, nil] the acting party's token, for explicit
46
- # delegation; requires actor_token_type
52
+ # delegation; overrides the client's actor source when set and requires
53
+ # actor_token_type
47
54
  # @param actor_token_type [String, nil]
48
55
  # @param client_assertion [String, nil] client-authentication assertion,
49
56
  # form-encoded in the body; requires client_assertion_type
@@ -57,6 +64,8 @@ module Keycardai
57
64
  # authentication. Defaults to the client's issuer.
58
65
  # @return [TokenResponse]
59
66
  # @raise [ConfigurationError] when the request sets client_id with HTTP Basic
67
+ # @raise [WorkloadIdentityConfigurationError, WorkloadIdentityRuntimeError]
68
+ # when the configured actor source fails
60
69
  # @raise [OAuthError] an RFC 6749 §5.2 error response (invalid_grant,
61
70
  # invalid_target, invalid_scope, unauthorized_client, ...)
62
71
  # @raise [HTTPError, ProtocolError, NetworkError]
@@ -85,8 +94,9 @@ module Keycardai
85
94
 
86
95
  # Impersonate a named user: a substitute-user token exchange for
87
96
  # privileged operations performed on the user's behalf. No actor token
88
- # is sent; the authorization server derives the acting party from client
89
- # authentication and records it in the issued token's act chain.
97
+ # is sent even when an actor source is configured; the authorization
98
+ # server derives the acting party from client authentication and records
99
+ # it in the issued token's act chain.
90
100
  #
91
101
  # @param user_identifier [String] the target user (becomes sub)
92
102
  # @param resource [String] target resource for the issued token
@@ -98,13 +108,13 @@ module Keycardai
98
108
  def impersonate(user_identifier:, resource:, scope: nil, issuer: nil)
99
109
  raise ArgumentError, "resource must be a non-empty string" if resource.nil? || resource.empty?
100
110
 
101
- exchange_token(
102
- subject_token: OAuth.build_substitute_user_token(user_identifier),
103
- subject_token_type: TokenType::SUBSTITUTE_USER,
104
- resource: resource,
105
- scope: scope,
106
- issuer: issuer
107
- )
111
+ overrides = {
112
+ "subject_token_type" => TokenType::SUBSTITUTE_USER,
113
+ "resource" => resource,
114
+ "scope" => scope
115
+ }.compact
116
+ post_exchange(OAuth.build_substitute_user_token(user_identifier), overrides, issuer || @issuer,
117
+ include_actor: false)
108
118
  end
109
119
 
110
120
  private
@@ -115,25 +125,35 @@ module Keycardai
115
125
 
116
126
  # An explicit client_assertion authenticates the client on its own, so
117
127
  # the credential adds no Basic header and prepares no assertion.
118
- def post_exchange(subject_token, overrides, issuer)
128
+ def post_exchange(subject_token, overrides, issuer, include_actor: true)
119
129
  params = { "grant_type" => GrantType::TOKEN_EXCHANGE, "subject_token" => subject_token,
120
130
  "subject_token_type" => TokenType::ACCESS_TOKEN }.merge(overrides)
121
- return post_token_request(params, issuer: issuer, authorization: nil) if overrides["client_assertion"]
131
+ if overrides["client_assertion"]
132
+ params = with_actor(params) if include_actor
133
+ return post_token_request(params, issuer: issuer, authorization: nil)
134
+ end
122
135
 
123
136
  authorization = @credential&.authorization_header(issuer: issuer)
124
137
  reject_client_id_with_basic(overrides, authorization)
138
+ params = with_actor(params) if include_actor
125
139
  params = with_assertion(params, issuer) if @credential && !authorization
126
140
  post_token_request(params, issuer: issuer, authorization: authorization)
127
141
  end
128
142
 
129
143
  # A request client_id never travels beside HTTP Basic: fail before any
130
- # HTTP request, including discovery.
144
+ # HTTP request, including discovery, and before calling the actor source.
131
145
  def reject_client_id_with_basic(overrides, authorization)
132
146
  return unless overrides.key?("client_id") && authorization&.match?(/\ABasic\s/i)
133
147
 
134
148
  raise ConfigurationError, "token exchange client_id cannot be sent with HTTP Basic"
135
149
  end
136
150
 
151
+ def with_actor(params)
152
+ return params if !@actor || params.key?("actor_token")
153
+
154
+ params.merge("actor_token" => WorkloadIdentity.fetch_token(@actor), "actor_token_type" => TokenType::JWT)
155
+ end
156
+
137
157
  def with_assertion(params, issuer)
138
158
  prepared = @credential.prepare_token_exchange_request(
139
159
  subject_token: params.fetch("subject_token"), resource: params["resource"],
@@ -125,9 +125,11 @@ module Keycardai
125
125
  def post_token_request(params, issuer: @issuer, authorization: @credential&.authorization_header(issuer: issuer))
126
126
  headers = { "Accept" => "application/json" }
127
127
  headers["Authorization"] = authorization if authorization
128
+ post_token_form(token_endpoint(issuer), params, headers)
129
+ end
128
130
 
129
- response = @http_client.post_form(token_endpoint(issuer), params.compact, headers: headers,
130
- timeout: @timeout)
131
+ def post_token_form(endpoint, params, headers)
132
+ response = @http_client.post_form(endpoint, params.compact, headers: headers, timeout: @timeout)
131
133
  TokenRequests.parse_response(response)
132
134
  end
133
135
  end
@@ -98,11 +98,14 @@ module Keycardai
98
98
  response = begin
99
99
  @http_client.get(url, headers: { "Metadata-Flavor" => "Google" }, timeout: @timeout)
100
100
  rescue NetworkError, ResponseBodyTooLargeError => e
101
- raise WorkloadIdentityRuntimeError.new("metadata server unreachable: #{e.message}", source: "gcp-metadata")
101
+ retryable = e.is_a?(NetworkError) || e.retryable?
102
+ raise WorkloadIdentityRuntimeError.new("metadata server unreachable: #{e.message}",
103
+ source: "gcp-metadata", retryable: retryable)
102
104
  end
103
105
  unless response.success?
106
+ retryable = response.status == 429 || response.status >= 500
104
107
  raise WorkloadIdentityRuntimeError.new("metadata server returned HTTP #{response.status}",
105
- source: "gcp-metadata")
108
+ source: "gcp-metadata", retryable: retryable)
106
109
  end
107
110
 
108
111
  response.body
@@ -118,6 +121,11 @@ module Keycardai
118
121
  class FlyTokenSource
119
122
  DEFAULT_SOCKET_PATH = "/.fly/api"
120
123
  TOKEN_PATH = "/v1/tokens/oidc"
124
+ TRANSIENT_SOCKET_ERRORS = [
125
+ Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ECONNABORTED,
126
+ Errno::ETIMEDOUT, Errno::EPIPE, Errno::EAGAIN, Errno::EWOULDBLOCK,
127
+ Errno::EINTR
128
+ ].freeze
121
129
 
122
130
  # @param audience [String, nil] included as {"aud": audience} when set
123
131
  # @param socket_path [String]
@@ -132,7 +140,9 @@ module Keycardai
132
140
  body = JSON.dump(@audience ? { "aud" => @audience } : {})
133
141
  status, response_body = post_over_socket(body)
134
142
  unless (200..299).cover?(status)
135
- raise WorkloadIdentityRuntimeError.new("fly API returned HTTP #{status}", source: "fly")
143
+ retryable = status == 429 || status >= 500
144
+ raise WorkloadIdentityRuntimeError.new("fly API returned HTTP #{status}",
145
+ source: "fly", retryable: retryable)
136
146
  end
137
147
 
138
148
  response_body
@@ -154,8 +164,9 @@ module Keycardai
154
164
  parse_http_response(socket.read)
155
165
  end
156
166
  rescue SystemCallError => e
167
+ retryable = TRANSIENT_SOCKET_ERRORS.any? { |err| e.is_a?(err) }
157
168
  raise WorkloadIdentityRuntimeError.new("fly API socket #{@socket_path} unreachable: #{e.message}",
158
- source: "fly")
169
+ source: "fly", retryable: retryable)
159
170
  end
160
171
 
161
172
  def parse_http_response(raw)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Keycardai
4
4
  module OAuth
5
- VERSION = "0.13.1"
5
+ VERSION = "0.15.0"
6
6
  end
7
7
  end
@@ -24,10 +24,52 @@ module Keycardai
24
24
  # @raise [ConfigurationError] when the source is missing or has neither
25
25
  # an identity_token method nor call
26
26
  def initialize(source:, client_id: nil)
27
- @source = normalize_source(source)
27
+ @source = self.class.normalize_source(source)
28
28
  @client_id = client_id
29
29
  end
30
30
 
31
+ # Normalize an identity-token source: return an object that responds to
32
+ # +identity_token+, wrap a bare callable in +FunctionTokenSource+, or
33
+ # raise +ConfigurationError+.
34
+ #
35
+ # @param source [#identity_token, #call]
36
+ # @return [#identity_token]
37
+ # @raise [ConfigurationError]
38
+ def self.normalize_source(source)
39
+ return source if source.respond_to?(:identity_token)
40
+ return FunctionTokenSource.new(source) if source.respond_to?(:call)
41
+
42
+ raise ConfigurationError, "WorkloadIdentity source must respond to identity_token or call"
43
+ end
44
+
45
+ # Fetch a token from a normalized identity-token source. Passes
46
+ # +WorkloadIdentityConfigurationError+ and +WorkloadIdentityRuntimeError+
47
+ # through untouched, and wraps any other failure or empty token in
48
+ # +WorkloadIdentityRuntimeError+ with the source identifier and cause.
49
+ #
50
+ # @param source [#identity_token]
51
+ # @return [String]
52
+ # @raise [WorkloadIdentityConfigurationError, WorkloadIdentityRuntimeError]
53
+ def self.fetch_token(source)
54
+ token = source.identity_token
55
+ if token.nil? || token.empty?
56
+ raise WorkloadIdentityRuntimeError.new("identity token source returned an empty token",
57
+ source: source_identifier_for(source))
58
+ end
59
+
60
+ token
61
+ rescue WorkloadIdentityRuntimeError, WorkloadIdentityConfigurationError
62
+ raise
63
+ rescue StandardError => e
64
+ raise WorkloadIdentityRuntimeError.new("identity token source failed: #{e.message}",
65
+ source: source_identifier_for(source))
66
+ end
67
+
68
+ def self.source_identifier_for(source)
69
+ source.respond_to?(:source_identifier) ? source.source_identifier : "custom"
70
+ end
71
+ private_class_method :source_identifier_for
72
+
31
73
  # Shows the client ID and the source's class.
32
74
  #
33
75
  # @return [String]
@@ -95,30 +137,8 @@ module Keycardai
95
137
 
96
138
  private
97
139
 
98
- def normalize_source(source)
99
- return source if source.respond_to?(:identity_token)
100
- return FunctionTokenSource.new(source) if source.respond_to?(:call)
101
-
102
- raise ConfigurationError, "WorkloadIdentity source must respond to identity_token or call"
103
- end
104
-
105
140
  def fetch_identity_token
106
- token = @source.identity_token
107
- if token.nil? || token.empty?
108
- raise WorkloadIdentityRuntimeError.new("identity token source returned an empty token",
109
- source: source_identifier)
110
- end
111
-
112
- token
113
- rescue WorkloadIdentityRuntimeError, WorkloadIdentityConfigurationError
114
- raise
115
- rescue StandardError => e
116
- raise WorkloadIdentityRuntimeError.new("identity token source failed: #{e.message}",
117
- source: source_identifier)
118
- end
119
-
120
- def source_identifier
121
- @source.respond_to?(:source_identifier) ? @source.source_identifier : "custom"
141
+ self.class.fetch_token(@source)
122
142
  end
123
143
 
124
144
  # Adapter making a bare callable usable as an identity-token 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.13.1
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keycard