standard_id 0.59.0 → 0.61.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 +110 -0
- data/README.md +121 -8
- data/app/controllers/concerns/standard_id/social_authentication.rb +5 -4
- data/app/controllers/concerns/standard_id/web_authentication.rb +2 -0
- data/app/forms/standard_id/web/reset_password_confirm_form.rb +2 -1
- data/app/mailers/standard_id/account_notice_mailer.rb +11 -0
- data/app/models/standard_id/authorization_code.rb +6 -3
- data/app/models/standard_id/client_application.rb +7 -6
- data/app/models/standard_id/code_challenge.rb +69 -1
- data/app/models/standard_id/password_credential.rb +141 -0
- data/app/models/standard_id/session.rb +22 -12
- data/app/views/standard_id/account_notice_mailer/password_changed.html.erb +22 -0
- data/app/views/standard_id/account_notice_mailer/password_changed.text.erb +10 -0
- data/config/locales/en.yml +7 -0
- data/db/migrate/20261122000000_add_code_digest_to_standard_id_code_challenges.rb +24 -0
- data/lib/generators/standard_id/install/templates/standard_id_reference.rb +20 -0
- data/lib/standard_id/config/callable_validator.rb +26 -3
- data/lib/standard_id/config/schema.rb +56 -0
- data/lib/standard_id/email_password/authentication.rb +8 -2
- data/lib/standard_id/email_password/password_reset.rb +2 -1
- data/lib/standard_id/email_password/verification.rb +5 -1
- data/lib/standard_id/engine.rb +1 -0
- data/lib/standard_id/error_reporting.rb +38 -0
- data/lib/standard_id/events/definitions.rb +5 -0
- data/lib/standard_id/events/subscribers/password_changed_notice_subscriber.rb +42 -0
- data/lib/standard_id/events.rb +47 -0
- data/lib/standard_id/health_contributions.rb +27 -0
- data/lib/standard_id/http_client.rb +39 -6
- data/lib/standard_id/keyed_digest.rb +65 -0
- data/lib/standard_id/oauth/password_flow.rb +1 -0
- data/lib/standard_id/passwordless/verification_service.rb +9 -7
- data/lib/standard_id/testing/otp_helpers.rb +58 -0
- data/lib/standard_id/testing.rb +1 -0
- data/lib/standard_id/version.rb +1 -1
- data/lib/standard_id.rb +4 -0
- metadata +11 -3
|
@@ -16,10 +16,151 @@ module StandardId
|
|
|
16
16
|
validates :login, presence: true, uniqueness: true
|
|
17
17
|
validates :password, confirmation: true, if: :validate_password?
|
|
18
18
|
|
|
19
|
+
# Why the next password change happens, published as
|
|
20
|
+
# CREDENTIAL_PASSWORD_CHANGED's +reason+: the engine's reset flows set
|
|
21
|
+
# :password_reset; otherwise it is :changed.
|
|
22
|
+
attr_writer :password_change_reason
|
|
23
|
+
|
|
24
|
+
# CREDENTIAL_PASSWORD_CHANGED, once an update that set a new password
|
|
25
|
+
# commits: the engine's resets and any host code that assigns +password+.
|
|
26
|
+
# Writing password_digest without going through +password=+ (a rehash of
|
|
27
|
+
# the same password) does not count.
|
|
28
|
+
#
|
|
29
|
+
# The digest +password=+ made is kept until a commit, and published only
|
|
30
|
+
# when it is the digest that commit left in the row. Nothing is decided at
|
|
31
|
+
# save time, so a rolled-back savepoint inside the transaction, or a
|
|
32
|
+
# rolled-back save retried later, still publishes when the new password
|
|
33
|
+
# finally commits; a commit that did not write it publishes nothing.
|
|
34
|
+
after_create_commit :forget_assigned_password
|
|
35
|
+
after_update_commit :publish_password_changed, if: :password_change_committed?
|
|
36
|
+
|
|
37
|
+
# has_secure_password's setter, remembering the digest it made so the
|
|
38
|
+
# commit can tell a new password from a direct write of the digest. An
|
|
39
|
+
# empty assignment changes nothing, as has_secure_password ignores it.
|
|
40
|
+
def password=(unencrypted_password)
|
|
41
|
+
super
|
|
42
|
+
@assigned_password_digest = (password_digest if unencrypted_password.present?)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The bcrypt cost has_secure_password hashes a new password at, worked out
|
|
46
|
+
# the way ActiveModel::SecurePassword does.
|
|
47
|
+
def self.password_cost
|
|
48
|
+
ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Re-hashes +password+ when the stored digest was made at a bcrypt cost
|
|
52
|
+
# other than .password_cost, so raising BCrypt::Engine.cost reaches
|
|
53
|
+
# existing passwords as their owners sign in instead of only new ones.
|
|
54
|
+
# Every password login calls it once +password+ has authenticated; turn it
|
|
55
|
+
# off with c.password.rehash_on_login = false.
|
|
56
|
+
#
|
|
57
|
+
# +authenticated_digest+ is the digest +password+ was checked against. The
|
|
58
|
+
# write is one UPDATE conditional on the row still holding it, so a reset
|
|
59
|
+
# that got there first wins and nothing changes. A caller that re-checks
|
|
60
|
+
# the digest under a lock (EmailPassword::Authentication, Verification)
|
|
61
|
+
# must call this after that check, inside the lock, or its own check would
|
|
62
|
+
# see the new digest. Nothing is reloaded, so the caller's preloaded
|
|
63
|
+
# associations survive.
|
|
64
|
+
#
|
|
65
|
+
# The digest is the fingerprint of the :remember_me and :password_reset
|
|
66
|
+
# tokens, so a rehash invalidates the user's outstanding remember-me
|
|
67
|
+
# cookies and reset links: mint any new one after calling this.
|
|
68
|
+
#
|
|
69
|
+
# Never raises: a failed rehash keeps the old digest and the login goes on.
|
|
70
|
+
#
|
|
71
|
+
# @return [Boolean] whether the digest was replaced
|
|
72
|
+
def rehash_password(password, authenticated_digest = password_digest)
|
|
73
|
+
return false unless StandardId.config.password.rehash_on_login && password_rehash_needed?
|
|
74
|
+
|
|
75
|
+
previous_cost = BCrypt::Password.new(password_digest).cost
|
|
76
|
+
attributes = { password_digest: BCrypt::Password.create(password, cost: self.class.password_cost), updated_at: Time.current }
|
|
77
|
+
# The savepoint keeps a failed UPDATE from aborting the caller's
|
|
78
|
+
# transaction (PostgreSQL refuses every later statement in it).
|
|
79
|
+
rehashed = self.class.transaction(requires_new: true) do
|
|
80
|
+
self.class.where(id: id, password_digest: authenticated_digest).update_all(attributes) == 1
|
|
81
|
+
end
|
|
82
|
+
return false unless rehashed
|
|
83
|
+
|
|
84
|
+
assign_attributes(attributes)
|
|
85
|
+
clear_attribute_changes(attributes.keys)
|
|
86
|
+
# Held until the caller's transaction commits, so a login that rolls
|
|
87
|
+
# back (a policy refusal inside EmailPassword::Authentication's lock)
|
|
88
|
+
# reports no rehash.
|
|
89
|
+
ActiveRecord.after_all_transactions_commit { publish_password_rehashed(previous_cost) }
|
|
90
|
+
true
|
|
91
|
+
rescue StandardError => e
|
|
92
|
+
report_failure("rehash password credential #{id}", e)
|
|
93
|
+
false
|
|
94
|
+
end
|
|
95
|
+
|
|
19
96
|
private
|
|
20
97
|
|
|
21
98
|
def validate_password?
|
|
22
99
|
password.present? || password_confirmation.present?
|
|
23
100
|
end
|
|
101
|
+
|
|
102
|
+
def password_rehash_needed?
|
|
103
|
+
BCrypt::Password.new(password_digest).cost != self.class.password_cost
|
|
104
|
+
rescue BCrypt::Errors::InvalidHash
|
|
105
|
+
false
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# The event carries the account: load the chain (a no-op where the caller
|
|
109
|
+
# preloaded it) so a subscriber under strict loading can walk it.
|
|
110
|
+
def publish_password_rehashed(previous_cost)
|
|
111
|
+
ActiveRecord::Associations::Preloader.new(records: [self], associations: { credential: { identifier: :account } }).call
|
|
112
|
+
StandardId::Events.publish(
|
|
113
|
+
StandardId::Events::CREDENTIAL_PASSWORD_REHASHED,
|
|
114
|
+
credential: self,
|
|
115
|
+
account: account,
|
|
116
|
+
previous_cost: previous_cost,
|
|
117
|
+
cost: self.class.password_cost
|
|
118
|
+
)
|
|
119
|
+
rescue StandardError => e
|
|
120
|
+
report_failure("publish CREDENTIAL_PASSWORD_REHASHED for password credential #{id}", e)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def report_failure(action, error)
|
|
124
|
+
StandardId::ErrorReporting.report(error, action: action)
|
|
125
|
+
logger = StandardId.logger
|
|
126
|
+
return unless logger.respond_to?(:error)
|
|
127
|
+
|
|
128
|
+
logger.error("[StandardId::PasswordCredential] Failed to #{action}: #{error.class}: #{error.message}")
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def forget_assigned_password
|
|
132
|
+
@assigned_password_digest = nil
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Read from the row rather than the record: after a rollback the record
|
|
136
|
+
# can still hold the assigned digest while the row does not.
|
|
137
|
+
def password_change_committed?
|
|
138
|
+
@assigned_password_digest.present? &&
|
|
139
|
+
self.class.where(id: id).pick(:password_digest) == @assigned_password_digest
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# The change has committed, so a failing subscriber must not raise out of
|
|
143
|
+
# the caller's save; report and carry on, as the reset flows do for
|
|
144
|
+
# CREDENTIAL_PASSWORD_RESET_COMPLETED. The event carries the account: load
|
|
145
|
+
# the chain so a subscriber under strict loading can walk it.
|
|
146
|
+
#
|
|
147
|
+
# +credential_id+ is explicit, as on PASSWORD_VALIDATED: the record keys
|
|
148
|
+
# are left out under events.publish_records = false, and so are their
|
|
149
|
+
# _id/_gid companions when publish_record_identifiers is off too.
|
|
150
|
+
def publish_password_changed
|
|
151
|
+
reason = @password_change_reason || :changed
|
|
152
|
+
forget_assigned_password
|
|
153
|
+
@password_change_reason = nil
|
|
154
|
+
ActiveRecord::Associations::Preloader.new(records: [self], associations: { credential: { identifier: :account } }).call
|
|
155
|
+
StandardId::Events.publish(
|
|
156
|
+
StandardId::Events::CREDENTIAL_PASSWORD_CHANGED,
|
|
157
|
+
credential: self,
|
|
158
|
+
credential_id: id,
|
|
159
|
+
account: credential&.account,
|
|
160
|
+
reason: reason
|
|
161
|
+
)
|
|
162
|
+
rescue StandardError => e
|
|
163
|
+
report_failure("publish CREDENTIAL_PASSWORD_CHANGED for password credential #{id}", e)
|
|
164
|
+
end
|
|
24
165
|
end
|
|
25
166
|
end
|
|
@@ -17,10 +17,20 @@ module StandardId
|
|
|
17
17
|
scope :revoked, -> { where.not(revoked_at: nil) }
|
|
18
18
|
|
|
19
19
|
scope :api_compatible, -> { where(type: ["StandardId::DeviceSession", "StandardId::ServiceSession"]) }
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
20
|
+
# One indexed query for every lookup hash the token may be stored under:
|
|
21
|
+
# the current secret's, plus one per `config.previous_secret_key_bases`
|
|
22
|
+
# entry (see StandardId::KeyedDigest).
|
|
23
|
+
scope :by_token, ->(token) { where(lookup_hash: lookup_hashes_for(token)) }
|
|
24
|
+
|
|
25
|
+
# SHA256 of the token under `secret` — an indexable locator, never the
|
|
26
|
+
# credential (see .authenticate_by_token).
|
|
27
|
+
def self.lookup_hash_for(token, secret: StandardId::KeyedDigest.current_secret)
|
|
28
|
+
Digest::SHA256.hexdigest("#{token}:#{secret}")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.lookup_hashes_for(token)
|
|
32
|
+
StandardId::KeyedDigest.candidates { |secret| lookup_hash_for(token, secret:) }
|
|
33
|
+
end
|
|
24
34
|
|
|
25
35
|
# Authenticate an opaque session token.
|
|
26
36
|
#
|
|
@@ -64,10 +74,8 @@ module StandardId
|
|
|
64
74
|
# with SHA256; this brings session tokens in line with both.
|
|
65
75
|
DIGEST_PREFIX = "standard_id.session.token_digest.v1:".freeze
|
|
66
76
|
|
|
67
|
-
def self.hmac_token_digest(token)
|
|
68
|
-
|
|
69
|
-
"SHA256", Rails.configuration.secret_key_base, "#{DIGEST_PREFIX}#{token}"
|
|
70
|
-
)
|
|
77
|
+
def self.hmac_token_digest(token, secret: StandardId::KeyedDigest.current_secret)
|
|
78
|
+
StandardId::KeyedDigest.hmac_hexdigest("#{DIGEST_PREFIX}#{token}", secret:)
|
|
71
79
|
end
|
|
72
80
|
|
|
73
81
|
# Timing-safe verification of `token` against this session's stored digest.
|
|
@@ -79,7 +87,8 @@ module StandardId
|
|
|
79
87
|
# way a config change never strands existing sessions, and no rewrite of
|
|
80
88
|
# stored digests is needed. Both branches end in a constant-time compare, so
|
|
81
89
|
# the response time carries no information about how much of the digest
|
|
82
|
-
# matched.
|
|
90
|
+
# matched. An HMAC digest is accepted under the current secret or any of
|
|
91
|
+
# `config.previous_secret_key_bases`; BCrypt digests are not keyed.
|
|
83
92
|
#
|
|
84
93
|
# @return [Boolean] false for a blank or malformed digest — never raises.
|
|
85
94
|
def authenticate_token(token)
|
|
@@ -92,8 +101,9 @@ module StandardId
|
|
|
92
101
|
BCrypt::Engine.hash_secret(token, stored.salt)
|
|
93
102
|
)
|
|
94
103
|
else
|
|
95
|
-
|
|
96
|
-
token_digest,
|
|
104
|
+
StandardId::KeyedDigest.secure_match?(
|
|
105
|
+
token_digest,
|
|
106
|
+
StandardId::KeyedDigest.candidates { |secret| self.class.hmac_token_digest(token, secret:) }
|
|
97
107
|
)
|
|
98
108
|
end
|
|
99
109
|
rescue BCrypt::Errors::InvalidHash, BCrypt::Errors::InvalidSalt
|
|
@@ -265,7 +275,7 @@ module StandardId
|
|
|
265
275
|
end
|
|
266
276
|
|
|
267
277
|
def generate_lookup_hash
|
|
268
|
-
self.lookup_hash =
|
|
278
|
+
self.lookup_hash = self.class.lookup_hash_for(token)
|
|
269
279
|
end
|
|
270
280
|
|
|
271
281
|
# Revoke any still-active refresh tokens before the session row is deleted,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<style>
|
|
6
|
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background-color: #f5f5f5; margin: 0; padding: 0; }
|
|
7
|
+
.container { max-width: 480px; margin: 40px auto; background-color: #ffffff; border-radius: 8px; padding: 40px; }
|
|
8
|
+
.footer { margin-top: 32px; font-size: 13px; color: #888888; text-align: center; }
|
|
9
|
+
</style>
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<div class="container">
|
|
13
|
+
<p><%= notice_copy(:greeting) %></p>
|
|
14
|
+
<p><%= notice_copy(:intro) %></p>
|
|
15
|
+
<p><%= notice_copy(:action) %></p>
|
|
16
|
+
<p><%= notice_copy(:warning) %></p>
|
|
17
|
+
<div class="footer">
|
|
18
|
+
<p><%= notice_copy(:footer) %></p>
|
|
19
|
+
</div>
|
|
20
|
+
</div>
|
|
21
|
+
</body>
|
|
22
|
+
</html>
|
data/config/locales/en.yml
CHANGED
|
@@ -28,3 +28,10 @@ en:
|
|
|
28
28
|
action: "If that was you, sign in the way you did before, or reset your password if you have forgotten it."
|
|
29
29
|
ignore: "If it wasn't you, you can safely ignore this email. Your account has not changed."
|
|
30
30
|
footer: "This is an automated message. Please do not reply."
|
|
31
|
+
password_changed:
|
|
32
|
+
subject: "Your password was changed"
|
|
33
|
+
greeting: "Hi,"
|
|
34
|
+
intro: "The password for your account was just changed."
|
|
35
|
+
action: "If you made this change, you don't need to do anything."
|
|
36
|
+
warning: "If you didn't, reset your password straight away and check your account for anything you don't recognise."
|
|
37
|
+
footer: "This is an automated message. Please do not reply."
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Passwordless / OTP codes were stored in plaintext in `code`, so anyone who
|
|
2
|
+
# could read the table could sign in with any active code. From this migration
|
|
3
|
+
# on, StandardId stores only `code_digest`, an HMAC-SHA256 of the code keyed on
|
|
4
|
+
# secret_key_base (StandardId::CodeChallenge.digest_code), and leaves `code`
|
|
5
|
+
# NULL on new rows.
|
|
6
|
+
#
|
|
7
|
+
# Both steps are metadata-only on PostgreSQL: a nullable string column with no
|
|
8
|
+
# default and no index (a challenge is found by realm/channel/target, then its
|
|
9
|
+
# digest is compared in constant time), and dropping a NOT NULL constraint.
|
|
10
|
+
#
|
|
11
|
+
# `code` is kept, nullable, for one transition: challenges issued before the
|
|
12
|
+
# deploy still carry a plaintext code and are verified against it until they
|
|
13
|
+
# expire (code_ttl, 10 minutes by default) and CleanupExpiredCodeChallengesJob
|
|
14
|
+
# deletes them. A later release drops the column; see docs/MIGRATION_GUIDE.md.
|
|
15
|
+
# Until this has run, the gem keeps writing the plaintext column as before.
|
|
16
|
+
#
|
|
17
|
+
# Rolling back restores NOT NULL on `code`, which fails while any digest-only
|
|
18
|
+
# row exists: delete those first (they are at most code_ttl old).
|
|
19
|
+
class AddCodeDigestToStandardIdCodeChallenges < ActiveRecord::Migration[8.0]
|
|
20
|
+
def change
|
|
21
|
+
add_column :standard_id_code_challenges, :code_digest, :string
|
|
22
|
+
change_column_null :standard_id_code_challenges, :code, true
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -104,6 +104,15 @@ StandardId.configure do |c|
|
|
|
104
104
|
# Default: Rails.logger
|
|
105
105
|
# c.logger = Rails.logger
|
|
106
106
|
|
|
107
|
+
# Old secret_key_base values (newest first) still accepted for StandardId's
|
|
108
|
+
# keyed digests — session tokens, OTP codes, authorization codes, registration
|
|
109
|
+
# access tokens — so rotating the secret does not sign everyone out. New
|
|
110
|
+
# digests always use the current secret. Keep an entry for the longest
|
|
111
|
+
# session lifetime, and rotate Rails' cookies and message verifiers too
|
|
112
|
+
# (README "Rotating secret_key_base").
|
|
113
|
+
# Default: []
|
|
114
|
+
# c.previous_secret_key_bases = [Rails.application.credentials.old_secret_key_base]
|
|
115
|
+
|
|
107
116
|
# Layout used by built-in ERB web views. Ignored when use_inertia is true.
|
|
108
117
|
# Default: nil (StandardId's packaged layout)
|
|
109
118
|
# c.web_layout = "application"
|
|
@@ -588,6 +597,17 @@ StandardId.configure do |c|
|
|
|
588
597
|
# c.sentry_context = ->(account, _session) {
|
|
589
598
|
# { segment: account.primary_profile&.type }
|
|
590
599
|
# }
|
|
600
|
+
|
|
601
|
+
# Wraps every outbound provider request (token exchange, userinfo, JWKS),
|
|
602
|
+
# e.g. in a circuit breaker. Receives provider: (:google, :apple, ...) and
|
|
603
|
+
# the request as a block; must call the block and return its result.
|
|
604
|
+
# Default: nil (the request runs directly)
|
|
605
|
+
# c.network_wrapper = ->(provider:, &b) { StandardCircuit.run(:"oauth_#{provider}", &b) }
|
|
606
|
+
|
|
607
|
+
# Receives errors StandardId handles and carries on from (a subscriber that
|
|
608
|
+
# raised after its change committed). Receives (error, context).
|
|
609
|
+
# Default: nil (Rails.error.report(error, handled: true, source: "standard_id"))
|
|
610
|
+
# c.error_reporter = ->(error, context) { Sentry.capture_exception(error, extra: context) }
|
|
591
611
|
end
|
|
592
612
|
|
|
593
613
|
# Notes:
|
|
@@ -27,6 +27,13 @@ module StandardId
|
|
|
27
27
|
CALLABLE_FIELDS = {
|
|
28
28
|
"profile_resolver" => { signature: "(account, profile_type)", arity: 2, kind: :positional },
|
|
29
29
|
"sentry_context" => { signature: "(account, session)", arity: 2, kind: :positional },
|
|
30
|
+
"error_reporter" => { signature: "(error, context)", arity: 2, kind: :positional },
|
|
31
|
+
"network_wrapper" => {
|
|
32
|
+
signature: "(provider:, &block)",
|
|
33
|
+
kind: :keyword,
|
|
34
|
+
keywords: %i[provider],
|
|
35
|
+
block: true
|
|
36
|
+
},
|
|
30
37
|
"after_account_created" => { signature: "(account, request, context)", arity: 3, kind: :positional },
|
|
31
38
|
"before_sign_in" => { signature: "(account, request, context)", arity: 3, kind: :positional },
|
|
32
39
|
"after_sign_in" => { signature: "(account, request, context)", arity: 3, kind: :positional },
|
|
@@ -142,6 +149,21 @@ module StandardId
|
|
|
142
149
|
when :positional then validate_positional!(path, callable, spec)
|
|
143
150
|
when :keyword then validate_keyword!(path, callable, spec[:keywords], spec[:signature])
|
|
144
151
|
end
|
|
152
|
+
validate_block_param!(path, callable, spec[:signature]) if spec[:block]
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# A wrapper callable (network_wrapper) runs the work it is handed as a
|
|
156
|
+
# block. A Proc can only receive that block through an explicit `&`
|
|
157
|
+
# parameter; without one the block is dropped and the work never runs.
|
|
158
|
+
# (A method or `#call` object can `yield` without declaring it, so only
|
|
159
|
+
# Procs are checked.)
|
|
160
|
+
def validate_block_param!(path, callable, signature)
|
|
161
|
+
return unless callable.is_a?(Proc)
|
|
162
|
+
return if callable.parameters.any? { |type, _| type == :block }
|
|
163
|
+
|
|
164
|
+
raise StandardId::ConfigurationError,
|
|
165
|
+
"StandardId config: `#{path}` must take the wrapped work as a block " \
|
|
166
|
+
"(e.g. `->(provider:, &block) { block.call }`). Expected signature #{signature}."
|
|
145
167
|
end
|
|
146
168
|
|
|
147
169
|
def validate_positional!(path, callable, spec)
|
|
@@ -205,10 +227,11 @@ module StandardId
|
|
|
205
227
|
|
|
206
228
|
# EmailPassword.call_hook passes only the keywords a hook declares, so a
|
|
207
229
|
# lambda with a required positional argument (`->(request) { }`) would
|
|
208
|
-
# raise on its first call. Limited to the email_password hooks
|
|
209
|
-
# are new: older keyword hooks keep accepting
|
|
230
|
+
# raise on its first call. Limited to the email_password hooks and
|
|
231
|
+
# network_wrapper, which are new: older keyword hooks keep accepting
|
|
232
|
+
# what they always did.
|
|
210
233
|
def validate_no_required_positional!(path, callable, params, signature)
|
|
211
|
-
return unless path.start_with?("email_password.") && lambda_callable?(callable)
|
|
234
|
+
return unless (path.start_with?("email_password.") || path == "network_wrapper") && lambda_callable?(callable)
|
|
212
235
|
return unless params.any? { |type, _| type == :req }
|
|
213
236
|
|
|
214
237
|
raise StandardId::ConfigurationError,
|
|
@@ -86,6 +86,20 @@ StandardId::ConfigSchema.define do
|
|
|
86
86
|
# deliberately superseded or deferred.
|
|
87
87
|
field :ignored_migrations, type: :array, default: -> { [] }
|
|
88
88
|
|
|
89
|
+
# Previous `secret_key_base` values, newest first, still ACCEPTED when
|
|
90
|
+
# looking up and verifying the engine's keyed digests: session tokens
|
|
91
|
+
# (lookup_hash and the HMAC token_digest), passwordless code digests,
|
|
92
|
+
# authorization codes and RFC 7592 registration access tokens. New digests
|
|
93
|
+
# are always written under the current secret. See StandardId::KeyedDigest.
|
|
94
|
+
#
|
|
95
|
+
# Empty (default) means rotating `secret_key_base` invalidates all of them
|
|
96
|
+
# at once, which is what it always did. To rotate without signing everyone
|
|
97
|
+
# out, list the old secret here for at least the longest session lifetime,
|
|
98
|
+
# alongside Rails' own rotations for its cookies and message verifiers
|
|
99
|
+
# (README "Rotating secret_key_base"). Each extra entry costs one more
|
|
100
|
+
# candidate per lookup and one more HMAC per verification.
|
|
101
|
+
field :previous_secret_key_bases, type: :array, default: -> { [] }
|
|
102
|
+
|
|
89
103
|
# Scope-aware authentication: maps scope names to profile-based access config.
|
|
90
104
|
# Each scope is a hash with keys: :profile_types (Array<String>), :after_sign_in_path,
|
|
91
105
|
# :no_profile_message, :label, :allow_registration, :authorizer.
|
|
@@ -148,6 +162,32 @@ StandardId::ConfigSchema.define do
|
|
|
148
162
|
# Raise StandardId::LoginMethodDenied.new("message") to refuse with your own.
|
|
149
163
|
# nil (the default) allows every method, i.e. no behaviour change.
|
|
150
164
|
field :login_method_policy, type: :any, default: nil
|
|
165
|
+
|
|
166
|
+
# network_wrapper: wraps every outbound HTTP connection StandardId::HttpClient
|
|
167
|
+
# makes (social-provider token, userinfo and JWKS calls), e.g. in a circuit
|
|
168
|
+
# breaker or a tracing span.
|
|
169
|
+
# Receives: provider: (Symbol — :google, :apple, ...; :standard_id when the
|
|
170
|
+
# caller names none) and a block that performs the request.
|
|
171
|
+
# Must call the block and return its result. Whatever the block raises
|
|
172
|
+
# (timeouts, refused connections) raises inside the wrapper; whatever the
|
|
173
|
+
# wrapper raises reaches the caller unchanged.
|
|
174
|
+
# nil (the default) runs the block directly.
|
|
175
|
+
#
|
|
176
|
+
# c.network_wrapper = ->(provider:, &b) { StandardCircuit.run(:"oauth_#{provider}", &b) }
|
|
177
|
+
field :network_wrapper, type: :any, default: nil
|
|
178
|
+
|
|
179
|
+
# error_reporter: receives errors StandardId handles and carries on from —
|
|
180
|
+
# today, an event subscriber that raised after the change it reports had
|
|
181
|
+
# already committed (a completed password reset, a password rehash, a
|
|
182
|
+
# social link).
|
|
183
|
+
# Receives: (error, context), where context is a Hash with at least
|
|
184
|
+
# source: "standard_id" and handled: true, and may also name
|
|
185
|
+
# what failed (event:, action:).
|
|
186
|
+
# nil (the default) reports to Rails.error, as StandardId always has:
|
|
187
|
+
# `Rails.error.report(error, handled: true, source: "standard_id")`.
|
|
188
|
+
#
|
|
189
|
+
# c.error_reporter = ->(error, context) { Sentry.capture_exception(error, extra: context) }
|
|
190
|
+
field :error_reporter, type: :any, default: nil
|
|
151
191
|
end
|
|
152
192
|
|
|
153
193
|
scope :events do
|
|
@@ -306,6 +346,22 @@ StandardId::ConfigSchema.define do
|
|
|
306
346
|
field :require_special_chars, type: :boolean, default: true
|
|
307
347
|
field :require_uppercase, type: :boolean, default: true
|
|
308
348
|
field :require_numbers, type: :boolean, default: true
|
|
349
|
+
# Re-hash a password on a successful login when its stored digest was
|
|
350
|
+
# made at a bcrypt cost other than the one has_secure_password uses now
|
|
351
|
+
# (BCrypt::Engine.cost), so raising the cost reaches existing passwords.
|
|
352
|
+
# A no-op until the cost changes. A rehash replaces the digest, which
|
|
353
|
+
# signs that user out of remember-me cookies and voids their reset links.
|
|
354
|
+
# See StandardId::PasswordCredential#rehash_password.
|
|
355
|
+
field :rehash_on_login, type: :boolean, default: true
|
|
356
|
+
# Who tells the owner, by email, that their password was changed or reset
|
|
357
|
+
# (CREDENTIAL_PASSWORD_CHANGED):
|
|
358
|
+
# :custom — (default) the engine sends nothing; subscribe to the event
|
|
359
|
+
# to send your own
|
|
360
|
+
# :built_in — the engine sends AccountNoticeMailer#password_changed to
|
|
361
|
+
# the credential's e-mail identifier
|
|
362
|
+
# Off by default so an upgrade does not start sending mail, nor a second
|
|
363
|
+
# notice next to one a host already sends on reset_completed.
|
|
364
|
+
field :changed_notice_delivery, type: :symbol, default: :custom
|
|
309
365
|
end
|
|
310
366
|
|
|
311
367
|
scope :reset_password do
|
|
@@ -12,7 +12,9 @@ module StandardId
|
|
|
12
12
|
# locked, after re-checking that the password has not changed since it was
|
|
13
13
|
# verified. Mint tokens inside it: PasswordReset.complete! takes the same
|
|
14
14
|
# lock to change the password and revoke access, so a login racing a reset
|
|
15
|
-
# either finishes first (and its tokens are revoked) or fails.
|
|
15
|
+
# either finishes first (and its tokens are revoked) or fails. The password
|
|
16
|
+
# is rehashed (PasswordCredential#rehash_password) only after that
|
|
17
|
+
# re-check passes, so the re-check never sees its own rehash.
|
|
16
18
|
module Authentication
|
|
17
19
|
class << self
|
|
18
20
|
# @return [Account]
|
|
@@ -45,10 +47,14 @@ module StandardId
|
|
|
45
47
|
)
|
|
46
48
|
|
|
47
49
|
raise StandardId::EmailPasswordError.new(:unverified) unless identifier.verified?
|
|
48
|
-
|
|
50
|
+
unless block_given?
|
|
51
|
+
password_credential.rehash_password(password, verified_digest)
|
|
52
|
+
return account
|
|
53
|
+
end
|
|
49
54
|
|
|
50
55
|
password_credential.with_lock do
|
|
51
56
|
failed!(email) unless password_credential.password_digest == verified_digest
|
|
57
|
+
password_credential.rehash_password(password, verified_digest)
|
|
52
58
|
yield account
|
|
53
59
|
end
|
|
54
60
|
end
|
|
@@ -76,6 +76,7 @@ module StandardId
|
|
|
76
76
|
raise TokenAlreadyUsed
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
+
password_credential.password_change_reason = :password_reset
|
|
79
80
|
password_credential.update!(password: password)
|
|
80
81
|
api_signup = !identifier.verified? && EmailPassword.api_signup?(identifier)
|
|
81
82
|
EmailPassword.complete_verification!(
|
|
@@ -145,7 +146,7 @@ module StandardId
|
|
|
145
146
|
account: account
|
|
146
147
|
)
|
|
147
148
|
rescue StandardError => e
|
|
148
|
-
|
|
149
|
+
StandardId::ErrorReporting.report(e, event: StandardId::Events.namespaced_event_name(StandardId::Events::CREDENTIAL_PASSWORD_RESET_COMPLETED))
|
|
149
150
|
logger = StandardId.logger
|
|
150
151
|
return unless logger.respond_to?(:error)
|
|
151
152
|
|
|
@@ -69,10 +69,14 @@ module StandardId
|
|
|
69
69
|
)
|
|
70
70
|
identifier.account
|
|
71
71
|
end
|
|
72
|
-
|
|
72
|
+
unless block_given?
|
|
73
|
+
password_credential.rehash_password(password, verified_digest)
|
|
74
|
+
return account
|
|
75
|
+
end
|
|
73
76
|
|
|
74
77
|
password_credential.with_lock do
|
|
75
78
|
password_unchanged!(password_credential, verified_digest)
|
|
79
|
+
password_credential.rehash_password(password, verified_digest)
|
|
76
80
|
yield account
|
|
77
81
|
end
|
|
78
82
|
end
|
data/lib/standard_id/engine.rb
CHANGED
|
@@ -80,6 +80,7 @@ module StandardId
|
|
|
80
80
|
StandardId::Events::Subscribers::AccountLockingSubscriber.attach
|
|
81
81
|
StandardId::Events::Subscribers::PasswordlessDeliverySubscriber.attach
|
|
82
82
|
StandardId::Events::Subscribers::PasswordResetDeliverySubscriber.attach
|
|
83
|
+
StandardId::Events::Subscribers::PasswordChangedNoticeSubscriber.attach
|
|
83
84
|
|
|
84
85
|
StandardId::Engine.warn_about_issuer_config!
|
|
85
86
|
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module StandardId
|
|
2
|
+
# Reports an error StandardId has handled and is carrying on from (see
|
|
3
|
+
# `config.error_reporter`). Every such report goes through here, so a host
|
|
4
|
+
# that routes them elsewhere catches all of them.
|
|
5
|
+
module ErrorReporting
|
|
6
|
+
SOURCE = "standard_id".freeze
|
|
7
|
+
|
|
8
|
+
module_function
|
|
9
|
+
|
|
10
|
+
# Without a configured reporter this is exactly the call the engine has
|
|
11
|
+
# always made, `Rails.error.report(error, handled: true, source:
|
|
12
|
+
# "standard_id")`; `context` goes only to a configured reporter.
|
|
13
|
+
#
|
|
14
|
+
# Every caller is a rescue that carries on, so a configured reporter that
|
|
15
|
+
# raises is logged and the error goes to the default report instead.
|
|
16
|
+
#
|
|
17
|
+
# @param error [Exception]
|
|
18
|
+
# @param context [Hash] extra detail for a configured reporter, e.g. the
|
|
19
|
+
# event whose subscriber raised
|
|
20
|
+
# @return [void]
|
|
21
|
+
def report(error, context = {})
|
|
22
|
+
reporter = StandardId.config.error_reporter
|
|
23
|
+
return report_to_rails(error) unless reporter.respond_to?(:call)
|
|
24
|
+
|
|
25
|
+
begin
|
|
26
|
+
reporter.call(error, context.merge(source: SOURCE, handled: true))
|
|
27
|
+
rescue StandardError => e
|
|
28
|
+
Rails.logger&.error("[StandardId] error_reporter raised #{e.class}: #{e.message}; reporting #{error.class} to Rails.error instead")
|
|
29
|
+
report_to_rails(error)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def report_to_rails(error)
|
|
34
|
+
Rails.error.report(error, handled: true, source: SOURCE)
|
|
35
|
+
end
|
|
36
|
+
private_class_method :report_to_rails
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -70,6 +70,9 @@ module StandardId
|
|
|
70
70
|
CREDENTIAL_PASSWORD_RESET_INITIATED = "credential.password.reset_initiated"
|
|
71
71
|
CREDENTIAL_PASSWORD_RESET_COMPLETED = "credential.password.reset_completed"
|
|
72
72
|
CREDENTIAL_PASSWORD_CHANGED = "credential.password.changed"
|
|
73
|
+
# A login re-hashed the password at the current bcrypt cost
|
|
74
|
+
# (PasswordCredential#rehash_password). The password itself is unchanged.
|
|
75
|
+
CREDENTIAL_PASSWORD_REHASHED = "credential.password.rehashed"
|
|
73
76
|
CREDENTIAL_CLIENT_SECRET_CREATED = "credential.client_secret.created"
|
|
74
77
|
CREDENTIAL_CLIENT_SECRET_ROTATED = "credential.client_secret.rotated"
|
|
75
78
|
CREDENTIAL_CLIENT_SECRET_REVOKED = "credential.client_secret.revoked"
|
|
@@ -156,6 +159,7 @@ module StandardId
|
|
|
156
159
|
CREDENTIAL_PASSWORD_RESET_INITIATED,
|
|
157
160
|
CREDENTIAL_PASSWORD_RESET_COMPLETED,
|
|
158
161
|
CREDENTIAL_PASSWORD_CHANGED,
|
|
162
|
+
CREDENTIAL_PASSWORD_REHASHED,
|
|
159
163
|
CREDENTIAL_CLIENT_SECRET_CREATED,
|
|
160
164
|
CREDENTIAL_CLIENT_SECRET_ROTATED,
|
|
161
165
|
CREDENTIAL_CLIENT_SECRET_REVOKED
|
|
@@ -201,6 +205,7 @@ module StandardId
|
|
|
201
205
|
CREDENTIAL_PASSWORD_RESET_INITIATED,
|
|
202
206
|
CREDENTIAL_PASSWORD_RESET_COMPLETED,
|
|
203
207
|
CREDENTIAL_PASSWORD_CHANGED,
|
|
208
|
+
CREDENTIAL_PASSWORD_REHASHED,
|
|
204
209
|
CREDENTIAL_CLIENT_SECRET_CREATED,
|
|
205
210
|
CREDENTIAL_CLIENT_SECRET_ROTATED,
|
|
206
211
|
CREDENTIAL_CLIENT_SECRET_REVOKED,
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module StandardId
|
|
2
|
+
module Events
|
|
3
|
+
module Subscribers
|
|
4
|
+
# Emails the owner when their password is changed or reset, while
|
|
5
|
+
# c.password.changed_notice_delivery is :built_in. Sent to the
|
|
6
|
+
# credential's e-mail identifier; a credential behind a username or phone
|
|
7
|
+
# identifier gets nothing. Under events.publish_records = false the
|
|
8
|
+
# payload carries no records, so the credential is loaded from
|
|
9
|
+
# +credential_id+.
|
|
10
|
+
class PasswordChangedNoticeSubscriber < Base
|
|
11
|
+
subscribe_to StandardId::Events::CREDENTIAL_PASSWORD_CHANGED
|
|
12
|
+
|
|
13
|
+
def call(event)
|
|
14
|
+
return unless built_in_delivery?
|
|
15
|
+
|
|
16
|
+
identifier = password_credential(event)&.credential&.identifier
|
|
17
|
+
return unless identifier.is_a?(StandardId::EmailIdentifier) && identifier.value.present?
|
|
18
|
+
|
|
19
|
+
StandardId::AccountNoticeMailer.with(email: identifier.value).password_changed.deliver_later
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def handle_error(error, event)
|
|
23
|
+
StandardId.logger.error(
|
|
24
|
+
"[StandardId::PasswordChangedNotice] Failed to deliver password changed notice " \
|
|
25
|
+
"for password credential #{event[:credential_id]}: #{error.message}"
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def password_credential(event)
|
|
32
|
+
event[:credential] ||
|
|
33
|
+
(event[:credential_id] && StandardId::PasswordCredential.includes(credential: :identifier).find_by(id: event[:credential_id]))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def built_in_delivery?
|
|
37
|
+
StandardId.config.password.changed_notice_delivery == :built_in
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|