authproto 0.3.0.beta1 → 0.3.0.beta3
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 +67 -7
- data/README.md +128 -5
- data/examples/confidential_client/Gemfile.lock +5 -1
- data/examples/confidential_client/README.md +12 -2
- data/examples/confidential_client/app.rb +23 -22
- data/examples/confidential_client/config/{client-metadata.example.json → oauth-client-metadata.example.json} +0 -1
- data/examples/confidential_client/scripts/generate_keys.rb +15 -9
- data/examples/minisky_console/Gemfile.lock +2 -2
- data/examples/minisky_console/README.md +20 -1
- data/examples/minisky_console/localhost.rb +13 -28
- data/examples/minisky_console/production.rb +10 -4
- data/lib/authproto/client.rb +185 -253
- data/lib/authproto/configuration.rb +37 -16
- data/lib/authproto/dpop/client.rb +96 -79
- data/lib/authproto/dpop/key_set.rb +157 -0
- data/lib/authproto/dpop/nonce_manager.rb +38 -39
- data/lib/authproto/dpop/proof_generator.rb +39 -50
- data/lib/authproto/dpop.rb +19 -0
- data/lib/authproto/errors.rb +1 -1
- data/lib/authproto/http_client.rb +17 -31
- data/lib/authproto/http_helpers.rb +85 -0
- data/lib/authproto/localhost_server.rb +15 -6
- data/lib/authproto/metadata/authorization_server.rb +364 -0
- data/lib/authproto/metadata/client_metadata.rb +587 -0
- data/lib/authproto/metadata/resource_server.rb +139 -0
- data/lib/authproto/{server_metadata.rb → metadata.rb} +7 -7
- data/lib/authproto/minisky.rb +41 -41
- data/lib/authproto/par/client.rb +60 -115
- data/lib/authproto/par/client_assertion.rb +95 -21
- data/lib/authproto/par/request.rb +60 -146
- data/lib/authproto/par/response.rb +9 -10
- data/lib/authproto/par.rb +5 -6
- data/lib/authproto/pkce.rb +29 -106
- data/lib/authproto/serialization/session.rb +36 -18
- data/lib/authproto/serialization/token_set.rb +6 -7
- data/lib/authproto/state/session.rb +93 -37
- data/lib/authproto/state/session_manager.rb +97 -23
- data/lib/authproto/state/token_set.rb +19 -19
- data/lib/authproto/state.rb +4 -4
- data/lib/authproto/storage/key_builder.rb +3 -11
- data/lib/authproto/storage/memory.rb +8 -2
- data/lib/authproto/token/refresh.rb +50 -91
- data/lib/authproto/validated_uri.rb +80 -0
- data/lib/authproto/version.rb +1 -1
- data/lib/authproto.rb +1 -1
- data/sig/authproto/client.rbs +11 -14
- data/sig/authproto/dependencies.rbs +2 -0
- data/sig/authproto/dpop/client.rbs +9 -9
- data/sig/authproto/dpop/{key_manager.rbs → key_set.rbs} +9 -11
- data/sig/authproto/dpop/nonce_manager.rbs +4 -5
- data/sig/authproto/dpop/proof_generator.rbs +10 -15
- data/sig/authproto/dpop.rbs +6 -0
- data/sig/authproto/http_client.rbs +2 -2
- data/sig/authproto/http_helpers.rbs +7 -0
- data/sig/authproto/localhost_server.rbs +1 -1
- data/sig/authproto/{server_metadata → metadata}/authorization_server.rbs +19 -12
- data/sig/authproto/metadata/client_metadata.rbs +119 -0
- data/sig/authproto/metadata/resource_server.rbs +28 -0
- data/sig/authproto/minisky_oauth.rbs +9 -4
- data/sig/authproto/par/client.rbs +8 -14
- data/sig/authproto/par/client_assertion.rbs +6 -1
- data/sig/authproto/par/request.rbs +5 -28
- data/sig/authproto/par/response.rbs +1 -5
- data/sig/authproto/pkce.rbs +6 -16
- data/sig/authproto/serialization/session.rbs +2 -2
- data/sig/authproto/state/session.rbs +13 -9
- data/sig/authproto/state/session_manager.rbs +9 -4
- data/sig/authproto/state/token_set.rbs +1 -2
- data/sig/authproto/storage/key_builder.rbs +1 -2
- data/sig/authproto/token/refresh.rbs +6 -7
- data/sig/authproto/validated_uri.rbs +36 -0
- data/sig/authproto.rbs +10 -5
- metadata +24 -27
- data/.rspec +0 -1
- data/.rubocop.yml +0 -31
- data/.tm_properties +0 -1
- data/.yardopts +0 -4
- data/Rakefile +0 -8
- data/examples/confidential_client/.gitignore +0 -5
- data/examples/minisky_console/.gitignore +0 -2
- data/lib/authproto/client_metadata.rb +0 -478
- data/lib/authproto/dpop/key_manager.rb +0 -223
- data/lib/authproto/serialization/dpop_key.rb +0 -38
- data/lib/authproto/server_metadata/authorization_server.rb +0 -267
- data/lib/authproto/server_metadata/origin_url.rb +0 -71
- data/lib/authproto/server_metadata/resource_server.rb +0 -98
- data/sig/authproto/client_metadata.rbs +0 -113
- data/sig/authproto/serialization/dpop_key.rbs +0 -14
- data/sig/authproto/server_metadata/origin_url.rbs +0 -21
- data/sig/authproto/server_metadata/resource_server.rbs +0 -27
|
@@ -7,6 +7,7 @@ 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'
|
|
@@ -65,7 +66,7 @@ module AuthProto
|
|
|
65
66
|
super(
|
|
66
67
|
"Token has expired",
|
|
67
68
|
token_type: token_type,
|
|
68
|
-
error_code:
|
|
69
|
+
error_code: 'token_expired',
|
|
69
70
|
retry_possible: true
|
|
70
71
|
)
|
|
71
72
|
end
|
|
@@ -84,7 +85,7 @@ module AuthProto
|
|
|
84
85
|
super(
|
|
85
86
|
"Token is invalid",
|
|
86
87
|
token_type: token_type,
|
|
87
|
-
error_code:
|
|
88
|
+
error_code: 'token_invalid',
|
|
88
89
|
retry_possible: false
|
|
89
90
|
)
|
|
90
91
|
end
|
|
@@ -104,8 +105,8 @@ module AuthProto
|
|
|
104
105
|
def initialize(message, retry_possible: true)
|
|
105
106
|
super(
|
|
106
107
|
message,
|
|
107
|
-
token_type:
|
|
108
|
-
error_code:
|
|
108
|
+
token_type: 'refresh',
|
|
109
|
+
error_code: 'refresh_failed',
|
|
109
110
|
retry_possible: retry_possible
|
|
110
111
|
)
|
|
111
112
|
end
|
|
@@ -116,6 +117,7 @@ module AuthProto
|
|
|
116
117
|
#
|
|
117
118
|
|
|
118
119
|
class Refresh
|
|
120
|
+
include HTTPHelpers
|
|
119
121
|
include MonitorMixin
|
|
120
122
|
|
|
121
123
|
# Maximum attempts for one refresh operation.
|
|
@@ -139,39 +141,49 @@ module AuthProto
|
|
|
139
141
|
attr_reader :dpop_client
|
|
140
142
|
|
|
141
143
|
# Server providing the token endpoint.
|
|
142
|
-
# @return [
|
|
144
|
+
# @return [Metadata::AuthorizationServer]
|
|
143
145
|
attr_reader :auth_server
|
|
144
146
|
|
|
145
147
|
# Client metadata used for optional confidential-client assertions.
|
|
146
|
-
# @return [ClientMetadata]
|
|
148
|
+
# @return [Metadata::ClientMetadata]
|
|
147
149
|
attr_reader :client_metadata
|
|
148
150
|
|
|
149
151
|
# Creates a synchronized refresh operation for an existing OAuth session.
|
|
150
152
|
#
|
|
151
153
|
# @param session [State::Session] session with an access and refresh token
|
|
152
154
|
# @param dpop_client [DPoP::Client] proof signer and nonce tracker
|
|
153
|
-
# @param auth_server [
|
|
154
|
-
# @param client_metadata [ClientMetadata] client ID and authentication key configuration
|
|
155
|
+
# @param auth_server [Metadata::AuthorizationServer] server with a token endpoint
|
|
156
|
+
# @param client_metadata [Metadata::ClientMetadata] client ID and authentication key configuration
|
|
157
|
+
# @param client_assertion [PAR::ClientAssertion, nil] reusable validated signer; required for confidential clients
|
|
158
|
+
# @raise [ArgumentError] if confidential-client metadata is supplied without an assertion signer
|
|
155
159
|
#
|
|
156
|
-
def initialize(session:, dpop_client:, auth_server:, client_metadata:)
|
|
160
|
+
def initialize(session:, dpop_client:, auth_server:, client_metadata:, client_assertion: nil)
|
|
157
161
|
super() # Initialize MonitorMixin
|
|
162
|
+
|
|
158
163
|
@session = session
|
|
159
164
|
@dpop_client = dpop_client
|
|
160
165
|
@auth_server = auth_server
|
|
161
166
|
@attempt_count = 0
|
|
162
167
|
@client_metadata = client_metadata
|
|
168
|
+
@client_assertion = client_assertion
|
|
169
|
+
|
|
170
|
+
if client_metadata.confidential? && !client_assertion.is_a?(PAR::ClientAssertion)
|
|
171
|
+
raise ArgumentError, "Confidential clients require a client_assertion signer"
|
|
172
|
+
end
|
|
163
173
|
end
|
|
164
174
|
|
|
165
175
|
# Exchanges a refresh token for a replacement token set.
|
|
166
176
|
#
|
|
167
|
-
#
|
|
168
|
-
# jitter
|
|
177
|
+
# Makes up to {MAX_RETRIES} attempts for eligible failures, with exponential backoff
|
|
178
|
+
# and jitter. Each attempt can retry one DPoP nonce challenge. Does not assign
|
|
169
179
|
# the returned tokens to the session or persist them; the caller must do so.
|
|
180
|
+
# HTTP 400/401 OAuth errors preserve the server's error code and description in the exception message.
|
|
170
181
|
#
|
|
171
182
|
# @return [State::TokenSet] validated replacement tokens
|
|
172
183
|
# @raise [RefreshError] if the session is not renewable, refresh fails, or retries are exhausted
|
|
184
|
+
# @raise [DPoP::Client::Error] if any response lacks a valid nonce or nonce persistence fails
|
|
173
185
|
#
|
|
174
|
-
def
|
|
186
|
+
def perform_refresh
|
|
175
187
|
synchronize do
|
|
176
188
|
raise RefreshError.new("No refresh token available", retry_possible: false) unless session.renewable?
|
|
177
189
|
raise RefreshError.new("No access token to refresh", retry_possible: false) if session.tokens.nil?
|
|
@@ -203,7 +215,6 @@ module AuthProto
|
|
|
203
215
|
end
|
|
204
216
|
end
|
|
205
217
|
|
|
206
|
-
# Reached max retries
|
|
207
218
|
raise RefreshError.new(
|
|
208
219
|
"Token refresh failed after #{MAX_RETRIES} attempts: #{last_error.message}",
|
|
209
220
|
retry_possible: false
|
|
@@ -217,37 +228,23 @@ module AuthProto
|
|
|
217
228
|
end
|
|
218
229
|
|
|
219
230
|
def request_token_refresh
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
# Handle DPoP nonce requirement
|
|
224
|
-
if requires_dpop_nonce?(response)
|
|
225
|
-
# Extract and store nonce from error response
|
|
226
|
-
extract_dpop_nonce(response)
|
|
227
|
-
dpop_client.process_response(response[:headers], auth_server.issuer)
|
|
228
|
-
|
|
229
|
-
# Retry request with nonce
|
|
230
|
-
response = make_token_request(session)
|
|
231
|
+
response = dpop_client.with_nonce_handling(session, auth_server.token_endpoint) do
|
|
232
|
+
make_token_request(session)
|
|
231
233
|
end
|
|
232
234
|
|
|
233
235
|
handle_refresh_response(response)
|
|
234
236
|
end
|
|
235
237
|
|
|
236
238
|
def make_token_request(session)
|
|
237
|
-
|
|
238
|
-
proof = dpop_client.generate_proof(
|
|
239
|
-
http_method: "POST",
|
|
240
|
-
http_uri: auth_server.token_endpoint
|
|
241
|
-
)
|
|
239
|
+
dpop_proof = dpop_client.generate_proof(session: session, method: 'POST', uri: auth_server.token_endpoint)
|
|
242
240
|
|
|
243
241
|
body = {
|
|
244
|
-
grant_type:
|
|
242
|
+
grant_type: 'refresh_token',
|
|
245
243
|
refresh_token: session.tokens.refresh_token,
|
|
246
244
|
scope: session.scope,
|
|
247
245
|
client_id: client_metadata.client_id
|
|
248
246
|
}
|
|
249
247
|
|
|
250
|
-
# Add client authentication if available
|
|
251
248
|
add_client_authentication(body) if client_metadata.confidential?
|
|
252
249
|
|
|
253
250
|
AuthProto.configuration.http_client.post(
|
|
@@ -255,45 +252,15 @@ module AuthProto
|
|
|
255
252
|
body: body,
|
|
256
253
|
headers: {
|
|
257
254
|
"Content-Type" => "application/x-www-form-urlencoded",
|
|
258
|
-
"DPoP" =>
|
|
255
|
+
"DPoP" => dpop_proof
|
|
259
256
|
}
|
|
260
257
|
)
|
|
261
258
|
end
|
|
262
259
|
|
|
263
|
-
def requires_dpop_nonce?(response)
|
|
264
|
-
return false unless response[:status] == 400
|
|
265
|
-
return false if response[:body].nil?
|
|
266
|
-
|
|
267
|
-
error_data = JSON.parse(response[:body])
|
|
268
|
-
error_data.is_a?(Hash) && error_data["error"] == "use_dpop_nonce"
|
|
269
|
-
rescue JSON::ParserError
|
|
270
|
-
false
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
def extract_dpop_nonce(response)
|
|
274
|
-
nonce = (response[:headers] || {}).detect { |k, v| k.downcase == "dpop-nonce" }&.last
|
|
275
|
-
|
|
276
|
-
unless nonce.is_a?(String) && !nonce.empty?
|
|
277
|
-
raise RefreshError.new("No DPoP nonce provided in error response", retry_possible: false)
|
|
278
|
-
end
|
|
279
|
-
|
|
280
|
-
nonce
|
|
281
|
-
end
|
|
282
|
-
|
|
283
260
|
def add_client_authentication(body)
|
|
284
|
-
return unless client_metadata.jwks && !client_metadata.jwks["keys"].empty?
|
|
285
|
-
|
|
286
|
-
signing_key = JOSE::JWK.from_map(client_metadata.jwks["keys"].first)
|
|
287
|
-
client_assertion = PAR::ClientAssertion.new(
|
|
288
|
-
client_id: client_metadata.client_id,
|
|
289
|
-
signing_key: signing_key
|
|
290
|
-
)
|
|
291
|
-
|
|
292
261
|
body.merge!(
|
|
293
262
|
client_assertion_type: PAR::CLIENT_ASSERTION_TYPE,
|
|
294
|
-
client_assertion: client_assertion.generate_jwt(
|
|
295
|
-
audience: auth_server.issuer
|
|
296
|
-
)
|
|
263
|
+
client_assertion: @client_assertion.generate_jwt(audience: auth_server.issuer)
|
|
297
264
|
)
|
|
298
265
|
end
|
|
299
266
|
|
|
@@ -301,10 +268,8 @@ module AuthProto
|
|
|
301
268
|
case response[:status]
|
|
302
269
|
when 200
|
|
303
270
|
process_successful_response(response)
|
|
304
|
-
when 400
|
|
305
|
-
|
|
306
|
-
when 401
|
|
307
|
-
raise RefreshError.new("Refresh token is invalid", retry_possible: false)
|
|
271
|
+
when 400, 401
|
|
272
|
+
handle_error_response(response)
|
|
308
273
|
when 429
|
|
309
274
|
handle_rate_limit_response(response)
|
|
310
275
|
else
|
|
@@ -319,18 +284,18 @@ module AuthProto
|
|
|
319
284
|
validate_refresh_response(data)
|
|
320
285
|
|
|
321
286
|
AuthProto::State::TokenSet.new(
|
|
322
|
-
access_token: data[
|
|
323
|
-
token_type: data[
|
|
324
|
-
expires_in: data[
|
|
325
|
-
refresh_token: data[
|
|
326
|
-
scope: data[
|
|
327
|
-
sub: data[
|
|
287
|
+
access_token: data['access_token'],
|
|
288
|
+
token_type: data['token_type'],
|
|
289
|
+
expires_in: data['expires_in'],
|
|
290
|
+
refresh_token: data['refresh_token'],
|
|
291
|
+
scope: data['scope'],
|
|
292
|
+
sub: data['sub']
|
|
328
293
|
)
|
|
329
294
|
rescue JSON::ParserError => e
|
|
330
295
|
raise RefreshError.new("Invalid response format: #{e.message}", retry_possible: false)
|
|
331
296
|
end
|
|
332
297
|
|
|
333
|
-
def
|
|
298
|
+
def handle_error_response(response)
|
|
334
299
|
raise RefreshError.new("Missing error response body", retry_possible: false) if response[:body].nil?
|
|
335
300
|
|
|
336
301
|
error_data = JSON.parse(response[:body])
|
|
@@ -339,13 +304,9 @@ module AuthProto
|
|
|
339
304
|
raise RefreshError.new("Invalid error response format", retry_possible: false)
|
|
340
305
|
end
|
|
341
306
|
|
|
342
|
-
error_description = error_data[
|
|
307
|
+
error_description = [error_data['error'], error_data['error_description']].compact.join(': ')
|
|
343
308
|
|
|
344
|
-
case error_data[
|
|
345
|
-
when "use_dpop_nonce"
|
|
346
|
-
extract_dpop_nonce(response)
|
|
347
|
-
dpop_client.process_response(response[:headers], auth_server.issuer)
|
|
348
|
-
raise RefreshError.new("Retry with DPoP nonce", retry_possible: true)
|
|
309
|
+
case error_data['error']
|
|
349
310
|
when "invalid_grant"
|
|
350
311
|
# The refresh token has been invalidated or already used
|
|
351
312
|
raise RefreshError.new(
|
|
@@ -364,7 +325,8 @@ module AuthProto
|
|
|
364
325
|
|
|
365
326
|
def handle_rate_limit_response(response)
|
|
366
327
|
# Extract retry-after if available
|
|
367
|
-
retry_after = response
|
|
328
|
+
retry_after = response_header(response, 'Retry-After')
|
|
329
|
+
retry_after = retry_after&.to_i || calculate_delay
|
|
368
330
|
raise RefreshError, "Rate limited - retry after #{retry_after} seconds"
|
|
369
331
|
end
|
|
370
332
|
|
|
@@ -373,7 +335,6 @@ module AuthProto
|
|
|
373
335
|
raise RefreshError.new("Token response must be a JSON object", retry_possible: false)
|
|
374
336
|
end
|
|
375
337
|
|
|
376
|
-
# Required fields
|
|
377
338
|
%w[access_token token_type expires_in scope sub].each do |field|
|
|
378
339
|
raise RefreshError.new("Missing #{field} in response", retry_possible: false) unless data[field]
|
|
379
340
|
end
|
|
@@ -384,28 +345,26 @@ module AuthProto
|
|
|
384
345
|
end
|
|
385
346
|
end
|
|
386
347
|
|
|
387
|
-
unless data[
|
|
348
|
+
unless data['expires_in'].is_a?(Integer)
|
|
388
349
|
raise RefreshError.new("Invalid expires_in in response", retry_possible: false)
|
|
389
350
|
end
|
|
390
351
|
|
|
391
|
-
unless data[
|
|
352
|
+
unless data['refresh_token'].nil? || data['refresh_token'].is_a?(String)
|
|
392
353
|
raise RefreshError.new("Invalid refresh_token in response", retry_possible: false)
|
|
393
354
|
end
|
|
394
355
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
raise RefreshError.new("Invalid token_type: #{data["token_type"]}", retry_possible: false)
|
|
356
|
+
unless data['token_type'].casecmp?('DPoP')
|
|
357
|
+
raise RefreshError.new("Invalid token_type: #{data['token_type']}", retry_possible: false)
|
|
398
358
|
end
|
|
399
359
|
|
|
400
|
-
# Scope must include original scopes
|
|
401
360
|
original_scopes = session.scope.split
|
|
402
|
-
response_scopes = data[
|
|
361
|
+
response_scopes = data['scope'].split
|
|
362
|
+
|
|
403
363
|
unless (original_scopes - response_scopes).empty?
|
|
404
364
|
raise RefreshError.new("Invalid scope in response", retry_possible: false)
|
|
405
365
|
end
|
|
406
366
|
|
|
407
|
-
|
|
408
|
-
return if data["sub"] == session.tokens.sub
|
|
367
|
+
return if data['sub'] == session.tokens.sub
|
|
409
368
|
|
|
410
369
|
raise RefreshError.new("Subject mismatch in response", retry_possible: false)
|
|
411
370
|
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'forwardable'
|
|
4
|
+
require 'uri'
|
|
5
|
+
|
|
6
|
+
module AuthProto
|
|
7
|
+
|
|
8
|
+
#
|
|
9
|
+
# An HTTP(S) URI with a required host and preserved source components.
|
|
10
|
+
#
|
|
11
|
+
# Unlike URI::HTTP, port is the explicit port string (or nil when omitted),
|
|
12
|
+
# userinfo preserves an empty component, and path treats "/" as empty.
|
|
13
|
+
# Other readers and string conversion delegate to the parsed URI.
|
|
14
|
+
#
|
|
15
|
+
|
|
16
|
+
class ValidatedURI
|
|
17
|
+
extend Forwardable
|
|
18
|
+
|
|
19
|
+
attr_reader :port, :userinfo, :path
|
|
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]
|
|
30
|
+
def_delegators :@uri, :scheme, :host, :hostname, :query, :fragment,
|
|
31
|
+
:user, :password, :registry, :opaque, :default_port,
|
|
32
|
+
:request_uri, :origin, :absolute?, :relative?, :hierarchical?,
|
|
33
|
+
:to_s, :to_str
|
|
34
|
+
|
|
35
|
+
# Parses an absolute HTTP(S) URL and preserves explicitly supplied components.
|
|
36
|
+
#
|
|
37
|
+
# @param url [String, URI::Generic] URL to parse; URI inputs use their serialized form
|
|
38
|
+
# @raise [URI::InvalidURIError] if the URL is malformed, not HTTP(S), or has no host
|
|
39
|
+
#
|
|
40
|
+
def initialize(url)
|
|
41
|
+
unless url.is_a?(String) || url.is_a?(URI::Generic)
|
|
42
|
+
raise URI::InvalidURIError, "URL must be a string or URI"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
source = url.to_s
|
|
46
|
+
@uri = URI.parse(source)
|
|
47
|
+
|
|
48
|
+
unless @uri.is_a?(URI::HTTP) && @uri.host && !@uri.host.empty?
|
|
49
|
+
raise URI::InvalidURIError, "URL must use HTTP or HTTPS and include a host"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
@userinfo, @port = URI.split(source).values_at(1, 3)
|
|
53
|
+
@path = (@uri.path == '/') ? '' : @uri.path
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Returns the explicitly supplied port, without substituting a scheme default.
|
|
57
|
+
#
|
|
58
|
+
# @return [String, nil] port text, or `nil` if omitted
|
|
59
|
+
#
|
|
60
|
+
def specified_port
|
|
61
|
+
port
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Returns the parsed port, using the scheme default when no port was supplied.
|
|
65
|
+
#
|
|
66
|
+
# @return [Integer] effective HTTP(S) port
|
|
67
|
+
#
|
|
68
|
+
def effective_port
|
|
69
|
+
@uri.port
|
|
70
|
+
end
|
|
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
|
+
#
|
|
76
|
+
def to_uri
|
|
77
|
+
@uri
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
data/lib/authproto/version.rb
CHANGED
data/lib/authproto.rb
CHANGED
|
@@ -5,7 +5,7 @@ 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/
|
|
8
|
+
require_relative 'authproto/validated_uri'
|
|
9
9
|
require_relative 'authproto/version'
|
|
10
10
|
|
|
11
11
|
#
|
data/sig/authproto/client.rbs
CHANGED
|
@@ -11,33 +11,30 @@ module AuthProto
|
|
|
11
11
|
|
|
12
12
|
attr_reader client_id: String
|
|
13
13
|
attr_reader redirect_uri: String
|
|
14
|
-
attr_reader client_metadata: ClientMetadata
|
|
14
|
+
attr_reader client_metadata: Metadata::ClientMetadata
|
|
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?, ?
|
|
19
|
-
def authorize: (?handle: String?, ?
|
|
20
|
-
def handle_callback: (code: String, state: String, iss: String) -> callback_result
|
|
18
|
+
def initialize: (client_id: String, redirect_uri: String, ?metadata: json_object?, ?signing_key: (json_object | JOSE::JWK)?) -> void
|
|
19
|
+
def authorize: (?handle: String?, ?pds: String?, ?scope: String) -> authorization_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
|
|
25
|
+
def delete_session: (String session_id) -> void
|
|
26
26
|
def cleanup_expired_sessions: () -> void
|
|
27
27
|
|
|
28
28
|
private
|
|
29
|
-
def load_client_metadata: (json_object? metadata) -> ClientMetadata
|
|
29
|
+
def load_client_metadata: (json_object? metadata) -> Metadata::ClientMetadata
|
|
30
30
|
def validate_redirect_uri: () -> void
|
|
31
|
-
def
|
|
32
|
-
def
|
|
33
|
-
def
|
|
34
|
-
def
|
|
35
|
-
def
|
|
36
|
-
def generate_authorization_url: (ServerMetadata::AuthorizationServer auth_server, State::Session session, ?login_hint: String?) -> String
|
|
31
|
+
def initialize_client_assertion: ((json_object | JOSE::JWK)? key) -> String?
|
|
32
|
+
def metadata_key_matches_public_key?: (json_object candidate, json_object public_key) -> bool
|
|
33
|
+
def validate_auth_params: (String? handle, String? pds, String scope) -> void
|
|
34
|
+
def resolve_auth_server_info: (String pds) -> Metadata::AuthorizationServer
|
|
35
|
+
def submit_authorization_par: (Metadata::AuthorizationServer authorization_server, State::Session session, ?login_hint: String?) -> String
|
|
37
36
|
def exchange_code: (code: String, session: State::Session) -> json_object
|
|
38
37
|
def make_token_request: (String code, State::Session session) -> http_response
|
|
39
|
-
def requires_dpop_nonce?: (http_response response) -> bool
|
|
40
|
-
def extract_dpop_nonce: (http_response response) -> String
|
|
41
38
|
def validate_token_response: (json_object response, State::Session session) -> void
|
|
42
39
|
end
|
|
43
40
|
end
|
|
@@ -1,23 +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: (
|
|
13
|
-
def
|
|
14
|
-
def
|
|
15
|
-
|
|
16
|
-
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
17
|
|
|
18
18
|
private
|
|
19
|
-
|
|
20
|
-
def
|
|
19
|
+
|
|
20
|
+
def nonce_challenge?: (String? challenge) -> bool
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
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,14 +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
|
|
15
|
-
def valid_nonce?: (String server_url) -> bool
|
|
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
|
|
16
14
|
|
|
17
15
|
private
|
|
16
|
+
def validate_session_id: (String session_id) -> void
|
|
18
17
|
def normalize_server_url: (String url) -> String
|
|
19
18
|
def validate_inputs: (String nonce, String server_url) -> void
|
|
20
19
|
def validate_server_url: (String server_url) -> void
|
|
@@ -1,35 +1,30 @@
|
|
|
1
1
|
module AuthProto
|
|
2
2
|
module DPoP
|
|
3
3
|
class ProofGenerator
|
|
4
|
-
@key_manager: AuthProto::DPoP::KeyManager
|
|
5
|
-
|
|
6
4
|
class ProofError < AuthProto::Error
|
|
7
5
|
end
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def initialize: (AuthProto::DPoP::KeyManager key_manager) -> void
|
|
7
|
+
def initialize: () -> void
|
|
12
8
|
|
|
13
9
|
def generate: (
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
key_set: KeySet,
|
|
11
|
+
method: String,
|
|
12
|
+
uri: String,
|
|
16
13
|
?nonce: String?,
|
|
17
|
-
?access_token: String
|
|
18
|
-
?ath: bool?
|
|
14
|
+
?access_token: String?
|
|
19
15
|
) -> JOSE::SignedBinary
|
|
20
16
|
|
|
21
17
|
private
|
|
22
18
|
|
|
23
|
-
def validate_inputs: (String
|
|
19
|
+
def validate_inputs: (String method, String uri) -> void
|
|
24
20
|
|
|
25
|
-
def build_header: () -> { typ: "dpop+jwt", alg: "ES256", jwk: Hash[String, untyped] }
|
|
21
|
+
def build_header: (KeySet key_set) -> { typ: "dpop+jwt", alg: "ES256", jwk: Hash[String, untyped] }
|
|
26
22
|
|
|
27
23
|
def build_payload: (
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
method: String,
|
|
25
|
+
uri: String,
|
|
30
26
|
?nonce: String?,
|
|
31
|
-
?access_token: String
|
|
32
|
-
?include_ath: bool?
|
|
27
|
+
?access_token: String?
|
|
33
28
|
) -> Hash[String, untyped]
|
|
34
29
|
|
|
35
30
|
def normalize_uri: (String uri) -> String
|
|
@@ -34,10 +34,10 @@ module AuthProto
|
|
|
34
34
|
def validate_ip: (URI::HTTP uri) -> void
|
|
35
35
|
def forbidden_ip?: (IPAddr ip) -> bool
|
|
36
36
|
def make_request: (URI::HTTP uri, ?Hash[String, String] headers, ?Integer redirect_count, ?follow_redirects: bool) -> Net::HTTPResponse
|
|
37
|
-
def make_post_request: (URI::HTTP uri, http_body body, ?Hash[String, String] headers
|
|
37
|
+
def make_post_request: (URI::HTTP uri, http_body body, ?Hash[String, String] headers) -> Net::HTTPResponse
|
|
38
38
|
def configure_http_client: (Net::HTTP http, ?ssl: bool) -> void
|
|
39
39
|
def add_security_headers: (Net::HTTPRequest request, Hash[String, String] headers) -> void
|
|
40
|
-
def handle_redirect: (original_uri: URI::HTTP, response: Net::HTTPResponse, headers: Hash[String, String], redirect_count: Integer
|
|
40
|
+
def handle_redirect: (original_uri: URI::HTTP, response: Net::HTTPResponse, headers: Hash[String, String], redirect_count: Integer) -> Net::HTTPResponse
|
|
41
41
|
def validate_response: (Net::HTTPResponse response) -> void
|
|
42
42
|
def check_success_status: (Net::HTTPResponse response) -> void
|
|
43
43
|
def check_content_length: (Net::HTTPResponse response) -> void
|