authproto 0.3.0.beta4 → 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 +21 -0
- 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 +413 -126
- 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 +23 -20
- data/lib/authproto/dpop/proof_generator.rb +6 -3
- data/lib/authproto/dpop/stored_nonce.rb +7 -5
- data/lib/authproto/dpop.rb +2 -0
- data/lib/authproto/encryption.rb +2 -0
- data/lib/authproto/http_client.rb +5 -5
- data/lib/authproto/http_helpers.rb +6 -0
- data/lib/authproto/metadata/authorization_server.rb +4 -3
- data/lib/authproto/metadata/client_metadata.rb +6 -2
- data/lib/authproto/metadata/resource_server.rb +4 -3
- 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 +2 -2
- data/lib/authproto/serialization/stored_nonce.rb +2 -2
- data/lib/authproto/state/session.rb +8 -41
- data/lib/authproto/state/session_info.rb +150 -0
- data/lib/authproto/state/session_manager.rb +18 -15
- data/lib/authproto/state.rb +3 -1
- 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/refresh.rb +2 -2
- data/lib/authproto/token/response.rb +2 -0
- data/lib/authproto/token/revocation.rb +4 -2
- data/lib/authproto/version.rb +1 -1
- data/lib/authproto.rb +6 -3
- data/sig/authproto/client.rbs +33 -10
- 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/http_client.rbs +23 -6
- data/sig/authproto/http_helpers.rbs +1 -0
- data/sig/authproto/metadata/authorization_server.rbs +3 -3
- data/sig/authproto/metadata/client_metadata.rbs +1 -1
- data/sig/authproto/metadata/resource_server.rbs +2 -2
- data/sig/authproto/minisky.rbs +57 -0
- data/sig/authproto/state/session.rbs +12 -6
- data/sig/authproto/state/session_info.rbs +23 -0
- data/sig/authproto/state/session_manager.rbs +7 -2
- 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/revocation.rbs +1 -0
- data/sig/authproto.rbs +3 -8
- metadata +37 -15
- data/examples/confidential_client/Gemfile +0 -14
- data/examples/confidential_client/README.md +0 -236
- data/examples/confidential_client/app.rb +0 -308
- 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
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,7 +35,7 @@ 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,
|
|
@@ -59,7 +59,7 @@ module AuthProto
|
|
|
59
59
|
session = AuthProto::State::Session.new(
|
|
60
60
|
client_id: data['client_id'],
|
|
61
61
|
dpop_key: AuthProto::DPoP::KeySet.from_jwk(data.fetch('dpop_key')),
|
|
62
|
-
scope: data['
|
|
62
|
+
scope: data['requested_scope'],
|
|
63
63
|
did: data['did'],
|
|
64
64
|
pds: data['pds'],
|
|
65
65
|
signing_key_id: data['signing_key_id'],
|
|
@@ -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
|
|
@@ -6,10 +6,13 @@ require 'securerandom'
|
|
|
6
6
|
require_relative '../dpop/key_set'
|
|
7
7
|
require_relative '../pkce'
|
|
8
8
|
require_relative '../state'
|
|
9
|
+
require_relative 'session_info'
|
|
9
10
|
|
|
10
11
|
module AuthProto
|
|
11
12
|
module State
|
|
12
13
|
|
|
14
|
+
#
|
|
15
|
+
# @api private
|
|
13
16
|
#
|
|
14
17
|
# Tracks state for an OAuth authorization flow session
|
|
15
18
|
#
|
|
@@ -47,7 +50,7 @@ module AuthProto
|
|
|
47
50
|
|
|
48
51
|
# Space-separated requested scopes.
|
|
49
52
|
# @return [String]
|
|
50
|
-
attr_reader :
|
|
53
|
+
attr_reader :requested_scope
|
|
51
54
|
|
|
52
55
|
# Secret PKCE verifier used for code exchange.
|
|
53
56
|
# @return [String]
|
|
@@ -107,7 +110,7 @@ module AuthProto
|
|
|
107
110
|
@client_id = client_id
|
|
108
111
|
@signing_key_id = signing_key_id&.dup&.freeze
|
|
109
112
|
@signing_key_thumbprint = signing_key_thumbprint&.dup&.freeze
|
|
110
|
-
@
|
|
113
|
+
@requested_scope = scope
|
|
111
114
|
@auth_server = auth_server
|
|
112
115
|
@did = did
|
|
113
116
|
@pds = pds&.dup&.freeze
|
|
@@ -181,46 +184,12 @@ module AuthProto
|
|
|
181
184
|
end
|
|
182
185
|
end
|
|
183
186
|
|
|
184
|
-
# Returns
|
|
185
|
-
#
|
|
186
|
-
# @return [String, nil] space-separated granted scopes, or `nil` before authorization
|
|
187
|
-
#
|
|
188
|
-
def granted_scope
|
|
189
|
-
synchronize do
|
|
190
|
-
@tokens&.scope
|
|
191
|
-
end
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
# Checks whether any requested scope token is absent from the current grant.
|
|
195
|
-
# Scope order and duplicate tokens do not affect the comparison.
|
|
196
|
-
#
|
|
197
|
-
# @return [Boolean] `false` before authorization, otherwise whether requested scopes are missing
|
|
198
|
-
#
|
|
199
|
-
def has_limited_scope?
|
|
200
|
-
synchronize do
|
|
201
|
-
@tokens ? !(@scope.split - @tokens.scope.split).empty? : false
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
|
|
205
|
-
# Returns the current tokens and session identifiers, with expiry in seconds from now.
|
|
187
|
+
# Returns a live view of the current public session data.
|
|
206
188
|
#
|
|
207
|
-
# @return [
|
|
189
|
+
# @return [SessionInfo] a session info object
|
|
208
190
|
#
|
|
209
191
|
def session_data
|
|
210
|
-
|
|
211
|
-
return nil unless @tokens
|
|
212
|
-
|
|
213
|
-
{
|
|
214
|
-
access_token: @tokens.access_token,
|
|
215
|
-
token_type: @tokens.token_type,
|
|
216
|
-
expires_in: (@tokens.expires_at - Time.now).to_i,
|
|
217
|
-
refresh_token: @tokens.refresh_token,
|
|
218
|
-
scope: @tokens.scope,
|
|
219
|
-
granted_scope: @tokens.scope,
|
|
220
|
-
session_id: @session_id,
|
|
221
|
-
did: @did
|
|
222
|
-
}
|
|
223
|
-
end
|
|
192
|
+
SessionInfo.new(self)
|
|
224
193
|
end
|
|
225
194
|
|
|
226
195
|
# Returns true if this session has not completed authorization yet and doesn't contain tokens.
|
|
@@ -246,8 +215,6 @@ module AuthProto
|
|
|
246
215
|
end
|
|
247
216
|
end
|
|
248
217
|
|
|
249
|
-
alias authorized? tokens_valid?
|
|
250
|
-
|
|
251
218
|
# Whether this session can refresh its tokens
|
|
252
219
|
# @return [Boolean]
|
|
253
220
|
def renewable?
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AuthProto
|
|
4
|
+
module State
|
|
5
|
+
|
|
6
|
+
#
|
|
7
|
+
# A representation of a session's data that's meant to be used from outside of the AuthProto library.
|
|
8
|
+
#
|
|
9
|
+
|
|
10
|
+
class SessionInfo
|
|
11
|
+
|
|
12
|
+
# Creates an instance of {SessionInfo} backed by the supplied {Session}.
|
|
13
|
+
#
|
|
14
|
+
# @param session [Session] session whose data this object will expose
|
|
15
|
+
#
|
|
16
|
+
def initialize(session)
|
|
17
|
+
@session = session
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The session's unique identifier used to load and update it in the configured storage.
|
|
21
|
+
# @return [String]
|
|
22
|
+
#
|
|
23
|
+
def session_id
|
|
24
|
+
@session.session_id
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Current access token, if authorized.
|
|
28
|
+
# @return [String, nil]
|
|
29
|
+
#
|
|
30
|
+
def access_token
|
|
31
|
+
@session.synchronize { @session.tokens&.access_token }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Current refresh token, if issued.
|
|
35
|
+
# @return [String, nil]
|
|
36
|
+
#
|
|
37
|
+
def refresh_token
|
|
38
|
+
@session.synchronize { @session.tokens&.refresh_token }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Expiry time of the current access token, if assigned.
|
|
42
|
+
# @return [Time, nil]
|
|
43
|
+
#
|
|
44
|
+
def expires_at
|
|
45
|
+
@session.synchronize { @session.tokens&.expires_at }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Remaining access token lifetime in seconds, if an access token is available.
|
|
49
|
+
# @return [Integer, nil]
|
|
50
|
+
#
|
|
51
|
+
def expires_in
|
|
52
|
+
@session.synchronize do
|
|
53
|
+
tokens = @session.tokens
|
|
54
|
+
(tokens.expires_at - Time.now).to_i if tokens
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Space-separated list of permissions requested during authorization.
|
|
59
|
+
# @return [String]
|
|
60
|
+
#
|
|
61
|
+
def requested_scope
|
|
62
|
+
@session.requested_scope
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Space-separated list of permissions that have been actually granted.
|
|
66
|
+
# @return [String, nil]
|
|
67
|
+
#
|
|
68
|
+
def granted_scope
|
|
69
|
+
@session.synchronize { @session.tokens&.scope }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The DID of the authorized account, if known.
|
|
73
|
+
# @return [String, nil]
|
|
74
|
+
#
|
|
75
|
+
def did
|
|
76
|
+
@session.did
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# The URL of the authorized account's PDS, if known.
|
|
80
|
+
# @return [String, nil]
|
|
81
|
+
#
|
|
82
|
+
def pds
|
|
83
|
+
@session.pds
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Returns the current session data properties as a Hash.
|
|
87
|
+
#
|
|
88
|
+
# @return [Hash{Symbol => String, Integer, Time, nil}] current session data
|
|
89
|
+
#
|
|
90
|
+
def to_h
|
|
91
|
+
@session.synchronize do
|
|
92
|
+
{
|
|
93
|
+
session_id: session_id,
|
|
94
|
+
access_token: access_token,
|
|
95
|
+
refresh_token: refresh_token,
|
|
96
|
+
expires_in: expires_in,
|
|
97
|
+
expires_at: expires_at,
|
|
98
|
+
requested_scope: requested_scope,
|
|
99
|
+
granted_scope: granted_scope,
|
|
100
|
+
did: did,
|
|
101
|
+
pds: pds
|
|
102
|
+
}
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# True if the authorization is still pending (has not completed the callback and received access tokens).
|
|
107
|
+
# @return [Boolean]
|
|
108
|
+
#
|
|
109
|
+
def pending?
|
|
110
|
+
@session.pending?
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# True if the access token is currently valid, including the expiry buffer.
|
|
114
|
+
# @return [Boolean]
|
|
115
|
+
#
|
|
116
|
+
def tokens_valid?
|
|
117
|
+
@session.tokens_valid?
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Whether the session is currently authorized.
|
|
121
|
+
# @return [Boolean]
|
|
122
|
+
#
|
|
123
|
+
alias authorized? tokens_valid?
|
|
124
|
+
|
|
125
|
+
# True if the session currently has a refresh token.
|
|
126
|
+
# @return [Boolean]
|
|
127
|
+
#
|
|
128
|
+
def renewable?
|
|
129
|
+
@session.renewable?
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Tells if some of the originally requested permissions are missing from the current grant.
|
|
133
|
+
# @return [Boolean]
|
|
134
|
+
#
|
|
135
|
+
def has_limited_scope?
|
|
136
|
+
@session.synchronize do
|
|
137
|
+
tokens = @session.tokens
|
|
138
|
+
tokens ? !(@session.requested_scope.split - tokens.scope.split).empty? : false
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# True if the authorization server has an advertised public token revocation endpoint.
|
|
143
|
+
# @return [Boolean]
|
|
144
|
+
#
|
|
145
|
+
def can_be_revoked?
|
|
146
|
+
@session.can_be_revoked?
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|