standard_id 0.61.0 → 0.62.0
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 +76 -0
- data/README.md +163 -20
- data/app/channels/concerns/standard_id/cable_authentication.rb +1 -0
- data/app/controllers/concerns/standard_id/api_authentication.rb +30 -0
- data/app/controllers/concerns/standard_id/web_authentication.rb +29 -7
- data/app/controllers/standard_id/api/step_up_controller.rb +92 -0
- data/app/controllers/standard_id/web/step_up_controller.rb +83 -0
- data/app/models/standard_id/client_application.rb +30 -1
- data/app/models/standard_id/password_credential.rb +76 -8
- data/app/models/standard_id/session.rb +45 -1
- data/app/views/standard_id/web/step_up/show.html.erb +65 -0
- data/config/brakeman.ignore +4 -0
- data/config/routes/api.rb +7 -0
- data/config/routes/web.rb +6 -0
- data/db/migrate/20261123000000_add_authenticated_at_to_standard_id_sessions.rb +15 -0
- data/lib/generators/standard_id/install/templates/standard_id_reference.rb +39 -5
- data/lib/standard_id/api/token_manager.rb +3 -0
- data/lib/standard_id/auth_lineage.rb +39 -8
- data/lib/standard_id/config/schema.rb +79 -5
- data/lib/standard_id/email_password/authentication.rb +1 -1
- data/lib/standard_id/email_password.rb +4 -5
- data/lib/standard_id/errors.rb +16 -0
- data/lib/standard_id/events/definitions.rb +14 -3
- data/lib/standard_id/events/subscribers/passwordless_delivery_subscriber.rb +3 -1
- data/lib/standard_id/jwt_service.rb +31 -2
- data/lib/standard_id/keyed_digest.rb +43 -5
- data/lib/standard_id/oauth/direct_mint_flow.rb +9 -3
- data/lib/standard_id/oauth/oauth_session_persistence.rb +24 -9
- data/lib/standard_id/oauth/password_flow.rb +4 -10
- data/lib/standard_id/oauth/token_grant_flow.rb +28 -3
- data/lib/standard_id/password_pepper.rb +88 -0
- data/lib/standard_id/passwordless/base_strategy.rb +5 -1
- data/lib/standard_id/passwordless/code_handoff.rb +72 -0
- data/lib/standard_id/passwordless.rb +25 -0
- data/lib/standard_id/step_up.rb +236 -0
- data/lib/standard_id/testing/otp_helpers.rb +5 -5
- data/lib/standard_id/testing/request_helpers.rb +8 -3
- data/lib/standard_id/token_minter.rb +6 -2
- data/lib/standard_id/version.rb +1 -1
- data/lib/standard_id/web/session_manager.rb +9 -2
- data/lib/standard_id.rb +2 -0
- metadata +8 -1
|
@@ -214,13 +214,42 @@ module StandardId
|
|
|
214
214
|
|
|
215
215
|
# Constant-time check of an RFC 7592 registration access token, under the
|
|
216
216
|
# current secret or any of `config.previous_secret_key_bases`.
|
|
217
|
+
# A token accepted under a previous secret is re-keyed (see
|
|
218
|
+
# #rekey_registration_access_token): these tokens live as long as the
|
|
219
|
+
# client, so without it the old secret could never be dropped.
|
|
217
220
|
def authenticate_registration_access_token(token)
|
|
218
221
|
return false unless dynamically_registered? && token.present?
|
|
219
222
|
|
|
220
|
-
StandardId::KeyedDigest.secure_match?(
|
|
223
|
+
matched = StandardId::KeyedDigest.secure_match?(
|
|
221
224
|
registration_access_token_digest,
|
|
222
225
|
StandardId::KeyedDigest.candidates { |secret| self.class.digest_registration_access_token(token.to_s, secret:) }
|
|
223
226
|
)
|
|
227
|
+
rekey_registration_access_token(token.to_s) if matched
|
|
228
|
+
matched
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# Rewrites the digest of an accepted registration access token under the
|
|
232
|
+
# current secret when it was stored under a previous one, in one
|
|
233
|
+
# conditional UPDATE (StandardId::KeyedDigest.rekey). No write when it is
|
|
234
|
+
# already current; never raises.
|
|
235
|
+
#
|
|
236
|
+
# @return [Boolean] whether the row was rewritten
|
|
237
|
+
def rekey_registration_access_token(token)
|
|
238
|
+
return false if StandardId::KeyedDigest.previous_secrets.empty?
|
|
239
|
+
|
|
240
|
+
current = self.class.digest_registration_access_token(token)
|
|
241
|
+
return false if registration_access_token_digest == current
|
|
242
|
+
|
|
243
|
+
rekeyed = StandardId::KeyedDigest.rekey(
|
|
244
|
+
StandardId::ClientApplication, id: id,
|
|
245
|
+
from: { registration_access_token_digest: registration_access_token_digest },
|
|
246
|
+
to: { registration_access_token_digest: current }
|
|
247
|
+
)
|
|
248
|
+
if rekeyed
|
|
249
|
+
self.registration_access_token_digest = current
|
|
250
|
+
clear_attribute_changes([:registration_access_token_digest])
|
|
251
|
+
end
|
|
252
|
+
rekeyed
|
|
224
253
|
end
|
|
225
254
|
|
|
226
255
|
# The RFC 7591 token_endpoint_auth_method a client information response
|
|
@@ -37,22 +37,64 @@ module StandardId
|
|
|
37
37
|
# has_secure_password's setter, remembering the digest it made so the
|
|
38
38
|
# commit can tell a new password from a direct write of the digest. An
|
|
39
39
|
# empty assignment changes nothing, as has_secure_password ignores it.
|
|
40
|
+
# Under c.password.pepper the digest is made from the peppered password
|
|
41
|
+
# (StandardId::PasswordPepper); without one, has_secure_password makes it.
|
|
40
42
|
def password=(unencrypted_password)
|
|
41
|
-
|
|
43
|
+
# has_secure_password's own test (nil clears, "" is ignored), so a
|
|
44
|
+
# whitespace-only password is peppered like any other.
|
|
45
|
+
if StandardId::PasswordPepper.current.present? && !unencrypted_password.nil? && !unencrypted_password.empty?
|
|
46
|
+
@password = unencrypted_password
|
|
47
|
+
self.password_digest = self.class.password_digest_for(unencrypted_password)
|
|
48
|
+
else
|
|
49
|
+
super
|
|
50
|
+
end
|
|
42
51
|
@assigned_password_digest = (password_digest if unencrypted_password.present?)
|
|
43
52
|
end
|
|
44
53
|
|
|
54
|
+
# has_secure_password's check, trying each pepper the digest may have been
|
|
55
|
+
# made under (StandardId::PasswordPepper.candidates): the current one, the
|
|
56
|
+
# previous ones, then none. Remembers which one matched, so
|
|
57
|
+
# #rehash_password can move the digest to the current pepper. With no
|
|
58
|
+
# pepper configured this is has_secure_password's own comparison.
|
|
59
|
+
#
|
|
60
|
+
# has_secure_password's +password_challenge+ validation compares the bare
|
|
61
|
+
# password and does not know about the pepper; check a current password
|
|
62
|
+
# with this method instead.
|
|
63
|
+
#
|
|
64
|
+
# @return [self, false]
|
|
65
|
+
def authenticate_password(unencrypted_password)
|
|
66
|
+
digest = password_digest
|
|
67
|
+
return false if digest.blank?
|
|
68
|
+
|
|
69
|
+
pepper = StandardId::PasswordPepper.matching_pepper(digest, unencrypted_password)
|
|
70
|
+
return false if pepper.nil?
|
|
71
|
+
|
|
72
|
+
@authenticated_pepper = [digest, pepper]
|
|
73
|
+
self
|
|
74
|
+
end
|
|
75
|
+
alias_method :authenticate, :authenticate_password
|
|
76
|
+
|
|
45
77
|
# The bcrypt cost has_secure_password hashes a new password at, worked out
|
|
46
78
|
# the way ActiveModel::SecurePassword does.
|
|
47
79
|
def self.password_cost
|
|
48
80
|
ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
|
|
49
81
|
end
|
|
50
82
|
|
|
83
|
+
# A new digest for +password+: bcrypt at .password_cost of the password
|
|
84
|
+
# under the current pepper (the bare password when none is set).
|
|
85
|
+
def self.password_digest_for(password)
|
|
86
|
+
peppered = StandardId::PasswordPepper.apply(password, StandardId::PasswordPepper.current)
|
|
87
|
+
BCrypt::Password.create(peppered, cost: password_cost)
|
|
88
|
+
end
|
|
89
|
+
|
|
51
90
|
# Re-hashes +password+ when the stored digest was made at a bcrypt cost
|
|
52
|
-
# other than .password_cost,
|
|
91
|
+
# other than .password_cost, or under a pepper other than the current one
|
|
92
|
+
# (a previous pepper, or none: see StandardId::PasswordPepper), so raising
|
|
93
|
+
# BCrypt::Engine.cost, or setting or rotating c.password.pepper, reaches
|
|
53
94
|
# existing passwords as their owners sign in instead of only new ones.
|
|
54
95
|
# Every password login calls it once +password+ has authenticated; turn it
|
|
55
|
-
# off with c.password.rehash_on_login = false.
|
|
96
|
+
# off with c.password.rehash_on_login = false. It is not a password change:
|
|
97
|
+
# CREDENTIAL_PASSWORD_CHANGED is not published and no notice is sent.
|
|
56
98
|
#
|
|
57
99
|
# +authenticated_digest+ is the digest +password+ was checked against. The
|
|
58
100
|
# write is one UPDATE conditional on the row still holding it, so a reset
|
|
@@ -70,10 +112,14 @@ module StandardId
|
|
|
70
112
|
#
|
|
71
113
|
# @return [Boolean] whether the digest was replaced
|
|
72
114
|
def rehash_password(password, authenticated_digest = password_digest)
|
|
73
|
-
return false unless StandardId.config.password.rehash_on_login
|
|
115
|
+
return false unless StandardId.config.password.rehash_on_login
|
|
116
|
+
|
|
117
|
+
pepper_changed = pepper_rehash_needed?(password, authenticated_digest)
|
|
118
|
+
return false if pepper_changed.nil?
|
|
119
|
+
return false unless pepper_changed || password_rehash_needed?
|
|
74
120
|
|
|
75
121
|
previous_cost = BCrypt::Password.new(password_digest).cost
|
|
76
|
-
attributes = { password_digest:
|
|
122
|
+
attributes = { password_digest: self.class.password_digest_for(password), updated_at: Time.current }
|
|
77
123
|
# The savepoint keeps a failed UPDATE from aborting the caller's
|
|
78
124
|
# transaction (PostgreSQL refuses every later statement in it).
|
|
79
125
|
rehashed = self.class.transaction(requires_new: true) do
|
|
@@ -86,7 +132,7 @@ module StandardId
|
|
|
86
132
|
# Held until the caller's transaction commits, so a login that rolls
|
|
87
133
|
# back (a policy refusal inside EmailPassword::Authentication's lock)
|
|
88
134
|
# reports no rehash.
|
|
89
|
-
ActiveRecord.after_all_transactions_commit { publish_password_rehashed(previous_cost) }
|
|
135
|
+
ActiveRecord.after_all_transactions_commit { publish_password_rehashed(previous_cost, pepper_changed) }
|
|
90
136
|
true
|
|
91
137
|
rescue StandardError => e
|
|
92
138
|
report_failure("rehash password credential #{id}", e)
|
|
@@ -105,16 +151,38 @@ module StandardId
|
|
|
105
151
|
false
|
|
106
152
|
end
|
|
107
153
|
|
|
154
|
+
# Whether +digest+ holds +password+ under a pepper other than the current
|
|
155
|
+
# one: true or false, or nil when +password+ does not match +digest+ at
|
|
156
|
+
# all, which must not be re-hashed. Always false while no pepper is
|
|
157
|
+
# configured, without another bcrypt comparison, so a host without one
|
|
158
|
+
# rehashes exactly as before. Reuses the pepper #authenticate matched for
|
|
159
|
+
# this digest; otherwise works it out again.
|
|
160
|
+
def pepper_rehash_needed?(password, digest)
|
|
161
|
+
return false unless StandardId::PasswordPepper.enabled?
|
|
162
|
+
|
|
163
|
+
pepper = if @authenticated_pepper&.first == digest
|
|
164
|
+
@authenticated_pepper.last
|
|
165
|
+
else
|
|
166
|
+
StandardId::PasswordPepper.matching_pepper(digest, password)
|
|
167
|
+
end
|
|
168
|
+
return nil if pepper.nil?
|
|
169
|
+
|
|
170
|
+
pepper != StandardId::PasswordPepper.current
|
|
171
|
+
rescue BCrypt::Errors::InvalidHash
|
|
172
|
+
nil
|
|
173
|
+
end
|
|
174
|
+
|
|
108
175
|
# The event carries the account: load the chain (a no-op where the caller
|
|
109
176
|
# preloaded it) so a subscriber under strict loading can walk it.
|
|
110
|
-
def publish_password_rehashed(previous_cost)
|
|
177
|
+
def publish_password_rehashed(previous_cost, pepper_changed = false)
|
|
111
178
|
ActiveRecord::Associations::Preloader.new(records: [self], associations: { credential: { identifier: :account } }).call
|
|
112
179
|
StandardId::Events.publish(
|
|
113
180
|
StandardId::Events::CREDENTIAL_PASSWORD_REHASHED,
|
|
114
181
|
credential: self,
|
|
115
182
|
account: account,
|
|
116
183
|
previous_cost: previous_cost,
|
|
117
|
-
cost: self.class.password_cost
|
|
184
|
+
cost: self.class.password_cost,
|
|
185
|
+
pepper_changed: pepper_changed
|
|
118
186
|
)
|
|
119
187
|
rescue StandardError => e
|
|
120
188
|
report_failure("publish CREDENTIAL_PASSWORD_REHASHED for password credential #{id}", e)
|
|
@@ -53,8 +53,10 @@ module StandardId
|
|
|
53
53
|
|
|
54
54
|
session = by_token(token).first
|
|
55
55
|
return nil if session.nil?
|
|
56
|
+
return nil unless session.authenticate_token(token)
|
|
56
57
|
|
|
57
|
-
session.
|
|
58
|
+
session.rekey_token(token)
|
|
59
|
+
session
|
|
58
60
|
end
|
|
59
61
|
|
|
60
62
|
# Keyed digest of an opaque session token — the default scheme.
|
|
@@ -110,6 +112,39 @@ module StandardId
|
|
|
110
112
|
false
|
|
111
113
|
end
|
|
112
114
|
|
|
115
|
+
# Re-keys a session that authenticated with +token+ under a previous
|
|
116
|
+
# `secret_key_base`: rewrites `lookup_hash`, and an HMAC `token_digest`,
|
|
117
|
+
# under the current secret in one conditional UPDATE (StandardId::KeyedDigest.rekey).
|
|
118
|
+
# Without it an old secret would have to stay in
|
|
119
|
+
# `config.previous_secret_key_bases` for the longest session lifetime.
|
|
120
|
+
#
|
|
121
|
+
# Call it only once +token+ has been accepted for this session. It writes
|
|
122
|
+
# nothing when both values are already current (always so with no
|
|
123
|
+
# previous secrets configured), when +token+ is not the one the stored
|
|
124
|
+
# lookup hash was derived from, or for a BCrypt digest (not keyed), and it
|
|
125
|
+
# never raises.
|
|
126
|
+
#
|
|
127
|
+
# @return [Boolean] whether the row was rewritten
|
|
128
|
+
def rekey_token(token)
|
|
129
|
+
return false if token.blank? || StandardId::KeyedDigest.previous_secrets.empty?
|
|
130
|
+
return false unless self.class.lookup_hashes_for(token).include?(lookup_hash)
|
|
131
|
+
|
|
132
|
+
updates = {}
|
|
133
|
+
current_lookup_hash = self.class.lookup_hash_for(token)
|
|
134
|
+
updates[:lookup_hash] = current_lookup_hash unless lookup_hash == current_lookup_hash
|
|
135
|
+
updates[:token_digest] = self.class.hmac_token_digest(token) if token_digest_under_previous_secret?(token)
|
|
136
|
+
return false if updates.empty?
|
|
137
|
+
|
|
138
|
+
rekeyed = StandardId::KeyedDigest.rekey(
|
|
139
|
+
StandardId::Session, id: id, from: { lookup_hash: lookup_hash, token_digest: token_digest }, to: updates
|
|
140
|
+
)
|
|
141
|
+
if rekeyed
|
|
142
|
+
updates.each { |attribute, value| self[attribute] = value }
|
|
143
|
+
clear_attribute_changes(updates.keys)
|
|
144
|
+
end
|
|
145
|
+
rekeyed
|
|
146
|
+
end
|
|
147
|
+
|
|
113
148
|
# Counts returned by the bulk-revocation class methods. Callers use these to
|
|
114
149
|
# emit their own aggregate event (see
|
|
115
150
|
# StandardId::Api::Oauth::RevocationsController#emit_token_revoked) — the
|
|
@@ -278,6 +313,15 @@ module StandardId
|
|
|
278
313
|
self.lookup_hash = self.class.lookup_hash_for(token)
|
|
279
314
|
end
|
|
280
315
|
|
|
316
|
+
# An HMAC digest of +token+ that is not the current secret's but verifies
|
|
317
|
+
# under a previous one.
|
|
318
|
+
def token_digest_under_previous_secret?(token)
|
|
319
|
+
return false if token_digest.blank? || token_digest.start_with?("$2")
|
|
320
|
+
return false if token_digest == self.class.hmac_token_digest(token)
|
|
321
|
+
|
|
322
|
+
authenticate_token(token)
|
|
323
|
+
end
|
|
324
|
+
|
|
281
325
|
# Revoke any still-active refresh tokens before the session row is deleted,
|
|
282
326
|
# so tokens don't become orphaned but usable.
|
|
283
327
|
def revoke_active_refresh_tokens
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<%# ERB fallback for non-Inertia rendering. Inertia clients receive the same
|
|
2
|
+
data through step_up_page_props. %>
|
|
3
|
+
<% props = step_up_page_props %>
|
|
4
|
+
<% content_for :title, "Confirm it's you" %>
|
|
5
|
+
|
|
6
|
+
<div class="flex min-h-full flex-col justify-center py-12 sm:px-6 lg:px-8">
|
|
7
|
+
<div class="sm:mx-auto sm:w-full sm:max-w-md">
|
|
8
|
+
<h2 class="mt-6 text-center text-2xl/9 font-bold tracking-tight text-gray-900 dark:text-white">Confirm it's you</h2>
|
|
9
|
+
<p class="mt-2 text-center text-sm text-gray-500 dark:text-gray-400">
|
|
10
|
+
<% if props[:target] %>
|
|
11
|
+
For your security, we need to check it's you before continuing. We'll send a code to
|
|
12
|
+
<span id="step-up-target" class="font-semibold text-gray-900 dark:text-white"><%= props[:target] %></span>.
|
|
13
|
+
<% else %>
|
|
14
|
+
There is no verified e-mail address or phone number on your account to send a code to.
|
|
15
|
+
<% end %>
|
|
16
|
+
</p>
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<div class="mt-10 sm:mx-auto sm:w-full sm:max-w-[480px]">
|
|
20
|
+
<div class="bg-white px-6 py-12 shadow sm:rounded-lg sm:px-12 dark:bg-gray-800/50 dark:shadow-none dark:outline dark:outline-1 dark:-outline-offset-1 dark:outline-white/10">
|
|
21
|
+
|
|
22
|
+
<% if flash[:alert].present? %>
|
|
23
|
+
<div class="mb-4 rounded-md bg-red-50 p-4 text-sm text-red-700 dark:bg-red-900/20 dark:text-red-300"><%= flash[:alert] %></div>
|
|
24
|
+
<% end %>
|
|
25
|
+
<% if flash[:notice].present? %>
|
|
26
|
+
<div class="mb-4 rounded-md bg-green-50 p-4 text-sm text-green-700 dark:bg-green-900/20 dark:text-green-300"><%= flash[:notice] %></div>
|
|
27
|
+
<% end %>
|
|
28
|
+
|
|
29
|
+
<% if props[:target] && props[:code_sent] %>
|
|
30
|
+
<%= form_with url: step_up_path, method: :patch, local: true, html: { id: "step-up-verify-form", class: "space-y-6" } do |form| %>
|
|
31
|
+
<div>
|
|
32
|
+
<%= form.label :code, "Verification code", class: "block text-sm/6 font-medium text-gray-900 dark:text-white" %>
|
|
33
|
+
<div class="mt-2">
|
|
34
|
+
<%= form.text_field :code, required: true, autofocus: true, autocomplete: "one-time-code", inputmode: "numeric", maxlength: otp_code_length, class: "block w-full rounded-md bg-white px-3 py-1.5 text-base text-gray-900 outline outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600 sm:text-sm/6 dark:bg-white/5 dark:text-white dark:outline-white/10 dark:placeholder:text-gray-500 dark:focus:outline-indigo-500" %>
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
<% if props[:password_required] %>
|
|
39
|
+
<div>
|
|
40
|
+
<%= form.label :password, "Password", class: "block text-sm/6 font-medium text-gray-900 dark:text-white" %>
|
|
41
|
+
<div class="mt-2">
|
|
42
|
+
<%= form.password_field :password, required: true, autocomplete: "current-password", class: "block w-full rounded-md bg-white px-3 py-1.5 text-base text-gray-900 outline outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600 sm:text-sm/6 dark:bg-white/5 dark:text-white dark:outline-white/10 dark:placeholder:text-gray-500 dark:focus:outline-indigo-500" %>
|
|
43
|
+
</div>
|
|
44
|
+
</div>
|
|
45
|
+
<% end %>
|
|
46
|
+
|
|
47
|
+
<div>
|
|
48
|
+
<%= form.submit "Confirm", class: "flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm/6 font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 dark:bg-indigo-500 dark:shadow-none dark:hover:bg-indigo-400 dark:focus-visible:outline-indigo-500" %>
|
|
49
|
+
</div>
|
|
50
|
+
<% end %>
|
|
51
|
+
<% end %>
|
|
52
|
+
|
|
53
|
+
<% if props[:target] %>
|
|
54
|
+
<%= form_with url: step_up_path, method: :post, local: true, html: { id: "step-up-send-form", class: props[:code_sent] ? "mt-6 text-center text-sm/6" : "" } do |send_form| %>
|
|
55
|
+
<% if props[:code_sent] %>
|
|
56
|
+
<span class="text-gray-500 dark:text-gray-400">Didn't get a code?</span>
|
|
57
|
+
<button type="submit" id="step-up-resend-button" class="font-semibold text-indigo-600 hover:text-indigo-500 dark:text-indigo-400 dark:hover:text-indigo-300">Resend code</button>
|
|
58
|
+
<% else %>
|
|
59
|
+
<%= send_form.submit "Send code", id: "step-up-send-button", class: "flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm/6 font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 dark:bg-indigo-500 dark:shadow-none dark:hover:bg-indigo-400 dark:focus-visible:outline-indigo-500" %>
|
|
60
|
+
<% end %>
|
|
61
|
+
<% end %>
|
|
62
|
+
<% end %>
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
data/config/brakeman.ignore
CHANGED
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
{
|
|
24
24
|
"fingerprint": "413b5740add2c365198a540734a9e8c12b1c0483f521e2db145a80f3d582a4cc",
|
|
25
25
|
"note": "after_sign_in hook redirect is controlled by the host app configuration, not user input"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"fingerprint": "e0a31d4fea6dd6a6d5caccc8565feea1b03d1d031bd585e56729e5e82437c41c",
|
|
29
|
+
"note": "HEAD vs GET distinction is inconsequential here; require_recent_authentication! remembers the return URL on GET only, and a HEAD simply returns to the default after step-up"
|
|
26
30
|
}
|
|
27
31
|
],
|
|
28
32
|
"updated": "2026-03-21",
|
data/config/routes/api.rb
CHANGED
|
@@ -24,6 +24,13 @@ StandardId::ApiEngine.routes.draw do
|
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
+
# Step-up (recent) authentication for bearer clients, StandardId::StepUp.
|
|
28
|
+
# Matched only while c.step_up.enabled is true.
|
|
29
|
+
constraints(->(_request) { StandardId::StepUp.enabled? }) do
|
|
30
|
+
post "step_up", to: "step_up#create", as: :step_up
|
|
31
|
+
post "step_up/verify", to: "step_up#verify", as: :step_up_verify
|
|
32
|
+
end
|
|
33
|
+
|
|
27
34
|
namespace :oidc do
|
|
28
35
|
resource :logout, only: [:show], controller: :logout
|
|
29
36
|
end
|
data/config/routes/web.rb
CHANGED
|
@@ -9,6 +9,12 @@ StandardId::WebEngine.routes.draw do
|
|
|
9
9
|
|
|
10
10
|
resource :login_verify, only: [:show, :update], controller: :login_verify
|
|
11
11
|
resource :logout, only: [:create], controller: :logout
|
|
12
|
+
|
|
13
|
+
# Step-up (recent) authentication, StandardId::StepUp. Drawn always but
|
|
14
|
+
# matched only while c.step_up.enabled is true, so /step_up 404s otherwise.
|
|
15
|
+
constraints(->(_request) { StandardId::StepUp.enabled? }) do
|
|
16
|
+
resource :step_up, only: [:show, :create, :update], controller: :step_up
|
|
17
|
+
end
|
|
12
18
|
resource :signup, only: [:show, :create], controller: :signup
|
|
13
19
|
|
|
14
20
|
# Social authentication callbacks (web flow)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# When a session (and the refresh tokens derived from it) last proved the
|
|
2
|
+
# account's identity: set at sign-in and on a step-up, never by remember-me or
|
|
3
|
+
# a refresh. Read by `require_recent_authentication!` (StandardId::StepUp).
|
|
4
|
+
#
|
|
5
|
+
# Two nullable datetime columns with no default and no index, so both are
|
|
6
|
+
# metadata-only on PostgreSQL. NULL reads as "not recently", which is what
|
|
7
|
+
# every session and refresh token created before this migration is: the guard
|
|
8
|
+
# asks those users to step up once. Until this has run, the gem writes neither
|
|
9
|
+
# column and the guard raises StandardId::ConfigurationError naming it.
|
|
10
|
+
class AddAuthenticatedAtToStandardIdSessions < ActiveRecord::Migration[8.0]
|
|
11
|
+
def change
|
|
12
|
+
add_column :standard_id_sessions, :authenticated_at, :datetime
|
|
13
|
+
add_column :standard_id_refresh_tokens, :authenticated_at, :datetime
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -106,9 +106,12 @@ StandardId.configure do |c|
|
|
|
106
106
|
|
|
107
107
|
# Old secret_key_base values (newest first) still accepted for StandardId's
|
|
108
108
|
# keyed digests — session tokens, OTP codes, authorization codes, registration
|
|
109
|
-
# access tokens — so rotating the secret does not
|
|
110
|
-
# digests always use the current secret
|
|
111
|
-
# session
|
|
109
|
+
# access tokens — and for HS256/384/512 JWTs, so rotating the secret does not
|
|
110
|
+
# sign everyone out. New digests and tokens always use the current secret,
|
|
111
|
+
# and a session or registration access token used under an old secret is
|
|
112
|
+
# re-keyed to the current one on that use. Keep an entry until the
|
|
113
|
+
# credentials you want to keep have been used once (at most the longest
|
|
114
|
+
# session lifetime), and rotate Rails' cookies and message verifiers too
|
|
112
115
|
# (README "Rotating secret_key_base").
|
|
113
116
|
# Default: []
|
|
114
117
|
# c.previous_secret_key_bases = [Rails.application.credentials.old_secret_key_base]
|
|
@@ -228,6 +231,32 @@ StandardId.configure do |c|
|
|
|
228
231
|
# Default: true
|
|
229
232
|
# c.password.require_numbers = true
|
|
230
233
|
|
|
234
|
+
# Pepper: a secret mixed into every password before bcrypt, as
|
|
235
|
+
# bcrypt(HMAC-SHA256(pepper, password)). Off (nil) by default. Turning it on
|
|
236
|
+
# locks nobody out: old digests still verify and move to the pepper as each
|
|
237
|
+
# user signs in. LOSING THE PEPPER FORCES EVERY PASSWORD HASHED UNDER IT TO
|
|
238
|
+
# BE RESET. To rotate, move the old value into previous_peppers.
|
|
239
|
+
# See README "Password pepper".
|
|
240
|
+
# c.password.pepper = Rails.application.credentials.password_pepper
|
|
241
|
+
# c.password.previous_peppers = []
|
|
242
|
+
# c.password.accept_unpeppered_passwords = true
|
|
243
|
+
|
|
244
|
+
# ---------------------------------------------------------------------------
|
|
245
|
+
# Step-up (recent) authentication
|
|
246
|
+
# ---------------------------------------------------------------------------
|
|
247
|
+
|
|
248
|
+
# Off by default. Draws /step_up (web) and POST /step_up, /step_up/verify
|
|
249
|
+
# (API), mints the OIDC auth_time claim, and lets controllers call
|
|
250
|
+
# require_recent_authentication!(within: 5.minutes). Needs migration
|
|
251
|
+
# 20261123000000. See README "Step-up authentication".
|
|
252
|
+
# c.step_up.enabled = false
|
|
253
|
+
# c.step_up.max_age = 300
|
|
254
|
+
# c.step_up.realm = "step_up"
|
|
255
|
+
# c.step_up.delivery = :built_in
|
|
256
|
+
# c.step_up.require_password = true
|
|
257
|
+
# c.step_up.url = nil
|
|
258
|
+
# c.step_up.token_profile_resolver = ->(account:, claims:) { account.profiles.find_by(id: claims[:gid]) }
|
|
259
|
+
|
|
231
260
|
# ---------------------------------------------------------------------------
|
|
232
261
|
# Passwordless (OTP)
|
|
233
262
|
# ---------------------------------------------------------------------------
|
|
@@ -297,11 +326,16 @@ StandardId.configure do |c|
|
|
|
297
326
|
# 0.43). It runs synchronously in the request, so I18n.locale etc. are still
|
|
298
327
|
# available. The same subscriber delivers Otp.issue(delivery: :custom) codes.
|
|
299
328
|
#
|
|
329
|
+
# Read the code with StandardId::Passwordless.issued_code(event), inside the
|
|
330
|
+
# subscriber: it is never part of the payload, and it works with
|
|
331
|
+
# c.events.publish_records = false too.
|
|
332
|
+
#
|
|
300
333
|
# StandardId::Events.subscribe(StandardId::Events::PASSWORDLESS_CODE_GENERATED) do |event|
|
|
301
334
|
# next if event[:skip_sender] # Otp.issue(delivery: :manual)
|
|
335
|
+
# code = StandardId::Passwordless.issued_code(event)
|
|
302
336
|
# case event[:channel]
|
|
303
|
-
# when "email" then PasswordlessMailer.with(code:
|
|
304
|
-
# when "sms" then SmsProvider.send_code(phone: event[:identifier], code:
|
|
337
|
+
# when "email" then PasswordlessMailer.with(code: code, to: event[:identifier]).deliver_later
|
|
338
|
+
# when "sms" then SmsProvider.send_code(phone: event[:identifier], code: code)
|
|
305
339
|
# end
|
|
306
340
|
# end
|
|
307
341
|
|
|
@@ -94,11 +94,14 @@ module StandardId
|
|
|
94
94
|
|
|
95
95
|
private
|
|
96
96
|
|
|
97
|
+
# The caller has just authenticated `account`, so the session proved its
|
|
98
|
+
# identity now (StandardId::StepUp).
|
|
97
99
|
def create_session_for(session_class, account:, device_id: nil, device_agent: nil)
|
|
98
100
|
base_attrs = {
|
|
99
101
|
account: account,
|
|
100
102
|
ip_address: StandardId::Utils::IpNormalizer.normalize(@request.remote_ip)
|
|
101
103
|
}
|
|
104
|
+
base_attrs[:authenticated_at] = Time.current if StandardId::StepUp.session_column?
|
|
102
105
|
|
|
103
106
|
case session_class.name
|
|
104
107
|
when "StandardId::DeviceSession"
|
|
@@ -10,6 +10,13 @@ module StandardId
|
|
|
10
10
|
# #auth_provider
|
|
11
11
|
# refresh_token grant: copied from the presented token to its successor.
|
|
12
12
|
#
|
|
13
|
+
# It also carries WHEN that authentication happened (`authenticated_at`), so
|
|
14
|
+
# step-up authentication (StandardId::StepUp) can tell how recently the
|
|
15
|
+
# account proved its identity: Session#authenticated_at, "authenticated_at"
|
|
16
|
+
# in an authorization code's metadata, RefreshToken#authenticated_at
|
|
17
|
+
# (migration 20261123000000). A lineage with no time has no
|
|
18
|
+
# `:authenticated_at` key at all.
|
|
19
|
+
#
|
|
13
20
|
# Sessions and authorization codes already have a JSON `metadata` column, so
|
|
14
21
|
# the lineage lives there under "auth_method" / "auth_provider". Refresh tokens
|
|
15
22
|
# get two columns (migration 20261004000000). Anything minted before 0.45, or
|
|
@@ -18,12 +25,16 @@ module StandardId
|
|
|
18
25
|
module AuthLineage
|
|
19
26
|
METHOD_KEY = "auth_method".freeze
|
|
20
27
|
PROVIDER_KEY = "auth_provider".freeze
|
|
28
|
+
AUTHENTICATED_AT_KEY = "authenticated_at".freeze
|
|
21
29
|
|
|
22
30
|
module_function
|
|
23
31
|
|
|
24
|
-
# @return [Hash] `{ auth_method: String|nil, auth_provider: String|nil }
|
|
25
|
-
|
|
26
|
-
|
|
32
|
+
# @return [Hash] `{ auth_method: String|nil, auth_provider: String|nil }`,
|
|
33
|
+
# plus `authenticated_at: Time` when one is given
|
|
34
|
+
def build(auth_method, provider = nil, authenticated_at: nil)
|
|
35
|
+
lineage = { auth_method: auth_method&.to_s.presence, auth_provider: provider&.to_s.presence }
|
|
36
|
+
lineage[:authenticated_at] = authenticated_at if authenticated_at
|
|
37
|
+
lineage
|
|
27
38
|
end
|
|
28
39
|
|
|
29
40
|
def empty
|
|
@@ -33,24 +44,33 @@ module StandardId
|
|
|
33
44
|
# Lineage stored on a session or authorization code `metadata` hash.
|
|
34
45
|
def from_metadata(metadata)
|
|
35
46
|
metadata = metadata.is_a?(Hash) ? metadata.stringify_keys : {}
|
|
36
|
-
build(metadata[METHOD_KEY], metadata[PROVIDER_KEY])
|
|
47
|
+
build(metadata[METHOD_KEY], metadata[PROVIDER_KEY], authenticated_at: parse_time(metadata[AUTHENTICATED_AT_KEY]))
|
|
37
48
|
end
|
|
38
49
|
|
|
50
|
+
# A session's time is its own `authenticated_at` column, not its metadata.
|
|
39
51
|
def from_session(session)
|
|
40
52
|
return empty unless session.respond_to?(:metadata)
|
|
41
53
|
|
|
42
|
-
from_metadata(session.metadata)
|
|
54
|
+
lineage = from_metadata(session.metadata).except(:authenticated_at)
|
|
55
|
+
authenticated_at = session.authenticated_at if session.respond_to?(:authenticated_at)
|
|
56
|
+
lineage[:authenticated_at] = authenticated_at if authenticated_at
|
|
57
|
+
lineage
|
|
43
58
|
end
|
|
44
59
|
|
|
45
60
|
def from_refresh_token(record)
|
|
46
61
|
return empty unless record && refresh_token_columns?
|
|
47
62
|
|
|
48
|
-
|
|
63
|
+
authenticated_at = record.authenticated_at if record.respond_to?(:authenticated_at)
|
|
64
|
+
build(record.auth_method, record.auth_provider, authenticated_at: authenticated_at)
|
|
49
65
|
end
|
|
50
66
|
|
|
51
67
|
# Merge into a `metadata` hash. Nothing is added for an empty lineage.
|
|
52
68
|
def to_metadata(lineage)
|
|
53
|
-
{
|
|
69
|
+
{
|
|
70
|
+
METHOD_KEY => lineage[:auth_method],
|
|
71
|
+
PROVIDER_KEY => lineage[:auth_provider],
|
|
72
|
+
AUTHENTICATED_AT_KEY => lineage[:authenticated_at]&.utc&.iso8601(6)
|
|
73
|
+
}.compact
|
|
54
74
|
end
|
|
55
75
|
|
|
56
76
|
# Attributes for a new RefreshToken row; empty until the host has run the
|
|
@@ -58,7 +78,9 @@ module StandardId
|
|
|
58
78
|
def refresh_token_attributes(lineage)
|
|
59
79
|
return {} unless refresh_token_columns?
|
|
60
80
|
|
|
61
|
-
{ auth_method: lineage[:auth_method], auth_provider: lineage[:auth_provider] }
|
|
81
|
+
attributes = { auth_method: lineage[:auth_method], auth_provider: lineage[:auth_provider] }
|
|
82
|
+
attributes[:authenticated_at] = lineage[:authenticated_at] if StandardId::StepUp.refresh_token_column?
|
|
83
|
+
attributes
|
|
62
84
|
end
|
|
63
85
|
|
|
64
86
|
# The policy's view: missing method → :unspecified.
|
|
@@ -66,6 +88,15 @@ module StandardId
|
|
|
66
88
|
{ auth_method: (lineage[:auth_method].presence || :unspecified).to_sym, provider: lineage[:auth_provider] }
|
|
67
89
|
end
|
|
68
90
|
|
|
91
|
+
def parse_time(value)
|
|
92
|
+
return value if value.is_a?(Time) || value.is_a?(ActiveSupport::TimeWithZone)
|
|
93
|
+
return nil if value.blank?
|
|
94
|
+
|
|
95
|
+
Time.iso8601(value.to_s)
|
|
96
|
+
rescue ArgumentError
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
|
|
69
100
|
def refresh_token_columns?
|
|
70
101
|
StandardId::RefreshToken.column_names.include?("auth_method")
|
|
71
102
|
rescue ActiveRecord::ActiveRecordError
|