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
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'ipaddr'
|
|
3
4
|
require 'jose'
|
|
4
5
|
require 'json'
|
|
5
6
|
require 'uri'
|
|
@@ -144,7 +145,7 @@ module AuthProto
|
|
|
144
145
|
# The document's `client_id` must equal the requested URL.
|
|
145
146
|
# Localhost HTTP client IDs generate virtual metadata without a network request.
|
|
146
147
|
#
|
|
147
|
-
# @param url [String] HTTPS metadata URL, or localhost URL for development
|
|
148
|
+
# @param url [String, URI::HTTPS] HTTPS metadata URL, or localhost URL for development
|
|
148
149
|
# @return [ClientMetadata] validated metadata
|
|
149
150
|
# @raise [InvalidClientMetadata] if metadata is missing, invalid/mismatched, not JSON,
|
|
150
151
|
# or transport isn't configured
|
|
@@ -153,6 +154,7 @@ module AuthProto
|
|
|
153
154
|
#
|
|
154
155
|
def self.from_url(url)
|
|
155
156
|
uri = ValidatedURI.new(url)
|
|
157
|
+
url = url.to_s
|
|
156
158
|
|
|
157
159
|
unless uri.scheme == 'https' || (uri.scheme == 'http' && uri.host == 'localhost')
|
|
158
160
|
raise InvalidClientMetadata, "client_id must use HTTPS except for localhost"
|
|
@@ -164,7 +166,7 @@ module AuthProto
|
|
|
164
166
|
|
|
165
167
|
response = AuthProto.configuration.http_client.get(url)
|
|
166
168
|
|
|
167
|
-
unless response
|
|
169
|
+
unless response_status(response) == 200
|
|
168
170
|
raise InvalidClientMetadata,
|
|
169
171
|
"expected HTTP 200 response for client metadata request, got #{response[:status]}"
|
|
170
172
|
end
|
|
@@ -272,11 +274,23 @@ module AuthProto
|
|
|
272
274
|
|
|
273
275
|
def valid_https_client_id?(client_id)
|
|
274
276
|
uri = ValidatedURI.new(client_id)
|
|
275
|
-
uri.scheme == 'https' && !uri.port && !uri.userinfo && !uri.fragment
|
|
277
|
+
uri.scheme == 'https' && !uri.port && !uri.userinfo && !uri.fragment &&
|
|
278
|
+
valid_client_id_hostname?(uri.hostname) && !uri.path.empty? && !uri.path.end_with?('/')
|
|
276
279
|
rescue URI::InvalidURIError
|
|
277
280
|
false
|
|
278
281
|
end
|
|
279
282
|
|
|
283
|
+
def valid_client_id_hostname?(hostname)
|
|
284
|
+
labels = hostname.downcase.split('.')
|
|
285
|
+
return false if labels.length < 2 || %w[local localhost test invalid example].include?(labels.last)
|
|
286
|
+
return false if hostname.match?(/\A[0-9.]+\z/)
|
|
287
|
+
|
|
288
|
+
IPAddr.new(hostname)
|
|
289
|
+
false
|
|
290
|
+
rescue IPAddr::InvalidAddressError
|
|
291
|
+
true
|
|
292
|
+
end
|
|
293
|
+
|
|
280
294
|
def valid_localhost_client_id?(client_id)
|
|
281
295
|
uri = ValidatedURI.new(client_id)
|
|
282
296
|
uri.scheme == 'http' && uri.host == 'localhost' && !uri.port && !uri.userinfo &&
|
|
@@ -289,6 +303,11 @@ module AuthProto
|
|
|
289
303
|
raise InvalidClientMetadata, "grant_types is required" unless grant_types
|
|
290
304
|
raise InvalidClientMetadata, "grant_types must be an array" unless grant_types.is_a?(Array)
|
|
291
305
|
|
|
306
|
+
unless grant_types.uniq.length == grant_types.length
|
|
307
|
+
# follows the reference implementation
|
|
308
|
+
raise InvalidClientMetadata, "grant_types must not contain duplicates"
|
|
309
|
+
end
|
|
310
|
+
|
|
292
311
|
unless grant_types.include?('authorization_code')
|
|
293
312
|
raise InvalidClientMetadata, "grant_types must include authorization_code"
|
|
294
313
|
end
|
|
@@ -346,6 +365,11 @@ module AuthProto
|
|
|
346
365
|
raise InvalidClientMetadata, "redirect URI must not contain credentials" if uri.userinfo
|
|
347
366
|
raise InvalidClientMetadata, "redirect URI must not contain a fragment" if uri.fragment
|
|
348
367
|
|
|
368
|
+
if ['localhost', '127.0.0.1', '[::1]'].include?(uri.host.downcase)
|
|
369
|
+
# follows the reference implementation
|
|
370
|
+
raise InvalidClientMetadata, "HTTPS redirect URI must not use a loopback host"
|
|
371
|
+
end
|
|
372
|
+
|
|
349
373
|
if uri.specified_port && uri.effective_port == 443
|
|
350
374
|
raise InvalidClientMetadata, "HTTPS redirect URI must not specify the default port"
|
|
351
375
|
end
|
|
@@ -413,6 +437,11 @@ module AuthProto
|
|
|
413
437
|
scope_values = scope.split(' ')
|
|
414
438
|
raise InvalidClientMetadata, "atproto scope is required" unless scope_values.include?('atproto')
|
|
415
439
|
|
|
440
|
+
unless scope_values.uniq.length == scope_values.length
|
|
441
|
+
# follows the reference implementation
|
|
442
|
+
raise InvalidClientMetadata, "scope must not contain duplicate tokens"
|
|
443
|
+
end
|
|
444
|
+
|
|
416
445
|
scope
|
|
417
446
|
end
|
|
418
447
|
|
|
@@ -543,6 +572,8 @@ module AuthProto
|
|
|
543
572
|
@token_endpoint_auth_method = metadata['token_endpoint_auth_method']
|
|
544
573
|
@token_endpoint_auth_signing_alg = metadata['token_endpoint_auth_signing_alg']
|
|
545
574
|
|
|
575
|
+
# Not technically required, but if the field is not set, the reference PDS sets it
|
|
576
|
+
# to (unsupported) `client_secret_basic`.
|
|
546
577
|
raise InvalidClientMetadata, "token_endpoint_auth_method is required" if @token_endpoint_auth_method.nil?
|
|
547
578
|
|
|
548
579
|
@jwks = metadata['jwks']
|
|
@@ -41,10 +41,10 @@ module AuthProto
|
|
|
41
41
|
# Fetches `/.well-known/oauth-protected-resource` using the configured transport.
|
|
42
42
|
# Requires HTTP 200 without redirects, a JSON object served as application/json,
|
|
43
43
|
# and a resource identifier that exactly matches the requested URL.
|
|
44
|
-
#
|
|
44
|
+
# Requires HTTPS even when the configured transport permits HTTP.
|
|
45
45
|
# The PDS URL must have no userinfo, path prefix, query or fragment.
|
|
46
46
|
#
|
|
47
|
-
# @param url [String] resource server/PDS base URL
|
|
47
|
+
# @param url [String, URI::HTTPS] resource server/PDS base URL
|
|
48
48
|
# @return [ResourceServer] validated resource server metadata
|
|
49
49
|
# @raise [InvalidAuthorizationServer] if HTTP transport, JSON parsing or metadata validation fails
|
|
50
50
|
# @raise [HTTPClient::SSRFError] if the transport rejects the initial destination
|
|
@@ -52,6 +52,7 @@ module AuthProto
|
|
|
52
52
|
#
|
|
53
53
|
def self.from_url(url)
|
|
54
54
|
validate_source_url(url)
|
|
55
|
+
url = url.to_s
|
|
55
56
|
|
|
56
57
|
response = fetch_metadata(url)
|
|
57
58
|
validate_response(response)
|
|
@@ -88,9 +89,9 @@ module AuthProto
|
|
|
88
89
|
def validate_source_url(url)
|
|
89
90
|
uri = ValidatedURI.new(url)
|
|
90
91
|
|
|
91
|
-
unless uri.path.empty? && !uri.userinfo && !uri.query && !uri.fragment
|
|
92
|
+
unless uri.scheme == 'https' && uri.path.empty? && !uri.userinfo && !uri.query && !uri.fragment
|
|
92
93
|
raise InvalidAuthorizationServer,
|
|
93
|
-
"Resource server URL must be an
|
|
94
|
+
"Resource server URL must be an HTTPS origin without userinfo, a path prefix, query or fragment"
|
|
94
95
|
end
|
|
95
96
|
rescue URI::InvalidURIError => e
|
|
96
97
|
raise InvalidAuthorizationServer, "Invalid resource server URL: #{e.message}"
|
|
@@ -99,13 +100,13 @@ module AuthProto
|
|
|
99
100
|
def fetch_metadata(url)
|
|
100
101
|
metadata_url = URI.join(url, "/.well-known/oauth-protected-resource")
|
|
101
102
|
|
|
102
|
-
AuthProto.configuration.http_client.get(metadata_url
|
|
103
|
+
AuthProto.configuration.http_client.get(metadata_url)
|
|
103
104
|
rescue HTTPClient::HTTPError => e
|
|
104
105
|
raise InvalidAuthorizationServer, "Failed to fetch resource server metadata: #{e.message}"
|
|
105
106
|
end
|
|
106
107
|
|
|
107
108
|
def validate_response(response)
|
|
108
|
-
unless response
|
|
109
|
+
unless response_status(response) == 200
|
|
109
110
|
raise InvalidAuthorizationServer, "Resource server metadata must return HTTP 200"
|
|
110
111
|
end
|
|
111
112
|
|
data/lib/authproto/minisky.rb
CHANGED
|
@@ -13,7 +13,7 @@ require_relative 'http_helpers'
|
|
|
13
13
|
# Requiring authproto alone does not load Minisky or this integration.
|
|
14
14
|
#
|
|
15
15
|
|
|
16
|
-
class Minisky::
|
|
16
|
+
class Minisky::OAuth
|
|
17
17
|
include AuthProto::HTTPHelpers
|
|
18
18
|
include Minisky::Requests
|
|
19
19
|
|
|
@@ -21,10 +21,6 @@ class Minisky::OAuthClient
|
|
|
21
21
|
# @return [Integer]
|
|
22
22
|
MAX_NONCE_RETRIES = 2
|
|
23
23
|
|
|
24
|
-
# PDS host or base URL used by Minisky.
|
|
25
|
-
# @return [String]
|
|
26
|
-
attr_reader :host
|
|
27
|
-
|
|
28
24
|
# OAuth client that owns the session, tokens and DPoP key.
|
|
29
25
|
# @return [AuthProto::Client]
|
|
30
26
|
attr_reader :oauth_client
|
|
@@ -33,36 +29,123 @@ class Minisky::OAuthClient
|
|
|
33
29
|
# @return [String, nil]
|
|
34
30
|
attr_reader :session_id
|
|
35
31
|
|
|
32
|
+
|
|
33
|
+
# Creates a Minisky instance configured for a localhost app.
|
|
34
|
+
#
|
|
35
|
+
# @param redirect_uri [String, URI::Generic] loopback callback URL used to complete authorization
|
|
36
|
+
# @param scope [String] space-separated scopes to declare in the generated client metadata
|
|
37
|
+
# @param session_id [String, nil] optionally, stored localhost session ID to resume
|
|
38
|
+
#
|
|
39
|
+
# @return [Minisky::OAuth] configured localhost client
|
|
40
|
+
# @raise [AuthProto::InvalidClientMetadata] if the redirect URI or scope is invalid
|
|
41
|
+
# @raise [Minisky::AuthError] if a supplied session ID cannot be restored
|
|
42
|
+
#
|
|
43
|
+
# @see AuthProto::Client.for_localhost
|
|
44
|
+
#
|
|
45
|
+
def self.for_localhost(redirect_uri:, scope: 'atproto', session_id: nil)
|
|
46
|
+
query = URI.encode_www_form(redirect_uri: redirect_uri, scope: scope)
|
|
47
|
+
|
|
48
|
+
self.new(client_id: "http://localhost/?#{query}", redirect_uri: redirect_uri, session_id: session_id)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# Creates a Minisky instance using an existing {AuthProto::Client}.
|
|
53
|
+
#
|
|
54
|
+
# Reuses the supplied client, including its metadata, signing key and session manager.
|
|
55
|
+
#
|
|
56
|
+
# @param oauth_client [AuthProto::Client] OAuth client to reuse
|
|
57
|
+
# @param session_id [String, nil] optionally, stored session ID to resume
|
|
58
|
+
#
|
|
59
|
+
# @return [Minisky::OAuth] Minisky client backed by the supplied OAuth client
|
|
60
|
+
# @raise [ArgumentError] if the supplied client is not an AuthProto client
|
|
61
|
+
# @raise [Minisky::AuthError] if a supplied session ID cannot be restored
|
|
62
|
+
#
|
|
63
|
+
def self.with_oauth_client(oauth_client, session_id: nil)
|
|
64
|
+
unless oauth_client.is_a?(AuthProto::Client)
|
|
65
|
+
raise ArgumentError, "oauth_client must be an AuthProto::Client"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
self.new(
|
|
69
|
+
client_id: oauth_client.client_id,
|
|
70
|
+
redirect_uri: oauth_client.redirect_uri,
|
|
71
|
+
oauth_client: oauth_client,
|
|
72
|
+
session_id: session_id
|
|
73
|
+
)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
|
|
36
77
|
# Creates an experimental Minisky client backed by an OAuth session.
|
|
37
78
|
#
|
|
38
79
|
# Disables Minisky's app-password token management and enables authenticated
|
|
39
80
|
# requests by default. Configure AuthProto's global storage/transport separately.
|
|
40
81
|
#
|
|
41
|
-
# @param host [String] PDS host or base URL used for XRPC requests
|
|
42
82
|
# @param client_id [String] OAuth client metadata URL or localhost client ID
|
|
43
|
-
# @param redirect_uri [String] registered callback URI
|
|
83
|
+
# @param redirect_uri [String, URI::Generic] registered callback URI
|
|
44
84
|
# @param metadata [Hash{String => Object}, nil] preloaded metadata, or `nil` to fetch it
|
|
45
85
|
# @param signing_key [Hash, JOSE::JWK, nil] private ES256 key for confidential-client authentication
|
|
46
86
|
# @param session_id [String, nil] stored session ID to resume; `nil` starts without an attached session
|
|
87
|
+
# @param oauth_client [AuthProto::Client, nil] existing OAuth client to reuse instead of creating one
|
|
47
88
|
#
|
|
89
|
+
# @raise [ArgumentError] if oauth_client is combined with metadata or signing_key
|
|
90
|
+
# @raise [Minisky::AuthError] if a supplied session ID cannot be restored
|
|
48
91
|
# @raise [AuthProto::InvalidClientMetadata] if metadata is invalid or unavailable
|
|
49
92
|
# @raise [AuthProto::Error] if OAuth client or DPoP initialization fails
|
|
50
93
|
# @raise [AuthProto::HTTPClient::HTTPError, AuthProto::HTTPClient::SSRFError] if fetching metadata fails
|
|
51
94
|
# @see AuthProto::Client#initialize
|
|
52
95
|
#
|
|
53
|
-
def initialize(
|
|
54
|
-
|
|
96
|
+
def initialize(client_id:, redirect_uri:, metadata: nil, signing_key: nil, session_id: nil, oauth_client: nil)
|
|
97
|
+
if oauth_client
|
|
98
|
+
raise ArgumentError, "Do not pass metadata when existing oauth_client is included" if !metadata.nil?
|
|
99
|
+
raise ArgumentError, "Do not pass signing_key when existing oauth_client is included" if !signing_key.nil?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
@host = nil
|
|
103
|
+
@did = nil
|
|
55
104
|
@session_id = session_id
|
|
56
105
|
|
|
57
106
|
@send_auth_headers = true
|
|
58
107
|
@auto_manage_tokens = false
|
|
59
108
|
|
|
60
|
-
@oauth_client = AuthProto::Client.new(
|
|
109
|
+
@oauth_client = oauth_client || AuthProto::Client.new(
|
|
61
110
|
client_id: client_id,
|
|
62
111
|
redirect_uri: redirect_uri,
|
|
63
112
|
metadata: metadata,
|
|
64
113
|
signing_key: signing_key
|
|
65
114
|
)
|
|
115
|
+
|
|
116
|
+
if session_id
|
|
117
|
+
session = @oauth_client.session_manager.get_session(session_id)
|
|
118
|
+
raise Minisky::AuthError, "OAuth session not found" unless session
|
|
119
|
+
|
|
120
|
+
@host = session.pds
|
|
121
|
+
@did = session.did
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Returns the PDS URL of the authenticated user.
|
|
126
|
+
#
|
|
127
|
+
# @return [String] session's PDS URL
|
|
128
|
+
# @raise [Minisky::AuthError] if the PDS is not known yet
|
|
129
|
+
#
|
|
130
|
+
def host
|
|
131
|
+
if @host.nil?
|
|
132
|
+
raise Minisky::AuthError, "Client has not completed authentication yet"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
@host
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Returns the DID of the authenticated user.
|
|
139
|
+
#
|
|
140
|
+
# @return [String] session's account DID
|
|
141
|
+
# @raise [Minisky::AuthError] if the DID is not known yet
|
|
142
|
+
#
|
|
143
|
+
def did
|
|
144
|
+
if @did.nil?
|
|
145
|
+
raise Minisky::AuthError, "Client has not completed authentication yet"
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
@did
|
|
66
149
|
end
|
|
67
150
|
|
|
68
151
|
# Returns no app-password credential configuration; OAuth owns this client's tokens.
|
|
@@ -83,27 +166,31 @@ class Minisky::OAuthClient
|
|
|
83
166
|
# @see AuthProto::Client#authorize
|
|
84
167
|
#
|
|
85
168
|
def authorize(**options)
|
|
86
|
-
oauth_client.authorize(**options)
|
|
169
|
+
result = oauth_client.authorize(**options)
|
|
170
|
+
@session_id = result.fetch(:session_id)
|
|
171
|
+
|
|
172
|
+
result
|
|
87
173
|
end
|
|
88
174
|
|
|
89
175
|
# Completes OAuth authorization and remembers the authenticated session ID.
|
|
90
176
|
#
|
|
91
177
|
# @param expected_session_id [String] pending session ID retained in the initiating browser session or native context
|
|
92
|
-
# @param
|
|
93
|
-
#
|
|
94
|
-
# @option options [String] :state callback state token
|
|
95
|
-
# @option options [String] :iss authorization server issuer
|
|
178
|
+
# @param callback_params [Hash{String, Symbol => Object}] callback query parameters from the URL the user gets
|
|
179
|
+
# redirected to after authorization (should contain `code`, `state` and `iss`, or `error`)
|
|
96
180
|
#
|
|
97
|
-
# @return [
|
|
181
|
+
# @return [AuthProto::State::SessionInfo] current tokens and session details
|
|
98
182
|
# @raise [AuthProto::Client::CallbackError] if expected session, state or issuer validation fails
|
|
99
183
|
# @raise [AuthProto::Client::TokenError] if token exchange or validation fails
|
|
100
184
|
# @raise [AuthProto::Error] if proofs, storage or serialization fail
|
|
101
185
|
# @see AuthProto::Client#handle_callback
|
|
102
186
|
#
|
|
103
|
-
def handle_callback(expected_session_id
|
|
104
|
-
oauth_client.handle_callback(
|
|
105
|
-
|
|
106
|
-
|
|
187
|
+
def handle_callback(callback_params, expected_session_id:)
|
|
188
|
+
result = oauth_client.handle_callback(callback_params, expected_session_id: expected_session_id)
|
|
189
|
+
@session_id = result.session_id
|
|
190
|
+
@host = result.pds
|
|
191
|
+
@did = result.did
|
|
192
|
+
|
|
193
|
+
result
|
|
107
194
|
end
|
|
108
195
|
|
|
109
196
|
# Sends an XRPC GET through Minisky, adding OAuth handling when authenticated.
|
|
@@ -131,10 +218,10 @@ class Minisky::OAuthClient
|
|
|
131
218
|
|
|
132
219
|
raise ArgumentError, "OAuth requests accept auth: true or false, not a bearer token" unless auth == true
|
|
133
220
|
|
|
134
|
-
|
|
135
|
-
|
|
221
|
+
uri = build_request_uri(method).dup
|
|
222
|
+
uri.query = URI.encode_www_form(params) if params && !params.empty?
|
|
136
223
|
|
|
137
|
-
response = oauth_request(Net::HTTP::Get,
|
|
224
|
+
response = oauth_request(Net::HTTP::Get, uri, headers || {})
|
|
138
225
|
handle_response(response)
|
|
139
226
|
end
|
|
140
227
|
|
|
@@ -166,8 +253,8 @@ class Minisky::OAuthClient
|
|
|
166
253
|
|
|
167
254
|
raise ArgumentError, "OAuth requests accept auth: true or false, not a bearer token" unless auth == true
|
|
168
255
|
|
|
169
|
-
|
|
170
|
-
|
|
256
|
+
uri = build_request_uri(method).dup
|
|
257
|
+
uri.query = URI.encode_www_form(params) if params && !params.empty?
|
|
171
258
|
headers = (headers || {}).dup
|
|
172
259
|
|
|
173
260
|
body = if data.is_a?(String) || data.nil?
|
|
@@ -179,7 +266,7 @@ class Minisky::OAuthClient
|
|
|
179
266
|
data.to_json
|
|
180
267
|
end
|
|
181
268
|
|
|
182
|
-
response = oauth_request(Net::HTTP::Post,
|
|
269
|
+
response = oauth_request(Net::HTTP::Post, uri, headers, body: body)
|
|
183
270
|
handle_response(response)
|
|
184
271
|
end
|
|
185
272
|
|
|
@@ -196,7 +283,7 @@ class Minisky::OAuthClient
|
|
|
196
283
|
|
|
197
284
|
# Refreshes and persists tokens for the current session via the OAuth client.
|
|
198
285
|
#
|
|
199
|
-
# @return [
|
|
286
|
+
# @return [AuthProto::State::SessionInfo] current tokens and session details
|
|
200
287
|
# @raise [AuthProto::Client::TokenError] if the session cannot be refreshed
|
|
201
288
|
# @raise [AuthProto::Token::RefreshError] if refresh fails
|
|
202
289
|
# @raise [AuthProto::Storage::StorageError] if token persistence fails
|
|
@@ -230,7 +317,7 @@ class Minisky::OAuthClient
|
|
|
230
317
|
|
|
231
318
|
private
|
|
232
319
|
|
|
233
|
-
def oauth_request(request_class,
|
|
320
|
+
def oauth_request(request_class, uri, headers, body: nil)
|
|
234
321
|
check_access
|
|
235
322
|
|
|
236
323
|
nonce_retries = 0
|
|
@@ -240,18 +327,18 @@ class Minisky::OAuthClient
|
|
|
240
327
|
# Fresh proof for every attempt; callers cannot override OAuth auth headers.
|
|
241
328
|
request_headers = headers.reject { |k, v| %w[authorization dpop].include?(k.to_s.downcase) }
|
|
242
329
|
request_headers.merge!(oauth_client.auth_headers(
|
|
243
|
-
session_id: session_id, method: request_class::METHOD, url:
|
|
330
|
+
session_id: session_id, method: request_class::METHOD, url: uri
|
|
244
331
|
))
|
|
245
332
|
|
|
246
|
-
request = request_class.new(
|
|
333
|
+
request = request_class.new(uri, request_headers)
|
|
247
334
|
request.body = body unless body.nil?
|
|
248
335
|
|
|
249
336
|
response = make_request(request)
|
|
250
|
-
response_hash = { status: response.code
|
|
337
|
+
response_hash = { status: response.code, headers: response.each_header.to_h, body: response.body }
|
|
251
338
|
|
|
252
|
-
oauth_client.
|
|
339
|
+
oauth_client.process_dpop(response_hash, url: uri, session_id: session_id)
|
|
253
340
|
|
|
254
|
-
if oauth_client.
|
|
341
|
+
if oauth_client.has_nonce_error?(response_hash)
|
|
255
342
|
return response if nonce_retries >= MAX_NONCE_RETRIES
|
|
256
343
|
nonce_retries += 1
|
|
257
344
|
else
|
data/lib/authproto/par/client.rb
CHANGED
|
@@ -11,6 +11,8 @@ require_relative 'response'
|
|
|
11
11
|
module AuthProto
|
|
12
12
|
module PAR
|
|
13
13
|
|
|
14
|
+
#
|
|
15
|
+
# @api private
|
|
14
16
|
#
|
|
15
17
|
# Client for making Pushed Authorization Requests (PAR) according to RFC 9126.
|
|
16
18
|
# Handles submitting authorization parameters to the PAR endpoint and building
|
|
@@ -94,11 +96,11 @@ module AuthProto
|
|
|
94
96
|
def submit(request, session:)
|
|
95
97
|
raise Error, "Expected a PAR::Request" unless request.is_a?(PAR::Request)
|
|
96
98
|
|
|
97
|
-
response = dpop_client.with_nonce_handling(session, @par_endpoint) do
|
|
99
|
+
response = dpop_client.with_nonce_handling(session.session_id, @par_endpoint) do
|
|
98
100
|
make_request(request, session: session)
|
|
99
101
|
end
|
|
100
102
|
|
|
101
|
-
if response
|
|
103
|
+
if response_status(response) == 201
|
|
102
104
|
handle_success_response(response)
|
|
103
105
|
else
|
|
104
106
|
handle_error_response(response)
|
|
@@ -6,10 +6,13 @@ require 'securerandom'
|
|
|
6
6
|
|
|
7
7
|
require_relative '../configuration'
|
|
8
8
|
require_relative '../errors'
|
|
9
|
+
require_relative '../par'
|
|
9
10
|
|
|
10
11
|
module AuthProto
|
|
11
12
|
module PAR
|
|
12
13
|
|
|
14
|
+
#
|
|
15
|
+
# @api private
|
|
13
16
|
#
|
|
14
17
|
# Generates client authentication JWTs according to RFC 7523 for AT Protocol OAuth.
|
|
15
18
|
# Creates signed assertions using ES256 with required claims including iss/sub (client_id),
|
|
@@ -19,7 +22,7 @@ module AuthProto
|
|
|
19
22
|
class ClientAssertion
|
|
20
23
|
|
|
21
24
|
# Raised when client assertion key import or JWT signing fails.
|
|
22
|
-
class Error <
|
|
25
|
+
class Error < PAR::Error; end
|
|
23
26
|
|
|
24
27
|
# JWS signing algorithm used for client assertions.
|
|
25
28
|
# @return [String]
|
data/lib/authproto/par.rb
CHANGED
data/lib/authproto/pkce.rb
CHANGED
|
@@ -35,10 +35,11 @@ module AuthProto
|
|
|
35
35
|
session_id: obj.session_id,
|
|
36
36
|
client_id: obj.client_id,
|
|
37
37
|
dpop_key: obj.dpop_key.private_jwk,
|
|
38
|
-
|
|
38
|
+
requested_scope: obj.requested_scope,
|
|
39
39
|
pkce_verifier: obj.pkce_verifier,
|
|
40
40
|
pkce_challenge: obj.pkce_challenge,
|
|
41
41
|
did: obj.did,
|
|
42
|
+
pds: obj.pds,
|
|
42
43
|
tokens: serialize_token_set(obj.tokens),
|
|
43
44
|
auth_server: serialize_auth_server(obj.auth_server)
|
|
44
45
|
}
|
|
@@ -58,8 +59,9 @@ module AuthProto
|
|
|
58
59
|
session = AuthProto::State::Session.new(
|
|
59
60
|
client_id: data['client_id'],
|
|
60
61
|
dpop_key: AuthProto::DPoP::KeySet.from_jwk(data.fetch('dpop_key')),
|
|
61
|
-
scope: data['
|
|
62
|
+
scope: data['requested_scope'],
|
|
62
63
|
did: data['did'],
|
|
64
|
+
pds: data['pds'],
|
|
63
65
|
signing_key_id: data['signing_key_id'],
|
|
64
66
|
signing_key_thumbprint: data['signing_key_thumbprint'],
|
|
65
67
|
authorization_expires_at: data['authorization_expires_at'] && Time.iso8601(data['authorization_expires_at']),
|
|
@@ -29,7 +29,7 @@ module AuthProto
|
|
|
29
29
|
def serialize_data(obj)
|
|
30
30
|
{
|
|
31
31
|
value: obj.value,
|
|
32
|
-
|
|
32
|
+
origin: obj.origin,
|
|
33
33
|
timestamp: obj.timestamp
|
|
34
34
|
}
|
|
35
35
|
end
|
|
@@ -37,7 +37,7 @@ module AuthProto
|
|
|
37
37
|
def deserialize_data(data)
|
|
38
38
|
AuthProto::DPoP::StoredNonce.new(
|
|
39
39
|
data["value"],
|
|
40
|
-
data["
|
|
40
|
+
data["origin"],
|
|
41
41
|
timestamp: Time.at(data["timestamp"])
|
|
42
42
|
)
|
|
43
43
|
end
|
|
@@ -20,6 +20,7 @@ module AuthProto
|
|
|
20
20
|
"TokenSet"
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
|
|
23
24
|
private
|
|
24
25
|
|
|
25
26
|
def validate_object(obj)
|
|
@@ -29,13 +30,15 @@ module AuthProto
|
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
def serialize_data(obj)
|
|
32
|
-
{
|
|
33
|
+
data = {
|
|
33
34
|
access_token: obj.access_token,
|
|
34
|
-
refresh_token: obj.refresh_token,
|
|
35
35
|
expires_at: obj.expires_at.utc.iso8601,
|
|
36
36
|
scope: obj.scope,
|
|
37
37
|
sub: obj.sub
|
|
38
38
|
}
|
|
39
|
+
|
|
40
|
+
data[:refresh_token] = obj.refresh_token if obj.refresh_token
|
|
41
|
+
data
|
|
39
42
|
end
|
|
40
43
|
|
|
41
44
|
def deserialize_data(data)
|