keycardai-oauth 0.11.2 → 0.11.4
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 +25 -0
- data/lib/keycardai/oauth/client_credentials_client.rb +1 -1
- data/lib/keycardai/oauth/device_authorization.rb +5 -2
- data/lib/keycardai/oauth/discovery.rb +1 -1
- data/lib/keycardai/oauth/errors.rb +7 -2
- data/lib/keycardai/oauth/http.rb +9 -1
- data/lib/keycardai/oauth/jwks_keyring.rb +31 -11
- data/lib/keycardai/oauth/metadata_retry.rb +127 -0
- data/lib/keycardai/oauth/token_exchange_client.rb +1 -1
- data/lib/keycardai/oauth/token_requests.rb +5 -6
- data/lib/keycardai/oauth/token_verifier.rb +1 -1
- data/lib/keycardai/oauth/version.rb +1 -1
- data/lib/keycardai/oauth.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a20f252dbd82d0937d4bdac24bdbe576187116b1d461bf3242e908fc90dacac8
|
|
4
|
+
data.tar.gz: 4b0a21509728dcec65cd2ce123a23b8b2a2414bc7ae948801c22ede55a8ebdd5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 613770b6603eb39aab91f67029a88f74de895e97f00fc22843ca2a5c6a5e66402ffea6f6b2b86be1fc97d9654d6ac661bd13b933b31395e68a9b3aa789e7a0ba
|
|
7
|
+
data.tar.gz: 649de559371625575cacbaf57351844bd7b010d337d898da8edd69fefb1414d31c03f484f20e6c9e70990ab2dcda40f216fbd2fe3fbb5e92b2b28fe3fedcaa4c
|
data/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,31 @@ 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.11.4-keycardai-oauth (2026-11-25)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
- fix(keycardai-oauth): pace oversized 5xx metadata responses (#92)
|
|
22
|
+
- Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
|
|
23
|
+
Co-authored-by: Larry Osakwe <larry@keycard.ai>
|
|
24
|
+
|
|
25
|
+
## 0.11.3-keycardai-oauth (2026-11-25)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
- fix(keycardai-oauth): pace retries for cached metadata fetches (#82)
|
|
29
|
+
- * fix(keycardai-oauth): pace retries for cached metadata fetches
|
|
30
|
+
- Co-Authored-By: Larry Osakwe <larry@keycard.ai>
|
|
31
|
+
- * test(keycardai-oauth): match base conformance spec slug
|
|
32
|
+
- Co-Authored-By: Larry Osakwe <larry@keycard.ai>
|
|
33
|
+
- * fix(keycardai-oauth): synchronize and bound metadata retry pacing
|
|
34
|
+
- Co-Authored-By: Larry Osakwe <larry@keycard.ai>
|
|
35
|
+
- * fix(keycardai-oauth): use monotonic retry deadlines in default clients
|
|
36
|
+
- Co-Authored-By: Larry Osakwe <larry@keycard.ai>
|
|
37
|
+
- * test(keycardai-oauth): an oversized 429 without Retry-After starts no retry delay
|
|
38
|
+
- ---------
|
|
39
|
+
- Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
|
|
40
|
+
Co-authored-by: Larry Osakwe <larry@keycard.ai>
|
|
41
|
+
Co-authored-by: Larry Osakwe <23560354+Larry-Osakwe@users.noreply.github.com>
|
|
42
|
+
|
|
18
43
|
## 0.11.2-keycardai-oauth (2026-11-25)
|
|
19
44
|
|
|
20
45
|
|
|
@@ -28,7 +28,7 @@ module Keycardai
|
|
|
28
28
|
# given, or a credential is combined with a raw pair
|
|
29
29
|
def initialize(issuer:, credential: nil, client_id: nil, client_secret: nil,
|
|
30
30
|
http_client: HTTP::NetHTTPClient.new, timeout: nil,
|
|
31
|
-
discovery_ttl: TokenRequests::DEFAULT_DISCOVERY_TTL, clock:
|
|
31
|
+
discovery_ttl: TokenRequests::DEFAULT_DISCOVERY_TTL, clock: MetadataRetry::WALL_CLOCK)
|
|
32
32
|
initialize_token_client(issuer: issuer, credential: credential, client_id: client_id,
|
|
33
33
|
client_secret: client_secret, http_client: http_client, timeout: timeout,
|
|
34
34
|
discovery_ttl: discovery_ttl, clock: clock)
|
|
@@ -152,7 +152,7 @@ module Keycardai
|
|
|
152
152
|
# sleep that does not respond to call, a timeout that is not
|
|
153
153
|
# positive, or a negative discovery_ttl
|
|
154
154
|
def initialize(issuer: nil, metadata: nil, credential: nil, client_id: nil,
|
|
155
|
-
http_client: HTTP::NetHTTPClient.new, timeout: nil, clock:
|
|
155
|
+
http_client: HTTP::NetHTTPClient.new, timeout: nil, clock: MetadataRetry::WALL_CLOCK,
|
|
156
156
|
sleep: ->(seconds) { Kernel.sleep(seconds) },
|
|
157
157
|
discovery_ttl: TokenRequests::DEFAULT_DISCOVERY_TTL)
|
|
158
158
|
ClientAuth.validate(credential: credential, client_id: client_id)
|
|
@@ -419,6 +419,7 @@ module Keycardai
|
|
|
419
419
|
@ttl = ttl
|
|
420
420
|
@discovered = nil
|
|
421
421
|
@discovered_at = nil
|
|
422
|
+
@retry = MetadataRetry.new(@clock)
|
|
422
423
|
@mutex = Mutex.new
|
|
423
424
|
end
|
|
424
425
|
|
|
@@ -451,7 +452,9 @@ module Keycardai
|
|
|
451
452
|
@mutex.synchronize do
|
|
452
453
|
return @discovered if @discovered && @clock.call - @discovered_at <= @ttl
|
|
453
454
|
|
|
454
|
-
fetched =
|
|
455
|
+
fetched = @retry.fetch(@issuer) do
|
|
456
|
+
OAuth.fetch_authorization_server_metadata(@issuer, http_client: @http_client, timeout: @timeout)
|
|
457
|
+
end
|
|
455
458
|
if ENDPOINTS.all? { |name| http_url?(fetched.public_send(name)) }
|
|
456
459
|
@discovered = fetched
|
|
457
460
|
@discovered_at = @clock.call
|
|
@@ -53,7 +53,7 @@ module Keycardai
|
|
|
53
53
|
response = http_client.get(url, headers: { "Accept" => "application/json" }, timeout: timeout)
|
|
54
54
|
unless response.success?
|
|
55
55
|
raise HTTPError.new("discovery for #{issuer} returned HTTP #{response.status}",
|
|
56
|
-
status: response.status, body: response.body)
|
|
56
|
+
status: response.status, body: response.body, retry_after: response.retry_after)
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
Discovery.parse_metadata(issuer, response.body)
|
|
@@ -20,11 +20,13 @@ module Keycardai
|
|
|
20
20
|
attr_reader :status
|
|
21
21
|
# @return [String]
|
|
22
22
|
attr_reader :body
|
|
23
|
+
attr_reader :retry_after
|
|
23
24
|
|
|
24
|
-
def initialize(message, status:, body: "")
|
|
25
|
+
def initialize(message, status:, body: "", retry_after: nil)
|
|
25
26
|
super(message)
|
|
26
27
|
@status = status
|
|
27
28
|
@body = body
|
|
29
|
+
@retry_after = retry_after
|
|
28
30
|
end
|
|
29
31
|
end
|
|
30
32
|
|
|
@@ -115,9 +117,12 @@ module Keycardai
|
|
|
115
117
|
class ResponseBodyTooLargeError < ProtocolError
|
|
116
118
|
# @return [Integer]
|
|
117
119
|
attr_reader :limit
|
|
120
|
+
# @return [String, nil]
|
|
121
|
+
attr_reader :retry_after
|
|
118
122
|
|
|
119
|
-
def initialize(status:, limit: MAX_RESPONSE_BODY_BYTES)
|
|
123
|
+
def initialize(status:, limit: MAX_RESPONSE_BODY_BYTES, retry_after: nil)
|
|
120
124
|
@limit = limit
|
|
125
|
+
@retry_after = retry_after
|
|
121
126
|
super("response body exceeded the #{limit}-byte decoded size limit (HTTP #{status})",
|
|
122
127
|
code: "invalid_response", status: status)
|
|
123
128
|
end
|
data/lib/keycardai/oauth/http.rb
CHANGED
|
@@ -33,6 +33,13 @@ module Keycardai
|
|
|
33
33
|
def success?
|
|
34
34
|
(200..299).cover?(status)
|
|
35
35
|
end
|
|
36
|
+
|
|
37
|
+
def retry_after
|
|
38
|
+
return unless headers.respond_to?(:find)
|
|
39
|
+
|
|
40
|
+
entry = headers.find { |name, _| name.to_s.casecmp?("Retry-After") }
|
|
41
|
+
Array(entry&.last).first
|
|
42
|
+
end
|
|
36
43
|
end
|
|
37
44
|
|
|
38
45
|
# Build an HTTP Basic Authorization header value (RFC 7617) for client
|
|
@@ -172,7 +179,8 @@ module Keycardai
|
|
|
172
179
|
def check_limit(size, response, limit)
|
|
173
180
|
return if limit.nil? || size <= limit
|
|
174
181
|
|
|
175
|
-
raise ResponseBodyTooLargeError.new(status: response.code.to_i, limit: limit
|
|
182
|
+
raise ResponseBodyTooLargeError.new(status: response.code.to_i, limit: limit,
|
|
183
|
+
retry_after: response.get_fields("Retry-After")&.first)
|
|
176
184
|
end
|
|
177
185
|
end
|
|
178
186
|
private_constant :BoundedBodyReader
|
|
@@ -36,7 +36,7 @@ module Keycardai
|
|
|
36
36
|
# @param clock [#call] returns the current Time; override in tests
|
|
37
37
|
def initialize(http_client: HTTP::NetHTTPClient.new, key_ttl: DEFAULT_KEY_TTL,
|
|
38
38
|
discovery_ttl: DEFAULT_DISCOVERY_TTL, fetch_timeout: DEFAULT_FETCH_TIMEOUT,
|
|
39
|
-
refetch_cooldown: DEFAULT_REFETCH_COOLDOWN, clock:
|
|
39
|
+
refetch_cooldown: DEFAULT_REFETCH_COOLDOWN, clock: MetadataRetry::WALL_CLOCK)
|
|
40
40
|
@http_client = http_client
|
|
41
41
|
@key_ttl = key_ttl
|
|
42
42
|
@discovery_ttl = discovery_ttl
|
|
@@ -46,6 +46,9 @@ module Keycardai
|
|
|
46
46
|
@keys = {}
|
|
47
47
|
@discovery = {}
|
|
48
48
|
@key_sets = {} # jwks_uri => { keys:, fetched_at: }
|
|
49
|
+
@discovery_retry = MetadataRetry.new(@clock)
|
|
50
|
+
@jwks_retry = MetadataRetry.new(@clock)
|
|
51
|
+
@jwks_locks = {}
|
|
49
52
|
@issuer_locks = {}
|
|
50
53
|
@mutex = Mutex.new
|
|
51
54
|
end
|
|
@@ -79,10 +82,14 @@ module Keycardai
|
|
|
79
82
|
@keys.delete_if { |(cached_issuer, _), _| cached_issuer == issuer }
|
|
80
83
|
entry = @discovery.delete(issuer)
|
|
81
84
|
@key_sets.delete(entry[:jwks_uri]) if entry
|
|
85
|
+
@discovery_retry.delete(issuer)
|
|
86
|
+
@jwks_retry.delete(entry[:jwks_uri]) if entry
|
|
82
87
|
else
|
|
83
88
|
@keys.clear
|
|
84
89
|
@discovery.clear
|
|
85
90
|
@key_sets.clear
|
|
91
|
+
@discovery_retry.clear
|
|
92
|
+
@jwks_retry.clear
|
|
86
93
|
end
|
|
87
94
|
end
|
|
88
95
|
end
|
|
@@ -122,11 +129,19 @@ module Keycardai
|
|
|
122
129
|
raise_not_found(issuer, kid) if now - cached[:fetched_at] < @refetch_cooldown
|
|
123
130
|
end
|
|
124
131
|
|
|
125
|
-
keys =
|
|
126
|
-
@mutex.synchronize { @key_sets[jwks_uri] = { keys: keys, fetched_at: now } }
|
|
132
|
+
keys = refresh_jwks(jwks_uri)
|
|
127
133
|
keys.find { |candidate| candidate["kid"] == kid } || raise_not_found(issuer, kid)
|
|
128
134
|
end
|
|
129
135
|
|
|
136
|
+
def refresh_jwks(jwks_uri)
|
|
137
|
+
lock = @mutex.synchronize { @jwks_locks[jwks_uri] ||= Mutex.new }
|
|
138
|
+
lock.synchronize do
|
|
139
|
+
@jwks_retry.fetch(jwks_uri) { fetch_jwks(jwks_uri) }.tap do |fetched|
|
|
140
|
+
@mutex.synchronize { @key_sets[jwks_uri] = { keys: fetched, fetched_at: now } }
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
130
145
|
def fresh_key_set(jwks_uri)
|
|
131
146
|
@mutex.synchronize do
|
|
132
147
|
entry = @key_sets[jwks_uri]
|
|
@@ -145,8 +160,9 @@ module Keycardai
|
|
|
145
160
|
end
|
|
146
161
|
return cached if cached
|
|
147
162
|
|
|
148
|
-
jwks_uri =
|
|
149
|
-
|
|
163
|
+
jwks_uri = @discovery_retry.fetch(issuer) do
|
|
164
|
+
discover_jwks_uri(issuer).tap { |uri| assert_same_origin(issuer, uri) }
|
|
165
|
+
end
|
|
150
166
|
@mutex.synchronize { @discovery[issuer] = { jwks_uri: jwks_uri, fetched_at: now } }
|
|
151
167
|
jwks_uri
|
|
152
168
|
end
|
|
@@ -170,12 +186,8 @@ module Keycardai
|
|
|
170
186
|
end
|
|
171
187
|
|
|
172
188
|
def fetch_jwks(jwks_uri)
|
|
173
|
-
response =
|
|
174
|
-
|
|
175
|
-
rescue NetworkError, ResponseBodyTooLargeError => e
|
|
176
|
-
raise JWKSFetchError, "JWKS fetch from #{jwks_uri} failed: #{e.message}"
|
|
177
|
-
end
|
|
178
|
-
raise JWKSFetchError, "JWKS fetch from #{jwks_uri} returned HTTP #{response.status}" unless response.success?
|
|
189
|
+
response = @http_client.get(jwks_uri, headers: { "Accept" => "application/json" }, timeout: @fetch_timeout)
|
|
190
|
+
raise_jwks_http_error(jwks_uri, response) unless response.success?
|
|
179
191
|
|
|
180
192
|
document = begin
|
|
181
193
|
JSON.parse(response.body)
|
|
@@ -183,6 +195,14 @@ module Keycardai
|
|
|
183
195
|
raise JWKSFetchError, "JWKS from #{jwks_uri} is invalid JSON"
|
|
184
196
|
end
|
|
185
197
|
document["keys"] || raise(JWKSFetchError, "JWKS from #{jwks_uri} has no keys field")
|
|
198
|
+
rescue NetworkError, ResponseBodyTooLargeError => e
|
|
199
|
+
raise JWKSFetchError, "JWKS fetch from #{jwks_uri} failed: #{e.message}"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def raise_jwks_http_error(jwks_uri, response)
|
|
203
|
+
error = HTTPError.new("JWKS endpoint returned HTTP #{response.status}",
|
|
204
|
+
status: response.status, retry_after: response.retry_after)
|
|
205
|
+
raise JWKSFetchError, "JWKS fetch from #{jwks_uri} returned HTTP #{response.status}", cause: error
|
|
186
206
|
end
|
|
187
207
|
|
|
188
208
|
def import_key(jwk)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Keycardai
|
|
6
|
+
module OAuth
|
|
7
|
+
# Gates caller-initiated metadata fetches by cache key.
|
|
8
|
+
class MetadataRetry
|
|
9
|
+
WALL_CLOCK = -> { Time.now }
|
|
10
|
+
MONOTONIC_CLOCK = -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }
|
|
11
|
+
MAX_ENTRIES = 128
|
|
12
|
+
|
|
13
|
+
def self.failure_cause(error)
|
|
14
|
+
error.is_a?(JWKSError) ? error.cause : error
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.retryable?(error)
|
|
18
|
+
if error.is_a?(NetworkError)
|
|
19
|
+
cause = error.cause
|
|
20
|
+
return !(cause.is_a?(OpenSSL::SSL::SSLError) && cause.message.include?("certificate verify failed"))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
response_error?(error) && (error.status == 429 || (500..599).cover?(error.status))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.response_error?(error)
|
|
27
|
+
error.is_a?(HTTPError) || error.is_a?(ResponseBodyTooLargeError)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize(clock, monotonic_clock: clock.equal?(WALL_CLOCK) ? MONOTONIC_CLOCK : clock)
|
|
31
|
+
@wall_clock = clock
|
|
32
|
+
@monotonic_clock = monotonic_clock
|
|
33
|
+
@states = {}
|
|
34
|
+
@mutex = Mutex.new
|
|
35
|
+
@generation = 0
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def fetch(key)
|
|
39
|
+
started = @mutex.synchronize do
|
|
40
|
+
state = @states[key]
|
|
41
|
+
raise state[:error] if state && @monotonic_clock.call < state[:until]
|
|
42
|
+
|
|
43
|
+
@generation
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
begin
|
|
47
|
+
value = yield
|
|
48
|
+
rescue Keycardai::Error => e
|
|
49
|
+
@mutex.synchronize { record(key, e) if @generation == started }
|
|
50
|
+
raise
|
|
51
|
+
end
|
|
52
|
+
@mutex.synchronize { @states.delete(key) if @generation == started }
|
|
53
|
+
value
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def delete(key)
|
|
57
|
+
@mutex.synchronize do
|
|
58
|
+
@generation += 1
|
|
59
|
+
@states.delete(key)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def clear
|
|
64
|
+
@mutex.synchronize do
|
|
65
|
+
@generation += 1
|
|
66
|
+
@states.clear
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def record(key, error)
|
|
73
|
+
cause = self.class.failure_cause(error)
|
|
74
|
+
return unless self.class.retryable?(cause)
|
|
75
|
+
|
|
76
|
+
now = @monotonic_clock.call
|
|
77
|
+
next_step = @states.dig(key, :next_step) || 1
|
|
78
|
+
delay = retry_after(cause)
|
|
79
|
+
return if delay.nil? && throttled?(cause)
|
|
80
|
+
|
|
81
|
+
if delay.nil?
|
|
82
|
+
delay = [30, next_step * rand(0.8...1.2)].min
|
|
83
|
+
next_step = [64, next_step * 2].min
|
|
84
|
+
end
|
|
85
|
+
remember(key, error: error, until: now + delay, next_step: next_step)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def remember(key, state)
|
|
89
|
+
@states.delete(key)
|
|
90
|
+
@states[key] = state
|
|
91
|
+
@states.shift if @states.size > MAX_ENTRIES
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def throttled?(error)
|
|
95
|
+
self.class.response_error?(error) && error.status == 429
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def retry_after(error)
|
|
99
|
+
return unless self.class.response_error?(error) && [429, 503].include?(error.status)
|
|
100
|
+
|
|
101
|
+
header = error.retry_after.to_s.strip
|
|
102
|
+
return [60, header.to_i].min if /\A[0-9]+\z/.match?(header)
|
|
103
|
+
|
|
104
|
+
wall_now = @wall_clock.call
|
|
105
|
+
date = http_date(header, wall_now)
|
|
106
|
+
(date - wall_now).clamp(0, 60) if date
|
|
107
|
+
rescue ArgumentError
|
|
108
|
+
nil
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def http_date(header, now)
|
|
112
|
+
date = Time.httpdate(header)
|
|
113
|
+
return date if header == date.httpdate || header == date.asctime || header == date.strftime("%a %b %d %T %Y")
|
|
114
|
+
|
|
115
|
+
date = adjust_obsolete_year(date, now.getutc)
|
|
116
|
+
date if header == date.strftime("%A, %d-%b-%y %T GMT")
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def adjust_obsolete_year(date, now)
|
|
120
|
+
year = date.year + ((now.year + 50 - date.year).div(100) * 100)
|
|
121
|
+
year -= 100 if year == now.year + 50 && date.strftime("%m%d%T") > now.strftime("%m%d%T")
|
|
122
|
+
Time.utc(year, date.month, date.day, date.hour, date.min, date.sec)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
private_constant :MetadataRetry
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -28,7 +28,7 @@ module Keycardai
|
|
|
28
28
|
# given, or a credential is combined with a raw pair
|
|
29
29
|
def initialize(issuer:, credential: nil, client_id: nil, client_secret: nil,
|
|
30
30
|
http_client: HTTP::NetHTTPClient.new, timeout: nil,
|
|
31
|
-
discovery_ttl: TokenRequests::DEFAULT_DISCOVERY_TTL, clock:
|
|
31
|
+
discovery_ttl: TokenRequests::DEFAULT_DISCOVERY_TTL, clock: MetadataRetry::WALL_CLOCK)
|
|
32
32
|
initialize_token_client(issuer: issuer, credential: credential, client_id: client_id,
|
|
33
33
|
client_secret: client_secret, http_client: http_client, timeout: timeout,
|
|
34
34
|
discovery_ttl: discovery_ttl, clock: clock)
|
|
@@ -64,7 +64,7 @@ module Keycardai
|
|
|
64
64
|
private
|
|
65
65
|
|
|
66
66
|
def initialize_token_client(issuer:, credential:, client_id:, client_secret:, http_client:, timeout:,
|
|
67
|
-
discovery_ttl: DEFAULT_DISCOVERY_TTL, clock:
|
|
67
|
+
discovery_ttl: DEFAULT_DISCOVERY_TTL, clock: MetadataRetry::WALL_CLOCK)
|
|
68
68
|
validate_client_auth(credential, client_id, client_secret)
|
|
69
69
|
|
|
70
70
|
@issuer = issuer
|
|
@@ -74,6 +74,7 @@ module Keycardai
|
|
|
74
74
|
@discovery_ttl = discovery_ttl
|
|
75
75
|
@clock = clock
|
|
76
76
|
@token_endpoints = {}
|
|
77
|
+
@token_endpoint_retry = MetadataRetry.new(@clock)
|
|
77
78
|
@token_endpoint_mutex = Mutex.new
|
|
78
79
|
end
|
|
79
80
|
|
|
@@ -88,13 +89,11 @@ module Keycardai
|
|
|
88
89
|
|
|
89
90
|
# Discover a zone's token endpoint and cache it for discovery_ttl, keyed
|
|
90
91
|
# by issuer so multi-zone clients never reuse another zone's endpoint.
|
|
91
|
-
#
|
|
92
|
-
#
|
|
93
|
-
# so concurrent first calls perform a single fetch; an interrupted caller
|
|
94
|
-
# releases it and leaves nothing behind for the others.
|
|
92
|
+
# Transient failures pace subsequent fetches. The mutex serializes
|
|
93
|
+
# cold-cache callers.
|
|
95
94
|
def token_endpoint(issuer = @issuer)
|
|
96
95
|
@token_endpoint_mutex.synchronize do
|
|
97
|
-
fresh_token_endpoint(issuer) || discover_token_endpoint(issuer)
|
|
96
|
+
fresh_token_endpoint(issuer) || @token_endpoint_retry.fetch(issuer) { discover_token_endpoint(issuer) }
|
|
98
97
|
end
|
|
99
98
|
end
|
|
100
99
|
|
|
@@ -87,7 +87,7 @@ module Keycardai
|
|
|
87
87
|
# @raise [ConfigurationError] no trusted issuer or explicitly empty audiences
|
|
88
88
|
def initialize(issuers:, audiences: nil, http_client: HTTP::NetHTTPClient.new,
|
|
89
89
|
key_ttl: JWKSKeyring::DEFAULT_KEY_TTL, discovery_ttl: JWKSKeyring::DEFAULT_DISCOVERY_TTL,
|
|
90
|
-
fetch_timeout: JWKSKeyring::DEFAULT_FETCH_TIMEOUT, clock:
|
|
90
|
+
fetch_timeout: JWKSKeyring::DEFAULT_FETCH_TIMEOUT, clock: MetadataRetry::WALL_CLOCK)
|
|
91
91
|
@issuers = Array(issuers).reject { |issuer| issuer.nil? || issuer.empty? }
|
|
92
92
|
raise ConfigurationError, "TokenVerifier requires at least one trusted issuer" if @issuers.empty?
|
|
93
93
|
raise ConfigurationError, "audiences must not be empty" if audiences.is_a?(Array) && audiences.empty?
|
data/lib/keycardai/oauth.rb
CHANGED
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.11.
|
|
4
|
+
version: 0.11.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Keycard
|
|
@@ -51,6 +51,7 @@ files:
|
|
|
51
51
|
- lib/keycardai/oauth/jwks_keyring.rb
|
|
52
52
|
- lib/keycardai/oauth/jwt_signer.rb
|
|
53
53
|
- lib/keycardai/oauth/jwt_verifier.rb
|
|
54
|
+
- lib/keycardai/oauth/metadata_retry.rb
|
|
54
55
|
- lib/keycardai/oauth/pkce.rb
|
|
55
56
|
- lib/keycardai/oauth/private_key.rb
|
|
56
57
|
- lib/keycardai/oauth/registration.rb
|