keycardai-oauth 0.6.1 → 0.7.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 +215 -0
- data/README.md +46 -2
- data/lib/keycardai/oauth/authenticate.rb +16 -7
- data/lib/keycardai/oauth/authorization_code.rb +47 -31
- data/lib/keycardai/oauth/client_authentication.rb +86 -0
- data/lib/keycardai/oauth/client_credentials_client.rb +12 -3
- 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 +101 -12
- data/lib/keycardai/oauth/token_exchange_client.rb +5 -2
- 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 +35 -5
- data/lib/keycardai/oauth.rb +6 -4
- 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: 1ffd084e2ae559bd7d4cb10acd883e13b81ccaf301348ff89a7b412aaedaf540
|
|
4
|
+
data.tar.gz: 7e8cc6b15aecffd015acf9ded6727abb83e2aa87cd35522c2e7694bc8414b82d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 10d0e36beca8ca688172e01ecdf7fdb1131ecd44acfad1e6634c2f71f77458f9d31f7aeeec5f19f25f9dce03198662f111c023e5c8e76eb11728d4054d9f0a2c
|
|
7
|
+
data.tar.gz: c663311fbc07ac0a8880f8197c3371483e177c61a70d531ddbe9c125e51eb3e6c2ebca7807267250207c93e35b068f2e225f31549711f6989052751dba81368d
|
data/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,221 @@ 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.1-keycardai-oauth (2026-11-23)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
- fix(keycardai-oauth): WorkloadIdentity requires client_id before an interactive grant (#66)
|
|
22
|
+
- * fix(keycardai-oauth): WorkloadIdentity requires client_id before an interactive grant
|
|
23
|
+
- The refresh and device grants need a v2 token credential, which the zone
|
|
24
|
+
resolves by client_id. WorkloadIdentity gets check_client_authentication,
|
|
25
|
+
and ClientAuth.validate calls it when a credential responds to it, so
|
|
26
|
+
refresh_authorization and DeviceAuthorizationClient.new raise
|
|
27
|
+
ConfigurationError before any request, discovery included, when client_id
|
|
28
|
+
is not set. Token exchange does not check.
|
|
29
|
+
- Workload Identity spec-version 4 (rows 4d, 4e), Authorization Code with
|
|
30
|
+
PKCE spec-version 8 (row 34), Device Authorization spec-version 5 (row 34).
|
|
31
|
+
- Refs SDK-122
|
|
32
|
+
- * fix(keycardai-oauth): check the credential before the challenge's resource metadata fetch
|
|
33
|
+
- * docs(keycardai-oauth): name the WorkloadIdentity client_id requirement on every interactive grant
|
|
34
|
+
- ---------
|
|
35
|
+
- Co-authored-by: Larry Osakwe <23560354+Larry-Osakwe@users.noreply.github.com>
|
|
36
|
+
Co-authored-by: Larry Osakwe <larry@keycard.ai>
|
|
37
|
+
|
|
38
|
+
## 0.7.0-keycardai-oauth (2026-11-19)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
- feat(keycardai-oauth): poll until done for the device grant (#57)
|
|
42
|
+
- * feat(keycardai-oauth): poll until done for the device grant
|
|
43
|
+
- DeviceAuthorizationClient#wait_for_token polls until the person approves
|
|
44
|
+
or denies the request, following RFC 8628 section 3.5. It waits the
|
|
45
|
+
interval before each poll, including the first. On authorization_pending
|
|
46
|
+
it polls again, and on slow_down it adds 5 seconds to the interval for
|
|
47
|
+
this poll and every later one.
|
|
48
|
+
- A network error, a request timeout, a 5xx, or a 429 stops the loop with
|
|
49
|
+
DevicePollError. Its interval is the wait the loop would have used next,
|
|
50
|
+
doubled after a network error or a timeout. Its cause is the original
|
|
51
|
+
failure, including the OAuthError from a 5xx or 429 with an OAuth error
|
|
52
|
+
body. To resume, call wait_for_token again with interval: error.interval.
|
|
53
|
+
Any other failure is raised as is. The credential's client
|
|
54
|
+
authentication is called outside every rescue, so its error is raised
|
|
55
|
+
as is whatever its class. When the next poll would start after
|
|
56
|
+
expires_at, the loop raises OAuthError expired_token without waiting or
|
|
57
|
+
sending that poll.
|
|
58
|
+
- Cancellation is a cancelled: callable, called at least every 0.5 seconds
|
|
59
|
+
of a wait, or a deadline: Time. The loop checks both before discovery,
|
|
60
|
+
during each wait, and right before each poll. Either one raises
|
|
61
|
+
CancelledError, and the loop does not call the credential or send
|
|
62
|
+
another request. A request in flight is not interrupted. The request
|
|
63
|
+
timeout bounds each poll whatever the deadline.
|
|
64
|
+
- The client takes clock: and sleep: callables, so tests run the loop
|
|
65
|
+
without real delays. A wait counts the seconds passed to sleep, so it
|
|
66
|
+
ends even if the clock does not advance. The token endpoint is resolved
|
|
67
|
+
once, before the first wait and the first expiry check.
|
|
68
|
+
- examples/device-authorization runs the flow against a zone, resumes
|
|
69
|
+
after a DevicePollError, and refreshes the token for one resource.
|
|
70
|
+
bin/selftest runs it against a stub zone.
|
|
71
|
+
- Spec: device-authorization.md spec-version 4, unit rows 8 to 19, 21, 26
|
|
72
|
+
to 29, and 31 to 33. Row 20 does not apply to Ruby (Divergences,
|
|
73
|
+
"Cancellation in Ruby"). The example labelled "20 (Ruby)" checks the
|
|
74
|
+
Ruby behavior instead.
|
|
75
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
76
|
+
- * fix(keycardai-oauth): no poll after a cancel or expiry during the credential call or a late wait
|
|
77
|
+
- wait_for_token checks for cancellation and expires_at again after the
|
|
78
|
+
credential's client authentication returns, right before the poll. It
|
|
79
|
+
checks expires_at again after each wait, so a wait that ends late does
|
|
80
|
+
not send a poll after the device code expired.
|
|
81
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
82
|
+
- * test(keycardai-oauth): slow_down increases add up across polls
|
|
83
|
+
- Row 9 gets two cases with two slow_down responses, one in a row and one
|
|
84
|
+
with authorization_pending between them. Each expects the interval to
|
|
85
|
+
rise by 5 each time (RFC 8628 Section 3.5). A loop that resets the
|
|
86
|
+
interval to authorization.interval + 5 fails both.
|
|
87
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
88
|
+
- ---------
|
|
89
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
90
|
+
- feat(keycardai-oauth): device authorization request and single poll (#55)
|
|
91
|
+
- * feat(keycardai-oauth): device authorization request and single poll
|
|
92
|
+
- Adds the device side of the RFC 8628 device authorization grant. The
|
|
93
|
+
poll loop follows in the next change.
|
|
94
|
+
- DeviceAuthorizationClient takes exactly one of credential: and
|
|
95
|
+
client_id:. request_device_authorization(scopes:, resources:) returns a
|
|
96
|
+
DeviceAuthorization, a Data value whose expires_at comes from the
|
|
97
|
+
injected clock. poll_token(authorization) polls the token endpoint once
|
|
98
|
+
and raises the server's OAuthError, including authorization_pending and
|
|
99
|
+
slow_down.
|
|
100
|
+
- Each operation checks the endpoint it sends to. A missing endpoint, or
|
|
101
|
+
one that is not an http or https URL, is a ConfigurationError before any
|
|
102
|
+
request. Discovered metadata is cached for discovery_ttl when both
|
|
103
|
+
endpoints are http or https URLs. Otherwise it is not cached, and the
|
|
104
|
+
next call fetches it again. A discovery failure is raised as is.
|
|
105
|
+
- A device authorization response that lacks a required field, has an
|
|
106
|
+
interval that is not a positive integer, or has a body that does not
|
|
107
|
+
decompress is a ProtocolError with code invalid_response.
|
|
108
|
+
- AuthorizationServerMetadata has a device_authorization_endpoint member.
|
|
109
|
+
Every member but issuer now defaults to nil, so metadata built by hand
|
|
110
|
+
with keywords keeps working. GrantType::DEVICE_CODE is the grant type.
|
|
111
|
+
- Spec: device-authorization.md spec-version 4, unit rows 1 to 7, 22 to 25,
|
|
112
|
+
29 (poll once), 30, and 31 and 32 (poll once and the device authorization
|
|
113
|
+
request). authorization-server-discovery.md rows 9 and 10.
|
|
114
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
115
|
+
- * fix(keycardai-oauth): read an empty verification_uri_complete as absent
|
|
116
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
117
|
+
- * fix(keycardai-oauth): DeviceAuthorization#inspect leaves out device_code
|
|
118
|
+
- A public client redeems device_code with its client_id alone (RFC 8628
|
|
119
|
+
Section 3.4), so logging the value leaked a working credential. Data's
|
|
120
|
+
inspect, to_s, and pretty_print now show every member except
|
|
121
|
+
device_code. to_h still includes it, so a stored authorization can poll.
|
|
122
|
+
Python's DeviceAuthorization leaves device_code out of repr the same
|
|
123
|
+
way.
|
|
124
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
125
|
+
- ---------
|
|
126
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
127
|
+
- feat(keycardai-oauth): refresh_authorization accepts a credential (#54)
|
|
128
|
+
- refresh_authorization takes credential: in place of client_id and
|
|
129
|
+
client_secret. The credential's client_authentication is called with the
|
|
130
|
+
token endpoint, and its headers and form fields go on the request. The
|
|
131
|
+
SDK does not add client_id; the credential's form fields include it when
|
|
132
|
+
the credential has one.
|
|
133
|
+
- client_id: is now optional, since a credential replaces it. These are
|
|
134
|
+
ConfigurationErrors, raised before discovery or any request:
|
|
135
|
+
- - a credential together with client_id or client_secret
|
|
136
|
+
- a credential that does not respond to client_authentication
|
|
137
|
+
- neither a credential nor a non-empty client_id, including a
|
|
138
|
+
client_secret without a client_id
|
|
139
|
+
- A call without client_id: used to raise ArgumentError for the missing
|
|
140
|
+
keyword. It now raises ConfigurationError. An empty client_id used to be
|
|
141
|
+
sent; it is now a ConfigurationError.
|
|
142
|
+
- Spec: authorization-code-pkce.md spec-version 5, unit rows 15 and 16.
|
|
143
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
144
|
+
- feat(keycardai-oauth): client authentication on credentials (#53)
|
|
145
|
+
- * feat(keycardai-oauth): client authentication on credentials
|
|
146
|
+
- ClientSecret, WebIdentity, and WorkloadIdentity respond to
|
|
147
|
+
client_authentication(issuer:, endpoint:). It returns a
|
|
148
|
+
ClientAuthentication with the headers and form fields that authenticate
|
|
149
|
+
one request, and never a grant parameter.
|
|
150
|
+
- - ClientSecret returns the HTTP Basic header for the zone named by
|
|
151
|
+
issuer. A multi-zone credential raises ConfigurationError for a zone it
|
|
152
|
+
has no credentials for.
|
|
153
|
+
- WebIdentity signs a new client_assertion with aud set to endpoint.
|
|
154
|
+
- WorkloadIdentity returns the source's token as client_assertion, and
|
|
155
|
+
client_id when configured.
|
|
156
|
+
- prepare_token_exchange_request uses the same form fields, and its params
|
|
157
|
+
are unchanged. The method is optional, so a credential defined outside
|
|
158
|
+
the gem keeps working without it.
|
|
159
|
+
- Spec: base.md spec-version 5 (credential operations), client-secret.md
|
|
160
|
+
row 7, web-identity.md rows 7 and 8, workload-identity.md rows 4b and 4c.
|
|
161
|
+
WebIdentity requires client_id at construction, so row 8 fails there.
|
|
162
|
+
Row 9 does not apply: WebIdentity has no audience_config.
|
|
163
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
164
|
+
- * fix(keycardai-oauth): ClientAuthentication and ClientSecret inspect leave out secrets
|
|
165
|
+
- headers and params hold a Basic header or a client assertion. Data's
|
|
166
|
+
inspect, to_s, and pretty_print showed both, so logging the object or
|
|
167
|
+
printing it in a console showed the client secret. All three now show
|
|
168
|
+
the header and form field names only. Python's ClientAuthentication
|
|
169
|
+
leaves both fields out of repr the same way.
|
|
170
|
+
- ClientSecret's inspect, to_s, and pretty_print show the client ID, or
|
|
171
|
+
the issuers of a multi-zone credential. An object that holds a
|
|
172
|
+
ClientSecret, such as a DeviceAuthorizationClient, showed the secret
|
|
173
|
+
through it.
|
|
174
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
175
|
+
- ---------
|
|
176
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
177
|
+
- fix(keycardai-oauth): a response body that does not decompress is a malformed response (#52)
|
|
178
|
+
- * fix(keycardai-oauth): a response body that does not decompress is a malformed response
|
|
179
|
+
- Net::HTTP decompresses a gzip or deflate body while it reads it. A body
|
|
180
|
+
that does not decompress raised Zlib::DataError, which NetHTTPClient did
|
|
181
|
+
not rescue, so a raw Zlib error left every operation in the gem.
|
|
182
|
+
- NetHTTPClient now returns the response with its status, an empty body,
|
|
183
|
+
and the Zlib error as body_error. Net::HTTP closes the connection, and
|
|
184
|
+
the next request opens a new one.
|
|
185
|
+
- On the token endpoint, a 2xx is a ProtocolError with code
|
|
186
|
+
invalid_response and the Zlib error as its cause. Any other status is an
|
|
187
|
+
HTTPError for that status, without an OAuth error body. Other operations
|
|
188
|
+
read the empty body as a malformed document and raise their own typed
|
|
189
|
+
error.
|
|
190
|
+
- Spec: device-authorization.md spec-version 4, poll once. Unit rows 31
|
|
191
|
+
and 32 are tested with the device grant later in this stack.
|
|
192
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
193
|
+
- * fix(keycardai-oauth): bound and type every NetHTTPClient failure
|
|
194
|
+
- Every exception Net::HTTP raises for a failed connection or response
|
|
195
|
+
becomes a NetworkError, including a status line or header it cannot
|
|
196
|
+
parse and an IOError. The client sets max_retries to 0, so a failed GET
|
|
197
|
+
is sent once, and applies the timeout to writes as well as to opening
|
|
198
|
+
the connection and reading.
|
|
199
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
200
|
+
- * fix(keycardai-oauth): a token response scope that is not a string is invalid_response
|
|
201
|
+
- TokenResponse.from_wire raises ProtocolError with code invalid_response
|
|
202
|
+
when scope is present and is not a string.
|
|
203
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
204
|
+
- * fix(keycardai-oauth): a response body that ends early is a NetworkError
|
|
205
|
+
- keycard-sdk-spec #109 makes a body that ends early a network error,
|
|
206
|
+
compressed or not. Net::HTTP returned a body shorter than its
|
|
207
|
+
Content-Length without an error, because ignore_eof defaults to true.
|
|
208
|
+
NetHTTPClient now sets it to false.
|
|
209
|
+
- NetHTTPClient also decompresses gzip and deflate bodies itself. Net::HTTP
|
|
210
|
+
drops the Zlib::BufError for a gzip stream that ends early inside a body
|
|
211
|
+
that arrived whole, and returns an empty body. A gzip stream that ends
|
|
212
|
+
early is now a NetworkError, with or without Content-Length. A gzip body
|
|
213
|
+
that arrived whole and is not gzip still gives a body_error with
|
|
214
|
+
Zlib::DataError, and the connection stays open for the next request.
|
|
215
|
+
- Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
216
|
+
- ---------
|
|
217
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
218
|
+
- fix(keycardai-oauth): percent-encode the client ID and secret in HTTP Basic (#56)
|
|
219
|
+
- RFC 6749 section 2.3.1 requires the client ID and secret to be encoded
|
|
220
|
+
before they go into the HTTP Basic credential. HTTP.basic_authorization
|
|
221
|
+
sent them as is, so a client ID with a colon split in the wrong place on
|
|
222
|
+
the server, and a secret with "%" decoded to a different value.
|
|
223
|
+
- Each value is now percent-encoded. ASCII letters, digits, and "-._~" stay
|
|
224
|
+
as they are, and a space becomes %20. Keycard zones decode each half with
|
|
225
|
+
decodeURIComponent (svc-sts packages/w3/src/auth/basic.js), and a form
|
|
226
|
+
decoder reads the same value back.
|
|
227
|
+
- Every Basic header goes through HTTP.basic_authorization: ClientSecret,
|
|
228
|
+
TokenExchangeClient, ClientCredentialsClient, exchange_authorization_code,
|
|
229
|
+
and refresh_authorization.
|
|
230
|
+
- Spec: client-secret.md spec-version 3, unit row 8.
|
|
231
|
+
- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
|
232
|
+
|
|
18
233
|
## 0.6.1-keycardai-oauth (2026-11-06)
|
|
19
234
|
|
|
20
235
|
|
data/README.md
CHANGED
|
@@ -16,6 +16,7 @@ Capabilities:
|
|
|
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,49 @@ 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 `WorkloadIdentity` needs `client_id:` (the public ID of a v2 token
|
|
131
|
+
credential) for refresh, the code exchange, `authenticate` and device
|
|
132
|
+
authorization; without it these raise `ConfigurationError` before any request.
|
|
133
|
+
A credential of your own must respond to `client_authentication`.
|
|
134
|
+
|
|
135
|
+
### Sign in a device with no browser
|
|
136
|
+
|
|
137
|
+
The device authorization grant (RFC 8628) gets a token for a person on a
|
|
138
|
+
process with no browser. Show the person the verification URI and the user
|
|
139
|
+
code, then wait while they confirm on another device.
|
|
140
|
+
|
|
141
|
+
```ruby
|
|
142
|
+
device = Keycardai::OAuth::DeviceAuthorizationClient.new(
|
|
143
|
+
issuer: "https://your-zone.keycard.cloud",
|
|
144
|
+
credential: credential, # or client_id: "..." for a public client
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
authorization = device.request_device_authorization(scopes: ["openid"])
|
|
148
|
+
puts "Open #{authorization.verification_uri_complete || authorization.verification_uri}"
|
|
149
|
+
puts "and confirm the code #{authorization.user_code}"
|
|
150
|
+
|
|
151
|
+
deadline = Time.now + 300
|
|
152
|
+
interval = nil
|
|
153
|
+
begin
|
|
154
|
+
token = device.wait_for_token(authorization, interval: interval, deadline: deadline)
|
|
155
|
+
rescue Keycardai::OAuth::DevicePollError => e
|
|
156
|
+
# A network error, a 5xx, or a 429 stopped the loop. e.cause is the failure.
|
|
157
|
+
interval = e.interval
|
|
158
|
+
retry
|
|
159
|
+
end
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`wait_for_token` raises `OAuthError` with `error == "access_denied"` when the
|
|
163
|
+
person denies the request, and `"expired_token"` when the code expires first.
|
|
164
|
+
Pass `cancelled:` (a callable) or `deadline:` (a `Time`) to stop the wait. A
|
|
165
|
+
request already in flight runs to completion. A token from a request with more
|
|
166
|
+
than one resource is not for any one of them. To get a token for one resource,
|
|
167
|
+
call `Keycardai::OAuth.refresh_authorization` with `resources: [that_resource]`.
|
|
168
|
+
See
|
|
169
|
+
[`examples/device-authorization`](https://github.com/keycardai/ruby-sdk/tree/main/examples/device-authorization).
|
|
170
|
+
|
|
127
171
|
### Act as a named user
|
|
128
172
|
|
|
129
173
|
```ruby
|
|
@@ -25,6 +25,9 @@ module Keycardai
|
|
|
25
25
|
# @param port [Integer] loopback port; 0 binds an ephemeral port
|
|
26
26
|
# @param callback_timeout [Numeric] seconds to wait for the redirect
|
|
27
27
|
# @param client_secret [String, nil] confidential clients only
|
|
28
|
+
# @param credential [#client_authentication, nil] confidential clients
|
|
29
|
+
# only, in place of client_secret: client_id goes on the authorize URL
|
|
30
|
+
# only, and the credential authenticates the code exchange
|
|
28
31
|
# @param verifier_length [Integer] PKCE verifier length, 43 to 128
|
|
29
32
|
# @param http_client [#get, #post_form] pluggable transport
|
|
30
33
|
# @param browser_opener [#call, nil] receives the authorize URL; defaults
|
|
@@ -34,10 +37,13 @@ module Keycardai
|
|
|
34
37
|
# @raise [InteractionTimeoutError] the user did not complete the redirect
|
|
35
38
|
# @raise [OAuthError] the authorization server denied the request
|
|
36
39
|
# @raise [ProtocolError] the redirect's state did not match (code state_mismatch)
|
|
40
|
+
# @raise [ConfigurationError] credential combined with client_secret,
|
|
41
|
+
# without client_authentication, or a WorkloadIdentity without client_id
|
|
37
42
|
def self.authenticate(issuer:, client_id:, scope: nil, resources: [], port: DEFAULT_CALLBACK_PORT,
|
|
38
|
-
callback_timeout: DEFAULT_CALLBACK_TIMEOUT, client_secret: nil,
|
|
43
|
+
callback_timeout: DEFAULT_CALLBACK_TIMEOUT, client_secret: nil, credential: nil,
|
|
39
44
|
verifier_length: PKCE::DEFAULT_VERIFIER_LENGTH,
|
|
40
45
|
http_client: HTTP::NetHTTPClient.new, browser_opener: nil, timeout: nil)
|
|
46
|
+
ClientAuth.validate(credential: credential, client_id: nil, client_secret: client_secret) if credential
|
|
41
47
|
authorization_endpoint = authorization_endpoint_for(issuer, http_client, timeout)
|
|
42
48
|
pair = PKCE.generate_pair(length: verifier_length)
|
|
43
49
|
state = SecureRandom.urlsafe_base64(24)
|
|
@@ -47,12 +53,11 @@ module Keycardai
|
|
|
47
53
|
client_id: client_id, scope: scope, resources: resources,
|
|
48
54
|
browser_opener: browser_opener)
|
|
49
55
|
code = server.wait_for_code(state: state, timeout: callback_timeout)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
)
|
|
56
|
+
# With a credential, client_id goes on the authorize URL only.
|
|
57
|
+
exchange_authorization_code(issuer, code: code, code_verifier: pair.code_verifier,
|
|
58
|
+
redirect_uri: server.redirect_uri,
|
|
59
|
+
client_id: credential ? nil : client_id, client_secret: client_secret,
|
|
60
|
+
credential: credential, http_client: http_client, timeout: timeout)
|
|
56
61
|
end
|
|
57
62
|
end
|
|
58
63
|
|
|
@@ -101,6 +106,10 @@ module Keycardai
|
|
|
101
106
|
# @param options [Hash] forwarded to authenticate
|
|
102
107
|
# @return [TokenResponse]
|
|
103
108
|
def self.authenticate_from_challenge(www_authenticate, http_client: HTTP::NetHTTPClient.new, **options)
|
|
109
|
+
# Credential errors surface before the resource metadata fetch.
|
|
110
|
+
if options[:credential]
|
|
111
|
+
ClientAuth.validate(credential: options[:credential], client_id: nil, client_secret: options[:client_secret])
|
|
112
|
+
end
|
|
104
113
|
issuer = resolve_issuer_from_challenge(www_authenticate, http_client: http_client,
|
|
105
114
|
timeout: options[:timeout])
|
|
106
115
|
authenticate(issuer: issuer, http_client: http_client, **options)
|
|
@@ -38,8 +38,12 @@ module Keycardai
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
# Exchange an authorization code for a token (RFC 6749 §4.1.3). A public
|
|
41
|
-
# client sends its client_id in the body
|
|
42
|
-
# authenticates with HTTP Basic and omits client_id from the body.
|
|
41
|
+
# client sends its client_id in the body. A confidential client
|
|
42
|
+
# authenticates with HTTP Basic and omits client_id from the body. A
|
|
43
|
+
# client with a credential adds the headers and form fields of the
|
|
44
|
+
# credential's client_authentication. The code is bound to the authorize
|
|
45
|
+
# URL's client_id, so a credential must authenticate as that client; the
|
|
46
|
+
# zone rejects a mismatch with invalid_grant.
|
|
43
47
|
#
|
|
44
48
|
# @param issuer [String] the zone's issuer URL; supplies the token endpoint
|
|
45
49
|
# @param code [String] the authorization code from the redirect
|
|
@@ -47,72 +51,84 @@ module Keycardai
|
|
|
47
51
|
# @param redirect_uri [String] must match the authorization request
|
|
48
52
|
# @param client_id [String, nil] public-client identifier
|
|
49
53
|
# @param client_secret [String, nil] confidential-client secret; requires client_id
|
|
54
|
+
# @param credential [#client_authentication, nil] authenticates the
|
|
55
|
+
# exchange in place of client_id and client_secret
|
|
50
56
|
# @param http_client [#get, #post_form] pluggable transport
|
|
51
57
|
# @param timeout [Numeric, nil]
|
|
52
58
|
# @return [TokenResponse]
|
|
59
|
+
# @raise [ConfigurationError] before any request: client_secret without
|
|
60
|
+
# client_id, credential set together with client_id or client_secret, a
|
|
61
|
+
# credential without client_authentication, or a WorkloadIdentity
|
|
62
|
+
# without client_id
|
|
53
63
|
# @raise [OAuthError] an RFC 6749 §5.2 error response (invalid_grant, ...)
|
|
54
|
-
# @raise [HTTPError, ProtocolError, NetworkError
|
|
64
|
+
# @raise [HTTPError, ProtocolError, NetworkError]
|
|
55
65
|
def self.exchange_authorization_code(issuer, code:, code_verifier:, redirect_uri:, client_id: nil,
|
|
56
|
-
client_secret: nil,
|
|
66
|
+
client_secret: nil, credential: nil,
|
|
57
67
|
http_client: HTTP::NetHTTPClient.new, timeout: nil)
|
|
58
68
|
raise ConfigurationError, "client_secret requires client_id" if client_secret && client_id.nil?
|
|
59
69
|
|
|
70
|
+
ClientAuth.validate(credential: credential, client_id: client_id, client_secret: client_secret) if credential
|
|
60
71
|
metadata = fetch_authorization_server_metadata(issuer, http_client: http_client, timeout: timeout)
|
|
61
72
|
endpoint = metadata.token_endpoint ||
|
|
62
73
|
raise(ProtocolError.new("metadata for #{issuer} has no token_endpoint", code: "invalid_metadata"))
|
|
63
74
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
params = {
|
|
67
|
-
"grant_type" => GrantType::AUTHORIZATION_CODE,
|
|
68
|
-
"code" => code,
|
|
69
|
-
"code_verifier" => code_verifier,
|
|
70
|
-
"redirect_uri" => redirect_uri,
|
|
71
|
-
"client_id" => client_secret ? nil : client_id
|
|
72
|
-
}.compact
|
|
75
|
+
params = [["grant_type", GrantType::AUTHORIZATION_CODE], ["code", code], ["code_verifier", code_verifier],
|
|
76
|
+
["redirect_uri", redirect_uri]]
|
|
73
77
|
headers = { "Accept" => "application/json" }
|
|
74
|
-
|
|
78
|
+
# With neither a client_id nor a credential, the exchange carries no client authentication.
|
|
79
|
+
if credential || client_id
|
|
80
|
+
params, headers = ClientAuth.apply(params, headers, issuer: issuer, endpoint: endpoint, credential: credential,
|
|
81
|
+
client_id: client_id, client_secret: client_secret)
|
|
82
|
+
end
|
|
75
83
|
|
|
76
|
-
|
|
84
|
+
# No key repeats in a code exchange, so the pairs become a hash.
|
|
85
|
+
TokenRequests.parse_response(http_client.post_form(endpoint, params.to_h, headers: headers, timeout: timeout))
|
|
77
86
|
end
|
|
78
87
|
|
|
79
88
|
# Redeem a refresh token for a new access token (RFC 6749 §6). Client
|
|
80
|
-
# authentication follows the
|
|
81
|
-
#
|
|
82
|
-
#
|
|
83
|
-
#
|
|
89
|
+
# authentication follows the grant that issued the refresh token. A
|
|
90
|
+
# public client sends client_id in the body. A confidential client
|
|
91
|
+
# authenticates with HTTP Basic and omits client_id from the body. A
|
|
92
|
+
# client with a credential adds the headers and form fields of the
|
|
93
|
+
# credential's client_authentication. The gem does not keep state: when the
|
|
94
|
+
# response has a refresh_token the server rotated it, and the caller
|
|
84
95
|
# stores it in place of the old one.
|
|
85
96
|
#
|
|
86
97
|
# @param issuer [String] the zone's issuer URL; supplies the token endpoint
|
|
87
98
|
# @param refresh_token [String] the refresh token from an earlier token response
|
|
88
|
-
# @param client_id [String]
|
|
99
|
+
# @param client_id [String, nil] the client the grant was issued to;
|
|
100
|
+
# required unless credential is set
|
|
89
101
|
# @param client_secret [String, nil] confidential-client secret
|
|
102
|
+
# @param credential [#client_authentication, nil] authenticates the
|
|
103
|
+
# refresh in place of client_id and client_secret
|
|
90
104
|
# @param resources [Array<String>] RFC 8707 resource indicators, one resource
|
|
91
105
|
# parameter per entry
|
|
92
106
|
# @param scopes [Array<String>] scopes to request, space-joined into scope
|
|
93
107
|
# @param http_client [#get, #post_form] pluggable transport
|
|
94
108
|
# @param timeout [Numeric, nil]
|
|
95
109
|
# @return [TokenResponse]
|
|
110
|
+
# @raise [ConfigurationError] before any request: credential set together
|
|
111
|
+
# with client_id or client_secret, a credential without
|
|
112
|
+
# client_authentication, a WorkloadIdentity without client_id, or
|
|
113
|
+
# neither credential nor client_id
|
|
96
114
|
# @raise [OAuthError] an RFC 6749 §5.2 error response; error == "invalid_grant"
|
|
97
115
|
# means the refresh token is expired, revoked, or unknown and the user must
|
|
98
116
|
# authorize again
|
|
99
117
|
# @raise [HTTPError] a non-2xx response with no OAuth error body (5xx, 429)
|
|
100
118
|
# @raise [NetworkError, ProtocolError]
|
|
101
|
-
def self.refresh_authorization(issuer, refresh_token:, client_id
|
|
102
|
-
scopes: [], http_client: HTTP::NetHTTPClient.new, timeout: nil)
|
|
119
|
+
def self.refresh_authorization(issuer, refresh_token:, client_id: nil, client_secret: nil, credential: nil,
|
|
120
|
+
resources: [], scopes: [], http_client: HTTP::NetHTTPClient.new, timeout: nil)
|
|
121
|
+
ClientAuth.validate(credential: credential, client_id: client_id, client_secret: client_secret)
|
|
103
122
|
metadata = fetch_authorization_server_metadata(issuer, http_client: http_client, timeout: timeout)
|
|
104
123
|
endpoint = metadata.token_endpoint ||
|
|
105
124
|
raise(ProtocolError.new("metadata for #{issuer} has no token_endpoint", code: "invalid_metadata"))
|
|
106
125
|
|
|
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
|
|
126
|
+
params = [["grant_type", GrantType::REFRESH_TOKEN], ["refresh_token", refresh_token]]
|
|
127
|
+
params << ["scope", Array(scopes).join(" ")] unless Array(scopes).empty?
|
|
128
|
+
params.concat(Array(resources).map { |resource| ["resource", resource] })
|
|
129
|
+
params, headers = ClientAuth.apply(params, { "Accept" => "application/json" },
|
|
130
|
+
issuer: issuer, endpoint: endpoint, credential: credential,
|
|
131
|
+
client_id: client_id, client_secret: client_secret)
|
|
116
132
|
|
|
117
133
|
TokenRequests.parse_response(http_client.post_form(endpoint, params, headers: headers, timeout: timeout))
|
|
118
134
|
end
|
|
@@ -0,0 +1,86 @@
|
|
|
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. A credential that responds to
|
|
45
|
+
# check_client_authentication is checked with it, so a WorkloadIdentity
|
|
46
|
+
# without client_id raises here. Call it before any request.
|
|
47
|
+
#
|
|
48
|
+
# @raise [ConfigurationError]
|
|
49
|
+
def validate(credential:, client_id:, client_secret: nil)
|
|
50
|
+
if credential
|
|
51
|
+
given = { "client_id" => client_id, "client_secret" => client_secret }.compact.keys
|
|
52
|
+
raise ConfigurationError, "pass a credential or #{given.join(" and ")}, not both" unless given.empty?
|
|
53
|
+
unless credential.respond_to?(:client_authentication)
|
|
54
|
+
raise ConfigurationError, "#{credential.class} does not provide client authentication"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
credential.check_client_authentication if credential.respond_to?(:check_client_authentication)
|
|
58
|
+
return
|
|
59
|
+
end
|
|
60
|
+
return unless client_id.nil? || client_id.empty?
|
|
61
|
+
|
|
62
|
+
raise ConfigurationError, "pass a credential or a client_id"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Add client authentication to a request. A credential adds what its
|
|
66
|
+
# client_authentication returns for this endpoint. A client_secret adds
|
|
67
|
+
# an HTTP Basic header. A public client adds client_id to the body.
|
|
68
|
+
#
|
|
69
|
+
# @param params [Array<Array(String, String)>] form fields as pairs, so
|
|
70
|
+
# repeated keys such as resource survive
|
|
71
|
+
# @param headers [Hash{String => String}]
|
|
72
|
+
# @return [Array(Array<Array(String, String)>, Hash{String => String})]
|
|
73
|
+
def apply(params, headers, issuer:, endpoint:, credential: nil, client_id: nil, client_secret: nil)
|
|
74
|
+
if credential
|
|
75
|
+
auth = credential.client_authentication(issuer: issuer, endpoint: endpoint)
|
|
76
|
+
[params + auth.params.to_a, headers.merge(auth.headers)]
|
|
77
|
+
elsif client_secret
|
|
78
|
+
[params, headers.merge("Authorization" => HTTP.basic_authorization(client_id, client_secret))]
|
|
79
|
+
else
|
|
80
|
+
[params + [["client_id", client_id]], headers]
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
private_constant :ClientAuth
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -41,21 +41,30 @@ module Keycardai
|
|
|
41
41
|
# @param client_assertion [String, nil] client-authentication assertion,
|
|
42
42
|
# form-encoded in the body
|
|
43
43
|
# @param client_assertion_type [String, nil]
|
|
44
|
+
# @param client_id [String, nil] the application credential the
|
|
45
|
+
# assertion authenticates as, form-encoded beside client_assertion;
|
|
46
|
+
# never sent with HTTP Basic
|
|
44
47
|
# @return [TokenResponse]
|
|
45
48
|
# @raise [OAuthError] an RFC 6749 §5.2 error response (invalid_client,
|
|
46
49
|
# invalid_scope, ...)
|
|
47
50
|
# @raise [HTTPError, ProtocolError, NetworkError]
|
|
48
51
|
def request_token(scope: nil, resource: nil, client_assertion: nil, client_assertion_type: nil,
|
|
49
|
-
issuer: nil)
|
|
52
|
+
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
|
+
|
|
50
58
|
post_token_request(
|
|
51
59
|
{
|
|
52
60
|
"grant_type" => GrantType::CLIENT_CREDENTIALS,
|
|
53
61
|
"scope" => scope,
|
|
54
62
|
"resource" => resource,
|
|
55
63
|
"client_assertion" => client_assertion,
|
|
56
|
-
"client_assertion_type" => client_assertion_type
|
|
64
|
+
"client_assertion_type" => client_assertion_type,
|
|
65
|
+
"client_id" => client_id
|
|
57
66
|
},
|
|
58
|
-
issuer:
|
|
67
|
+
issuer: target
|
|
59
68
|
)
|
|
60
69
|
end
|
|
61
70
|
end
|