authproto 0.3.0.beta3 → 0.3.0.beta5
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 +39 -1
- data/README.md +474 -311
- data/examples/basic_webapp/Gemfile +14 -0
- data/examples/{confidential_client → basic_webapp}/Gemfile.lock +1 -1
- data/examples/basic_webapp/README.md +300 -0
- data/examples/basic_webapp/app.rb +351 -0
- data/examples/basic_webapp/config/oauth-client-metadata.localhost.json +11 -0
- data/examples/basic_webapp/public/styles.css +346 -0
- data/examples/basic_webapp/scripts/generate_keys.rb +35 -0
- data/examples/basic_webapp/views/authorized.erb +48 -0
- data/examples/basic_webapp/views/index.erb +48 -0
- data/examples/{confidential_client → basic_webapp}/views/layout.erb +2 -2
- data/examples/bookmarks/Gemfile +16 -0
- data/examples/bookmarks/Gemfile.lock +158 -0
- data/examples/bookmarks/README.md +170 -0
- data/examples/bookmarks/Rakefile +23 -0
- data/examples/bookmarks/app/account.rb +16 -0
- data/examples/bookmarks/app/bookmark.rb +89 -0
- data/examples/bookmarks/app/init.rb +26 -0
- data/examples/bookmarks/app/oauth.rb +43 -0
- data/examples/bookmarks/app/server.rb +184 -0
- data/examples/bookmarks/config/oauth-client-metadata.localhost.json +11 -0
- data/examples/bookmarks/config/oauth-client-metadata.production.json +19 -0
- data/examples/bookmarks/config.ru +5 -0
- data/examples/bookmarks/db/migrate/20261107004141_create_bookmarks.rb +27 -0
- data/examples/bookmarks/db/schema.rb +33 -0
- data/examples/bookmarks/public/styles.css +59 -0
- data/examples/bookmarks/scripts/generate_keys.rb +35 -0
- data/examples/bookmarks/views/bookmarks.erb +86 -0
- data/examples/bookmarks/views/index.erb +17 -0
- data/examples/bookmarks/views/layout.erb +20 -0
- data/examples/minisky_console/Gemfile.lock +2 -2
- data/examples/minisky_console/README.md +29 -25
- data/examples/minisky_console/app.rb +3 -3
- data/examples/minisky_console/localhost.rb +5 -7
- data/examples/minisky_console/production.rb +6 -8
- data/lib/authproto/client.rb +505 -151
- data/lib/authproto/configuration.rb +1 -1
- data/lib/authproto/dpop/client.rb +28 -25
- data/lib/authproto/dpop/key_set.rb +2 -0
- data/lib/authproto/dpop/nonce_manager.rb +24 -25
- data/lib/authproto/dpop/proof_generator.rb +9 -5
- data/lib/authproto/dpop/stored_nonce.rb +7 -5
- data/lib/authproto/dpop.rb +2 -0
- data/lib/authproto/encryption.rb +31 -12
- data/lib/authproto/http_client.rb +13 -12
- data/lib/authproto/http_helpers.rb +6 -0
- data/lib/authproto/metadata/authorization_server.rb +15 -3
- data/lib/authproto/metadata/client_metadata.rb +34 -3
- data/lib/authproto/metadata/resource_server.rb +7 -6
- data/lib/authproto/minisky.rb +120 -33
- data/lib/authproto/par/client.rb +4 -2
- data/lib/authproto/par/client_assertion.rb +4 -1
- data/lib/authproto/par/request.rb +2 -0
- data/lib/authproto/par/response.rb +2 -0
- data/lib/authproto/par.rb +2 -0
- data/lib/authproto/pkce.rb +2 -0
- data/lib/authproto/serialization/session.rb +4 -2
- data/lib/authproto/serialization/stored_nonce.rb +2 -2
- data/lib/authproto/serialization/token_set.rb +5 -2
- data/lib/authproto/state/session.rb +23 -20
- data/lib/authproto/state/session_info.rb +150 -0
- data/lib/authproto/state/session_manager.rb +46 -19
- data/lib/authproto/state/token_set.rb +6 -3
- data/lib/authproto/state.rb +4 -2
- data/lib/authproto/storage/interface.rb +10 -0
- data/lib/authproto/storage/key_builder.rb +3 -3
- data/lib/authproto/storage/memory.rb +24 -18
- data/lib/authproto/storage/sqlite.rb +14 -0
- data/lib/authproto/token/identity.rb +54 -0
- data/lib/authproto/token/refresh.rb +44 -54
- data/lib/authproto/token/response.rb +75 -0
- data/lib/authproto/token/revocation.rb +94 -0
- data/lib/authproto/version.rb +1 -1
- data/lib/authproto.rb +6 -3
- data/sig/authproto/client.rbs +38 -13
- data/sig/authproto/dpop/client.rbs +9 -3
- data/sig/authproto/dpop/nonce_manager.rbs +6 -6
- data/sig/authproto/dpop/proof_generator.rbs +4 -4
- data/sig/authproto/dpop/stored_nonce.rbs +2 -2
- data/sig/authproto/encryption.rbs +4 -2
- data/sig/authproto/http_client.rbs +23 -7
- data/sig/authproto/http_helpers.rbs +1 -0
- data/sig/authproto/metadata/authorization_server.rbs +3 -3
- data/sig/authproto/metadata/client_metadata.rbs +2 -1
- data/sig/authproto/metadata/resource_server.rbs +2 -2
- data/sig/authproto/minisky.rbs +57 -0
- data/sig/authproto/state/session.rbs +13 -4
- data/sig/authproto/state/session_info.rbs +23 -0
- data/sig/authproto/state/session_manager.rbs +10 -3
- data/sig/authproto/storage/interface.rbs +1 -0
- data/sig/authproto/storage/key_builder.rbs +1 -1
- data/sig/authproto/storage/memory.rbs +1 -1
- data/sig/authproto/storage/sqlite.rbs +1 -0
- data/sig/authproto/token/refresh.rbs +2 -1
- data/sig/authproto/token/response.rbs +12 -0
- data/sig/authproto/token/revocation.rbs +15 -0
- data/sig/authproto.rbs +3 -8
- metadata +53 -19
- data/examples/confidential_client/Gemfile +0 -14
- data/examples/confidential_client/README.md +0 -236
- data/examples/confidential_client/app.rb +0 -291
- data/examples/confidential_client/public/styles.css +0 -70
- data/examples/confidential_client/scripts/generate_keys.rb +0 -21
- data/examples/confidential_client/views/authorized.erb +0 -46
- data/examples/confidential_client/views/index.erb +0 -44
- data/sig/authproto/minisky_oauth.rbs +0 -27
- /data/examples/{confidential_client/config/oauth-client-metadata.example.json → basic_webapp/config/oauth-client-metadata.production.json} +0 -0
- /data/examples/{confidential_client → basic_webapp}/config.ru +0 -0
- /data/examples/{confidential_client → basic_webapp}/screenshots/screenshot-1-sign-in.png +0 -0
- /data/examples/{confidential_client → basic_webapp}/screenshots/screenshot-2-success.png +0 -0
|
@@ -11,6 +11,9 @@ require_relative '../http_helpers'
|
|
|
11
11
|
require_relative '../par'
|
|
12
12
|
require_relative '../par/client_assertion'
|
|
13
13
|
require_relative '../state/token_set'
|
|
14
|
+
require_relative 'identity'
|
|
15
|
+
require_relative 'response'
|
|
16
|
+
require_relative 'revocation'
|
|
14
17
|
|
|
15
18
|
module AuthProto
|
|
16
19
|
|
|
@@ -118,6 +121,7 @@ module AuthProto
|
|
|
118
121
|
|
|
119
122
|
class Refresh
|
|
120
123
|
include HTTPHelpers
|
|
124
|
+
include Identity
|
|
121
125
|
include MonitorMixin
|
|
122
126
|
|
|
123
127
|
# Maximum attempts for one refresh operation.
|
|
@@ -178,6 +182,8 @@ module AuthProto
|
|
|
178
182
|
# and jitter. Each attempt can retry one DPoP nonce challenge. Does not assign
|
|
179
183
|
# the returned tokens to the session or persist them; the caller must do so.
|
|
180
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.
|
|
181
187
|
#
|
|
182
188
|
# @return [State::TokenSet] validated replacement tokens
|
|
183
189
|
# @raise [RefreshError] if the session is not renewable, refresh fails, or retries are exhausted
|
|
@@ -188,6 +194,8 @@ module AuthProto
|
|
|
188
194
|
raise RefreshError.new("No refresh token available", retry_possible: false) unless session.renewable?
|
|
189
195
|
raise RefreshError.new("No access token to refresh", retry_possible: false) if session.tokens.nil?
|
|
190
196
|
|
|
197
|
+
validate_identity
|
|
198
|
+
|
|
191
199
|
with_retries do
|
|
192
200
|
request_token_refresh
|
|
193
201
|
end
|
|
@@ -197,6 +205,31 @@ module AuthProto
|
|
|
197
205
|
|
|
198
206
|
private
|
|
199
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
|
+
|
|
200
233
|
def with_retries
|
|
201
234
|
@attempt_count = 0
|
|
202
235
|
last_error = nil
|
|
@@ -228,7 +261,7 @@ module AuthProto
|
|
|
228
261
|
end
|
|
229
262
|
|
|
230
263
|
def request_token_refresh
|
|
231
|
-
response = dpop_client.with_nonce_handling(session, auth_server.token_endpoint) do
|
|
264
|
+
response = dpop_client.with_nonce_handling(session.session_id, auth_server.token_endpoint) do
|
|
232
265
|
make_token_request(session)
|
|
233
266
|
end
|
|
234
267
|
|
|
@@ -241,7 +274,6 @@ module AuthProto
|
|
|
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
|
|
|
@@ -265,7 +297,7 @@ module AuthProto
|
|
|
265
297
|
end
|
|
266
298
|
|
|
267
299
|
def handle_refresh_response(response)
|
|
268
|
-
case response
|
|
300
|
+
case response_status(response)
|
|
269
301
|
when 200
|
|
270
302
|
process_successful_response(response)
|
|
271
303
|
when 400, 401
|
|
@@ -278,21 +310,11 @@ module AuthProto
|
|
|
278
310
|
end
|
|
279
311
|
|
|
280
312
|
def process_successful_response(response)
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
AuthProto::State::TokenSet.new(
|
|
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']
|
|
293
|
-
)
|
|
294
|
-
rescue JSON::ParserError => e
|
|
295
|
-
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)
|
|
296
318
|
end
|
|
297
319
|
|
|
298
320
|
def handle_error_response(response)
|
|
@@ -330,43 +352,11 @@ module AuthProto
|
|
|
330
352
|
raise RefreshError, "Rate limited - retry after #{retry_after} seconds"
|
|
331
353
|
end
|
|
332
354
|
|
|
333
|
-
def
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
%w[access_token token_type expires_in scope sub].each do |field|
|
|
339
|
-
raise RefreshError.new("Missing #{field} in response", retry_possible: false) unless data[field]
|
|
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)
|
|
340
359
|
end
|
|
341
|
-
|
|
342
|
-
%w[access_token token_type scope sub].each do |field|
|
|
343
|
-
unless data[field].is_a?(String) && !data[field].empty?
|
|
344
|
-
raise RefreshError.new("Invalid #{field} in response", retry_possible: false)
|
|
345
|
-
end
|
|
346
|
-
end
|
|
347
|
-
|
|
348
|
-
unless data['expires_in'].is_a?(Integer)
|
|
349
|
-
raise RefreshError.new("Invalid expires_in in response", retry_possible: false)
|
|
350
|
-
end
|
|
351
|
-
|
|
352
|
-
unless data['refresh_token'].nil? || data['refresh_token'].is_a?(String)
|
|
353
|
-
raise RefreshError.new("Invalid refresh_token in response", retry_possible: false)
|
|
354
|
-
end
|
|
355
|
-
|
|
356
|
-
unless data['token_type'].casecmp?('DPoP')
|
|
357
|
-
raise RefreshError.new("Invalid token_type: #{data['token_type']}", retry_possible: false)
|
|
358
|
-
end
|
|
359
|
-
|
|
360
|
-
original_scopes = session.scope.split
|
|
361
|
-
response_scopes = data['scope'].split
|
|
362
|
-
|
|
363
|
-
unless (original_scopes - response_scopes).empty?
|
|
364
|
-
raise RefreshError.new("Invalid scope in response", retry_possible: false)
|
|
365
|
-
end
|
|
366
|
-
|
|
367
|
-
return if data['sub'] == session.tokens.sub
|
|
368
|
-
|
|
369
|
-
raise RefreshError.new("Subject mismatch in response", retry_possible: false)
|
|
370
360
|
end
|
|
371
361
|
end
|
|
372
362
|
end
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
|
|
43
|
+
# OpenID Connect is explicitly not supported
|
|
44
|
+
raise Error, "Token response must not include id_token" if data.key?('id_token')
|
|
45
|
+
|
|
46
|
+
['access_token', 'token_type', 'expires_in', 'scope', 'sub'].each do |field|
|
|
47
|
+
raise Error, "Missing #{field} in token response" if data[field].nil?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
unless data['expires_in'].is_a?(Integer) && data['expires_in'].positive?
|
|
51
|
+
raise Error, "Invalid expires_in in token response: must be a positive integer"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
if data.key?('refresh_token') && data['refresh_token'].nil?
|
|
55
|
+
raise Error, "Invalid refresh_token in token response"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
tokens = State::TokenSet.new(
|
|
59
|
+
access_token: data['access_token'],
|
|
60
|
+
refresh_token: data.fetch('refresh_token', current_tokens&.refresh_token),
|
|
61
|
+
token_type: data['token_type'],
|
|
62
|
+
expires_in: data['expires_in'],
|
|
63
|
+
scope: data['scope'],
|
|
64
|
+
sub: data['sub']
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
raise Error, "Invalid scope in token response: missing atproto" unless tokens.scope.split.include?('atproto')
|
|
68
|
+
|
|
69
|
+
tokens
|
|
70
|
+
rescue JSON::ParserError, ArgumentError => e
|
|
71
|
+
raise Error, "Invalid token response: #{e.message}"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../configuration'
|
|
4
|
+
require_relative '../errors'
|
|
5
|
+
require_relative '../http_helpers'
|
|
6
|
+
require_relative '../par'
|
|
7
|
+
|
|
8
|
+
module AuthProto
|
|
9
|
+
module Token
|
|
10
|
+
|
|
11
|
+
#
|
|
12
|
+
# Revokes tokens using the issuing server and the session's authentication keys.
|
|
13
|
+
#
|
|
14
|
+
|
|
15
|
+
class Revocation
|
|
16
|
+
include HTTPHelpers
|
|
17
|
+
|
|
18
|
+
# Error raised when revocation is unavailable or rejected by the server.
|
|
19
|
+
class Error < AuthProto::Error; end
|
|
20
|
+
|
|
21
|
+
# Creates a revoker using the original session's server and authentication keys.
|
|
22
|
+
#
|
|
23
|
+
# @param session [State::Session] session bound to the issuing authorization server
|
|
24
|
+
# @param dpop_client [DPoP::Client] proof signer and nonce tracker
|
|
25
|
+
# @param client_metadata [Metadata::ClientMetadata] client authentication configuration
|
|
26
|
+
# @param client_assertion [PAR::ClientAssertion, nil] confidential-client assertion signer
|
|
27
|
+
#
|
|
28
|
+
def initialize(session:, dpop_client:, client_metadata:, client_assertion: nil)
|
|
29
|
+
@session = session
|
|
30
|
+
@dpop_client = dpop_client
|
|
31
|
+
@client_metadata = client_metadata
|
|
32
|
+
@client_assertion = client_assertion
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Revokes an access or refresh token and requires a successful response.
|
|
36
|
+
# The response body is ignored, including for tokens already revoked.
|
|
37
|
+
#
|
|
38
|
+
# @param token [String] nonempty token to revoke
|
|
39
|
+
# @param token_type [String, Symbol] `access_token` or `refresh_token`
|
|
40
|
+
# @return [void]
|
|
41
|
+
# @raise [ArgumentError] if the token or hint is invalid
|
|
42
|
+
# @raise [Error] if the endpoint is absent or the server rejects revocation
|
|
43
|
+
# @raise [DPoP::Client::Error, HTTPClient::HTTPError] if proof or transport handling fails
|
|
44
|
+
#
|
|
45
|
+
def revoke_token(token, token_type:)
|
|
46
|
+
raise ArgumentError, "Token must be a nonempty string" unless token.is_a?(String) && !token.empty?
|
|
47
|
+
|
|
48
|
+
token_type = token_type.to_s if token_type.is_a?(Symbol)
|
|
49
|
+
|
|
50
|
+
unless %w[access_token refresh_token].include?(token_type)
|
|
51
|
+
raise ArgumentError, "Invalid token_type"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
endpoint = @session.auth_server&.revocation_endpoint
|
|
55
|
+
raise Error, "Authorization server does not advertise a revocation endpoint" unless endpoint
|
|
56
|
+
|
|
57
|
+
response = @dpop_client.with_nonce_handling(@session.session_id, endpoint) do
|
|
58
|
+
make_request(token, endpoint, token_type)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
raise Error, "Token revocation failed: HTTP #{response[:status]}" unless response_status(response) == 200
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
def make_request(token, endpoint, token_type)
|
|
68
|
+
body = {
|
|
69
|
+
token: token,
|
|
70
|
+
token_type_hint: token_type,
|
|
71
|
+
client_id: @client_metadata.client_id
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if @client_metadata.confidential?
|
|
75
|
+
body.merge!(
|
|
76
|
+
client_assertion_type: PAR::CLIENT_ASSERTION_TYPE,
|
|
77
|
+
client_assertion: @client_assertion.generate_jwt(audience: @session.auth_server.issuer)
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
proof = @dpop_client.generate_proof(session: @session, method: 'POST', uri: endpoint)
|
|
82
|
+
|
|
83
|
+
AuthProto.configuration.http_client.post(
|
|
84
|
+
endpoint,
|
|
85
|
+
body: body,
|
|
86
|
+
headers: {
|
|
87
|
+
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
88
|
+
'DPoP' => proof
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
data/lib/authproto/version.rb
CHANGED
data/lib/authproto.rb
CHANGED
|
@@ -9,9 +9,12 @@ require_relative 'authproto/validated_uri'
|
|
|
9
9
|
require_relative 'authproto/version'
|
|
10
10
|
|
|
11
11
|
#
|
|
12
|
-
# AuthProto is a Ruby
|
|
13
|
-
#
|
|
14
|
-
#
|
|
12
|
+
# AuthProto is a Ruby implementation of an AT Protocol specific OAuth client.
|
|
13
|
+
#
|
|
14
|
+
# Supports both public and confidential clients, handles DPoP, nonce management, client and server metadata JSON,
|
|
15
|
+
# PAR, PKCE, client assertions for confidential clients, storage of session state, and at rest encryption of stored
|
|
16
|
+
# tokens. Includes SQLite, Redis and in-memory storage backends, and an adapter for using the Minisky gem with OAuth
|
|
17
|
+
# login support.
|
|
15
18
|
#
|
|
16
19
|
|
|
17
20
|
module AuthProto
|
data/sig/authproto/client.rbs
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
module AuthProto
|
|
2
2
|
class Client
|
|
3
|
+
include HTTPHelpers
|
|
3
4
|
class CallbackError < AuthProto::Error
|
|
5
|
+
attr_reader error_code: String?
|
|
6
|
+
attr_reader error_description: String?
|
|
7
|
+
attr_reader error_uri: String?
|
|
8
|
+
def initialize: (String message, ?error_code: String?, ?error_description: String?, ?error_uri: String?) -> void
|
|
4
9
|
end
|
|
5
10
|
class TokenError < AuthProto::Error
|
|
6
11
|
end
|
|
@@ -13,28 +18,48 @@ module AuthProto
|
|
|
13
18
|
attr_reader redirect_uri: String
|
|
14
19
|
attr_reader client_metadata: Metadata::ClientMetadata
|
|
15
20
|
attr_reader session_manager: State::SessionManager
|
|
16
|
-
attr_reader dpop_client: DPoP::Client
|
|
17
21
|
|
|
18
|
-
def
|
|
19
|
-
|
|
20
|
-
def
|
|
21
|
-
|
|
22
|
-
def
|
|
22
|
+
def self.localhost_app?: (String client_id) -> bool
|
|
23
|
+
|
|
24
|
+
def self.for_localhost: (redirect_uri: (String | URI::Generic), ?scope: String) -> instance
|
|
25
|
+
|
|
26
|
+
def initialize: (
|
|
27
|
+
client_id: String,
|
|
28
|
+
redirect_uri: (String | URI::Generic),
|
|
29
|
+
?metadata: json_object?,
|
|
30
|
+
?signing_key: (json_object | JOSE::JWK)?
|
|
31
|
+
) -> void
|
|
32
|
+
def http: () -> _HTTPClient?
|
|
33
|
+
def authorize: (?handle: String?, ?pds: (String | URI::HTTPS)?, ?scope: String) -> authorization_result
|
|
34
|
+
def handle_callback: (
|
|
35
|
+
Hash[String | Symbol, untyped] callback_params, expected_session_id: String
|
|
36
|
+
) -> State::SessionInfo
|
|
37
|
+
def get_session: (String session_id) -> State::SessionInfo?
|
|
38
|
+
def refresh_token: (String session_id) -> State::SessionInfo
|
|
23
39
|
def authorized?: (String session_id) -> bool
|
|
24
|
-
def auth_headers: (session_id: String, method: String, url: String) -> Hash[String, String]
|
|
25
|
-
def
|
|
26
|
-
def
|
|
40
|
+
def auth_headers: (session_id: String, method: String, url: (String | URI::HTTPS)) -> Hash[String, String]
|
|
41
|
+
def process_dpop: (http_response response, url: (String | URI::HTTPS), session_id: String) -> void
|
|
42
|
+
def has_nonce_error?: (http_response response) -> bool
|
|
43
|
+
def with_nonce_handling: (String session_id, (String | URI::HTTPS) url) { () -> http_response } -> http_response
|
|
44
|
+
def delete_session: (String session_id, ?revoke: bool) -> void
|
|
27
45
|
|
|
28
46
|
private
|
|
29
47
|
def load_client_metadata: (json_object? metadata) -> Metadata::ClientMetadata
|
|
30
48
|
def validate_redirect_uri: () -> void
|
|
49
|
+
def loopback_redirect_components: (String url) -> Array[String?]?
|
|
31
50
|
def initialize_client_assertion: ((json_object | JOSE::JWK)? key) -> String?
|
|
32
51
|
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
|
|
52
|
+
def validate_auth_params: (String? handle, (String | URI::HTTPS)? pds, String scope) -> void
|
|
34
53
|
def resolve_auth_server_info: (String pds) -> Metadata::AuthorizationServer
|
|
35
|
-
def submit_authorization_par: (
|
|
36
|
-
|
|
54
|
+
def submit_authorization_par: (
|
|
55
|
+
Metadata::AuthorizationServer authorization_server,
|
|
56
|
+
State::Session session,
|
|
57
|
+
?login_hint: String?
|
|
58
|
+
) -> String
|
|
59
|
+
def validate_callback_params: (Hash[String | Symbol, untyped] callback_params) -> Hash[String, untyped]
|
|
60
|
+
def exchange_code: (code: String, session: State::Session) -> State::TokenSet
|
|
37
61
|
def make_token_request: (String code, State::Session session) -> http_response
|
|
38
|
-
def
|
|
62
|
+
def validate_token_identity: (State::TokenSet tokens, State::Session session) -> void
|
|
63
|
+
def revoke_rejected_refresh_token: (String token, State::Session session) -> void
|
|
39
64
|
end
|
|
40
65
|
end
|
|
@@ -9,14 +9,20 @@ module AuthProto
|
|
|
9
9
|
attr_reader nonce_manager: NonceManager
|
|
10
10
|
|
|
11
11
|
def initialize: (?nonce_ttl: Integer?) -> void
|
|
12
|
-
def generate_proof: (
|
|
12
|
+
def generate_proof: (
|
|
13
|
+
session: State::Session,
|
|
14
|
+
method: String,
|
|
15
|
+
uri: (String | URI::HTTPS),
|
|
16
|
+
?access_token: String?
|
|
17
|
+
) -> JOSE::SignedBinary
|
|
13
18
|
def has_nonce_error?: (http_response response) -> bool
|
|
14
|
-
def process_response: (http_response response,
|
|
19
|
+
def process_response: (http_response response, url: (String | URI::HTTPS), session_id: String) -> void
|
|
15
20
|
|
|
16
|
-
def with_nonce_handling: (
|
|
21
|
+
def with_nonce_handling: (String session_id, (String | URI::HTTPS) url) { () -> http_response } -> http_response
|
|
17
22
|
|
|
18
23
|
private
|
|
19
24
|
|
|
25
|
+
def validate_response_fields: (http_response response) -> void
|
|
20
26
|
def nonce_challenge?: (String? challenge) -> bool
|
|
21
27
|
end
|
|
22
28
|
end
|
|
@@ -8,15 +8,15 @@ module AuthProto
|
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def initialize: (?ttl: Integer?) -> void
|
|
11
|
-
def update: (nonce: String,
|
|
12
|
-
def get: (
|
|
13
|
-
def clear: (
|
|
11
|
+
def update: (nonce: String, url: (String | URI::HTTPS), session_id: String) -> void
|
|
12
|
+
def get: (url: (String | URI::HTTPS), session_id: String) -> String?
|
|
13
|
+
def clear: (url: (String | URI::HTTPS), session_id: String) -> bool
|
|
14
14
|
|
|
15
15
|
private
|
|
16
16
|
def validate_session_id: (String session_id) -> void
|
|
17
|
-
def
|
|
18
|
-
def validate_inputs: (String nonce, String
|
|
19
|
-
def
|
|
17
|
+
def normalize_url: ((String | URI::HTTPS) url) -> String
|
|
18
|
+
def validate_inputs: (String nonce, (String | URI::HTTPS) url) -> void
|
|
19
|
+
def validate_url: ((String | URI::HTTPS) url) -> void
|
|
20
20
|
end
|
|
21
21
|
end
|
|
22
22
|
end
|
|
@@ -9,25 +9,25 @@ module AuthProto
|
|
|
9
9
|
def generate: (
|
|
10
10
|
key_set: KeySet,
|
|
11
11
|
method: String,
|
|
12
|
-
uri: String,
|
|
12
|
+
uri: (String | URI::HTTPS),
|
|
13
13
|
?nonce: String?,
|
|
14
14
|
?access_token: String?
|
|
15
15
|
) -> JOSE::SignedBinary
|
|
16
16
|
|
|
17
17
|
private
|
|
18
18
|
|
|
19
|
-
def validate_inputs: (String method, String uri) -> void
|
|
19
|
+
def validate_inputs: (String method, (String | URI::HTTPS) uri) -> void
|
|
20
20
|
|
|
21
21
|
def build_header: (KeySet key_set) -> { typ: "dpop+jwt", alg: "ES256", jwk: Hash[String, untyped] }
|
|
22
22
|
|
|
23
23
|
def build_payload: (
|
|
24
24
|
method: String,
|
|
25
|
-
uri: String,
|
|
25
|
+
uri: (String | URI::HTTPS),
|
|
26
26
|
?nonce: String?,
|
|
27
27
|
?access_token: String?
|
|
28
28
|
) -> Hash[String, untyped]
|
|
29
29
|
|
|
30
|
-
def normalize_uri: (String uri) -> String
|
|
30
|
+
def normalize_uri: ((String | URI::HTTPS) uri) -> String
|
|
31
31
|
|
|
32
32
|
def generate_access_token_hash: (String access_token) -> String
|
|
33
33
|
end
|
|
@@ -2,9 +2,9 @@ module AuthProto
|
|
|
2
2
|
module DPoP
|
|
3
3
|
class StoredNonce
|
|
4
4
|
attr_reader value: String
|
|
5
|
-
attr_reader
|
|
5
|
+
attr_reader origin: String
|
|
6
6
|
attr_reader timestamp: Integer | Time
|
|
7
|
-
def initialize: (String value, String
|
|
7
|
+
def initialize: (String value, String origin, ?timestamp: (Integer | Time)?) -> void
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
end
|
|
@@ -11,13 +11,15 @@ module AuthProto
|
|
|
11
11
|
class DecryptionError < Error
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
+
CIPHER: String
|
|
15
|
+
VERSION: Integer
|
|
16
|
+
MASTER_KEY_BYTES: Integer
|
|
17
|
+
|
|
14
18
|
module HKDF
|
|
15
19
|
def self.derive: (_ToS secret, salt: String, info: String, length: Integer) -> String
|
|
16
20
|
end
|
|
17
21
|
|
|
18
22
|
class Service
|
|
19
|
-
CIPHER: String
|
|
20
|
-
VERSION: Integer
|
|
21
23
|
def initialize: () -> void
|
|
22
24
|
def encrypt: (_ToS data, context: String) -> envelope
|
|
23
25
|
def decrypt: (envelope encrypted, context: String) -> String
|
|
@@ -21,25 +21,41 @@ module AuthProto
|
|
|
21
21
|
attr_reader headers: Hash[String, String]
|
|
22
22
|
attr_reader redirect_count: Integer
|
|
23
23
|
attr_reader body: http_body
|
|
24
|
-
def self.new: (
|
|
24
|
+
def self.new: (
|
|
25
|
+
URI::HTTP original_uri,
|
|
26
|
+
Symbol method,
|
|
27
|
+
Net::HTTPResponse response,
|
|
28
|
+
Hash[String, String] headers,
|
|
29
|
+
Integer redirect_count,
|
|
30
|
+
http_body body
|
|
31
|
+
) -> instance
|
|
25
32
|
| (original_uri: URI::HTTP, method: Symbol, response: Net::HTTPResponse, headers: Hash[String, String], redirect_count: Integer, body: http_body) -> instance
|
|
26
33
|
end
|
|
27
34
|
|
|
28
35
|
def initialize: (?timeout: Numeric, ?verify_ssl: bool) -> void
|
|
29
|
-
def get: (String url, ?Hash[String, String] headers, ?follow_redirects: bool) -> http_response
|
|
30
|
-
def post: (String url, ?body: http_body, ?headers: Hash[String, String]) -> http_response
|
|
36
|
+
def get: ((String | URI::HTTPS) url, ?Hash[String, String] headers, ?follow_redirects: bool) -> http_response
|
|
37
|
+
def post: ((String | URI::HTTPS) url, ?body: http_body, ?headers: Hash[String, String]) -> http_response
|
|
31
38
|
|
|
32
39
|
private
|
|
33
|
-
def validate_uri: (String url) -> URI::HTTP
|
|
40
|
+
def validate_uri: ((String | URI::HTTPS) url) -> URI::HTTP
|
|
34
41
|
def validate_ip: (URI::HTTP uri) -> void
|
|
35
42
|
def forbidden_ip?: (IPAddr ip) -> bool
|
|
36
|
-
def make_request: (
|
|
43
|
+
def make_request: (
|
|
44
|
+
URI::HTTP uri,
|
|
45
|
+
?Hash[String, String] headers,
|
|
46
|
+
?Integer redirect_count,
|
|
47
|
+
?follow_redirects: bool
|
|
48
|
+
) -> Net::HTTPResponse
|
|
37
49
|
def make_post_request: (URI::HTTP uri, http_body body, ?Hash[String, String] headers) -> Net::HTTPResponse
|
|
38
50
|
def configure_http_client: (Net::HTTP http, ?ssl: bool) -> void
|
|
39
51
|
def add_security_headers: (Net::HTTPRequest request, Hash[String, String] headers) -> void
|
|
40
|
-
def handle_redirect: (
|
|
52
|
+
def handle_redirect: (
|
|
53
|
+
original_uri: URI::HTTP,
|
|
54
|
+
response: Net::HTTPResponse,
|
|
55
|
+
headers: Hash[String, String],
|
|
56
|
+
redirect_count: Integer
|
|
57
|
+
) -> Net::HTTPResponse
|
|
41
58
|
def validate_response: (Net::HTTPResponse response) -> void
|
|
42
|
-
def check_success_status: (Net::HTTPResponse response) -> void
|
|
43
59
|
def check_content_length: (Net::HTTPResponse response) -> void
|
|
44
60
|
end
|
|
45
61
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
module AuthProto
|
|
2
2
|
module HTTPHelpers
|
|
3
3
|
private
|
|
4
|
+
def response_status: (http_response response) -> (Integer | String)
|
|
4
5
|
def authentication_challenge: (String? header, scheme: String) -> Hash[String, String]?
|
|
5
6
|
def response_header: (http_response response, String name, ?strip_parameters: bool) -> String?
|
|
6
7
|
end
|
|
@@ -31,10 +31,10 @@ module AuthProto
|
|
|
31
31
|
def initialize: (Hash[String, untyped] metadata) -> void
|
|
32
32
|
|
|
33
33
|
# Fetches and validates Authorization Server metadata from an issuer URL
|
|
34
|
-
# @param url [String] Authorization Server issuer URL
|
|
34
|
+
# @param url [String, URI::HTTPS] Authorization Server issuer URL
|
|
35
35
|
# @return [AuthorizationServer] new instance with fetched metadata
|
|
36
36
|
# @raise [InvalidAuthorizationServer] if metadata is invalid
|
|
37
|
-
def self.from_url: (String url) -> AuthorizationServer
|
|
37
|
+
def self.from_url: ((String | URI::HTTPS) url) -> AuthorizationServer
|
|
38
38
|
|
|
39
39
|
def to_h: () -> Hash[Symbol, untyped]
|
|
40
40
|
|
|
@@ -66,7 +66,7 @@ module AuthProto
|
|
|
66
66
|
|
|
67
67
|
private def self.fetch_metadata: (String issuer) -> http_response
|
|
68
68
|
|
|
69
|
-
private def self.validate_source_url: (String issuer) -> void
|
|
69
|
+
private def self.validate_source_url: ((String | URI::HTTPS) issuer) -> void
|
|
70
70
|
|
|
71
71
|
private def self.validate_response: (http_response response) -> void
|
|
72
72
|
|
|
@@ -54,7 +54,7 @@ module AuthProto
|
|
|
54
54
|
|
|
55
55
|
def initialize: (Hash[String, untyped] metadata) -> void
|
|
56
56
|
|
|
57
|
-
def self.from_url: (String url) -> ClientMetadata
|
|
57
|
+
def self.from_url: ((String | URI::HTTPS) url) -> ClientMetadata
|
|
58
58
|
|
|
59
59
|
private def self.from_localhost_url: (String url) -> ClientMetadata
|
|
60
60
|
|
|
@@ -67,6 +67,7 @@ module AuthProto
|
|
|
67
67
|
def validate_client_id: (String client_id) -> String
|
|
68
68
|
|
|
69
69
|
def valid_https_client_id?: (String client_id) -> bool
|
|
70
|
+
def valid_client_id_hostname?: (String hostname) -> bool
|
|
70
71
|
|
|
71
72
|
def valid_localhost_client_id?: (String client_id) -> bool
|
|
72
73
|
|
|
@@ -8,7 +8,7 @@ module AuthProto
|
|
|
8
8
|
|
|
9
9
|
def initialize: (Hash[String, untyped] metadata) -> void
|
|
10
10
|
|
|
11
|
-
def self.from_url: (String url) -> ResourceServer
|
|
11
|
+
def self.from_url: ((String | URI::HTTPS) url) -> ResourceServer
|
|
12
12
|
|
|
13
13
|
private
|
|
14
14
|
|
|
@@ -16,7 +16,7 @@ module AuthProto
|
|
|
16
16
|
|
|
17
17
|
private def self.fetch_metadata: (String url) -> http_response
|
|
18
18
|
|
|
19
|
-
private def self.validate_source_url: (String url) -> void
|
|
19
|
+
private def self.validate_source_url: ((String | URI::HTTPS) url) -> void
|
|
20
20
|
|
|
21
21
|
private def self.parse_metadata: (String? body) -> Hash[String, untyped]
|
|
22
22
|
|