keycardai-oauth 0.6.0 → 0.7.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 +4 -4
- data/CHANGELOG.md +203 -0
- data/LICENSE +1 -1
- data/README.md +44 -3
- data/lib/keycardai/oauth/authorization_code.rb +22 -16
- data/lib/keycardai/oauth/client_authentication.rb +81 -0
- data/lib/keycardai/oauth/client_secret.rb +31 -4
- data/lib/keycardai/oauth/device_authorization.rb +520 -0
- data/lib/keycardai/oauth/discovery.rb +11 -1
- data/lib/keycardai/oauth/errors.rb +32 -6
- data/lib/keycardai/oauth/http.rb +106 -17
- data/lib/keycardai/oauth/jwks_keyring.rb +41 -4
- data/lib/keycardai/oauth/token_requests.rb +11 -5
- data/lib/keycardai/oauth/token_types.rb +16 -7
- data/lib/keycardai/oauth/version.rb +1 -1
- data/lib/keycardai/oauth/web_identity.rb +21 -5
- data/lib/keycardai/oauth/workload_identity.rb +16 -2
- data/lib/keycardai/oauth.rb +6 -6
- 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: 45d2df1885f97c90426bf39a853f4d5143710e7cf281095c72b8bd65cddd78fa
|
|
4
|
+
data.tar.gz: 17331a897f01eb3815c8af159421bd7a15ddcc3814281abb0ed9ab1d9d4fa6cb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 122d103e7bcf1e174f79115464742ccf5ff1ea31b7985e5e363d7fd1a30f6aada593d13eba5b5bd45076d462964e6eff6cce189a9dd0bf4afe7569db7f656d48
|
|
7
|
+
data.tar.gz: af2c2e30f97e265f2e61514e0a8a0813a75326d0963f90cd959329ee39d5c9e4f4115a95f8b6c6fc62dc7a0d35a8fc71d9035187fc4a7ff61b712d8608629eee
|
data/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,209 @@ 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.7.0-keycardai-oauth (2026-11-19)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
- feat(keycardai-oauth): poll until done for the device grant (#57)
|
|
22
|
+
- * feat(keycardai-oauth): poll until done for the device grant
|
|
23
|
+
- DeviceAuthorizationClient#wait_for_token polls until the person approves
|
|
24
|
+
or denies the request, following RFC 8628 section 3.5. It waits the
|
|
25
|
+
interval before each poll, including the first. On authorization_pending
|
|
26
|
+
it polls again, and on slow_down it adds 5 seconds to the interval for
|
|
27
|
+
this poll and every later one.
|
|
28
|
+
- A network error, a request timeout, a 5xx, or a 429 stops the loop with
|
|
29
|
+
DevicePollError. Its interval is the wait the loop would have used next,
|
|
30
|
+
doubled after a network error or a timeout. Its cause is the original
|
|
31
|
+
failure, including the OAuthError from a 5xx or 429 with an OAuth error
|
|
32
|
+
body. To resume, call wait_for_token again with interval: error.interval.
|
|
33
|
+
Any other failure is raised as is. The credential's client
|
|
34
|
+
authentication is called outside every rescue, so its error is raised
|
|
35
|
+
as is whatever its class. When the next poll would start after
|
|
36
|
+
expires_at, the loop raises OAuthError expired_token without waiting or
|
|
37
|
+
sending that poll.
|
|
38
|
+
- Cancellation is a cancelled: callable, called at least every 0.5 seconds
|
|
39
|
+
of a wait, or a deadline: Time. The loop checks both before discovery,
|
|
40
|
+
during each wait, and right before each poll. Either one raises
|
|
41
|
+
CancelledError, and the loop does not call the credential or send
|
|
42
|
+
another request. A request in flight is not interrupted. The request
|
|
43
|
+
timeout bounds each poll whatever the deadline.
|
|
44
|
+
- The client takes clock: and sleep: callables, so tests run the loop
|
|
45
|
+
without real delays. A wait counts the seconds passed to sleep, so it
|
|
46
|
+
ends even if the clock does not advance. The token endpoint is resolved
|
|
47
|
+
once, before the first wait and the first expiry check.
|
|
48
|
+
- examples/device-authorization runs the flow against a zone, resumes
|
|
49
|
+
after a DevicePollError, and refreshes the token for one resource.
|
|
50
|
+
bin/selftest runs it against a stub zone.
|
|
51
|
+
- Spec: device-authorization.md spec-version 4, unit rows 8 to 19, 21, 26
|
|
52
|
+
to 29, and 31 to 33. Row 20 does not apply to Ruby (Divergences,
|
|
53
|
+
"Cancellation in Ruby"). The example labelled "20 (Ruby)" checks the
|
|
54
|
+
Ruby behavior instead.
|
|
55
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
56
|
+
- * fix(keycardai-oauth): no poll after a cancel or expiry during the credential call or a late wait
|
|
57
|
+
- wait_for_token checks for cancellation and expires_at again after the
|
|
58
|
+
credential's client authentication returns, right before the poll. It
|
|
59
|
+
checks expires_at again after each wait, so a wait that ends late does
|
|
60
|
+
not send a poll after the device code expired.
|
|
61
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
62
|
+
- * test(keycardai-oauth): slow_down increases add up across polls
|
|
63
|
+
- Row 9 gets two cases with two slow_down responses, one in a row and one
|
|
64
|
+
with authorization_pending between them. Each expects the interval to
|
|
65
|
+
rise by 5 each time (RFC 8628 Section 3.5). A loop that resets the
|
|
66
|
+
interval to authorization.interval + 5 fails both.
|
|
67
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
68
|
+
- ---------
|
|
69
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
70
|
+
- feat(keycardai-oauth): device authorization request and single poll (#55)
|
|
71
|
+
- * feat(keycardai-oauth): device authorization request and single poll
|
|
72
|
+
- Adds the device side of the RFC 8628 device authorization grant. The
|
|
73
|
+
poll loop follows in the next change.
|
|
74
|
+
- DeviceAuthorizationClient takes exactly one of credential: and
|
|
75
|
+
client_id:. request_device_authorization(scopes:, resources:) returns a
|
|
76
|
+
DeviceAuthorization, a Data value whose expires_at comes from the
|
|
77
|
+
injected clock. poll_token(authorization) polls the token endpoint once
|
|
78
|
+
and raises the server's OAuthError, including authorization_pending and
|
|
79
|
+
slow_down.
|
|
80
|
+
- Each operation checks the endpoint it sends to. A missing endpoint, or
|
|
81
|
+
one that is not an http or https URL, is a ConfigurationError before any
|
|
82
|
+
request. Discovered metadata is cached for discovery_ttl when both
|
|
83
|
+
endpoints are http or https URLs. Otherwise it is not cached, and the
|
|
84
|
+
next call fetches it again. A discovery failure is raised as is.
|
|
85
|
+
- A device authorization response that lacks a required field, has an
|
|
86
|
+
interval that is not a positive integer, or has a body that does not
|
|
87
|
+
decompress is a ProtocolError with code invalid_response.
|
|
88
|
+
- AuthorizationServerMetadata has a device_authorization_endpoint member.
|
|
89
|
+
Every member but issuer now defaults to nil, so metadata built by hand
|
|
90
|
+
with keywords keeps working. GrantType::DEVICE_CODE is the grant type.
|
|
91
|
+
- Spec: device-authorization.md spec-version 4, unit rows 1 to 7, 22 to 25,
|
|
92
|
+
29 (poll once), 30, and 31 and 32 (poll once and the device authorization
|
|
93
|
+
request). authorization-server-discovery.md rows 9 and 10.
|
|
94
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
95
|
+
- * fix(keycardai-oauth): read an empty verification_uri_complete as absent
|
|
96
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
97
|
+
- * fix(keycardai-oauth): DeviceAuthorization#inspect leaves out device_code
|
|
98
|
+
- A public client redeems device_code with its client_id alone (RFC 8628
|
|
99
|
+
Section 3.4), so logging the value leaked a working credential. Data's
|
|
100
|
+
inspect, to_s, and pretty_print now show every member except
|
|
101
|
+
device_code. to_h still includes it, so a stored authorization can poll.
|
|
102
|
+
Python's DeviceAuthorization leaves device_code out of repr the same
|
|
103
|
+
way.
|
|
104
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
105
|
+
- ---------
|
|
106
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
107
|
+
- feat(keycardai-oauth): refresh_authorization accepts a credential (#54)
|
|
108
|
+
- refresh_authorization takes credential: in place of client_id and
|
|
109
|
+
client_secret. The credential's client_authentication is called with the
|
|
110
|
+
token endpoint, and its headers and form fields go on the request. The
|
|
111
|
+
SDK does not add client_id; the credential's form fields include it when
|
|
112
|
+
the credential has one.
|
|
113
|
+
- client_id: is now optional, since a credential replaces it. These are
|
|
114
|
+
ConfigurationErrors, raised before discovery or any request:
|
|
115
|
+
- - a credential together with client_id or client_secret
|
|
116
|
+
- a credential that does not respond to client_authentication
|
|
117
|
+
- neither a credential nor a non-empty client_id, including a
|
|
118
|
+
client_secret without a client_id
|
|
119
|
+
- A call without client_id: used to raise ArgumentError for the missing
|
|
120
|
+
keyword. It now raises ConfigurationError. An empty client_id used to be
|
|
121
|
+
sent; it is now a ConfigurationError.
|
|
122
|
+
- Spec: authorization-code-pkce.md spec-version 5, unit rows 15 and 16.
|
|
123
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
124
|
+
- feat(keycardai-oauth): client authentication on credentials (#53)
|
|
125
|
+
- * feat(keycardai-oauth): client authentication on credentials
|
|
126
|
+
- ClientSecret, WebIdentity, and WorkloadIdentity respond to
|
|
127
|
+
client_authentication(issuer:, endpoint:). It returns a
|
|
128
|
+
ClientAuthentication with the headers and form fields that authenticate
|
|
129
|
+
one request, and never a grant parameter.
|
|
130
|
+
- - ClientSecret returns the HTTP Basic header for the zone named by
|
|
131
|
+
issuer. A multi-zone credential raises ConfigurationError for a zone it
|
|
132
|
+
has no credentials for.
|
|
133
|
+
- WebIdentity signs a new client_assertion with aud set to endpoint.
|
|
134
|
+
- WorkloadIdentity returns the source's token as client_assertion, and
|
|
135
|
+
client_id when configured.
|
|
136
|
+
- prepare_token_exchange_request uses the same form fields, and its params
|
|
137
|
+
are unchanged. The method is optional, so a credential defined outside
|
|
138
|
+
the gem keeps working without it.
|
|
139
|
+
- Spec: base.md spec-version 5 (credential operations), client-secret.md
|
|
140
|
+
row 7, web-identity.md rows 7 and 8, workload-identity.md rows 4b and 4c.
|
|
141
|
+
WebIdentity requires client_id at construction, so row 8 fails there.
|
|
142
|
+
Row 9 does not apply: WebIdentity has no audience_config.
|
|
143
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
144
|
+
- * fix(keycardai-oauth): ClientAuthentication and ClientSecret inspect leave out secrets
|
|
145
|
+
- headers and params hold a Basic header or a client assertion. Data's
|
|
146
|
+
inspect, to_s, and pretty_print showed both, so logging the object or
|
|
147
|
+
printing it in a console showed the client secret. All three now show
|
|
148
|
+
the header and form field names only. Python's ClientAuthentication
|
|
149
|
+
leaves both fields out of repr the same way.
|
|
150
|
+
- ClientSecret's inspect, to_s, and pretty_print show the client ID, or
|
|
151
|
+
the issuers of a multi-zone credential. An object that holds a
|
|
152
|
+
ClientSecret, such as a DeviceAuthorizationClient, showed the secret
|
|
153
|
+
through it.
|
|
154
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
155
|
+
- ---------
|
|
156
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
157
|
+
- fix(keycardai-oauth): a response body that does not decompress is a malformed response (#52)
|
|
158
|
+
- * fix(keycardai-oauth): a response body that does not decompress is a malformed response
|
|
159
|
+
- Net::HTTP decompresses a gzip or deflate body while it reads it. A body
|
|
160
|
+
that does not decompress raised Zlib::DataError, which NetHTTPClient did
|
|
161
|
+
not rescue, so a raw Zlib error left every operation in the gem.
|
|
162
|
+
- NetHTTPClient now returns the response with its status, an empty body,
|
|
163
|
+
and the Zlib error as body_error. Net::HTTP closes the connection, and
|
|
164
|
+
the next request opens a new one.
|
|
165
|
+
- On the token endpoint, a 2xx is a ProtocolError with code
|
|
166
|
+
invalid_response and the Zlib error as its cause. Any other status is an
|
|
167
|
+
HTTPError for that status, without an OAuth error body. Other operations
|
|
168
|
+
read the empty body as a malformed document and raise their own typed
|
|
169
|
+
error.
|
|
170
|
+
- Spec: device-authorization.md spec-version 4, poll once. Unit rows 31
|
|
171
|
+
and 32 are tested with the device grant later in this stack.
|
|
172
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
173
|
+
- * fix(keycardai-oauth): bound and type every NetHTTPClient failure
|
|
174
|
+
- Every exception Net::HTTP raises for a failed connection or response
|
|
175
|
+
becomes a NetworkError, including a status line or header it cannot
|
|
176
|
+
parse and an IOError. The client sets max_retries to 0, so a failed GET
|
|
177
|
+
is sent once, and applies the timeout to writes as well as to opening
|
|
178
|
+
the connection and reading.
|
|
179
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
180
|
+
- * fix(keycardai-oauth): a token response scope that is not a string is invalid_response
|
|
181
|
+
- TokenResponse.from_wire raises ProtocolError with code invalid_response
|
|
182
|
+
when scope is present and is not a string.
|
|
183
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
184
|
+
- * fix(keycardai-oauth): a response body that ends early is a NetworkError
|
|
185
|
+
- keycard-sdk-spec #109 makes a body that ends early a network error,
|
|
186
|
+
compressed or not. Net::HTTP returned a body shorter than its
|
|
187
|
+
Content-Length without an error, because ignore_eof defaults to true.
|
|
188
|
+
NetHTTPClient now sets it to false.
|
|
189
|
+
- NetHTTPClient also decompresses gzip and deflate bodies itself. Net::HTTP
|
|
190
|
+
drops the Zlib::BufError for a gzip stream that ends early inside a body
|
|
191
|
+
that arrived whole, and returns an empty body. A gzip stream that ends
|
|
192
|
+
early is now a NetworkError, with or without Content-Length. A gzip body
|
|
193
|
+
that arrived whole and is not gzip still gives a body_error with
|
|
194
|
+
Zlib::DataError, and the connection stays open for the next request.
|
|
195
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
196
|
+
- ---------
|
|
197
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
198
|
+
- fix(keycardai-oauth): percent-encode the client ID and secret in HTTP Basic (#56)
|
|
199
|
+
- RFC 6749 section 2.3.1 requires the client ID and secret to be encoded
|
|
200
|
+
before they go into the HTTP Basic credential. HTTP.basic_authorization
|
|
201
|
+
sent them as is, so a client ID with a colon split in the wrong place on
|
|
202
|
+
the server, and a secret with "%" decoded to a different value.
|
|
203
|
+
- Each value is now percent-encoded. ASCII letters, digits, and "-._~" stay
|
|
204
|
+
as they are, and a space becomes %20. Keycard zones decode each half with
|
|
205
|
+
decodeURIComponent (svc-sts packages/w3/src/auth/basic.js), and a form
|
|
206
|
+
decoder reads the same value back.
|
|
207
|
+
- Every Basic header goes through HTTP.basic_authorization: ClientSecret,
|
|
208
|
+
TokenExchangeClient, ClientCredentialsClient, exchange_authorization_code,
|
|
209
|
+
and refresh_authorization.
|
|
210
|
+
- Spec: client-secret.md spec-version 3, unit row 8.
|
|
211
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
212
|
+
|
|
213
|
+
## 0.6.1-keycardai-oauth (2026-11-06)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
- fix(keycardai-oauth): throttle JWKS refetches for unknown key ids, and time out zone requests by default (#49)
|
|
217
|
+
- JWKSKeyring keeps the last key set per jwks_uri for key_ttl, looks a kid up there before fetching, refetches for a missing kid at most once per refetch_cooldown (default 30 seconds, 0 fetches per miss), and drops the set in invalidate. NetHTTPClient gives a request with no timeout: 10 seconds instead of Net::HTTP's 60, so every request to the zone is bounded while callers can still pass timeout:. Follows typescript-sdk #309 and #310. SDK-93.
|
|
218
|
+
- Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
|
|
219
|
+
Co-authored-by: Larry Osakwe <larry@keycard.ai>
|
|
220
|
+
|
|
18
221
|
## 0.6.0-keycardai-oauth (2026-10-09)
|
|
19
222
|
|
|
20
223
|
|
data/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT LICENSE
|
|
2
2
|
|
|
3
|
-
Copyright
|
|
3
|
+
Copyright (c) 2026 Keycard Labs, Inc.
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
data/README.md
CHANGED
|
@@ -11,11 +11,12 @@ OAuth 2.0 primitives for the Keycard platform. The foundation gem of the
|
|
|
11
11
|
bundle add keycardai-oauth
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
Capabilities
|
|
14
|
+
Capabilities:
|
|
15
15
|
|
|
16
16
|
- Token exchange and impersonation (RFC 8693)
|
|
17
17
|
- Client credentials grant (RFC 6749 §4.4)
|
|
18
18
|
- Authorization code + PKCE, including the challenge-driven loopback flow (RFC 8252)
|
|
19
|
+
- Device authorization grant (RFC 8628)
|
|
19
20
|
- Dynamic client registration (RFC 7591)
|
|
20
21
|
- Authorization server discovery (RFC 8414), including the OIDC Discovery 1.0 §3 endpoints
|
|
21
22
|
- UserInfo: the signed-in user's identity claims (OIDC Core 1.0 §5.3)
|
|
@@ -106,10 +107,10 @@ change server-side.
|
|
|
106
107
|
|
|
107
108
|
### Refresh a stored grant
|
|
108
109
|
|
|
109
|
-
A token response from the authorization-code flow may
|
|
110
|
+
A token response from the authorization-code flow may have a `refresh_token`.
|
|
110
111
|
Redeem it with `refresh_authorization`, which authenticates the client the way
|
|
111
112
|
`exchange_authorization_code` does. The gem keeps no state: when the response
|
|
112
|
-
|
|
113
|
+
has a new `refresh_token`, the server rotated it and you store it in place
|
|
113
114
|
of the old one. `OAuthError` with `error == "invalid_grant"` means the user must
|
|
114
115
|
authorize again.
|
|
115
116
|
|
|
@@ -124,6 +125,46 @@ token = Keycardai::OAuth.refresh_authorization(
|
|
|
124
125
|
stored_refresh_token = token.refresh_token if token.refresh_token
|
|
125
126
|
```
|
|
126
127
|
|
|
128
|
+
A client with a credential passes `credential:` in place of `client_id` and
|
|
129
|
+
`client_secret`. `ClientSecret`, `WebIdentity`, and `WorkloadIdentity` all work
|
|
130
|
+
here. A credential of your own must respond to `client_authentication`.
|
|
131
|
+
|
|
132
|
+
### Sign in a device with no browser
|
|
133
|
+
|
|
134
|
+
The device authorization grant (RFC 8628) gets a token for a person on a
|
|
135
|
+
process with no browser. Show the person the verification URI and the user
|
|
136
|
+
code, then wait while they confirm on another device.
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
device = Keycardai::OAuth::DeviceAuthorizationClient.new(
|
|
140
|
+
issuer: "https://your-zone.keycard.cloud",
|
|
141
|
+
credential: credential, # or client_id: "..." for a public client
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
authorization = device.request_device_authorization(scopes: ["openid"])
|
|
145
|
+
puts "Open #{authorization.verification_uri_complete || authorization.verification_uri}"
|
|
146
|
+
puts "and confirm the code #{authorization.user_code}"
|
|
147
|
+
|
|
148
|
+
deadline = Time.now + 300
|
|
149
|
+
interval = nil
|
|
150
|
+
begin
|
|
151
|
+
token = device.wait_for_token(authorization, interval: interval, deadline: deadline)
|
|
152
|
+
rescue Keycardai::OAuth::DevicePollError => e
|
|
153
|
+
# A network error, a 5xx, or a 429 stopped the loop. e.cause is the failure.
|
|
154
|
+
interval = e.interval
|
|
155
|
+
retry
|
|
156
|
+
end
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`wait_for_token` raises `OAuthError` with `error == "access_denied"` when the
|
|
160
|
+
person denies the request, and `"expired_token"` when the code expires first.
|
|
161
|
+
Pass `cancelled:` (a callable) or `deadline:` (a `Time`) to stop the wait. A
|
|
162
|
+
request already in flight runs to completion. A token from a request with more
|
|
163
|
+
than one resource is not for any one of them. To get a token for one resource,
|
|
164
|
+
call `Keycardai::OAuth.refresh_authorization` with `resources: [that_resource]`.
|
|
165
|
+
See
|
|
166
|
+
[`examples/device-authorization`](https://github.com/keycardai/ruby-sdk/tree/main/examples/device-authorization).
|
|
167
|
+
|
|
127
168
|
### Act as a named user
|
|
128
169
|
|
|
129
170
|
```ruby
|
|
@@ -77,42 +77,48 @@ module Keycardai
|
|
|
77
77
|
end
|
|
78
78
|
|
|
79
79
|
# Redeem a refresh token for a new access token (RFC 6749 §6). Client
|
|
80
|
-
# authentication follows the
|
|
81
|
-
#
|
|
82
|
-
#
|
|
83
|
-
#
|
|
80
|
+
# authentication follows the grant that issued the refresh token. A
|
|
81
|
+
# public client sends client_id in the body. A confidential client
|
|
82
|
+
# authenticates with HTTP Basic and omits client_id from the body. A
|
|
83
|
+
# client with a credential adds the headers and form fields of the
|
|
84
|
+
# credential's client_authentication. The gem does not keep state: when the
|
|
85
|
+
# response has a refresh_token the server rotated it, and the caller
|
|
84
86
|
# stores it in place of the old one.
|
|
85
87
|
#
|
|
86
88
|
# @param issuer [String] the zone's issuer URL; supplies the token endpoint
|
|
87
89
|
# @param refresh_token [String] the refresh token from an earlier token response
|
|
88
|
-
# @param client_id [String]
|
|
90
|
+
# @param client_id [String, nil] the client the grant was issued to;
|
|
91
|
+
# required unless credential is set
|
|
89
92
|
# @param client_secret [String, nil] confidential-client secret
|
|
93
|
+
# @param credential [#client_authentication, nil] authenticates the
|
|
94
|
+
# refresh in place of client_id and client_secret
|
|
90
95
|
# @param resources [Array<String>] RFC 8707 resource indicators, one resource
|
|
91
96
|
# parameter per entry
|
|
92
97
|
# @param scopes [Array<String>] scopes to request, space-joined into scope
|
|
93
98
|
# @param http_client [#get, #post_form] pluggable transport
|
|
94
99
|
# @param timeout [Numeric, nil]
|
|
95
100
|
# @return [TokenResponse]
|
|
101
|
+
# @raise [ConfigurationError] before any request: credential set together
|
|
102
|
+
# with client_id or client_secret, a credential without
|
|
103
|
+
# client_authentication, or neither credential nor client_id
|
|
96
104
|
# @raise [OAuthError] an RFC 6749 §5.2 error response; error == "invalid_grant"
|
|
97
105
|
# means the refresh token is expired, revoked, or unknown and the user must
|
|
98
106
|
# authorize again
|
|
99
107
|
# @raise [HTTPError] a non-2xx response with no OAuth error body (5xx, 429)
|
|
100
108
|
# @raise [NetworkError, ProtocolError]
|
|
101
|
-
def self.refresh_authorization(issuer, refresh_token:, client_id
|
|
102
|
-
scopes: [], http_client: HTTP::NetHTTPClient.new, timeout: nil)
|
|
109
|
+
def self.refresh_authorization(issuer, refresh_token:, client_id: nil, client_secret: nil, credential: nil,
|
|
110
|
+
resources: [], scopes: [], http_client: HTTP::NetHTTPClient.new, timeout: nil)
|
|
111
|
+
ClientAuth.validate(credential: credential, client_id: client_id, client_secret: client_secret)
|
|
103
112
|
metadata = fetch_authorization_server_metadata(issuer, http_client: http_client, timeout: timeout)
|
|
104
113
|
endpoint = metadata.token_endpoint ||
|
|
105
114
|
raise(ProtocolError.new("metadata for #{issuer} has no token_endpoint", code: "invalid_metadata"))
|
|
106
115
|
|
|
107
|
-
params =
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
params = params.to_a + Array(resources).map { |resource| ["resource", resource] }
|
|
114
|
-
headers = { "Accept" => "application/json" }
|
|
115
|
-
headers["Authorization"] = HTTP.basic_authorization(client_id, client_secret) if client_secret
|
|
116
|
+
params = [["grant_type", GrantType::REFRESH_TOKEN], ["refresh_token", refresh_token]]
|
|
117
|
+
params << ["scope", Array(scopes).join(" ")] unless Array(scopes).empty?
|
|
118
|
+
params.concat(Array(resources).map { |resource| ["resource", resource] })
|
|
119
|
+
params, headers = ClientAuth.apply(params, { "Accept" => "application/json" },
|
|
120
|
+
issuer: issuer, endpoint: endpoint, credential: credential,
|
|
121
|
+
client_id: client_id, client_secret: client_secret)
|
|
116
122
|
|
|
117
123
|
TokenRequests.parse_response(http_client.post_form(endpoint, params, headers: headers, timeout: timeout))
|
|
118
124
|
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Keycardai
|
|
4
|
+
module OAuth
|
|
5
|
+
# The headers and form fields that authenticate a client on one request
|
|
6
|
+
# to the authorization server. A credential that provides client
|
|
7
|
+
# authentication responds to client_authentication(issuer:, endpoint:)
|
|
8
|
+
# and returns one. The method is optional, so a credential defined
|
|
9
|
+
# outside the SDK keeps working without it.
|
|
10
|
+
#
|
|
11
|
+
# It never includes a grant parameter such as grant_type or
|
|
12
|
+
# subject_token_type. The caller merges it into its own request.
|
|
13
|
+
ClientAuthentication = Data.define(:headers, :params) do
|
|
14
|
+
# @param headers [Hash{String => String}]
|
|
15
|
+
# @param params [Hash{String => String}] form fields
|
|
16
|
+
def initialize(headers: {}, params: {})
|
|
17
|
+
super
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Shows header and form field names only. The values include a client
|
|
21
|
+
# secret or a client assertion. NEVER log #to_h.
|
|
22
|
+
#
|
|
23
|
+
# @return [String]
|
|
24
|
+
def inspect
|
|
25
|
+
"#<data #{self.class} headers=#{headers.keys.inspect}, params=#{params.keys.inspect}>"
|
|
26
|
+
end
|
|
27
|
+
alias_method :to_s, :inspect
|
|
28
|
+
|
|
29
|
+
# Used by pp.
|
|
30
|
+
def pretty_print(printer)
|
|
31
|
+
printer.text(inspect)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Internals for operations that authenticate with a credential's client
|
|
36
|
+
# authentication, a client_id and client_secret pair, or a public
|
|
37
|
+
# client_id. Not public API.
|
|
38
|
+
module ClientAuth
|
|
39
|
+
module_function
|
|
40
|
+
|
|
41
|
+
# Raise ConfigurationError when credential is set with client_id or
|
|
42
|
+
# client_secret, when credential does not respond to
|
|
43
|
+
# client_authentication, or when neither credential nor a non-empty
|
|
44
|
+
# client_id is set. Call it before any request.
|
|
45
|
+
#
|
|
46
|
+
# @raise [ConfigurationError]
|
|
47
|
+
def validate(credential:, client_id:, client_secret: nil)
|
|
48
|
+
if credential
|
|
49
|
+
given = { "client_id" => client_id, "client_secret" => client_secret }.compact.keys
|
|
50
|
+
raise ConfigurationError, "pass a credential or #{given.join(" and ")}, not both" unless given.empty?
|
|
51
|
+
return if credential.respond_to?(:client_authentication)
|
|
52
|
+
|
|
53
|
+
raise ConfigurationError, "#{credential.class} does not provide client authentication"
|
|
54
|
+
end
|
|
55
|
+
return unless client_id.nil? || client_id.empty?
|
|
56
|
+
|
|
57
|
+
raise ConfigurationError, "pass a credential or a client_id"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Add client authentication to a request. A credential adds what its
|
|
61
|
+
# client_authentication returns for this endpoint. A client_secret adds
|
|
62
|
+
# an HTTP Basic header. A public client adds client_id to the body.
|
|
63
|
+
#
|
|
64
|
+
# @param params [Array<Array(String, String)>] form fields as pairs, so
|
|
65
|
+
# repeated keys such as resource survive
|
|
66
|
+
# @param headers [Hash{String => String}]
|
|
67
|
+
# @return [Array(Array<Array(String, String)>, Hash{String => String})]
|
|
68
|
+
def apply(params, headers, issuer:, endpoint:, credential: nil, client_id: nil, client_secret: nil)
|
|
69
|
+
if credential
|
|
70
|
+
auth = credential.client_authentication(issuer: issuer, endpoint: endpoint)
|
|
71
|
+
[params + auth.params.to_a, headers.merge(auth.headers)]
|
|
72
|
+
elsif client_secret
|
|
73
|
+
[params, headers.merge("Authorization" => HTTP.basic_authorization(client_id, client_secret))]
|
|
74
|
+
else
|
|
75
|
+
[params + [["client_id", client_id]], headers]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
private_constant :ClientAuth
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -7,9 +7,10 @@ module Keycardai
|
|
|
7
7
|
# credential type, suitable for development and workloads that can safely
|
|
8
8
|
# hold a long-lived secret.
|
|
9
9
|
#
|
|
10
|
-
# Implements the Credential interface: authorization_header(issuer:)
|
|
11
|
-
# prepare_token_exchange_request. Never reads
|
|
12
|
-
# caller sources the secret from its own
|
|
10
|
+
# Implements the Credential interface: authorization_header(issuer:),
|
|
11
|
+
# prepare_token_exchange_request, and client_authentication. Never reads
|
|
12
|
+
# environment variables; the caller sources the secret from its own
|
|
13
|
+
# config or secret manager.
|
|
13
14
|
#
|
|
14
15
|
# Single zone:
|
|
15
16
|
# ClientSecret.new("client_abc", "secret")
|
|
@@ -62,7 +63,33 @@ module Keycardai
|
|
|
62
63
|
HTTP.basic_authorization(client_id, client_secret)
|
|
63
64
|
end
|
|
64
65
|
|
|
65
|
-
#
|
|
66
|
+
# Client authentication for one request: the Basic header and no form
|
|
67
|
+
# fields.
|
|
68
|
+
#
|
|
69
|
+
# @param issuer [String, nil] the target zone; required for multi-zone
|
|
70
|
+
# @param endpoint [String] the URL the request goes to; unused here
|
|
71
|
+
# @return [ClientAuthentication]
|
|
72
|
+
# @raise [ConfigurationError] multi-zone lookup for an unconfigured zone
|
|
73
|
+
def client_authentication(issuer:, endpoint:)
|
|
74
|
+
ClientAuthentication.new(headers: { "Authorization" => authorization_header(issuer: issuer) })
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Shows the client ID, or the issuers of a multi-zone credential. Never
|
|
78
|
+
# shows a client secret.
|
|
79
|
+
#
|
|
80
|
+
# @return [String]
|
|
81
|
+
def inspect
|
|
82
|
+
shown = multi_zone? ? "issuers=#{issuers.inspect}" : "client_id=#{@pair.first.inspect}"
|
|
83
|
+
"#<#{self.class} #{shown}>"
|
|
84
|
+
end
|
|
85
|
+
alias to_s inspect
|
|
86
|
+
|
|
87
|
+
# Used by pp.
|
|
88
|
+
def pretty_print(printer)
|
|
89
|
+
printer.text(inspect)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Build the token-exchange form parameters. Client authentication goes
|
|
66
93
|
# in the Basic header, never in the body.
|
|
67
94
|
#
|
|
68
95
|
# @param subject_token [String]
|