keycardai-oauth 0.18.0 → 0.18.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 +3 -0
- data/lib/keycardai/oauth/jwks_keyring.rb +30 -28
- data/lib/keycardai/oauth/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d36a74f13e0df16a28c0a6251c8205776c0f8de329727ce18fcb2b9fdcce9dfc
|
|
4
|
+
data.tar.gz: a134012f91636e35412122424dfd4faf801a7dcfcbe352d40015d5eaa6c1c1c3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9f39ade6e67f37d03307bc2150d8bb65e1a137b5d24abb7438528a533dd2ac243eaa9fddf96881b01aea22d82d08ee74f8f6e218a7e2fe7512caf1e27bffff0f
|
|
7
|
+
data.tar.gz: 627daa71857429430c8a536755c663d641fee788380f9033d438998a39afbf6d3ea9221e6315dc0c25df7e70ff735c59cd344fa05a4ac2c613e181a72a323734
|
data/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,9 @@ 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.18.1-keycardai-oauth (2026-12-03)
|
|
19
|
+
- fix(keycardai-oauth): JWKS sets with entries the keyring cannot use (SDK-190) (#142)
|
|
20
|
+
|
|
18
21
|
## 0.18.0-keycardai-oauth (2026-11-26)
|
|
19
22
|
|
|
20
23
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "base64"
|
|
3
4
|
require "json"
|
|
4
5
|
require "jwt"
|
|
6
|
+
require "openssl"
|
|
5
7
|
require "uri"
|
|
6
8
|
|
|
7
9
|
module Keycardai
|
|
@@ -61,7 +63,7 @@ module Keycardai
|
|
|
61
63
|
# @raise [JWKSDiscoveryError] discovery failed or metadata has no jwks_uri
|
|
62
64
|
# @raise [JWKSUriValidationError] jwks_uri is cross-origin with the issuer
|
|
63
65
|
# @raise [JWKSFetchError] the JWKS endpoint failed
|
|
64
|
-
# @raise [JWKSKeyNotFoundError] the
|
|
66
|
+
# @raise [JWKSKeyNotFoundError] the fetched JWKS has no usable key for the kid
|
|
65
67
|
def key(issuer, kid)
|
|
66
68
|
cached = @mutex.synchronize { fresh_key(issuer, kid) }
|
|
67
69
|
return cached if cached
|
|
@@ -109,28 +111,38 @@ module Keycardai
|
|
|
109
111
|
end
|
|
110
112
|
|
|
111
113
|
def resolve(issuer, kid)
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
key = key_from_set(kid, resolve_jwks_uri(issuer))
|
|
115
|
+
raise JWKSKeyNotFoundError, "kid #{kid.inspect} not found in JWKS for #{issuer}" unless key
|
|
114
116
|
|
|
115
|
-
key
|
|
116
|
-
cache_key(issuer, kid, key)
|
|
117
|
-
key
|
|
117
|
+
key.tap { cache_key(issuer, kid, key) }
|
|
118
118
|
end
|
|
119
119
|
|
|
120
|
-
#
|
|
121
|
-
#
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
jwk = cached[:keys].find { |candidate| candidate["kid"] == kid }
|
|
127
|
-
return jwk if jwk
|
|
128
|
-
|
|
129
|
-
raise_not_found(issuer, kid) if now - cached[:fetched_at] < @refetch_cooldown
|
|
120
|
+
# Try the cached set first. If it has no usable key for the kid, refetch
|
|
121
|
+
# at most once per cooldown so a rotated-in key can be found.
|
|
122
|
+
def key_from_set(kid, jwks_uri)
|
|
123
|
+
if (cached = fresh_key_set(jwks_uri))
|
|
124
|
+
key = find_key(cached[:keys], kid)
|
|
125
|
+
return key if key || now - cached[:fetched_at] < @refetch_cooldown
|
|
130
126
|
end
|
|
131
127
|
|
|
132
|
-
|
|
133
|
-
|
|
128
|
+
find_key(refresh_jwks(jwks_uri), kid)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def find_key(keys, kid)
|
|
132
|
+
keys.lazy.grep(Hash).filter_map do |jwk|
|
|
133
|
+
next unless [jwk["kid"], jwk["kty"], jwk.fetch("use", "sig"),
|
|
134
|
+
jwk.fetch("alg", "RS256")] == [kid, "RSA", "sig", "RS256"]
|
|
135
|
+
|
|
136
|
+
next unless (parameters = jwk.values_at("n", "e")).all?(/\A[A-Za-z0-9_-]+={0,2}\z/)
|
|
137
|
+
|
|
138
|
+
Base64.urlsafe_decode64(parameters.first)
|
|
139
|
+
Base64.urlsafe_decode64(parameters.last)
|
|
140
|
+
|
|
141
|
+
key = JWT::JWK.import(jwk.transform_keys(&:to_sym)).verify_key
|
|
142
|
+
key if key.is_a?(OpenSSL::PKey::RSA)
|
|
143
|
+
rescue JWT::DecodeError, OpenSSL::OpenSSLError, ArgumentError
|
|
144
|
+
nil
|
|
145
|
+
end.first
|
|
134
146
|
end
|
|
135
147
|
|
|
136
148
|
def refresh_jwks(jwks_uri)
|
|
@@ -149,10 +161,6 @@ module Keycardai
|
|
|
149
161
|
end
|
|
150
162
|
end
|
|
151
163
|
|
|
152
|
-
def raise_not_found(issuer, kid)
|
|
153
|
-
raise JWKSKeyNotFoundError, "kid #{kid.inspect} not found in JWKS for #{issuer}"
|
|
154
|
-
end
|
|
155
|
-
|
|
156
164
|
def resolve_jwks_uri(issuer)
|
|
157
165
|
cached = @mutex.synchronize do
|
|
158
166
|
entry = @discovery[issuer]
|
|
@@ -205,12 +213,6 @@ module Keycardai
|
|
|
205
213
|
raise JWKSFetchError, "JWKS fetch from #{jwks_uri} returned HTTP #{response.status}", cause: error
|
|
206
214
|
end
|
|
207
215
|
|
|
208
|
-
def import_key(jwk)
|
|
209
|
-
JWT::JWK.import(jwk.transform_keys(&:to_sym)).verify_key
|
|
210
|
-
rescue JWT::JWKError, ArgumentError
|
|
211
|
-
raise JWKSKeyNotFoundError, "JWK with kid #{jwk["kid"].inspect} could not be converted to a verification key"
|
|
212
|
-
end
|
|
213
|
-
|
|
214
216
|
def cache_key(issuer, kid, key)
|
|
215
217
|
@mutex.synchronize do
|
|
216
218
|
@keys.shift while @keys.size >= MAX_CACHED_KEYS
|