keycardai-oauth 0.17.0 → 0.17.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 +4 -4
- data/CHANGELOG.md +14 -0
- data/lib/keycardai/oauth/client_authentication.rb +2 -2
- data/lib/keycardai/oauth/deadline_socket.rb +87 -0
- data/lib/keycardai/oauth/device_authorization.rb +49 -15
- data/lib/keycardai/oauth/http.rb +23 -17
- data/lib/keycardai/oauth/request_deadline.rb +47 -0
- data/lib/keycardai/oauth/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f8ea26ce58ef5fe06ab2c0a381bd0612e14426594904e00cfd0a2611c1c0d468
|
|
4
|
+
data.tar.gz: aef6b5afdaabcd72cedb139080ff153a1327adc9ff7cfa51f9ab73eaa1f795e3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 12949b72e931ff0eaea9b1d9ee920dc7b582b84164e4350a45797c3639e8a00f6efe285f4dbd7fd3d4d317e6de2bf797bd33549f7e4ff492f190605f1aea98bf
|
|
7
|
+
data.tar.gz: 2e516f15ee72120de6fcfc00b9ddc825f8bfb6d27ea2b0d1694828d09accd8493d4b11e9c1afe017ba087c23a50978b80ceaa24510f1767c8b796c997046921c
|
data/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,20 @@ 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.17.1-keycardai-oauth (2026-11-26)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
- fix(keycardai-oauth): the request timeout bounds the credential call and the whole request (SDK-121) (#120)
|
|
22
|
+
- * fix(keycardai-oauth): the request timeout bounds the credential call and the whole request (SDK-121)
|
|
23
|
+
- * fix(keycardai-oauth): account for proxy lookup before connecting (SDK-121)
|
|
24
|
+
- * fix(keycardai-oauth): return complete responses after the deadline (SDK-121)
|
|
25
|
+
- * fix(keycardai-oauth): require explicit credential timeout keywords (SDK-121)
|
|
26
|
+
- * fix(keycardai-oauth): scope the request deadline to the device grant (SDK-121)
|
|
27
|
+
- * test(keycardai-oauth): leave the forwarding example unnumbered (SDK-121)
|
|
28
|
+
- * fix(keycardai-oauth): reflect on credential methods without dispatch (SDK-121)
|
|
29
|
+
- ---------
|
|
30
|
+
- Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
|
|
31
|
+
|
|
18
32
|
## 0.17.0-keycardai-oauth (2026-11-26)
|
|
19
33
|
|
|
20
34
|
|
|
@@ -68,9 +68,9 @@ module Keycardai
|
|
|
68
68
|
# repeated keys such as resource survive
|
|
69
69
|
# @param headers [Hash{String => String}]
|
|
70
70
|
# @return [Array(Array<Array(String, String)>, Hash{String => String})]
|
|
71
|
-
def apply(params, headers, issuer:, endpoint:, credential: nil, client_id: nil, client_secret: nil)
|
|
71
|
+
def apply(params, headers, issuer:, endpoint:, credential: nil, client_id: nil, client_secret: nil, options: {})
|
|
72
72
|
if credential
|
|
73
|
-
auth = credential.client_authentication(issuer: issuer, endpoint: endpoint)
|
|
73
|
+
auth = credential.client_authentication(issuer: issuer, endpoint: endpoint, **options)
|
|
74
74
|
[params + auth.params.to_a, headers.merge(auth.headers)]
|
|
75
75
|
elsif client_secret
|
|
76
76
|
[params, headers.merge("Authorization" => HTTP.basic_authorization(client_id, client_secret))]
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Keycardai
|
|
4
|
+
module OAuth
|
|
5
|
+
module HTTP
|
|
6
|
+
# Net::HTTP can read multiple times before yielding a body chunk, for
|
|
7
|
+
# example while parsing a header or the framing around a chunk.
|
|
8
|
+
module DeadlineSocket
|
|
9
|
+
attr_writer :deadline_session
|
|
10
|
+
|
|
11
|
+
def read_nonblock(...)
|
|
12
|
+
request_deadline&.remaining
|
|
13
|
+
super
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def write_nonblock(...)
|
|
17
|
+
request_deadline&.remaining
|
|
18
|
+
super
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def wait_readable(timeout = nil)
|
|
22
|
+
super(bounded_wait(timeout))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def wait_writable(timeout = nil)
|
|
26
|
+
super(bounded_wait(timeout))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def request_deadline
|
|
32
|
+
@deadline_session.request_deadline
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def bounded_wait(timeout)
|
|
36
|
+
remaining = request_deadline&.remaining
|
|
37
|
+
[timeout, remaining].compact.min
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
private_constant :DeadlineSocket
|
|
41
|
+
|
|
42
|
+
# Applies a fresh budget to connections and sockets on a reused session.
|
|
43
|
+
module DeadlineSession
|
|
44
|
+
attr_accessor :request_deadline
|
|
45
|
+
|
|
46
|
+
def start_with_deadline(deadline)
|
|
47
|
+
self.request_deadline = deadline
|
|
48
|
+
apply_deadline
|
|
49
|
+
start unless started?
|
|
50
|
+
apply_deadline
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def apply_deadline
|
|
56
|
+
remaining = request_deadline.remaining
|
|
57
|
+
self.open_timeout = self.read_timeout = self.write_timeout = remaining
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def connect
|
|
61
|
+
# Proxy selection can resolve the destination before Net::HTTP opens
|
|
62
|
+
# a socket. Cache the selection before setting the connect budget.
|
|
63
|
+
proxy_uri if proxy_from_env?
|
|
64
|
+
apply_deadline
|
|
65
|
+
super
|
|
66
|
+
bound_socket(@socket.io)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def timeouted_connect(...)
|
|
70
|
+
super.tap { |socket| bound_socket(socket) }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def ssl_socket_connect(socket, _timeout)
|
|
74
|
+
super(socket, request_deadline.remaining)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def bound_socket(socket)
|
|
78
|
+
[socket, socket.to_io].uniq.each do |io|
|
|
79
|
+
io.extend(DeadlineSocket)
|
|
80
|
+
io.deadline_session = self
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
private_constant :DeadlineSession
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -135,8 +135,11 @@ module Keycardai
|
|
|
135
135
|
# @param client_id [String, nil] public client; sent in every body
|
|
136
136
|
# @param http_client [#get, #post_form] pluggable transport
|
|
137
137
|
# @param timeout [Numeric, nil] per-request timeout in seconds, for
|
|
138
|
-
# discovery, the device authorization request, and each poll.
|
|
139
|
-
#
|
|
138
|
+
# discovery, the device authorization request, and each poll. The
|
|
139
|
+
# device requests share their budget with client authentication.
|
|
140
|
+
# nil uses the transport's default, 10 seconds for NetHTTPClient.
|
|
141
|
+
# Credentials can accept a timeout in seconds or a deadline Time
|
|
142
|
+
# to stop cooperatively. Otherwise the client checks when they return.
|
|
140
143
|
# @param clock [#call] returns the current Time; read for expires_at,
|
|
141
144
|
# the poll waits, and the discovery cache
|
|
142
145
|
# @param sleep [#call] waits the given number of seconds. Inject fakes
|
|
@@ -158,8 +161,8 @@ module Keycardai
|
|
|
158
161
|
@issuer = resolve_issuer(issuer, metadata)
|
|
159
162
|
@credential = credential
|
|
160
163
|
@client_id = client_id
|
|
161
|
-
@
|
|
162
|
-
|
|
164
|
+
@request = DeviceRequest.new(issuer: @issuer, credential: credential, client_id: client_id,
|
|
165
|
+
http_client: http_client, timeout: timeout)
|
|
163
166
|
@clock = clock
|
|
164
167
|
@sleep = sleep
|
|
165
168
|
@endpoints = DeviceEndpoints.new(issuer: @issuer, metadata: metadata, http_client: http_client,
|
|
@@ -324,13 +327,9 @@ module Keycardai
|
|
|
324
327
|
end
|
|
325
328
|
|
|
326
329
|
def post(endpoint, params)
|
|
327
|
-
|
|
328
|
-
@
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
def authenticate(endpoint, params)
|
|
332
|
-
ClientAuth.apply(params, { "Accept" => "application/json" }, issuer: @issuer, endpoint: endpoint,
|
|
333
|
-
credential: @credential, client_id: @client_id)
|
|
330
|
+
deadline = @request.deadline
|
|
331
|
+
params, headers = @request.authenticate(endpoint, params, @request.credential_options(deadline))
|
|
332
|
+
@request.send_form(endpoint, params, headers, deadline)
|
|
334
333
|
end
|
|
335
334
|
|
|
336
335
|
def starting_interval(authorization, interval)
|
|
@@ -373,10 +372,12 @@ module Keycardai
|
|
|
373
372
|
# poll. The credential is called outside every rescue, so its failure
|
|
374
373
|
# is raised as is.
|
|
375
374
|
def poll_step(authorization, endpoint, interval, cancellation)
|
|
376
|
-
|
|
375
|
+
deadline = @request.deadline
|
|
376
|
+
options = with_poll_transport_errors(interval) { @request.credential_options(deadline) }
|
|
377
|
+
params, headers = @request.authenticate(endpoint, poll_params(authorization), options)
|
|
377
378
|
cancellation.check
|
|
378
379
|
check_expired(authorization)
|
|
379
|
-
response =
|
|
380
|
+
response = with_poll_transport_errors(interval) { @request.send_form(endpoint, params, headers, deadline) }
|
|
380
381
|
begin
|
|
381
382
|
[TokenRequests.parse_response(response), interval]
|
|
382
383
|
rescue HTTPError => e
|
|
@@ -384,8 +385,8 @@ module Keycardai
|
|
|
384
385
|
end
|
|
385
386
|
end
|
|
386
387
|
|
|
387
|
-
def
|
|
388
|
-
|
|
388
|
+
def with_poll_transport_errors(interval)
|
|
389
|
+
yield
|
|
389
390
|
rescue NetworkError => e
|
|
390
391
|
# RFC 8628 §3.5: wait longer after a connection timeout.
|
|
391
392
|
raise DevicePollError.new("device token poll failed: #{e.message}", interval: interval * 2)
|
|
@@ -410,6 +411,39 @@ module Keycardai
|
|
|
410
411
|
end
|
|
411
412
|
end
|
|
412
413
|
|
|
414
|
+
# The credential step and the HTTP step draw from the same request budget.
|
|
415
|
+
class DeviceRequest
|
|
416
|
+
def initialize(issuer:, credential:, client_id:, http_client:, timeout:)
|
|
417
|
+
@issuer = issuer
|
|
418
|
+
@credential = credential
|
|
419
|
+
@client_id = client_id
|
|
420
|
+
@http_client = http_client
|
|
421
|
+
@timeout = timeout || (HTTP::NetHTTPClient::DEFAULT_TIMEOUT if http_client.is_a?(HTTP::NetHTTPClient))
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
def deadline
|
|
425
|
+
RequestDeadline.new(@timeout) if @timeout
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
def credential_options(deadline)
|
|
429
|
+
deadline&.credential_options(@credential) || {}
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
def authenticate(endpoint, params, options)
|
|
433
|
+
ClientAuth.apply(params, { "Accept" => "application/json" },
|
|
434
|
+
issuer: @issuer, endpoint: endpoint, credential: @credential, client_id: @client_id,
|
|
435
|
+
options: options)
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
def send_form(endpoint, params, headers, deadline)
|
|
439
|
+
remaining = deadline&.remaining
|
|
440
|
+
# Injected transports keep the numeric timeout interface.
|
|
441
|
+
timeout = @http_client.instance_of?(HTTP::NetHTTPClient) ? deadline : remaining
|
|
442
|
+
@http_client.post_form(endpoint, params, headers: headers, timeout: timeout)
|
|
443
|
+
end
|
|
444
|
+
end
|
|
445
|
+
private_constant :DeviceRequest
|
|
446
|
+
|
|
413
447
|
# The device grant's two endpoints, from pre-discovered metadata or from
|
|
414
448
|
# discovery. Discovered metadata is cached for +ttl+ seconds when both
|
|
415
449
|
# endpoints are http or https URLs. Otherwise it is not cached, and the
|
data/lib/keycardai/oauth/http.rb
CHANGED
|
@@ -5,6 +5,9 @@ require "net/http"
|
|
|
5
5
|
require "uri"
|
|
6
6
|
require "zlib"
|
|
7
7
|
|
|
8
|
+
require_relative "request_deadline"
|
|
9
|
+
require_relative "deadline_socket"
|
|
10
|
+
|
|
8
11
|
module Keycardai
|
|
9
12
|
module OAuth
|
|
10
13
|
# Pluggable HTTP transport. Every network operation in the SDK goes
|
|
@@ -188,22 +191,19 @@ module Keycardai
|
|
|
188
191
|
# Default transport backed by Net::HTTP. TLS is used for https URLs.
|
|
189
192
|
#
|
|
190
193
|
# Sessions are kept open and reused per instance, per thread, per
|
|
191
|
-
# (host, port, scheme). A thread only ever touches its own
|
|
192
|
-
# the request path takes no lock; a small mutex guards only
|
|
193
|
-
# of per-thread session maps, and dead threads are swept from
|
|
194
|
-
# it is touched. There is no transparent retry: a keepalive
|
|
195
|
-
# the server dropped while idle surfaces as NetworkError on the
|
|
196
|
-
# request, the same as any other failure.
|
|
194
|
+
# (host, port, scheme, timeout policy). A thread only ever touches its own
|
|
195
|
+
# sessions, so the request path takes no lock; a small mutex guards only
|
|
196
|
+
# the registry of per-thread session maps, and dead threads are swept from
|
|
197
|
+
# it whenever it is touched. There is no transparent retry: a keepalive
|
|
198
|
+
# connection the server dropped while idle surfaces as NetworkError on the
|
|
199
|
+
# next request, the same as any other failure.
|
|
197
200
|
#
|
|
198
201
|
# Call {#close} when no requests are in flight to finish every session.
|
|
199
202
|
# An instance that is never closed, such as the throwaway client built by
|
|
200
203
|
# module-level function defaults, keeps its sessions until it is garbage
|
|
201
204
|
# collected, which closes the underlying sockets.
|
|
202
205
|
class NetHTTPClient
|
|
203
|
-
#
|
|
204
|
-
# timeout: 10 seconds, so a request to the zone cannot wait for
|
|
205
|
-
# Net::HTTP's 60. Each one bounds a single step of the request, such as
|
|
206
|
-
# one read from the socket, not the whole request.
|
|
206
|
+
# Timeout in seconds when the caller supplies none.
|
|
207
207
|
DEFAULT_TIMEOUT = 10
|
|
208
208
|
|
|
209
209
|
# What a request raises when the connection or the response fails:
|
|
@@ -285,12 +285,15 @@ module Keycardai
|
|
|
285
285
|
private
|
|
286
286
|
|
|
287
287
|
def perform(uri, request, timeout)
|
|
288
|
-
|
|
288
|
+
deadline = timeout.is_a?(RequestDeadline)
|
|
289
|
+
http = session_for(uri, deadline: deadline)
|
|
289
290
|
configure_http(http, timeout, request)
|
|
290
291
|
response, body, body_error = execute_request(http, request)
|
|
291
292
|
build_response(response, body, body_error)
|
|
292
293
|
rescue *TRANSPORT_ERRORS => e
|
|
293
294
|
raise NetworkError, "request to #{uri.host} failed: #{e.class}"
|
|
295
|
+
ensure
|
|
296
|
+
http.request_deadline = nil if deadline && http
|
|
294
297
|
end
|
|
295
298
|
|
|
296
299
|
def execute_request(http, request)
|
|
@@ -311,10 +314,12 @@ module Keycardai
|
|
|
311
314
|
end
|
|
312
315
|
|
|
313
316
|
def configure_http(http, timeout, request)
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
317
|
+
if timeout.is_a?(RequestDeadline)
|
|
318
|
+
http.start_with_deadline(timeout)
|
|
319
|
+
else
|
|
320
|
+
http.open_timeout = http.read_timeout = http.write_timeout = timeout || DEFAULT_TIMEOUT
|
|
321
|
+
http.start unless http.started?
|
|
322
|
+
end
|
|
318
323
|
# Net::HTTP does not report a gzip stream that ends early, so the
|
|
319
324
|
# client decompresses the body itself. Assigning Accept-Encoding
|
|
320
325
|
# turns off Net::HTTP's decompression.
|
|
@@ -325,13 +330,14 @@ module Keycardai
|
|
|
325
330
|
Response.new(status: response.code.to_i, headers: response.to_hash, body: body, body_error: body_error)
|
|
326
331
|
end
|
|
327
332
|
|
|
328
|
-
def session_for(uri)
|
|
333
|
+
def session_for(uri, deadline:)
|
|
329
334
|
sessions = sessions_for_current_thread
|
|
330
|
-
key = [uri.host, uri.port, uri.scheme]
|
|
335
|
+
key = [uri.host, uri.port, uri.scheme, deadline]
|
|
331
336
|
http = sessions[key]
|
|
332
337
|
return http if http&.started?
|
|
333
338
|
|
|
334
339
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
340
|
+
http.extend(DeadlineSession) if deadline
|
|
335
341
|
http.use_ssl = uri.scheme == "https"
|
|
336
342
|
# Net::HTTP retries a failed GET once by default.
|
|
337
343
|
http.max_retries = 0
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Keycardai
|
|
4
|
+
module OAuth
|
|
5
|
+
# One monotonic budget for a request, independent of wall-clock changes.
|
|
6
|
+
class RequestDeadline
|
|
7
|
+
def initialize(timeout)
|
|
8
|
+
@expires_at = monotonic_time + timeout
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def remaining
|
|
12
|
+
seconds = @expires_at - monotonic_time
|
|
13
|
+
raise NetworkError, "request deadline exceeded" unless seconds.positive?
|
|
14
|
+
|
|
15
|
+
seconds
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def credential_options(credential)
|
|
19
|
+
seconds = remaining
|
|
20
|
+
return {} unless credential
|
|
21
|
+
|
|
22
|
+
parameters = credential_parameters(credential)
|
|
23
|
+
options = {}
|
|
24
|
+
options[:deadline] = Time.now + seconds if accepts_keyword?(parameters, :deadline)
|
|
25
|
+
options[:timeout] = seconds if accepts_keyword?(parameters, :timeout)
|
|
26
|
+
options
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def credential_parameters(credential)
|
|
32
|
+
Kernel.instance_method(:method).bind_call(credential, :client_authentication).parameters
|
|
33
|
+
rescue NameError
|
|
34
|
+
[]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def accepts_keyword?(parameters, keyword)
|
|
38
|
+
parameters.any? { |kind, name| name == keyword && %i[key keyreq].include?(kind) }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def monotonic_time
|
|
42
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
private_constant :RequestDeadline
|
|
46
|
+
end
|
|
47
|
+
end
|
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.17.
|
|
4
|
+
version: 0.17.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Keycard
|
|
@@ -44,6 +44,7 @@ files:
|
|
|
44
44
|
- lib/keycardai/oauth/client_credentials_client.rb
|
|
45
45
|
- lib/keycardai/oauth/client_secret.rb
|
|
46
46
|
- lib/keycardai/oauth/credential_discovery.rb
|
|
47
|
+
- lib/keycardai/oauth/deadline_socket.rb
|
|
47
48
|
- lib/keycardai/oauth/device_authorization.rb
|
|
48
49
|
- lib/keycardai/oauth/discovery.rb
|
|
49
50
|
- lib/keycardai/oauth/errors.rb
|
|
@@ -57,6 +58,7 @@ files:
|
|
|
57
58
|
- lib/keycardai/oauth/printed_form.rb
|
|
58
59
|
- lib/keycardai/oauth/private_key.rb
|
|
59
60
|
- lib/keycardai/oauth/registration.rb
|
|
61
|
+
- lib/keycardai/oauth/request_deadline.rb
|
|
60
62
|
- lib/keycardai/oauth/resource_binding.rb
|
|
61
63
|
- lib/keycardai/oauth/substitute_user.rb
|
|
62
64
|
- lib/keycardai/oauth/token_exchange_client.rb
|