authproto 0.3.0.beta2 → 0.3.0.beta4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -0
- data/README.md +108 -5
- data/examples/confidential_client/Gemfile.lock +1 -1
- data/examples/confidential_client/README.md +11 -1
- data/examples/confidential_client/app.rb +35 -17
- data/examples/confidential_client/scripts/generate_keys.rb +1 -1
- data/examples/minisky_console/Gemfile.lock +2 -2
- data/examples/minisky_console/README.md +20 -1
- data/examples/minisky_console/localhost.rb +5 -3
- data/examples/minisky_console/production.rb +9 -3
- data/lib/authproto/client.rb +173 -123
- data/lib/authproto/configuration.rb +37 -16
- data/lib/authproto/dpop/client.rb +105 -40
- data/lib/authproto/dpop/{key_manager.rb → key_set.rb} +54 -120
- data/lib/authproto/dpop/nonce_manager.rb +38 -29
- data/lib/authproto/dpop/proof_generator.rb +21 -35
- data/lib/authproto/dpop.rb +19 -0
- data/lib/authproto/encryption.rb +29 -12
- data/lib/authproto/http_client.rb +8 -7
- data/lib/authproto/http_helpers.rb +85 -0
- data/lib/authproto/metadata/authorization_server.rb +23 -6
- data/lib/authproto/metadata/client_metadata.rb +46 -6
- data/lib/authproto/metadata/resource_server.rb +7 -5
- data/lib/authproto/minisky.rb +36 -34
- data/lib/authproto/par/client.rb +22 -73
- data/lib/authproto/par/client_assertion.rb +47 -16
- data/lib/authproto/par/request.rb +4 -8
- data/lib/authproto/par.rb +2 -1
- data/lib/authproto/serialization/session.rb +33 -13
- data/lib/authproto/serialization/token_set.rb +6 -4
- data/lib/authproto/state/session.rb +102 -6
- data/lib/authproto/state/session_manager.rb +120 -22
- data/lib/authproto/state/token_set.rb +22 -19
- data/lib/authproto/state.rb +2 -2
- data/lib/authproto/storage/key_builder.rb +3 -11
- data/lib/authproto/storage/memory.rb +8 -2
- data/lib/authproto/token/identity.rb +54 -0
- data/lib/authproto/token/refresh.rb +66 -109
- data/lib/authproto/token/response.rb +73 -0
- data/lib/authproto/token/revocation.rb +92 -0
- data/lib/authproto/validated_uri.rb +23 -0
- data/lib/authproto/version.rb +1 -1
- data/lib/authproto.rb +0 -1
- data/sig/authproto/client.rbs +10 -10
- data/sig/authproto/dependencies.rbs +2 -0
- data/sig/authproto/dpop/client.rbs +11 -4
- data/sig/authproto/dpop/{key_manager.rbs → key_set.rbs} +9 -11
- data/sig/authproto/dpop/nonce_manager.rbs +4 -4
- data/sig/authproto/dpop/proof_generator.rbs +5 -10
- data/sig/authproto/dpop.rbs +6 -0
- data/sig/authproto/encryption.rbs +4 -2
- data/sig/authproto/http_client.rbs +0 -1
- data/sig/authproto/http_helpers.rbs +7 -0
- data/sig/authproto/localhost_server.rbs +1 -1
- data/sig/authproto/metadata/authorization_server.rbs +1 -0
- data/sig/authproto/metadata/client_metadata.rbs +3 -1
- data/sig/authproto/metadata/resource_server.rbs +1 -0
- data/sig/authproto/minisky_oauth.rbs +5 -2
- data/sig/authproto/par/client.rbs +4 -8
- data/sig/authproto/par/client_assertion.rbs +4 -0
- data/sig/authproto/par/request.rbs +0 -9
- data/sig/authproto/state/session.rbs +13 -3
- data/sig/authproto/state/session_manager.rbs +10 -3
- data/sig/authproto/state/token_set.rbs +1 -2
- data/sig/authproto/storage/key_builder.rbs +1 -2
- data/sig/authproto/token/refresh.rbs +5 -5
- data/sig/authproto/token/response.rbs +12 -0
- data/sig/authproto/token/revocation.rbs +14 -0
- data/sig/authproto.rbs +6 -3
- metadata +30 -23
- data/.rspec +0 -1
- data/.rubocop.yml +0 -31
- data/.tm_properties +0 -1
- data/.yardopts +0 -5
- data/AGENTS.md +0 -80
- data/Rakefile +0 -8
- data/examples/confidential_client/.gitignore +0 -6
- data/examples/minisky_console/.gitignore +0 -2
- data/lib/authproto/serialization/dpop_key.rb +0 -38
- data/misc/yard_attributes.rb +0 -34
- data/sig/authproto/serialization/dpop_key.rbs +0 -14
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'monitor'
|
|
4
4
|
require 'securerandom'
|
|
5
5
|
|
|
6
|
+
require_relative '../dpop/key_set'
|
|
6
7
|
require_relative '../pkce'
|
|
7
8
|
require_relative '../state'
|
|
8
9
|
|
|
@@ -20,14 +21,30 @@ module AuthProto
|
|
|
20
21
|
# @return [String]
|
|
21
22
|
attr_reader :session_id
|
|
22
23
|
|
|
23
|
-
# Random OAuth state token for callback validation.
|
|
24
|
-
# @return [String]
|
|
24
|
+
# Random OAuth state token for callback validation, cleared after authorization completes.
|
|
25
|
+
# @return [String, nil]
|
|
25
26
|
attr_reader :state_token
|
|
26
27
|
|
|
28
|
+
# Deadline for completing pending authorization, cleared after authorization completes.
|
|
29
|
+
# @return [Time, nil]
|
|
30
|
+
attr_reader :authorization_expires_at
|
|
31
|
+
|
|
27
32
|
# OAuth client identifier.
|
|
28
33
|
# @return [String]
|
|
29
34
|
attr_reader :client_id
|
|
30
35
|
|
|
36
|
+
# Client-authentication key ID, or nil for public clients.
|
|
37
|
+
# @return [String, nil]
|
|
38
|
+
attr_reader :signing_key_id
|
|
39
|
+
|
|
40
|
+
# RFC 7638 SHA-256 thumbprint of the client-authentication public key.
|
|
41
|
+
# @return [String, nil]
|
|
42
|
+
attr_reader :signing_key_thumbprint
|
|
43
|
+
|
|
44
|
+
# Private DPoP key belonging to this authorization session.
|
|
45
|
+
# @return [DPoP::KeySet]
|
|
46
|
+
attr_reader :dpop_key
|
|
47
|
+
|
|
31
48
|
# Space-separated requested scopes.
|
|
32
49
|
# @return [String]
|
|
33
50
|
attr_reader :scope
|
|
@@ -48,6 +65,10 @@ module AuthProto
|
|
|
48
65
|
# @return [String, nil]
|
|
49
66
|
attr_reader :did
|
|
50
67
|
|
|
68
|
+
# PDS URL used to discover the authorization server.
|
|
69
|
+
# @return [String, nil]
|
|
70
|
+
attr_reader :pds
|
|
71
|
+
|
|
51
72
|
# Most recently assigned tokens, if any.
|
|
52
73
|
# @return [TokenSet, nil]
|
|
53
74
|
attr_reader :tokens
|
|
@@ -58,17 +79,42 @@ module AuthProto
|
|
|
58
79
|
# @param scope [String] requested space-separated scopes
|
|
59
80
|
# @param auth_server [Metadata::AuthorizationServer, nil] pre-resolved authorization server
|
|
60
81
|
# @param did [String, nil] pre-resolved account DID
|
|
82
|
+
# @param pds [String, nil] PDS URL used for authorization server discovery
|
|
83
|
+
# @param signing_key_id [String, nil] client-authentication key ID selected before PAR
|
|
84
|
+
# @param signing_key_thumbprint [String, nil] RFC 7638 SHA-256 thumbprint of that key
|
|
85
|
+
# @param dpop_key [DPoP::KeySet] session key; a fresh key is generated for new sessions by default
|
|
86
|
+
# @param authorization_expires_at [Time, nil] deadline for completing pending authorization
|
|
87
|
+
# @raise [ArgumentError] if the signing key ID, thumbprint or DPoP key is invalid
|
|
88
|
+
# @raise [DPoP::KeySet::KeyError] if DPoP key generation fails
|
|
61
89
|
# @raise [PKCE::Error] if PKCE generation fails
|
|
62
90
|
#
|
|
63
|
-
def initialize(client_id:, scope:, auth_server: nil, did: nil
|
|
91
|
+
def initialize(client_id:, scope:, auth_server: nil, did: nil, pds: nil,
|
|
92
|
+
signing_key_id: nil, signing_key_thumbprint: nil,
|
|
93
|
+
dpop_key: DPoP::KeySet.new, authorization_expires_at: nil)
|
|
64
94
|
super() # Initialize MonitorMixin
|
|
65
95
|
|
|
66
|
-
|
|
67
|
-
|
|
96
|
+
unless signing_key_id.nil? || (signing_key_id.is_a?(String) && !signing_key_id.empty?)
|
|
97
|
+
raise ArgumentError, "signing_key_id must be a nonempty string or nil"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
unless signing_key_thumbprint.nil? || (signing_key_thumbprint.is_a?(String) && !signing_key_thumbprint.empty?)
|
|
101
|
+
raise ArgumentError, "signing_key_thumbprint must be a nonempty string or nil"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
raise ArgumentError, "dpop_key must be a DPoP::KeySet" unless dpop_key.is_a?(DPoP::KeySet)
|
|
105
|
+
|
|
106
|
+
@dpop_key = dpop_key
|
|
68
107
|
@client_id = client_id
|
|
108
|
+
@signing_key_id = signing_key_id&.dup&.freeze
|
|
109
|
+
@signing_key_thumbprint = signing_key_thumbprint&.dup&.freeze
|
|
69
110
|
@scope = scope
|
|
70
111
|
@auth_server = auth_server
|
|
71
112
|
@did = did
|
|
113
|
+
@pds = pds&.dup&.freeze
|
|
114
|
+
@authorization_expires_at = authorization_expires_at
|
|
115
|
+
|
|
116
|
+
@session_id = SecureRandom.uuid
|
|
117
|
+
@state_token = SecureRandom.urlsafe_base64(32)
|
|
72
118
|
|
|
73
119
|
pkce = PKCE.new
|
|
74
120
|
@pkce_verifier = pkce.verifier
|
|
@@ -77,6 +123,17 @@ module AuthProto
|
|
|
77
123
|
@tokens = nil
|
|
78
124
|
end
|
|
79
125
|
|
|
126
|
+
# Clears the OAuth state token and pending authorization deadline after successful authorization.
|
|
127
|
+
#
|
|
128
|
+
# @return [nil]
|
|
129
|
+
#
|
|
130
|
+
def clear_state_token
|
|
131
|
+
synchronize do
|
|
132
|
+
@state_token = nil
|
|
133
|
+
@authorization_expires_at = nil
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
80
137
|
# Binds this session to an authorization server.
|
|
81
138
|
#
|
|
82
139
|
# @param server [Metadata::AuthorizationServer] resolved authorization server
|
|
@@ -124,6 +181,27 @@ module AuthProto
|
|
|
124
181
|
end
|
|
125
182
|
end
|
|
126
183
|
|
|
184
|
+
# Returns the scope granted by the latest token response, including after refresh.
|
|
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
|
+
|
|
127
205
|
# Returns the current tokens and session identifiers, with expiry in seconds from now.
|
|
128
206
|
#
|
|
129
207
|
# @return [Hash{Symbol => String, Integer, nil}, nil] session data, or `nil` before tokens are assigned
|
|
@@ -138,20 +216,38 @@ module AuthProto
|
|
|
138
216
|
expires_in: (@tokens.expires_at - Time.now).to_i,
|
|
139
217
|
refresh_token: @tokens.refresh_token,
|
|
140
218
|
scope: @tokens.scope,
|
|
219
|
+
granted_scope: @tokens.scope,
|
|
141
220
|
session_id: @session_id,
|
|
142
221
|
did: @did
|
|
143
222
|
}
|
|
144
223
|
end
|
|
145
224
|
end
|
|
146
225
|
|
|
226
|
+
# Returns true if this session has not completed authorization yet and doesn't contain tokens.
|
|
227
|
+
# @return [Boolean]
|
|
228
|
+
def pending?
|
|
229
|
+
synchronize do
|
|
230
|
+
@tokens.nil?
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# Whether the authorization server advertises a token revocation endpoint.
|
|
235
|
+
# @return [Boolean]
|
|
236
|
+
#
|
|
237
|
+
def can_be_revoked?
|
|
238
|
+
!auth_server&.revocation_endpoint.nil?
|
|
239
|
+
end
|
|
240
|
+
|
|
147
241
|
# Whether this session has valid access tokens
|
|
148
242
|
# @return [Boolean]
|
|
149
|
-
def
|
|
243
|
+
def tokens_valid?
|
|
150
244
|
synchronize do
|
|
151
245
|
!@tokens.nil? && !@tokens.expired?
|
|
152
246
|
end
|
|
153
247
|
end
|
|
154
248
|
|
|
249
|
+
alias authorized? tokens_valid?
|
|
250
|
+
|
|
155
251
|
# Whether this session can refresh its tokens
|
|
156
252
|
# @return [Boolean]
|
|
157
253
|
def renewable?
|
|
@@ -14,73 +14,134 @@ module AuthProto
|
|
|
14
14
|
|
|
15
15
|
class SessionManager
|
|
16
16
|
|
|
17
|
-
# Creates a session manager and its encrypted session serializer.
|
|
17
|
+
# Creates a session manager bound to one client ID and its encrypted session serializer.
|
|
18
|
+
#
|
|
19
|
+
# @param client_id [String] client ID that all managed sessions must match
|
|
20
|
+
# @param supported_signing_keys [Hash{String => String}] key IDs mapped to RFC 7638 SHA-256 thumbprints
|
|
21
|
+
# @raise [ArgumentError] if the client ID, key IDs or thumbprints are not nonempty strings
|
|
18
22
|
# @raise [ArgumentError] if `ATPROTO_MASTER_KEY` is not valid Base64
|
|
19
23
|
#
|
|
20
|
-
def initialize
|
|
24
|
+
def initialize(client_id:, supported_signing_keys: {})
|
|
25
|
+
unless client_id.is_a?(String) && !client_id.empty?
|
|
26
|
+
raise ArgumentError, "client_id must be a nonempty string"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
keys = supported_signing_keys
|
|
30
|
+
|
|
31
|
+
unless keys.is_a?(Hash) && keys.all? { |id, thumb| [id, thumb].all? { |x| x.is_a?(String) && !x.empty? }}
|
|
32
|
+
raise ArgumentError, "supported_signing_keys must map nonempty key IDs to nonempty thumbprints"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
@client_id = client_id.dup.freeze
|
|
36
|
+
@supported_signing_keys = keys.to_h { |id, thumb| [id.dup.freeze, thumb.dup.freeze] }.freeze
|
|
21
37
|
@serializer = Serialization::Session.new
|
|
22
38
|
end
|
|
23
39
|
|
|
24
40
|
# Creates and stores a session and its state-token lookup under a storage lock.
|
|
41
|
+
# The signing key thumbprint is taken from the configured key mapping.
|
|
25
42
|
#
|
|
26
43
|
# @param client_id [String] OAuth client ID
|
|
27
44
|
# @param scope [String] requested space-separated scopes
|
|
28
45
|
# @param auth_server [Metadata::AuthorizationServer, nil] pre-resolved authorization server
|
|
29
46
|
# @param did [String, nil] pre-resolved account DID
|
|
47
|
+
# @param pds [String, nil] PDS URL used for authorization server discovery
|
|
48
|
+
# @param signing_key_id [String, nil] client-authentication key ID selected before PAR
|
|
30
49
|
#
|
|
31
50
|
# @return [Session] newly created session
|
|
51
|
+
# @raise [ArgumentError] if the signing key ID is not configured
|
|
52
|
+
# @raise [SessionError] if the client ID does not match this manager
|
|
53
|
+
# @raise [DPoP::KeySet::KeyError] if session key generation fails
|
|
32
54
|
# @raise [PKCE::Error] if session PKCE generation fails
|
|
33
55
|
# @raise [Storage::StorageError] if writing or locking fails
|
|
34
56
|
# @raise [Serialization::ValidationError] if the session cannot be serialized
|
|
35
57
|
# @raise [Encryption::EncryptionError] if sensitive fields cannot be encrypted
|
|
36
58
|
#
|
|
37
|
-
def create_session(client_id:, scope:, auth_server: nil, did: nil)
|
|
59
|
+
def create_session(client_id:, scope:, auth_server: nil, did: nil, pds: nil, signing_key_id: nil)
|
|
60
|
+
validate_client_id(client_id)
|
|
61
|
+
|
|
62
|
+
if signing_key_id && !@supported_signing_keys.key?(signing_key_id)
|
|
63
|
+
raise ArgumentError, "Unsupported signing_key_id: #{signing_key_id}"
|
|
64
|
+
end
|
|
65
|
+
|
|
38
66
|
session = Session.new(
|
|
39
67
|
client_id: client_id,
|
|
40
68
|
scope: scope,
|
|
41
69
|
auth_server: auth_server,
|
|
42
|
-
did: did
|
|
70
|
+
did: did,
|
|
71
|
+
pds: pds,
|
|
72
|
+
signing_key_id: signing_key_id,
|
|
73
|
+
signing_key_thumbprint: @supported_signing_keys[signing_key_id],
|
|
74
|
+
authorization_expires_at: Time.now + AuthProto.configuration.pending_session_ttl
|
|
43
75
|
)
|
|
44
76
|
|
|
45
|
-
# Store
|
|
77
|
+
# Store the session and state mapping with the same pending authorization lifetime.
|
|
46
78
|
session_key = Storage::KeyBuilder.session_key(session.session_id)
|
|
47
79
|
state_key = Storage::KeyBuilder.state_key(session.state_token)
|
|
48
80
|
|
|
49
81
|
AuthProto.storage.with_lock(session_key, ttl: 30) do
|
|
82
|
+
ttl = session_ttl(session)
|
|
50
83
|
serialized = @serializer.serialize(session)
|
|
51
|
-
AuthProto.storage.set(session_key, serialized)
|
|
52
|
-
AuthProto.storage.set(state_key, session.session_id)
|
|
84
|
+
AuthProto.storage.set(session_key, serialized, ttl: ttl)
|
|
85
|
+
AuthProto.storage.set(state_key, session.session_id, ttl: ttl)
|
|
53
86
|
end
|
|
54
87
|
|
|
55
88
|
session
|
|
56
89
|
end
|
|
57
90
|
|
|
58
|
-
# Persists a session
|
|
91
|
+
# Persists a session under a storage lock.
|
|
92
|
+
#
|
|
93
|
+
# Pending sessions retain their original deadline. Non-renewable authenticated sessions
|
|
94
|
+
# expire with their access token; renewable sessions have no storage expiry.
|
|
59
95
|
#
|
|
60
96
|
# @param session [Session] session to persist
|
|
61
97
|
# @return [Session] the supplied session
|
|
62
98
|
# @raise [Storage::StorageError] if writing or locking fails
|
|
63
99
|
# @raise [Serialization::ValidationError] if the session cannot be serialized
|
|
64
100
|
# @raise [Encryption::EncryptionError] if sensitive fields cannot be encrypted
|
|
101
|
+
# @raise [SessionError] if the client ID differs, the pending deadline is missing or the session has expired
|
|
65
102
|
#
|
|
66
103
|
def update_session(session)
|
|
104
|
+
validate_client_id(session.client_id)
|
|
105
|
+
|
|
67
106
|
session_key = Storage::KeyBuilder.session_key(session.session_id)
|
|
68
107
|
|
|
69
108
|
AuthProto.storage.with_lock(session_key, ttl: 30) do
|
|
109
|
+
ttl = session_ttl(session)
|
|
70
110
|
serialized = @serializer.serialize(session)
|
|
71
|
-
AuthProto.storage.set(session_key, serialized)
|
|
72
|
-
|
|
73
|
-
state_key = Storage::KeyBuilder.state_key(session.state_token)
|
|
74
|
-
AuthProto.storage.set(state_key, session.session_id)
|
|
111
|
+
AuthProto.storage.set(session_key, serialized, ttl: ttl)
|
|
75
112
|
end
|
|
76
113
|
|
|
77
114
|
session
|
|
78
115
|
end
|
|
79
116
|
|
|
117
|
+
# Persists successful authorization without its state token and removes the callback lookup.
|
|
118
|
+
#
|
|
119
|
+
# @param session [Session] session with successfully validated and assigned tokens
|
|
120
|
+
# @return [Session] the supplied session, with its state token cleared
|
|
121
|
+
# @raise [SessionError] if the client ID does not match this manager
|
|
122
|
+
# @raise [Storage::StorageError] if writing, deletion or locking fails
|
|
123
|
+
# @raise [Serialization::ValidationError] if the session cannot be serialized
|
|
124
|
+
# @raise [Encryption::EncryptionError] if sensitive fields cannot be encrypted
|
|
125
|
+
#
|
|
126
|
+
def complete_authorization(session)
|
|
127
|
+
validate_client_id(session.client_id)
|
|
128
|
+
|
|
129
|
+
state_token = session.state_token
|
|
130
|
+
session.clear_state_token
|
|
131
|
+
update_session(session)
|
|
132
|
+
|
|
133
|
+
AuthProto.storage.delete(Storage::KeyBuilder.state_key(state_token)) if state_token
|
|
134
|
+
|
|
135
|
+
session
|
|
136
|
+
end
|
|
137
|
+
|
|
80
138
|
# Loads and decrypts a session by ID.
|
|
81
139
|
#
|
|
82
140
|
# Returns `nil` for missing, unreadable or malformed sessions and for expired
|
|
83
|
-
# sessions that have no refresh token.
|
|
141
|
+
# sessions that have no refresh token. Sessions with an unsupported signing key ID
|
|
142
|
+
# or a missing/mismatching key thumbprint are unavailable but remain in storage.
|
|
143
|
+
# Sessions with a different client ID are unavailable but remain in storage.
|
|
144
|
+
# Public and legacy sessions without a key ID are accepted. Read/deserialization errors are logged.
|
|
84
145
|
#
|
|
85
146
|
# @param session_id [String] session identifier
|
|
86
147
|
# @return [Session, nil] restored session, or `nil` if unavailable
|
|
@@ -98,7 +159,19 @@ module AuthProto
|
|
|
98
159
|
|
|
99
160
|
begin
|
|
100
161
|
session = @serializer.deserialize(serialized)
|
|
101
|
-
return nil
|
|
162
|
+
return nil unless session.client_id == @client_id
|
|
163
|
+
|
|
164
|
+
if session.signing_key_id
|
|
165
|
+
thumbprint = @supported_signing_keys[session.signing_key_id]
|
|
166
|
+
return nil unless thumbprint && thumbprint == session.signing_key_thumbprint
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
if session.pending?
|
|
170
|
+
deadline = session.authorization_expires_at
|
|
171
|
+
return nil if !deadline || deadline <= Time.now
|
|
172
|
+
else
|
|
173
|
+
return nil if !session.tokens_valid? && !session.renewable?
|
|
174
|
+
end
|
|
102
175
|
|
|
103
176
|
session
|
|
104
177
|
rescue StandardError => e
|
|
@@ -111,13 +184,13 @@ module AuthProto
|
|
|
111
184
|
#
|
|
112
185
|
# Storage read errors are logged and treated as a missing session.
|
|
113
186
|
#
|
|
114
|
-
# @param
|
|
187
|
+
# @param state_token [String, nil] callback state token
|
|
115
188
|
# @return [Session, nil] restored session, or `nil` if unavailable
|
|
116
189
|
#
|
|
117
|
-
def
|
|
118
|
-
return nil unless
|
|
190
|
+
def get_session_by_state_token(state_token)
|
|
191
|
+
return nil unless state_token
|
|
119
192
|
|
|
120
|
-
state_key = Storage::KeyBuilder.state_key(
|
|
193
|
+
state_key = Storage::KeyBuilder.state_key(state_token)
|
|
121
194
|
|
|
122
195
|
begin
|
|
123
196
|
session_id = AuthProto.storage.get(state_key)
|
|
@@ -127,7 +200,9 @@ module AuthProto
|
|
|
127
200
|
return nil
|
|
128
201
|
end
|
|
129
202
|
|
|
130
|
-
get_session(session_id)
|
|
203
|
+
session = get_session(session_id)
|
|
204
|
+
|
|
205
|
+
session if !session.nil? && session.state_token == state_token
|
|
131
206
|
end
|
|
132
207
|
|
|
133
208
|
# Deletes a session and its state mapping if it can be retrieved.
|
|
@@ -141,11 +216,9 @@ module AuthProto
|
|
|
141
216
|
return unless session
|
|
142
217
|
|
|
143
218
|
session_key = Storage::KeyBuilder.session_key(session_id)
|
|
144
|
-
state_key = Storage::KeyBuilder.state_key(session.state_token)
|
|
145
|
-
|
|
146
219
|
AuthProto.storage.with_lock(session_key, ttl: 30) do
|
|
147
220
|
AuthProto.storage.delete(session_key)
|
|
148
|
-
AuthProto.storage.delete(state_key)
|
|
221
|
+
AuthProto.storage.delete(Storage::KeyBuilder.state_key(session.state_token)) if session.state_token
|
|
149
222
|
end
|
|
150
223
|
end
|
|
151
224
|
|
|
@@ -157,6 +230,31 @@ module AuthProto
|
|
|
157
230
|
def cleanup_expired
|
|
158
231
|
# No-op - expiry handled by storage TTL and retrieval validation
|
|
159
232
|
end
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
private
|
|
236
|
+
|
|
237
|
+
def validate_client_id(client_id)
|
|
238
|
+
return if client_id == @client_id
|
|
239
|
+
|
|
240
|
+
raise SessionError, "Session client_id does not match manager client_id"
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def session_ttl(session)
|
|
244
|
+
return nil if session.renewable?
|
|
245
|
+
|
|
246
|
+
if session.pending?
|
|
247
|
+
expires_at = session.authorization_expires_at
|
|
248
|
+
raise SessionError, "Pending session has no authorization deadline" unless expires_at
|
|
249
|
+
else
|
|
250
|
+
expires_at = session.tokens.expires_at
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
remaining = expires_at - Time.now
|
|
254
|
+
raise SessionError, "Session has expired" unless remaining.positive?
|
|
255
|
+
|
|
256
|
+
remaining.ceil
|
|
257
|
+
end
|
|
160
258
|
end
|
|
161
259
|
end
|
|
162
260
|
end
|
|
@@ -17,10 +17,6 @@ module AuthProto
|
|
|
17
17
|
# @return [String, nil]
|
|
18
18
|
attr_reader :refresh_token
|
|
19
19
|
|
|
20
|
-
# Token scheme; always `DPoP`.
|
|
21
|
-
# @return [String]
|
|
22
|
-
attr_reader :token_type
|
|
23
|
-
|
|
24
20
|
# Space-separated granted scopes.
|
|
25
21
|
# @return [String]
|
|
26
22
|
attr_reader :scope
|
|
@@ -36,35 +32,38 @@ module AuthProto
|
|
|
36
32
|
# Creates a token set and calculates its expiry from the current time.
|
|
37
33
|
#
|
|
38
34
|
# @param access_token [String] nonempty DPoP-bound access token
|
|
39
|
-
# @param token_type [String] must be `DPoP`
|
|
40
|
-
# @param expires_in [Integer] lifetime in seconds; negative
|
|
35
|
+
# @param token_type [String] must be `DPoP` (case-insensitive)
|
|
36
|
+
# @param expires_in [Integer] lifetime of the access token in seconds; negative value means it's already expired
|
|
41
37
|
# @param scope [String] nonempty space-separated granted scopes
|
|
42
38
|
# @param sub [String] nonempty account DID
|
|
43
|
-
# @param refresh_token [String, nil] refresh token, if issued
|
|
44
|
-
# @raise [ArgumentError] if
|
|
39
|
+
# @param refresh_token [String, nil] nonempty refresh token, if issued
|
|
40
|
+
# @raise [ArgumentError] if token fields have invalid types or values
|
|
45
41
|
#
|
|
46
|
-
def initialize(
|
|
47
|
-
access_token:,
|
|
48
|
-
token_type:,
|
|
49
|
-
expires_in:,
|
|
50
|
-
scope:,
|
|
51
|
-
sub:,
|
|
52
|
-
refresh_token: nil
|
|
53
|
-
)
|
|
42
|
+
def initialize(access_token:, token_type:, expires_in:, scope:, sub:, refresh_token: nil)
|
|
54
43
|
validate_token_type(token_type)
|
|
44
|
+
|
|
55
45
|
validate_required('access_token', access_token)
|
|
56
46
|
validate_required('scope', scope)
|
|
57
47
|
validate_required('sub', sub)
|
|
48
|
+
validate_required('refresh_token', refresh_token) unless refresh_token.nil?
|
|
49
|
+
|
|
58
50
|
validate_expires_in(expires_in)
|
|
59
51
|
|
|
60
52
|
@access_token = access_token
|
|
61
53
|
@refresh_token = refresh_token
|
|
62
|
-
@token_type = token_type
|
|
63
54
|
@scope = scope
|
|
64
55
|
@sub = sub
|
|
65
56
|
@expires_at = Time.now + expires_in
|
|
66
57
|
end
|
|
67
58
|
|
|
59
|
+
# Returns the token scheme, which is always `DPoP`.
|
|
60
|
+
#
|
|
61
|
+
# @return [String] `DPoP`
|
|
62
|
+
#
|
|
63
|
+
def token_type
|
|
64
|
+
'DPoP'
|
|
65
|
+
end
|
|
66
|
+
|
|
68
67
|
# Whether this token set includes a refresh token
|
|
69
68
|
# @return [Boolean]
|
|
70
69
|
def renewable?
|
|
@@ -84,11 +83,15 @@ module AuthProto
|
|
|
84
83
|
private
|
|
85
84
|
|
|
86
85
|
def validate_token_type(type)
|
|
87
|
-
|
|
86
|
+
unless type.is_a?(String) && type.casecmp?('DPoP')
|
|
87
|
+
raise ArgumentError, "token_type must be DPoP"
|
|
88
|
+
end
|
|
88
89
|
end
|
|
89
90
|
|
|
90
91
|
def validate_required(name, value)
|
|
91
|
-
|
|
92
|
+
unless value.is_a?(String) && !value.empty?
|
|
93
|
+
raise ArgumentError, "#{name} must be a nonempty string"
|
|
94
|
+
end
|
|
92
95
|
end
|
|
93
96
|
|
|
94
97
|
def validate_expires_in(expires_in)
|
data/lib/authproto/state.rb
CHANGED
|
@@ -24,7 +24,7 @@ module AuthProto
|
|
|
24
24
|
# with support for lookup by session ID or state token.
|
|
25
25
|
#
|
|
26
26
|
# @example Creating and managing a session
|
|
27
|
-
# manager = AuthProto::State::SessionManager.new
|
|
27
|
+
# manager = AuthProto::State::SessionManager.new(client_id: 'https://myapp.com/oauth-client-metadata.json')
|
|
28
28
|
#
|
|
29
29
|
# # Create a new session
|
|
30
30
|
# session = manager.create_session(
|
|
@@ -45,7 +45,7 @@ module AuthProto
|
|
|
45
45
|
#
|
|
46
46
|
# # Lookup session later
|
|
47
47
|
# session = manager.get_session(session.session_id)
|
|
48
|
-
# if session.
|
|
48
|
+
# if session.tokens_valid?
|
|
49
49
|
# puts "Access token: #{session.tokens.access_token}"
|
|
50
50
|
# end
|
|
51
51
|
#
|
|
@@ -39,20 +39,12 @@ module AuthProto
|
|
|
39
39
|
|
|
40
40
|
# Builds a namespaced storage key for a DPoP nonce.
|
|
41
41
|
#
|
|
42
|
+
# @param session_id [String] OAuth session identifier
|
|
42
43
|
# @param server_url [String] server origin URL
|
|
43
44
|
# @return [String] colon-separated key beginning with `atproto:`
|
|
44
45
|
#
|
|
45
|
-
def nonce_key(server_url)
|
|
46
|
-
build_key("nonce", server_url)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
# Builds a namespaced storage key for a DPoP key.
|
|
50
|
-
#
|
|
51
|
-
# @param client_id [String] OAuth client ID
|
|
52
|
-
# @return [String] colon-separated key beginning with `atproto:`
|
|
53
|
-
#
|
|
54
|
-
def dpop_key(client_id)
|
|
55
|
-
build_key("dpop", client_id)
|
|
46
|
+
def nonce_key(session_id, server_url)
|
|
47
|
+
build_key("nonce", session_id, server_url)
|
|
56
48
|
end
|
|
57
49
|
|
|
58
50
|
# Builds a namespaced storage key for a lock.
|
|
@@ -32,7 +32,7 @@ module AuthProto
|
|
|
32
32
|
#
|
|
33
33
|
# @param key [String] nonempty key beginning with `atproto:`
|
|
34
34
|
# @param value [Object] value retained by reference
|
|
35
|
-
# @param ttl [Integer, nil] positive expiry in seconds, or `nil`
|
|
35
|
+
# @param ttl [Integer, nil] positive expiry in seconds, or `nil` to clear any expiry
|
|
36
36
|
# @return [true] if the write succeeds
|
|
37
37
|
# @raise [StorageError] if key or TTL validation fails
|
|
38
38
|
#
|
|
@@ -42,7 +42,13 @@ module AuthProto
|
|
|
42
42
|
|
|
43
43
|
@monitor.synchronize do
|
|
44
44
|
@data[key] = value
|
|
45
|
-
|
|
45
|
+
|
|
46
|
+
if ttl
|
|
47
|
+
set_expiration(key, ttl)
|
|
48
|
+
else
|
|
49
|
+
@expirations.delete(key)
|
|
50
|
+
end
|
|
51
|
+
|
|
46
52
|
true
|
|
47
53
|
end
|
|
48
54
|
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'didkit/did'
|
|
4
|
+
require 'uri'
|
|
5
|
+
|
|
6
|
+
require_relative '../errors'
|
|
7
|
+
require_relative '../validated_uri'
|
|
8
|
+
|
|
9
|
+
module AuthProto
|
|
10
|
+
module Token
|
|
11
|
+
|
|
12
|
+
# Error raised when a token subject cannot be bound to the session's PDS.
|
|
13
|
+
class IdentityError < AuthProto::Error; end
|
|
14
|
+
|
|
15
|
+
# Error raised when the subject or its resolved PDS differs from the session binding.
|
|
16
|
+
class IdentityMismatchError < IdentityError; end
|
|
17
|
+
|
|
18
|
+
# Error raised when a DID document cannot be fetched.
|
|
19
|
+
class IdentityLookupError < IdentityError; end
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
#
|
|
23
|
+
# Shared verification of a token subject's current PDS.
|
|
24
|
+
#
|
|
25
|
+
|
|
26
|
+
module Identity
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def validate_subject_pds(subject, session)
|
|
31
|
+
raise IdentityError, "Session is missing its PDS; authorize again" unless session.pds
|
|
32
|
+
|
|
33
|
+
begin
|
|
34
|
+
pds = DIDKit::DID.new(subject).document.pds_endpoint
|
|
35
|
+
rescue DIDKit::DIDError, DIDKit::FormatError => e
|
|
36
|
+
raise IdentityError, "Invalid token subject: #{e.message}"
|
|
37
|
+
rescue StandardError => e
|
|
38
|
+
raise IdentityLookupError, "Could not resolve token subject: #{e.message}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
raise IdentityError, "Subject DID document has no PDS endpoint" unless pds
|
|
42
|
+
|
|
43
|
+
pds_uri = ValidatedURI.new(pds)
|
|
44
|
+
session_uri = ValidatedURI.new(session.pds)
|
|
45
|
+
|
|
46
|
+
unless pds_uri.origin.casecmp?(session_uri.origin)
|
|
47
|
+
raise IdentityMismatchError, "Subject PDS does not match session PDS"
|
|
48
|
+
end
|
|
49
|
+
rescue URI::InvalidURIError => e
|
|
50
|
+
raise IdentityError, "Invalid token subject: #{e.message}"
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|