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
|
@@ -89,15 +89,22 @@ StandardId::ConfigSchema.define do
|
|
|
89
89
|
# Previous `secret_key_base` values, newest first, still ACCEPTED when
|
|
90
90
|
# looking up and verifying the engine's keyed digests: session tokens
|
|
91
91
|
# (lookup_hash and the HMAC token_digest), passwordless code digests,
|
|
92
|
-
# authorization codes and RFC 7592 registration access tokens.
|
|
93
|
-
#
|
|
92
|
+
# authorization codes and RFC 7592 registration access tokens. JWTs signed
|
|
93
|
+
# with HS256/384/512 (keyed on `secret_key_base`) also still verify under
|
|
94
|
+
# these; asymmetric keys rotate through `oauth.previous_signing_keys`. New
|
|
95
|
+
# digests and tokens are always written under the current secret. See
|
|
96
|
+
# StandardId::KeyedDigest and JwtService.symmetric_verification_keys.
|
|
94
97
|
#
|
|
95
98
|
# Empty (default) means rotating `secret_key_base` invalidates all of them
|
|
96
99
|
# at once, which is what it always did. To rotate without signing everyone
|
|
97
|
-
# out, list the old secret here
|
|
98
|
-
#
|
|
100
|
+
# out, list the old secret here. A session or registration access token
|
|
101
|
+
# that authenticates under it is re-keyed to the current secret on that
|
|
102
|
+
# use, so the entry is needed only until the credentials you want to keep
|
|
103
|
+
# have been used once (at most the longest session lifetime). Rotate Rails'
|
|
104
|
+
# own cookies and message verifiers alongside it
|
|
99
105
|
# (README "Rotating secret_key_base"). Each extra entry costs one more
|
|
100
|
-
# candidate per lookup and one more HMAC per verification
|
|
106
|
+
# candidate per lookup and one more HMAC per verification (for a JWT, only
|
|
107
|
+
# when it fails to verify under the newer secrets).
|
|
101
108
|
field :previous_secret_key_bases, type: :array, default: -> { [] }
|
|
102
109
|
|
|
103
110
|
# Scope-aware authentication: maps scope names to profile-based access config.
|
|
@@ -353,6 +360,34 @@ StandardId::ConfigSchema.define do
|
|
|
353
360
|
# signs that user out of remember-me cookies and voids their reset links.
|
|
354
361
|
# See StandardId::PasswordCredential#rehash_password.
|
|
355
362
|
field :rehash_on_login, type: :boolean, default: true
|
|
363
|
+
# A secret mixed into every password before bcrypt, as
|
|
364
|
+
# bcrypt(HMAC-SHA256(pepper, password)), so the digests cannot be cracked
|
|
365
|
+
# from a database dump alone. nil (default) hashes the bare password, as
|
|
366
|
+
# has_secure_password always has. Read it from credentials, never from
|
|
367
|
+
# source; at least 32 random bytes (`SecureRandom.hex(32)`).
|
|
368
|
+
#
|
|
369
|
+
# LOSING THE PEPPER FORCES EVERY PASSWORD HASHED UNDER IT TO BE RESET:
|
|
370
|
+
# there is no way to verify those digests without it.
|
|
371
|
+
#
|
|
372
|
+
# Turning it on locks nobody out: existing (unpeppered) digests still
|
|
373
|
+
# verify, and each user's next password login re-hashes under the pepper
|
|
374
|
+
# (needs rehash_on_login). See StandardId::PasswordPepper and the README,
|
|
375
|
+
# "Password pepper".
|
|
376
|
+
field :pepper, type: :string, default: nil
|
|
377
|
+
# Peppers that were current before, newest first, still ACCEPTED when
|
|
378
|
+
# verifying a password; a login that matches one re-hashes under the
|
|
379
|
+
# current pepper. To rotate, move the old pepper here and set the new one.
|
|
380
|
+
# Keep an entry until no digest made under it can be left (every user has
|
|
381
|
+
# signed in since, or the rest have reset); dropping it early forces those
|
|
382
|
+
# users to reset. Each entry costs one more bcrypt comparison on a wrong
|
|
383
|
+
# password.
|
|
384
|
+
field :previous_peppers, type: :array, default: -> { [] }
|
|
385
|
+
# Whether a password with no pepper still verifies while a pepper is set:
|
|
386
|
+
# digests made before the pepper was turned on. On by default so turning
|
|
387
|
+
# it on locks nobody out. Turn it off only once no unpeppered digest can be
|
|
388
|
+
# left, or those users must reset; it saves one bcrypt comparison per
|
|
389
|
+
# wrong password. Ignored while no pepper is set.
|
|
390
|
+
field :accept_unpeppered_passwords, type: :boolean, default: true
|
|
356
391
|
# Who tells the owner, by email, that their password was changed or reset
|
|
357
392
|
# (CREDENTIAL_PASSWORD_CHANGED):
|
|
358
393
|
# :custom — (default) the engine sends nothing; subscribe to the event
|
|
@@ -364,6 +399,45 @@ StandardId::ConfigSchema.define do
|
|
|
364
399
|
field :changed_notice_delivery, type: :symbol, default: :custom
|
|
365
400
|
end
|
|
366
401
|
|
|
402
|
+
# Step-up (recent) authentication: `require_recent_authentication!` in web
|
|
403
|
+
# and API controllers, and the flow that re-proves identity with a fresh
|
|
404
|
+
# one-time code (and the password, where the account has one). Off by
|
|
405
|
+
# default: the step-up routes 404, access tokens carry no `auth_time`, and
|
|
406
|
+
# nothing changes unless a controller calls the guard. See StandardId::StepUp
|
|
407
|
+
# and the README, "Step-up authentication".
|
|
408
|
+
scope :step_up do
|
|
409
|
+
# Draws the step-up endpoints (web /step_up, API /step_up and
|
|
410
|
+
# /step_up/verify) and adds the OIDC `auth_time` claim to access tokens.
|
|
411
|
+
# The guard refuses to run while this is false.
|
|
412
|
+
field :enabled, type: :boolean, default: false
|
|
413
|
+
# The default `within:` of require_recent_authentication!, in seconds.
|
|
414
|
+
field :max_age, type: :integer, default: 300
|
|
415
|
+
# The StandardId::Otp realm step-up codes are issued and verified in, kept
|
|
416
|
+
# apart from sign-in codes so one can never stand in for the other.
|
|
417
|
+
field :realm, type: :string, default: "step_up"
|
|
418
|
+
# How the code is delivered (StandardId::Otp delivery modes):
|
|
419
|
+
# :built_in — (default) follow c.passwordless.delivery: the engine's
|
|
420
|
+
# PasswordlessMailer when that is :built_in, else your
|
|
421
|
+
# PASSWORDLESS_CODE_GENERATED subscriber (realm "step_up")
|
|
422
|
+
# :custom — always your subscriber, so the message can say why
|
|
423
|
+
field :delivery, type: :symbol, default: :built_in
|
|
424
|
+
# Also ask for the password when the account has one. Off only for hosts
|
|
425
|
+
# whose second factor is the code alone.
|
|
426
|
+
field :require_password, type: :boolean, default: true
|
|
427
|
+
# Where the web guard sends a stale session. nil (default) is "/step_up",
|
|
428
|
+
# the web engine's page when it is mounted at "/"; set it when the engine
|
|
429
|
+
# is mounted elsewhere or the host draws its own page.
|
|
430
|
+
field :url, type: :string, default: nil
|
|
431
|
+
# The profile the API step-up re-mints the access token for, when its
|
|
432
|
+
# audience is bound to a profile type (oauth.audience_profile_types) and
|
|
433
|
+
# the account may hold several: a callable taking `account:` and
|
|
434
|
+
# `claims:` (the current token's decoded claims, e.g. your own `gid`)
|
|
435
|
+
# and returning the profile, passed to TokenMinter.mint(profile:). nil
|
|
436
|
+
# (default) uses the strict audience → profile lookup, which refuses an
|
|
437
|
+
# account with several active profiles of the bound type.
|
|
438
|
+
field :token_profile_resolver, type: :any, default: nil
|
|
439
|
+
end
|
|
440
|
+
|
|
367
441
|
scope :reset_password do
|
|
368
442
|
# Password reset email delivery mode:
|
|
369
443
|
# :custom — (default) host app handles delivery via event subscriber for
|
|
@@ -31,7 +31,7 @@ module StandardId
|
|
|
31
31
|
|
|
32
32
|
password_credential = EmailPassword.find_password_credential(email)
|
|
33
33
|
unless password_credential
|
|
34
|
-
|
|
34
|
+
StandardId::PasswordPepper.dummy_authenticate(password)
|
|
35
35
|
failed!(email)
|
|
36
36
|
end
|
|
37
37
|
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require "concurrent/delay"
|
|
2
|
-
|
|
3
1
|
module StandardId
|
|
4
2
|
# Email + password sign-up and sign-in for JSON clients
|
|
5
3
|
# (Api::EmailPasswordController), switched on with
|
|
@@ -30,8 +28,9 @@ module StandardId
|
|
|
30
28
|
module EmailPassword
|
|
31
29
|
# A digest to compare against when no credential exists, so a miss costs
|
|
32
30
|
# the same bcrypt work as a hit and timing does not reveal whether the
|
|
33
|
-
# address is registered.
|
|
34
|
-
|
|
31
|
+
# address is registered. Compared through PasswordPepper.dummy_authenticate,
|
|
32
|
+
# once per pepper a real digest would be tried under.
|
|
33
|
+
DUMMY_PASSWORD_DIGEST = StandardId::PasswordPepper::DUMMY_DIGEST
|
|
35
34
|
|
|
36
35
|
# Identifier#signup_source of a sign-up made through Registration. A reset
|
|
37
36
|
# runs `after_email_verified` only for these, and
|
|
@@ -83,7 +82,7 @@ module StandardId
|
|
|
83
82
|
password = password.to_s
|
|
84
83
|
password_credential = find_password_credential(email)
|
|
85
84
|
unless password_credential
|
|
86
|
-
|
|
85
|
+
StandardId::PasswordPepper.dummy_authenticate(password)
|
|
87
86
|
return nil
|
|
88
87
|
end
|
|
89
88
|
return nil if password.empty? || !password_credential.authenticate(password)
|
data/lib/standard_id/errors.rb
CHANGED
|
@@ -6,6 +6,22 @@ module StandardId
|
|
|
6
6
|
class ExpiredSessionError < InvalidSessionError; end
|
|
7
7
|
class RevokedSessionError < InvalidSessionError; end
|
|
8
8
|
|
|
9
|
+
# The session is valid but did not prove the account's identity recently
|
|
10
|
+
# enough for this action (require_recent_authentication!). Not an
|
|
11
|
+
# InvalidSessionError: signing in again is not the remedy, a step-up is, and
|
|
12
|
+
# a host's `rescue_from InvalidSessionError` must not swallow it into a
|
|
13
|
+
# sign-out. ApiAuthentication renders it as RFC 9470's
|
|
14
|
+
# `insufficient_user_authentication` challenge.
|
|
15
|
+
class StepUpRequiredError < StandardError
|
|
16
|
+
# @return [Integer] the age, in seconds, the authentication may have
|
|
17
|
+
attr_reader :max_age
|
|
18
|
+
|
|
19
|
+
def initialize(message = "A more recent authentication is required", max_age: nil)
|
|
20
|
+
@max_age = max_age&.to_i
|
|
21
|
+
super(message)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
9
25
|
# Account errors
|
|
10
26
|
#
|
|
11
27
|
# These deliberately do NOT descend from InvalidSessionError, even though
|
|
@@ -11,6 +11,11 @@ module StandardId
|
|
|
11
11
|
# config.login_method_policy refused the method for the account, after
|
|
12
12
|
# the credential was proven and before any session was created.
|
|
13
13
|
AUTHENTICATION_METHOD_DENIED = "authentication.method.denied"
|
|
14
|
+
# Step-up authentication (StandardId::StepUp): a code was issued to a
|
|
15
|
+
# signed-in account, the account re-proved its identity, or it did not.
|
|
16
|
+
STEP_UP_STARTED = "authentication.step_up.started"
|
|
17
|
+
STEP_UP_SUCCEEDED = "authentication.step_up.succeeded"
|
|
18
|
+
STEP_UP_FAILED = "authentication.step_up.failed"
|
|
14
19
|
|
|
15
20
|
SESSION_CREATING = "session.creating"
|
|
16
21
|
SESSION_CREATED = "session.created"
|
|
@@ -70,8 +75,9 @@ module StandardId
|
|
|
70
75
|
CREDENTIAL_PASSWORD_RESET_INITIATED = "credential.password.reset_initiated"
|
|
71
76
|
CREDENTIAL_PASSWORD_RESET_COMPLETED = "credential.password.reset_completed"
|
|
72
77
|
CREDENTIAL_PASSWORD_CHANGED = "credential.password.changed"
|
|
73
|
-
# A login re-hashed the password at the current bcrypt cost
|
|
74
|
-
# (PasswordCredential#rehash_password). The
|
|
78
|
+
# A login re-hashed the password at the current bcrypt cost or under the
|
|
79
|
+
# current c.password.pepper (PasswordCredential#rehash_password). The
|
|
80
|
+
# password itself is unchanged.
|
|
75
81
|
CREDENTIAL_PASSWORD_REHASHED = "credential.password.rehashed"
|
|
76
82
|
CREDENTIAL_CLIENT_SECRET_CREATED = "credential.client_secret.created"
|
|
77
83
|
CREDENTIAL_CLIENT_SECRET_ROTATED = "credential.client_secret.rotated"
|
|
@@ -85,7 +91,10 @@ module StandardId
|
|
|
85
91
|
PASSWORD_VALIDATION_FAILED,
|
|
86
92
|
OTP_VALIDATED,
|
|
87
93
|
OTP_VALIDATION_FAILED,
|
|
88
|
-
AUTHENTICATION_METHOD_DENIED
|
|
94
|
+
AUTHENTICATION_METHOD_DENIED,
|
|
95
|
+
STEP_UP_STARTED,
|
|
96
|
+
STEP_UP_SUCCEEDED,
|
|
97
|
+
STEP_UP_FAILED
|
|
89
98
|
].freeze
|
|
90
99
|
|
|
91
100
|
SESSION_EVENTS = [
|
|
@@ -172,6 +181,8 @@ module StandardId
|
|
|
172
181
|
PASSWORD_VALIDATION_FAILED,
|
|
173
182
|
OTP_VALIDATION_FAILED,
|
|
174
183
|
AUTHENTICATION_METHOD_DENIED,
|
|
184
|
+
STEP_UP_SUCCEEDED,
|
|
185
|
+
STEP_UP_FAILED,
|
|
175
186
|
# Session
|
|
176
187
|
SESSION_CREATED,
|
|
177
188
|
SESSION_REVOKED,
|
|
@@ -34,7 +34,9 @@ module StandardId
|
|
|
34
34
|
return unless self.class.handles?(event.payload)
|
|
35
35
|
|
|
36
36
|
identifier = event[:identifier]
|
|
37
|
-
|
|
37
|
+
# The record, or (under events.publish_records = false) the one
|
|
38
|
+
# being published, from CodeHandoff.
|
|
39
|
+
challenge = StandardId::Passwordless::CodeHandoff.challenge_for(event)
|
|
38
40
|
code = challenge&.code
|
|
39
41
|
|
|
40
42
|
return if identifier.blank? || code.blank?
|
|
@@ -10,7 +10,8 @@ module StandardId
|
|
|
10
10
|
BASE_SESSION_FIELDS = %i[account_id client_id scopes grant_type aud claims]
|
|
11
11
|
|
|
12
12
|
# Supported signing algorithms categorized by type
|
|
13
|
-
# Symmetric: use shared secret (Rails.application.secret_key_base
|
|
13
|
+
# Symmetric: use shared secret (Rails.application.secret_key_base; tokens
|
|
14
|
+
# signed under a config.previous_secret_key_bases entry still verify)
|
|
14
15
|
# Asymmetric: use key pairs (RSA or EC private key)
|
|
15
16
|
SUPPORTED_ALGORITHMS = {
|
|
16
17
|
# HMAC (symmetric)
|
|
@@ -103,6 +104,20 @@ module StandardId
|
|
|
103
104
|
end
|
|
104
105
|
end
|
|
105
106
|
|
|
107
|
+
# The secrets an HMAC-signed (HS256/384/512) token is accepted under: the
|
|
108
|
+
# current `secret_key_base` first, then each
|
|
109
|
+
# `config.previous_secret_key_bases` entry. Tokens are always signed with
|
|
110
|
+
# the current one (.signing_key). Asymmetric keys rotate through
|
|
111
|
+
# `oauth.previous_signing_keys` instead, so this is empty for them.
|
|
112
|
+
#
|
|
113
|
+
# Pass it as the key to .verify to give a config-free caller the same
|
|
114
|
+
# rotation window.
|
|
115
|
+
def self.symmetric_verification_keys
|
|
116
|
+
return [] if asymmetric?
|
|
117
|
+
|
|
118
|
+
[Rails.application.secret_key_base, *StandardId::KeyedDigest.previous_secrets].uniq
|
|
119
|
+
end
|
|
120
|
+
|
|
106
121
|
def self.key_id
|
|
107
122
|
return nil unless asymmetric?
|
|
108
123
|
|
|
@@ -214,7 +229,7 @@ module StandardId
|
|
|
214
229
|
end
|
|
215
230
|
end
|
|
216
231
|
|
|
217
|
-
decoded = JWT.decode(token, verification_key, true, options)
|
|
232
|
+
decoded = asymmetric? ? JWT.decode(token, verification_key, true, options) : decode_symmetric(token, options)
|
|
218
233
|
decoded.first.with_indifferent_access
|
|
219
234
|
rescue JWT::InvalidAudError
|
|
220
235
|
raise StandardId::InvalidAudienceError.new(
|
|
@@ -372,6 +387,20 @@ module StandardId
|
|
|
372
387
|
|
|
373
388
|
private
|
|
374
389
|
|
|
390
|
+
# Tries each of .symmetric_verification_keys in turn. Only a signature
|
|
391
|
+
# mismatch moves on to the next secret: an expired token, a wrong issuer
|
|
392
|
+
# or audience is a property of the token, so it raises out at once and
|
|
393
|
+
# .decode handles it as before. With no previous secrets this is the
|
|
394
|
+
# single decode under the current secret it always was.
|
|
395
|
+
def self.decode_symmetric(token, options)
|
|
396
|
+
keys = symmetric_verification_keys
|
|
397
|
+
keys.each_with_index do |key, index|
|
|
398
|
+
return JWT.decode(token, key, true, options)
|
|
399
|
+
rescue JWT::VerificationError
|
|
400
|
+
raise if index == keys.size - 1
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
375
404
|
# Parses a previous_signing_keys entry into { kid:, key:, algorithm: }
|
|
376
405
|
# Accepts either:
|
|
377
406
|
# - A PEM string or Pathname (uses current algorithm's key class)
|
|
@@ -7,9 +7,9 @@ module StandardId
|
|
|
7
7
|
# Every keyed digest the engine stores (Session#lookup_hash and
|
|
8
8
|
# #token_digest, CodeChallenge#code_digest, AuthorizationCode#code_hash,
|
|
9
9
|
# ClientApplication#registration_access_token_digest) is derived from
|
|
10
|
-
# `secret_key_base`,
|
|
11
|
-
# them: all users signed out, every
|
|
12
|
-
# token dead.
|
|
10
|
+
# `secret_key_base`, as is the HS256/384/512 JWT signing key, so rotating
|
|
11
|
+
# that secret used to strand every one of them: all users signed out, every
|
|
12
|
+
# in-flight code, registration access token and HMAC-signed JWT dead.
|
|
13
13
|
#
|
|
14
14
|
# `config.previous_secret_key_bases` is the opt-in that prevents it.
|
|
15
15
|
# Digests are always WRITTEN under the current secret; they are READ (looked
|
|
@@ -19,6 +19,11 @@ module StandardId
|
|
|
19
19
|
# `config.action_dispatch.cookies_rotations`), but keeps no list of previous
|
|
20
20
|
# `secret_key_base` values a gem could read, hence the setting. The shape
|
|
21
21
|
# mirrors Rodauth's `hmac_old_secret`.
|
|
22
|
+
#
|
|
23
|
+
# Long-lived credentials (session tokens, registration access tokens) that
|
|
24
|
+
# authenticate under a previous secret are re-keyed on the spot (.rekey), so
|
|
25
|
+
# an old secret only has to stay listed until every such credential has been
|
|
26
|
+
# used once since the rotation, or has expired.
|
|
22
27
|
module KeyedDigest
|
|
23
28
|
module_function
|
|
24
29
|
|
|
@@ -34,8 +39,13 @@ module StandardId
|
|
|
34
39
|
# secrets configured this is one element and every caller does exactly the
|
|
35
40
|
# work it did before.
|
|
36
41
|
def secrets
|
|
37
|
-
|
|
38
|
-
|
|
42
|
+
[current_secret, *previous_secrets].uniq
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# `config.previous_secret_key_bases` as strings, blank entries dropped.
|
|
46
|
+
# JwtService also verifies HMAC-signed JWTs under these.
|
|
47
|
+
def previous_secrets
|
|
48
|
+
Array(StandardId.config.previous_secret_key_bases).map(&:to_s).reject(&:blank?)
|
|
39
49
|
end
|
|
40
50
|
|
|
41
51
|
# HMAC-SHA256 of +data+ under +secret+, hex-encoded. Callers domain-separate
|
|
@@ -51,6 +61,34 @@ module StandardId
|
|
|
51
61
|
secrets.map { |secret| yield secret }
|
|
52
62
|
end
|
|
53
63
|
|
|
64
|
+
# Rewrites the digest columns of a row that just authenticated under a
|
|
65
|
+
# previous secret to their current-secret values, in a single UPDATE that
|
|
66
|
+
# applies only while the row still holds +from+ (so a concurrent re-key,
|
|
67
|
+
# rotation or revocation write is never overwritten). Runs no callbacks,
|
|
68
|
+
# touches no timestamps and publishes nothing.
|
|
69
|
+
#
|
|
70
|
+
# It is an optimisation of the rotation window, never part of
|
|
71
|
+
# authentication: any error (a read-only replica, a lock timeout, a missing
|
|
72
|
+
# column) is logged without the digests and swallowed, and a savepoint
|
|
73
|
+
# keeps a failed statement from aborting a surrounding transaction.
|
|
74
|
+
#
|
|
75
|
+
# @param model [Class<ActiveRecord::Base>]
|
|
76
|
+
# @param id [Object] the row's primary key
|
|
77
|
+
# @param from [Hash] column => value the row must still hold
|
|
78
|
+
# @param to [Hash] column => current-secret value
|
|
79
|
+
# @return [Boolean] whether the row was rewritten
|
|
80
|
+
def rekey(model, id:, from:, to:)
|
|
81
|
+
update = -> { model.unscoped.where(model.primary_key => id, **from).update_all(to) }
|
|
82
|
+
rows = model.with_connection(&:transaction_open?) ? model.transaction(requires_new: true) { update.call } : update.call
|
|
83
|
+
rows == 1
|
|
84
|
+
rescue StandardError => e
|
|
85
|
+
StandardId.logger.warn(
|
|
86
|
+
"[StandardId::KeyedDigest] Could not re-key #{model.name} #{id} under the current secret_key_base " \
|
|
87
|
+
"(#{e.class}); it still authenticates under the previous secret"
|
|
88
|
+
)
|
|
89
|
+
false
|
|
90
|
+
end
|
|
91
|
+
|
|
54
92
|
# Constant-time check of +stored+ against every candidate. Compares against
|
|
55
93
|
# all of them rather than stopping at the first match, so the response time
|
|
56
94
|
# does not say which secret a digest was written under.
|
|
@@ -17,7 +17,7 @@ module StandardId
|
|
|
17
17
|
|
|
18
18
|
def initialize(account:, audience:, grant_type:, scopes: nil, client_id: nil, claims: {}, expires_in: nil,
|
|
19
19
|
refresh_token: false, request: nil, auth_method: nil, auth_provider: nil, profile: nil, audit_errors: :raise,
|
|
20
|
-
requested_scopes: nil)
|
|
20
|
+
requested_scopes: nil, authenticated_at: nil)
|
|
21
21
|
super({ audience: audience }, request || NullRequest.new)
|
|
22
22
|
@profile = profile
|
|
23
23
|
@audit_requested_scopes = normalize_audit_scopes(requested_scopes)
|
|
@@ -36,6 +36,7 @@ module StandardId
|
|
|
36
36
|
@request_given = !request.nil?
|
|
37
37
|
@auth_method = auth_method
|
|
38
38
|
@auth_provider = auth_provider
|
|
39
|
+
@authenticated_at = authenticated_at
|
|
39
40
|
end
|
|
40
41
|
|
|
41
42
|
# Public so TokenMinter can report the jti the audit event carried.
|
|
@@ -162,7 +163,7 @@ module StandardId
|
|
|
162
163
|
# later refresh; nil is recorded as :unspecified, which such a policy
|
|
163
164
|
# refuses — so pass it when a policy is configured.
|
|
164
165
|
def refresh_token_auth_lineage
|
|
165
|
-
StandardId::AuthLineage.build(@auth_method, @auth_provider)
|
|
166
|
+
StandardId::AuthLineage.build(@auth_method, @auth_provider, authenticated_at: @authenticated_at)
|
|
166
167
|
end
|
|
167
168
|
|
|
168
169
|
# Resolvers and custom_claims see `request: nil` for a sessionless mint,
|
|
@@ -174,9 +175,14 @@ module StandardId
|
|
|
174
175
|
# Host-supplied claims ride on top of the standard payload and the
|
|
175
176
|
# resolver-derived claims, but can never override a reserved JWT key or a
|
|
176
177
|
# base session field (same rule as config.oauth.custom_claims).
|
|
178
|
+
#
|
|
179
|
+
# Under c.step_up nor can it set `auth_time`, recorded or not: pass
|
|
180
|
+
# +authenticated_at:+ instead.
|
|
177
181
|
def build_jwt_payload(expires_in)
|
|
182
|
+
payload = super(expires_in)
|
|
178
183
|
extra = @claims.except(*StandardId::JwtService::RESERVED_JWT_KEYS, *StandardId::JwtService::BASE_SESSION_FIELDS)
|
|
179
|
-
|
|
184
|
+
extra = extra.except(:auth_time) if StandardId::StepUp.enabled?
|
|
185
|
+
payload.merge(extra).compact
|
|
180
186
|
end
|
|
181
187
|
|
|
182
188
|
# A session is only materialised when the caller passed a real request:
|
|
@@ -14,21 +14,26 @@ module StandardId
|
|
|
14
14
|
module OauthSessionPersistence
|
|
15
15
|
module_function
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
# +authenticated_at+ (when the account last proved its identity, see
|
|
18
|
+
# StandardId::StepUp) is stamped on the row; a reused row keeps the later
|
|
19
|
+
# of its own and this one.
|
|
20
|
+
def persist!(session_class:, account:, request:, audience:, grant_type:, authenticated_at: nil)
|
|
18
21
|
case session_class.name
|
|
19
22
|
when "StandardId::DeviceSession"
|
|
20
23
|
upsert_device_session!(
|
|
21
24
|
account: account,
|
|
22
25
|
request: request,
|
|
23
26
|
audience: audience,
|
|
24
|
-
grant_type: grant_type
|
|
27
|
+
grant_type: grant_type,
|
|
28
|
+
authenticated_at: authenticated_at
|
|
25
29
|
)
|
|
26
30
|
when "StandardId::BrowserSession"
|
|
27
31
|
StandardId::BrowserSession.create!(
|
|
28
32
|
account: account,
|
|
29
33
|
ip_address: StandardId::Utils::IpNormalizer.normalize(request.remote_ip),
|
|
30
34
|
user_agent: request.user_agent.presence || "OAuth:#{grant_type}",
|
|
31
|
-
expires_at: StandardId::BrowserSession.expiry
|
|
35
|
+
expires_at: StandardId::BrowserSession.expiry,
|
|
36
|
+
**authenticated_at_attributes(authenticated_at)
|
|
32
37
|
)
|
|
33
38
|
else
|
|
34
39
|
raise StandardId::ConfigurationError,
|
|
@@ -49,7 +54,7 @@ module StandardId
|
|
|
49
54
|
# audit trail and admin session lists still read it); a new sign-in gets
|
|
50
55
|
# a new row. Expiry is deliberately NOT part of eligibility: an expired
|
|
51
56
|
# but unrevoked row is reused with its expires_at bumped, as before.
|
|
52
|
-
def upsert_device_session!(account:, request:, audience:, grant_type:)
|
|
57
|
+
def upsert_device_session!(account:, request:, audience:, grant_type:, authenticated_at: nil)
|
|
53
58
|
user_agent = request.user_agent
|
|
54
59
|
device_id = stable_device_id(account: account, user_agent: user_agent, audience: audience)
|
|
55
60
|
ip_address = StandardId::Utils::IpNormalizer.normalize(request.remote_ip)
|
|
@@ -69,7 +74,7 @@ module StandardId
|
|
|
69
74
|
account.class.where(id: account.id).lock.first
|
|
70
75
|
|
|
71
76
|
existing = active_device_session(account: account, device_id: device_id)
|
|
72
|
-
return refresh_device_session!(existing, ip_address: ip_address, user_agent: user_agent) if existing
|
|
77
|
+
return refresh_device_session!(existing, ip_address: ip_address, user_agent: user_agent, authenticated_at: authenticated_at) if existing
|
|
73
78
|
|
|
74
79
|
begin
|
|
75
80
|
# Savepoint, so a unique violation does not abort the enclosing token
|
|
@@ -81,7 +86,8 @@ module StandardId
|
|
|
81
86
|
device_id: device_id,
|
|
82
87
|
device_agent: user_agent.presence || "OAuth:#{grant_type}",
|
|
83
88
|
ip_address: ip_address || "0.0.0.0",
|
|
84
|
-
expires_at: StandardId::DeviceSession.expiry
|
|
89
|
+
expires_at: StandardId::DeviceSession.expiry,
|
|
90
|
+
**authenticated_at_attributes(authenticated_at)
|
|
85
91
|
)
|
|
86
92
|
end
|
|
87
93
|
rescue ActiveRecord::RecordNotUnique
|
|
@@ -90,7 +96,7 @@ module StandardId
|
|
|
90
96
|
winner = active_device_session(account: account, device_id: device_id)
|
|
91
97
|
raise unless winner
|
|
92
98
|
|
|
93
|
-
refresh_device_session!(winner, ip_address: ip_address, user_agent: user_agent)
|
|
99
|
+
refresh_device_session!(winner, ip_address: ip_address, user_agent: user_agent, authenticated_at: authenticated_at)
|
|
94
100
|
end
|
|
95
101
|
end
|
|
96
102
|
|
|
@@ -105,15 +111,24 @@ module StandardId
|
|
|
105
111
|
.first
|
|
106
112
|
end
|
|
107
113
|
|
|
108
|
-
def refresh_device_session!(session, ip_address:, user_agent:)
|
|
114
|
+
def refresh_device_session!(session, ip_address:, user_agent:, authenticated_at: nil)
|
|
115
|
+
latest = [session.try(:authenticated_at), authenticated_at].compact.max
|
|
109
116
|
session.update!(
|
|
110
117
|
expires_at: StandardId::DeviceSession.expiry,
|
|
111
118
|
ip_address: ip_address || session.ip_address,
|
|
112
|
-
device_agent: user_agent || session.device_agent
|
|
119
|
+
device_agent: user_agent || session.device_agent,
|
|
120
|
+
**authenticated_at_attributes(latest)
|
|
113
121
|
)
|
|
114
122
|
session
|
|
115
123
|
end
|
|
116
124
|
|
|
125
|
+
# Nothing is written before migration 20261123000000 has run.
|
|
126
|
+
def authenticated_at_attributes(authenticated_at)
|
|
127
|
+
return {} if authenticated_at.nil? || !StandardId::StepUp.session_column?
|
|
128
|
+
|
|
129
|
+
{ authenticated_at: authenticated_at }
|
|
130
|
+
end
|
|
131
|
+
|
|
117
132
|
def stable_device_id(account:, user_agent:, audience:)
|
|
118
133
|
audience_key = Array(audience).join(",")
|
|
119
134
|
Digest::SHA256.hexdigest("oauth:#{audience_key}:#{account.id}:#{user_agent}")[0, 36]
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
require "concurrent/delay"
|
|
2
|
-
|
|
3
1
|
module StandardId
|
|
4
2
|
module Oauth
|
|
5
3
|
class PasswordFlow < TokenGrantFlow
|
|
6
4
|
expect_params :username, :password, :client_id
|
|
7
5
|
permit_params :client_secret, :audience, :scope, :realm
|
|
8
6
|
|
|
9
|
-
DUMMY_PASSWORD_DIGEST =
|
|
7
|
+
DUMMY_PASSWORD_DIGEST = StandardId::PasswordPepper::DUMMY_DIGEST
|
|
10
8
|
|
|
11
9
|
def authenticate!
|
|
12
10
|
validate_client_secret!(params[:client_id], params[:client_secret]) if params[:client_secret].present?
|
|
@@ -103,9 +101,9 @@ module StandardId
|
|
|
103
101
|
.includes(credential: { identifier: :account })
|
|
104
102
|
.find_by(login: username)
|
|
105
103
|
|
|
106
|
-
# Perform
|
|
107
|
-
# to prevent user enumeration via response timing differences.
|
|
108
|
-
|
|
104
|
+
# Perform the dummy bcrypt comparisons when the credential doesn't
|
|
105
|
+
# exist to prevent user enumeration via response timing differences.
|
|
106
|
+
StandardId::PasswordPepper.dummy_authenticate(password) unless @credential
|
|
109
107
|
|
|
110
108
|
# has_secure_password's authenticate returns false (not nil) on a wrong
|
|
111
109
|
# password, so `&.account` would call false.account → NoMethodError (500).
|
|
@@ -124,10 +122,6 @@ module StandardId
|
|
|
124
122
|
end
|
|
125
123
|
end
|
|
126
124
|
|
|
127
|
-
def dummy_password_digest
|
|
128
|
-
DUMMY_PASSWORD_DIGEST.value
|
|
129
|
-
end
|
|
130
|
-
|
|
131
125
|
def default_scope
|
|
132
126
|
"read"
|
|
133
127
|
end
|
|
@@ -176,10 +176,16 @@ module StandardId
|
|
|
176
176
|
account: account,
|
|
177
177
|
request: request,
|
|
178
178
|
audience: audience,
|
|
179
|
-
grant_type: grant_type
|
|
179
|
+
grant_type: grant_type,
|
|
180
|
+
authenticated_at: token_authenticated_at
|
|
180
181
|
)
|
|
181
182
|
end
|
|
182
183
|
|
|
184
|
+
# With c.step_up on, the OIDC `auth_time` claim (seconds since the epoch)
|
|
185
|
+
# says when the account last proved its identity, for
|
|
186
|
+
# ApiAuthentication#require_recent_authentication!. Only the recorded
|
|
187
|
+
# time may set it: a custom claim of that name is dropped, even when no
|
|
188
|
+
# time was recorded, so no claim can stand in for an authentication.
|
|
183
189
|
def build_jwt_payload(expires_in)
|
|
184
190
|
base_payload = {
|
|
185
191
|
sub: subject_id,
|
|
@@ -189,7 +195,25 @@ module StandardId
|
|
|
189
195
|
aud: audience
|
|
190
196
|
}.compact
|
|
191
197
|
|
|
192
|
-
base_payload.merge(claims_from_scope_mapping).merge(claims_from_custom_claims)
|
|
198
|
+
payload = base_payload.merge(claims_from_scope_mapping).merge(claims_from_custom_claims)
|
|
199
|
+
return payload unless StandardId::StepUp.enabled?
|
|
200
|
+
|
|
201
|
+
payload = payload.except(:auth_time, "auth_time")
|
|
202
|
+
authenticated_at = token_authenticated_at
|
|
203
|
+
payload[:auth_time] = authenticated_at.to_i if authenticated_at
|
|
204
|
+
payload
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# When the account behind this token last proved its identity: now for a
|
|
208
|
+
# grant that authenticates it (one with a login_method_policy_context),
|
|
209
|
+
# the recorded time for one derived from an earlier authentication
|
|
210
|
+
# (authorization_code, refresh_token), nil when unknown or when there is
|
|
211
|
+
# no account. Read off refresh_token_auth_lineage, which already says how
|
|
212
|
+
# the authentication was made, so both travel together.
|
|
213
|
+
def token_authenticated_at
|
|
214
|
+
return @token_authenticated_at if defined?(@token_authenticated_at)
|
|
215
|
+
|
|
216
|
+
@token_authenticated_at = refresh_token_auth_lineage[:authenticated_at]
|
|
193
217
|
end
|
|
194
218
|
|
|
195
219
|
def token_expiry
|
|
@@ -261,7 +285,8 @@ module StandardId
|
|
|
261
285
|
context = login_method_policy_context
|
|
262
286
|
return StandardId::AuthLineage.empty if context.nil?
|
|
263
287
|
|
|
264
|
-
|
|
288
|
+
@authenticating_at ||= Time.current
|
|
289
|
+
StandardId::AuthLineage.build(context[:auth_method], context[:provider], authenticated_at: @authenticating_at)
|
|
265
290
|
end
|
|
266
291
|
|
|
267
292
|
# The parent session for the refresh token issued by THIS grant, when the
|