authproto 0.3.0.beta2 → 0.3.0.beta4
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 +36 -0
- data/README.md +108 -5
- data/examples/confidential_client/Gemfile.lock +1 -1
- data/examples/confidential_client/README.md +11 -1
- data/examples/confidential_client/app.rb +35 -17
- data/examples/confidential_client/scripts/generate_keys.rb +1 -1
- data/examples/minisky_console/Gemfile.lock +2 -2
- data/examples/minisky_console/README.md +20 -1
- data/examples/minisky_console/localhost.rb +5 -3
- data/examples/minisky_console/production.rb +9 -3
- data/lib/authproto/client.rb +173 -123
- data/lib/authproto/configuration.rb +37 -16
- data/lib/authproto/dpop/client.rb +105 -40
- data/lib/authproto/dpop/{key_manager.rb → key_set.rb} +54 -120
- data/lib/authproto/dpop/nonce_manager.rb +38 -29
- data/lib/authproto/dpop/proof_generator.rb +21 -35
- data/lib/authproto/dpop.rb +19 -0
- data/lib/authproto/encryption.rb +29 -12
- data/lib/authproto/http_client.rb +8 -7
- data/lib/authproto/http_helpers.rb +85 -0
- data/lib/authproto/metadata/authorization_server.rb +23 -6
- data/lib/authproto/metadata/client_metadata.rb +46 -6
- data/lib/authproto/metadata/resource_server.rb +7 -5
- data/lib/authproto/minisky.rb +36 -34
- data/lib/authproto/par/client.rb +22 -73
- data/lib/authproto/par/client_assertion.rb +47 -16
- data/lib/authproto/par/request.rb +4 -8
- data/lib/authproto/par.rb +2 -1
- data/lib/authproto/serialization/session.rb +33 -13
- data/lib/authproto/serialization/token_set.rb +6 -4
- data/lib/authproto/state/session.rb +102 -6
- data/lib/authproto/state/session_manager.rb +120 -22
- data/lib/authproto/state/token_set.rb +22 -19
- data/lib/authproto/state.rb +2 -2
- data/lib/authproto/storage/key_builder.rb +3 -11
- data/lib/authproto/storage/memory.rb +8 -2
- data/lib/authproto/token/identity.rb +54 -0
- data/lib/authproto/token/refresh.rb +66 -109
- data/lib/authproto/token/response.rb +73 -0
- data/lib/authproto/token/revocation.rb +92 -0
- data/lib/authproto/validated_uri.rb +23 -0
- data/lib/authproto/version.rb +1 -1
- data/lib/authproto.rb +0 -1
- data/sig/authproto/client.rbs +10 -10
- data/sig/authproto/dependencies.rbs +2 -0
- data/sig/authproto/dpop/client.rbs +11 -4
- data/sig/authproto/dpop/{key_manager.rbs → key_set.rbs} +9 -11
- data/sig/authproto/dpop/nonce_manager.rbs +4 -4
- data/sig/authproto/dpop/proof_generator.rbs +5 -10
- data/sig/authproto/dpop.rbs +6 -0
- data/sig/authproto/encryption.rbs +4 -2
- data/sig/authproto/http_client.rbs +0 -1
- data/sig/authproto/http_helpers.rbs +7 -0
- data/sig/authproto/localhost_server.rbs +1 -1
- data/sig/authproto/metadata/authorization_server.rbs +1 -0
- data/sig/authproto/metadata/client_metadata.rbs +3 -1
- data/sig/authproto/metadata/resource_server.rbs +1 -0
- data/sig/authproto/minisky_oauth.rbs +5 -2
- data/sig/authproto/par/client.rbs +4 -8
- data/sig/authproto/par/client_assertion.rbs +4 -0
- data/sig/authproto/par/request.rbs +0 -9
- data/sig/authproto/state/session.rbs +13 -3
- data/sig/authproto/state/session_manager.rbs +10 -3
- data/sig/authproto/state/token_set.rbs +1 -2
- data/sig/authproto/storage/key_builder.rbs +1 -2
- data/sig/authproto/token/refresh.rbs +5 -5
- data/sig/authproto/token/response.rbs +12 -0
- data/sig/authproto/token/revocation.rbs +14 -0
- data/sig/authproto.rbs +6 -3
- metadata +30 -23
- data/.rspec +0 -1
- data/.rubocop.yml +0 -31
- data/.tm_properties +0 -1
- data/.yardopts +0 -5
- data/AGENTS.md +0 -80
- data/Rakefile +0 -8
- data/examples/confidential_client/.gitignore +0 -6
- data/examples/minisky_console/.gitignore +0 -2
- data/lib/authproto/serialization/dpop_key.rb +0 -38
- data/misc/yard_attributes.rb +0 -34
- data/sig/authproto/serialization/dpop_key.rbs +0 -14
|
@@ -7,9 +7,13 @@ require 'monitor'
|
|
|
7
7
|
require_relative '../configuration'
|
|
8
8
|
require_relative '../errors'
|
|
9
9
|
require_relative '../http_client'
|
|
10
|
+
require_relative '../http_helpers'
|
|
10
11
|
require_relative '../par'
|
|
11
12
|
require_relative '../par/client_assertion'
|
|
12
13
|
require_relative '../state/token_set'
|
|
14
|
+
require_relative 'identity'
|
|
15
|
+
require_relative 'response'
|
|
16
|
+
require_relative 'revocation'
|
|
13
17
|
|
|
14
18
|
module AuthProto
|
|
15
19
|
|
|
@@ -116,6 +120,8 @@ module AuthProto
|
|
|
116
120
|
#
|
|
117
121
|
|
|
118
122
|
class Refresh
|
|
123
|
+
include HTTPHelpers
|
|
124
|
+
include Identity
|
|
119
125
|
include MonitorMixin
|
|
120
126
|
|
|
121
127
|
# Maximum attempts for one refresh operation.
|
|
@@ -152,9 +158,10 @@ module AuthProto
|
|
|
152
158
|
# @param dpop_client [DPoP::Client] proof signer and nonce tracker
|
|
153
159
|
# @param auth_server [Metadata::AuthorizationServer] server with a token endpoint
|
|
154
160
|
# @param client_metadata [Metadata::ClientMetadata] client ID and authentication key configuration
|
|
155
|
-
# @param
|
|
161
|
+
# @param client_assertion [PAR::ClientAssertion, nil] reusable validated signer; required for confidential clients
|
|
162
|
+
# @raise [ArgumentError] if confidential-client metadata is supplied without an assertion signer
|
|
156
163
|
#
|
|
157
|
-
def initialize(session:, dpop_client:, auth_server:, client_metadata:,
|
|
164
|
+
def initialize(session:, dpop_client:, auth_server:, client_metadata:, client_assertion: nil)
|
|
158
165
|
super() # Initialize MonitorMixin
|
|
159
166
|
|
|
160
167
|
@session = session
|
|
@@ -162,23 +169,33 @@ module AuthProto
|
|
|
162
169
|
@auth_server = auth_server
|
|
163
170
|
@attempt_count = 0
|
|
164
171
|
@client_metadata = client_metadata
|
|
165
|
-
@
|
|
172
|
+
@client_assertion = client_assertion
|
|
173
|
+
|
|
174
|
+
if client_metadata.confidential? && !client_assertion.is_a?(PAR::ClientAssertion)
|
|
175
|
+
raise ArgumentError, "Confidential clients require a client_assertion signer"
|
|
176
|
+
end
|
|
166
177
|
end
|
|
167
178
|
|
|
168
179
|
# Exchanges a refresh token for a replacement token set.
|
|
169
180
|
#
|
|
170
|
-
#
|
|
171
|
-
# jitter
|
|
181
|
+
# Makes up to {MAX_RETRIES} attempts for eligible failures, with exponential backoff
|
|
182
|
+
# and jitter. Each attempt can retry one DPoP nonce challenge. Does not assign
|
|
172
183
|
# the returned tokens to the session or persist them; the caller must do so.
|
|
184
|
+
# HTTP 400/401 OAuth errors preserve the server's error code and description in the exception message.
|
|
185
|
+
# Retains the existing refresh token when the response omits a replacement.
|
|
186
|
+
# Verifies the subject's current PDS before the exchange and attempts revocation on identity mismatch.
|
|
173
187
|
#
|
|
174
188
|
# @return [State::TokenSet] validated replacement tokens
|
|
175
189
|
# @raise [RefreshError] if the session is not renewable, refresh fails, or retries are exhausted
|
|
190
|
+
# @raise [DPoP::Client::Error] if any response lacks a valid nonce or nonce persistence fails
|
|
176
191
|
#
|
|
177
192
|
def perform_refresh
|
|
178
193
|
synchronize do
|
|
179
194
|
raise RefreshError.new("No refresh token available", retry_possible: false) unless session.renewable?
|
|
180
195
|
raise RefreshError.new("No access token to refresh", retry_possible: false) if session.tokens.nil?
|
|
181
196
|
|
|
197
|
+
validate_identity
|
|
198
|
+
|
|
182
199
|
with_retries do
|
|
183
200
|
request_token_refresh
|
|
184
201
|
end
|
|
@@ -188,6 +205,31 @@ module AuthProto
|
|
|
188
205
|
|
|
189
206
|
private
|
|
190
207
|
|
|
208
|
+
def validate_identity
|
|
209
|
+
validate_subject_pds(session.did, session)
|
|
210
|
+
rescue IdentityMismatchError => e
|
|
211
|
+
revoke_rejected_refresh_token(session.tokens.refresh_token) if session.tokens.refresh_token
|
|
212
|
+
raise RefreshError.new(e.message, retry_possible: false)
|
|
213
|
+
rescue IdentityError => e
|
|
214
|
+
raise RefreshError.new(e.message, retry_possible: false)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
def revoke_rejected_refresh_token(token)
|
|
218
|
+
return unless session.can_be_revoked?
|
|
219
|
+
|
|
220
|
+
revocation = Revocation.new(
|
|
221
|
+
session: session,
|
|
222
|
+
dpop_client: dpop_client,
|
|
223
|
+
client_metadata: client_metadata,
|
|
224
|
+
client_assertion: @client_assertion
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
revocation.revoke_token(token, :token_type => :refresh_token)
|
|
228
|
+
rescue StandardError
|
|
229
|
+
# Cleanup failures must not replace the original identity error.
|
|
230
|
+
nil
|
|
231
|
+
end
|
|
232
|
+
|
|
191
233
|
def with_retries
|
|
192
234
|
@attempt_count = 0
|
|
193
235
|
last_error = nil
|
|
@@ -219,29 +261,19 @@ module AuthProto
|
|
|
219
261
|
end
|
|
220
262
|
|
|
221
263
|
def request_token_refresh
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
# Handle DPoP nonce requirement
|
|
226
|
-
if requires_dpop_nonce?(response)
|
|
227
|
-
# Extract and store nonce from error response
|
|
228
|
-
extract_dpop_nonce(response)
|
|
229
|
-
dpop_client.process_response(response[:headers], auth_server.issuer)
|
|
230
|
-
|
|
231
|
-
# Retry request with nonce
|
|
232
|
-
response = make_token_request(session)
|
|
264
|
+
response = dpop_client.with_nonce_handling(session, auth_server.token_endpoint) do
|
|
265
|
+
make_token_request(session)
|
|
233
266
|
end
|
|
234
267
|
|
|
235
268
|
handle_refresh_response(response)
|
|
236
269
|
end
|
|
237
270
|
|
|
238
271
|
def make_token_request(session)
|
|
239
|
-
dpop_proof = dpop_client.generate_proof(method: 'POST', uri: auth_server.token_endpoint)
|
|
272
|
+
dpop_proof = dpop_client.generate_proof(session: session, method: 'POST', uri: auth_server.token_endpoint)
|
|
240
273
|
|
|
241
274
|
body = {
|
|
242
275
|
grant_type: 'refresh_token',
|
|
243
276
|
refresh_token: session.tokens.refresh_token,
|
|
244
|
-
scope: session.scope,
|
|
245
277
|
client_id: client_metadata.client_id
|
|
246
278
|
}
|
|
247
279
|
|
|
@@ -257,35 +289,10 @@ module AuthProto
|
|
|
257
289
|
)
|
|
258
290
|
end
|
|
259
291
|
|
|
260
|
-
def requires_dpop_nonce?(response)
|
|
261
|
-
return false unless response[:status] == 400
|
|
262
|
-
return false if response[:body].nil?
|
|
263
|
-
|
|
264
|
-
error_data = JSON.parse(response[:body])
|
|
265
|
-
error_data.is_a?(Hash) && error_data['error'] == DPoP::ERROR_USE_NONCE
|
|
266
|
-
rescue JSON::ParserError
|
|
267
|
-
false
|
|
268
|
-
end
|
|
269
|
-
|
|
270
|
-
def extract_dpop_nonce(response)
|
|
271
|
-
nonce = (response[:headers] || {}).detect { |k, v| k.downcase == 'dpop-nonce' }&.last
|
|
272
|
-
|
|
273
|
-
unless nonce.is_a?(String) && !nonce.empty?
|
|
274
|
-
raise RefreshError.new("No DPoP nonce provided in error response", retry_possible: false)
|
|
275
|
-
end
|
|
276
|
-
|
|
277
|
-
nonce
|
|
278
|
-
end
|
|
279
|
-
|
|
280
292
|
def add_client_authentication(body)
|
|
281
|
-
client_assertion = PAR::ClientAssertion.new(
|
|
282
|
-
client_id: client_metadata.client_id,
|
|
283
|
-
signing_key: @signing_key
|
|
284
|
-
)
|
|
285
|
-
|
|
286
293
|
body.merge!(
|
|
287
294
|
client_assertion_type: PAR::CLIENT_ASSERTION_TYPE,
|
|
288
|
-
client_assertion: client_assertion.generate_jwt(audience: auth_server.issuer)
|
|
295
|
+
client_assertion: @client_assertion.generate_jwt(audience: auth_server.issuer)
|
|
289
296
|
)
|
|
290
297
|
end
|
|
291
298
|
|
|
@@ -293,10 +300,8 @@ module AuthProto
|
|
|
293
300
|
case response[:status]
|
|
294
301
|
when 200
|
|
295
302
|
process_successful_response(response)
|
|
296
|
-
when 400
|
|
297
|
-
|
|
298
|
-
when 401
|
|
299
|
-
raise RefreshError.new("Refresh token is invalid", retry_possible: false)
|
|
303
|
+
when 400, 401
|
|
304
|
+
handle_error_response(response)
|
|
300
305
|
when 429
|
|
301
306
|
handle_rate_limit_response(response)
|
|
302
307
|
else
|
|
@@ -305,24 +310,14 @@ module AuthProto
|
|
|
305
310
|
end
|
|
306
311
|
|
|
307
312
|
def process_successful_response(response)
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
AuthProto::State::TokenSet.new(
|
|
314
|
-
access_token: data['access_token'],
|
|
315
|
-
token_type: data['token_type'],
|
|
316
|
-
expires_in: data['expires_in'],
|
|
317
|
-
refresh_token: data['refresh_token'],
|
|
318
|
-
scope: data['scope'],
|
|
319
|
-
sub: data['sub']
|
|
320
|
-
)
|
|
321
|
-
rescue JSON::ParserError => e
|
|
322
|
-
raise RefreshError.new("Invalid response format: #{e.message}", retry_possible: false)
|
|
313
|
+
tokens = Response.parse(response, current_tokens: session.tokens)
|
|
314
|
+
validate_refresh_identity(tokens)
|
|
315
|
+
tokens
|
|
316
|
+
rescue Response::Error => e
|
|
317
|
+
raise RefreshError.new(e.message, retry_possible: false)
|
|
323
318
|
end
|
|
324
319
|
|
|
325
|
-
def
|
|
320
|
+
def handle_error_response(response)
|
|
326
321
|
raise RefreshError.new("Missing error response body", retry_possible: false) if response[:body].nil?
|
|
327
322
|
|
|
328
323
|
error_data = JSON.parse(response[:body])
|
|
@@ -331,13 +326,9 @@ module AuthProto
|
|
|
331
326
|
raise RefreshError.new("Invalid error response format", retry_possible: false)
|
|
332
327
|
end
|
|
333
328
|
|
|
334
|
-
error_description = error_data['
|
|
329
|
+
error_description = [error_data['error'], error_data['error_description']].compact.join(': ')
|
|
335
330
|
|
|
336
331
|
case error_data['error']
|
|
337
|
-
when DPoP::ERROR_USE_NONCE
|
|
338
|
-
extract_dpop_nonce(response)
|
|
339
|
-
dpop_client.process_response(response[:headers], auth_server.issuer)
|
|
340
|
-
raise RefreshError.new("Retry with DPoP nonce", retry_possible: true)
|
|
341
332
|
when "invalid_grant"
|
|
342
333
|
# The refresh token has been invalidated or already used
|
|
343
334
|
raise RefreshError.new(
|
|
@@ -356,50 +347,16 @@ module AuthProto
|
|
|
356
347
|
|
|
357
348
|
def handle_rate_limit_response(response)
|
|
358
349
|
# Extract retry-after if available
|
|
359
|
-
retry_after = response
|
|
350
|
+
retry_after = response_header(response, 'Retry-After')
|
|
351
|
+
retry_after = retry_after&.to_i || calculate_delay
|
|
360
352
|
raise RefreshError, "Rate limited - retry after #{retry_after} seconds"
|
|
361
353
|
end
|
|
362
354
|
|
|
363
|
-
def
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
# Required fields
|
|
369
|
-
%w[access_token token_type expires_in scope sub].each do |field|
|
|
370
|
-
raise RefreshError.new("Missing #{field} in response", retry_possible: false) unless data[field]
|
|
371
|
-
end
|
|
372
|
-
|
|
373
|
-
%w[access_token token_type scope sub].each do |field|
|
|
374
|
-
unless data[field].is_a?(String) && !data[field].empty?
|
|
375
|
-
raise RefreshError.new("Invalid #{field} in response", retry_possible: false)
|
|
376
|
-
end
|
|
377
|
-
end
|
|
378
|
-
|
|
379
|
-
unless data['expires_in'].is_a?(Integer)
|
|
380
|
-
raise RefreshError.new("Invalid expires_in in response", retry_possible: false)
|
|
381
|
-
end
|
|
382
|
-
|
|
383
|
-
unless data['refresh_token'].nil? || data['refresh_token'].is_a?(String)
|
|
384
|
-
raise RefreshError.new("Invalid refresh_token in response", retry_possible: false)
|
|
385
|
-
end
|
|
386
|
-
|
|
387
|
-
# Token type must be DPoP
|
|
388
|
-
unless data['token_type'] == 'DPoP'
|
|
389
|
-
raise RefreshError.new("Invalid token_type: #{data['token_type']}", retry_possible: false)
|
|
390
|
-
end
|
|
391
|
-
|
|
392
|
-
# Scope must include original scopes
|
|
393
|
-
original_scopes = session.scope.split
|
|
394
|
-
response_scopes = data['scope'].split
|
|
395
|
-
unless (original_scopes - response_scopes).empty?
|
|
396
|
-
raise RefreshError.new("Invalid scope in response", retry_possible: false)
|
|
355
|
+
def validate_refresh_identity(tokens)
|
|
356
|
+
if tokens.sub != session.tokens.sub
|
|
357
|
+
revoke_rejected_refresh_token(tokens.refresh_token) if tokens.refresh_token
|
|
358
|
+
raise RefreshError.new("Subject mismatch in response", retry_possible: false)
|
|
397
359
|
end
|
|
398
|
-
|
|
399
|
-
# Subject must match
|
|
400
|
-
return if data['sub'] == session.tokens.sub
|
|
401
|
-
|
|
402
|
-
raise RefreshError.new("Subject mismatch in response", retry_possible: false)
|
|
403
360
|
end
|
|
404
361
|
end
|
|
405
362
|
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
require_relative '../errors'
|
|
6
|
+
require_relative '../http_helpers'
|
|
7
|
+
require_relative '../state/token_set'
|
|
8
|
+
|
|
9
|
+
module AuthProto
|
|
10
|
+
module Token
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
# Parses successful token responses shared by code exchange and refresh.
|
|
14
|
+
#
|
|
15
|
+
|
|
16
|
+
class Response
|
|
17
|
+
extend HTTPHelpers
|
|
18
|
+
|
|
19
|
+
# Error raised when a successful token response is malformed.
|
|
20
|
+
class Error < AuthProto::Error; end
|
|
21
|
+
|
|
22
|
+
# Validates the response representation and constructs its token set.
|
|
23
|
+
# Identity checks remain the responsibility of the calling authorization flow.
|
|
24
|
+
# Rejects OpenID Connect `id_token` fields, matching the ATProto reference client.
|
|
25
|
+
#
|
|
26
|
+
# @param response [Hash] successful HTTP response from the token endpoint
|
|
27
|
+
# @param current_tokens [State::TokenSet, nil] current tokens whose refresh token is retained if omitted
|
|
28
|
+
# @return [State::TokenSet] validated tokens
|
|
29
|
+
# @raise [Error] if the response has an invalid content type, body or token fields
|
|
30
|
+
#
|
|
31
|
+
def self.parse(response, current_tokens: nil)
|
|
32
|
+
content_type = response_header(response, 'Content-Type', strip_parameters: true)
|
|
33
|
+
|
|
34
|
+
unless content_type&.casecmp?('application/json')
|
|
35
|
+
raise Error, "Token response Content-Type must be application/json"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
raise Error, "Missing token response body" unless response[:body].is_a?(String)
|
|
39
|
+
|
|
40
|
+
data = JSON.parse(response[:body])
|
|
41
|
+
raise Error, "Token response must be a JSON object" unless data.is_a?(Hash)
|
|
42
|
+
raise Error, "Token response must not include id_token" if data.key?('id_token')
|
|
43
|
+
|
|
44
|
+
['access_token', 'token_type', 'expires_in', 'scope', 'sub'].each do |field|
|
|
45
|
+
raise Error, "Missing #{field} in token response" if data[field].nil?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
unless data['expires_in'].is_a?(Integer) && data['expires_in'].positive?
|
|
49
|
+
raise Error, "Invalid expires_in in token response: must be a positive integer"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
if data.key?('refresh_token') && data['refresh_token'].nil?
|
|
53
|
+
raise Error, "Invalid refresh_token in token response"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
tokens = State::TokenSet.new(
|
|
57
|
+
access_token: data['access_token'],
|
|
58
|
+
refresh_token: data.fetch('refresh_token', current_tokens&.refresh_token),
|
|
59
|
+
token_type: data['token_type'],
|
|
60
|
+
expires_in: data['expires_in'],
|
|
61
|
+
scope: data['scope'],
|
|
62
|
+
sub: data['sub']
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
raise Error, "Invalid scope in token response: missing atproto" unless tokens.scope.split.include?('atproto')
|
|
66
|
+
|
|
67
|
+
tokens
|
|
68
|
+
rescue JSON::ParserError, ArgumentError => e
|
|
69
|
+
raise Error, "Invalid token response: #{e.message}"
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../configuration'
|
|
4
|
+
require_relative '../errors'
|
|
5
|
+
require_relative '../par'
|
|
6
|
+
|
|
7
|
+
module AuthProto
|
|
8
|
+
module Token
|
|
9
|
+
|
|
10
|
+
#
|
|
11
|
+
# Revokes tokens using the issuing server and the session's authentication keys.
|
|
12
|
+
#
|
|
13
|
+
|
|
14
|
+
class Revocation
|
|
15
|
+
|
|
16
|
+
# Error raised when revocation is unavailable or rejected by the server.
|
|
17
|
+
class Error < AuthProto::Error; end
|
|
18
|
+
|
|
19
|
+
# Creates a revoker using the original session's server and authentication keys.
|
|
20
|
+
#
|
|
21
|
+
# @param session [State::Session] session bound to the issuing authorization server
|
|
22
|
+
# @param dpop_client [DPoP::Client] proof signer and nonce tracker
|
|
23
|
+
# @param client_metadata [Metadata::ClientMetadata] client authentication configuration
|
|
24
|
+
# @param client_assertion [PAR::ClientAssertion, nil] confidential-client assertion signer
|
|
25
|
+
#
|
|
26
|
+
def initialize(session:, dpop_client:, client_metadata:, client_assertion: nil)
|
|
27
|
+
@session = session
|
|
28
|
+
@dpop_client = dpop_client
|
|
29
|
+
@client_metadata = client_metadata
|
|
30
|
+
@client_assertion = client_assertion
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Revokes an access or refresh token and requires a successful response.
|
|
34
|
+
# The response body is ignored, including for tokens already revoked.
|
|
35
|
+
#
|
|
36
|
+
# @param token [String] nonempty token to revoke
|
|
37
|
+
# @param token_type [String, Symbol] `access_token` or `refresh_token`
|
|
38
|
+
# @return [void]
|
|
39
|
+
# @raise [ArgumentError] if the token or hint is invalid
|
|
40
|
+
# @raise [Error] if the endpoint is absent or the server rejects revocation
|
|
41
|
+
# @raise [DPoP::Client::Error, HTTPClient::HTTPError] if proof or transport handling fails
|
|
42
|
+
#
|
|
43
|
+
def revoke_token(token, token_type:)
|
|
44
|
+
raise ArgumentError, "Token must be a nonempty string" unless token.is_a?(String) && !token.empty?
|
|
45
|
+
|
|
46
|
+
token_type = token_type.to_s if token_type.is_a?(Symbol)
|
|
47
|
+
|
|
48
|
+
unless %w[access_token refresh_token].include?(token_type)
|
|
49
|
+
raise ArgumentError, "Invalid token_type"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
endpoint = @session.auth_server&.revocation_endpoint
|
|
53
|
+
raise Error, "Authorization server does not advertise a revocation endpoint" unless endpoint
|
|
54
|
+
|
|
55
|
+
response = @dpop_client.with_nonce_handling(@session, endpoint) do
|
|
56
|
+
make_request(token, endpoint, token_type)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
raise Error, "Token revocation failed: HTTP #{response[:status]}" unless response[:status] == 200
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def make_request(token, endpoint, token_type)
|
|
66
|
+
body = {
|
|
67
|
+
token: token,
|
|
68
|
+
token_type_hint: token_type,
|
|
69
|
+
client_id: @client_metadata.client_id
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if @client_metadata.confidential?
|
|
73
|
+
body.merge!(
|
|
74
|
+
client_assertion_type: PAR::CLIENT_ASSERTION_TYPE,
|
|
75
|
+
client_assertion: @client_assertion.generate_jwt(audience: @session.auth_server.issuer)
|
|
76
|
+
)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
proof = @dpop_client.generate_proof(session: @session, method: 'POST', uri: endpoint)
|
|
80
|
+
|
|
81
|
+
AuthProto.configuration.http_client.post(
|
|
82
|
+
endpoint,
|
|
83
|
+
body: body,
|
|
84
|
+
headers: {
|
|
85
|
+
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
86
|
+
'DPoP' => proof
|
|
87
|
+
}
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -18,11 +18,22 @@ module AuthProto
|
|
|
18
18
|
|
|
19
19
|
attr_reader :port, :userinfo, :path
|
|
20
20
|
|
|
21
|
+
# @!attribute [r] port
|
|
22
|
+
# Explicit port text from the input URL, or `nil` when omitted.
|
|
23
|
+
# @return [String, nil]
|
|
24
|
+
# @!attribute [r] userinfo
|
|
25
|
+
# User information from the input URL, including an explicitly empty component.
|
|
26
|
+
# @return [String, nil]
|
|
27
|
+
# @!attribute [r] path
|
|
28
|
+
# URL path, treating the root path `/` as an empty string.
|
|
29
|
+
# @return [String]
|
|
21
30
|
def_delegators :@uri, :scheme, :host, :hostname, :query, :fragment,
|
|
22
31
|
:user, :password, :registry, :opaque, :default_port,
|
|
23
32
|
:request_uri, :origin, :absolute?, :relative?, :hierarchical?,
|
|
24
33
|
:to_s, :to_str
|
|
25
34
|
|
|
35
|
+
# Parses an absolute HTTP(S) URL and preserves explicitly supplied components.
|
|
36
|
+
#
|
|
26
37
|
# @param url [String, URI::Generic] URL to parse; URI inputs use their serialized form
|
|
27
38
|
# @raise [URI::InvalidURIError] if the URL is malformed, not HTTP(S), or has no host
|
|
28
39
|
#
|
|
@@ -42,14 +53,26 @@ module AuthProto
|
|
|
42
53
|
@path = (@uri.path == '/') ? '' : @uri.path
|
|
43
54
|
end
|
|
44
55
|
|
|
56
|
+
# Returns the explicitly supplied port, without substituting a scheme default.
|
|
57
|
+
#
|
|
58
|
+
# @return [String, nil] port text, or `nil` if omitted
|
|
59
|
+
#
|
|
45
60
|
def specified_port
|
|
46
61
|
port
|
|
47
62
|
end
|
|
48
63
|
|
|
64
|
+
# Returns the parsed port, using the scheme default when no port was supplied.
|
|
65
|
+
#
|
|
66
|
+
# @return [Integer] effective HTTP(S) port
|
|
67
|
+
#
|
|
49
68
|
def effective_port
|
|
50
69
|
@uri.port
|
|
51
70
|
end
|
|
52
71
|
|
|
72
|
+
# Returns the underlying parsed Ruby URI object.
|
|
73
|
+
#
|
|
74
|
+
# @return [URI::HTTP, URI::HTTPS] parsed URI; this is the original object, not a copy
|
|
75
|
+
#
|
|
53
76
|
def to_uri
|
|
54
77
|
@uri
|
|
55
78
|
end
|
data/lib/authproto/version.rb
CHANGED
data/lib/authproto.rb
CHANGED
|
@@ -5,7 +5,6 @@ require_relative 'authproto/configuration'
|
|
|
5
5
|
require_relative 'authproto/http_client'
|
|
6
6
|
require_relative 'authproto/insecure_http_client'
|
|
7
7
|
require_relative 'authproto/localhost_server'
|
|
8
|
-
require_relative 'authproto/serialization/dpop_key'
|
|
9
8
|
require_relative 'authproto/validated_uri'
|
|
10
9
|
require_relative 'authproto/version'
|
|
11
10
|
|
data/sig/authproto/client.rbs
CHANGED
|
@@ -15,28 +15,28 @@ module AuthProto
|
|
|
15
15
|
attr_reader session_manager: State::SessionManager
|
|
16
16
|
attr_reader dpop_client: DPoP::Client
|
|
17
17
|
|
|
18
|
-
def initialize: (client_id: String, redirect_uri: String, ?metadata: json_object?, ?
|
|
18
|
+
def initialize: (client_id: String, redirect_uri: String, ?metadata: json_object?, ?signing_key: (json_object | JOSE::JWK)?) -> void
|
|
19
19
|
def authorize: (?handle: String?, ?pds: String?, ?scope: String) -> authorization_result
|
|
20
|
-
def handle_callback: (code: String, state: String, iss: String) -> callback_result
|
|
20
|
+
def handle_callback: (code: String, state: String, iss: String, expected_session_id: String) -> callback_result
|
|
21
21
|
def get_tokens: (String session_id) -> token_info?
|
|
22
22
|
def refresh_token: (String session_id) -> refreshed_tokens
|
|
23
23
|
def authorized?: (String session_id) -> bool
|
|
24
24
|
def auth_headers: (session_id: String, method: String, url: String) -> Hash[String, String]
|
|
25
|
-
def delete_session: (String session_id) -> void
|
|
25
|
+
def delete_session: (String session_id, ?revoke: bool) -> void
|
|
26
26
|
def cleanup_expired_sessions: () -> void
|
|
27
27
|
|
|
28
28
|
private
|
|
29
29
|
def load_client_metadata: (json_object? metadata) -> Metadata::ClientMetadata
|
|
30
30
|
def validate_redirect_uri: () -> void
|
|
31
|
-
def
|
|
32
|
-
def
|
|
31
|
+
def loopback_redirect_components: (String url) -> Array[String?]?
|
|
32
|
+
def initialize_client_assertion: ((json_object | JOSE::JWK)? key) -> String?
|
|
33
|
+
def metadata_key_matches_public_key?: (json_object candidate, json_object public_key) -> bool
|
|
33
34
|
def validate_auth_params: (String? handle, String? pds, String scope) -> void
|
|
34
|
-
def
|
|
35
|
+
def resolve_auth_server_info: (String pds) -> Metadata::AuthorizationServer
|
|
35
36
|
def submit_authorization_par: (Metadata::AuthorizationServer authorization_server, State::Session session, ?login_hint: String?) -> String
|
|
36
|
-
def exchange_code: (code: String, session: State::Session) ->
|
|
37
|
+
def exchange_code: (code: String, session: State::Session) -> State::TokenSet
|
|
37
38
|
def make_token_request: (String code, State::Session session) -> http_response
|
|
38
|
-
def
|
|
39
|
-
def
|
|
40
|
-
def validate_token_response: (json_object response, State::Session session) -> void
|
|
39
|
+
def validate_token_identity: (State::TokenSet tokens, State::Session session) -> void
|
|
40
|
+
def revoke_rejected_refresh_token: (String token, State::Session session) -> void
|
|
41
41
|
end
|
|
42
42
|
end
|
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
module AuthProto
|
|
2
2
|
module DPoP
|
|
3
3
|
class Client
|
|
4
|
+
include HTTPHelpers
|
|
4
5
|
class Error < AuthProto::Error
|
|
5
6
|
end
|
|
6
7
|
|
|
7
|
-
attr_reader key_manager: KeyManager
|
|
8
8
|
attr_reader proof_generator: ProofGenerator
|
|
9
9
|
attr_reader nonce_manager: NonceManager
|
|
10
10
|
|
|
11
|
-
def initialize: (?
|
|
12
|
-
def generate_proof: (method: String, uri: String, ?access_token: String
|
|
13
|
-
def
|
|
11
|
+
def initialize: (?nonce_ttl: Integer?) -> void
|
|
12
|
+
def generate_proof: (session: State::Session, method: String, uri: String, ?access_token: String?) -> JOSE::SignedBinary
|
|
13
|
+
def has_nonce_error?: (http_response response) -> bool
|
|
14
|
+
def process_response: (http_response response, server_url: String, session_id: String) -> void
|
|
15
|
+
|
|
16
|
+
def with_nonce_handling: (State::Session session, String url) { () -> http_response } -> http_response
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def nonce_challenge?: (String? challenge) -> bool
|
|
14
21
|
end
|
|
15
22
|
end
|
|
16
23
|
end
|
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
module AuthProto
|
|
2
2
|
module DPoP
|
|
3
|
-
class
|
|
3
|
+
class KeySet
|
|
4
4
|
class KeyError < AuthProto::Error
|
|
5
5
|
end
|
|
6
6
|
|
|
7
7
|
CURVE: String
|
|
8
|
-
|
|
9
|
-
attr_reader keypair: JOSE::JWK
|
|
8
|
+
attr_reader key: JOSE::JWK
|
|
10
9
|
|
|
11
|
-
def initialize: (?JOSE::JWK?
|
|
12
|
-
def generate_keypair: () -> JOSE::JWK
|
|
10
|
+
def initialize: (?JOSE::JWK? key) -> void
|
|
13
11
|
def public_jwk: () -> json_object
|
|
14
|
-
def sign: (String data) -> JOSE::SignedBinary
|
|
15
12
|
def sign_segments: (Hash[String | Symbol, untyped] header, Hash[String | Symbol, untyped] payload) -> JOSE::SignedBinary
|
|
16
|
-
def
|
|
17
|
-
def
|
|
18
|
-
def to_jwk: (?include_private: bool) -> JOSE::Map
|
|
19
|
-
def self.from_jwk: (json_object | JOSE::Map jwk) -> KeyManager
|
|
13
|
+
def private_jwk: () -> json_object
|
|
14
|
+
def self.from_jwk: (json_object | JOSE::Map jwk) -> KeySet
|
|
20
15
|
|
|
21
16
|
private
|
|
22
|
-
def
|
|
17
|
+
def generate_keypair: () -> JOSE::JWK
|
|
18
|
+
def sign: (String data) -> JOSE::SignedBinary
|
|
19
|
+
def deep_stringify_keys: (untyped obj) -> untyped
|
|
20
|
+
def verify: (String signed_jws) -> bool
|
|
23
21
|
def validate_keypair: () -> void
|
|
24
22
|
end
|
|
25
23
|
end
|
|
@@ -7,13 +7,13 @@ module AuthProto
|
|
|
7
7
|
class NonceError < AuthProto::Error
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
DEFAULT_TTL: Integer
|
|
11
10
|
def initialize: (?ttl: Integer?) -> void
|
|
12
|
-
def update: (nonce: String, server_url: String) -> void
|
|
13
|
-
def get: (String
|
|
14
|
-
def clear: (String
|
|
11
|
+
def update: (nonce: String, server_url: String, session_id: String) -> void
|
|
12
|
+
def get: (server_url: String, session_id: String) -> String?
|
|
13
|
+
def clear: (server_url: String, session_id: String) -> bool
|
|
15
14
|
|
|
16
15
|
private
|
|
16
|
+
def validate_session_id: (String session_id) -> void
|
|
17
17
|
def normalize_server_url: (String url) -> String
|
|
18
18
|
def validate_inputs: (String nonce, String server_url) -> void
|
|
19
19
|
def validate_server_url: (String server_url) -> void
|