standard_id 0.61.0 → 0.63.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 +145 -0
- data/README.md +236 -32
- 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/audience_verification.rb +6 -5
- data/app/controllers/concerns/standard_id/web_authentication.rb +29 -7
- data/app/controllers/standard_id/api/base_controller.rb +10 -5
- data/app/controllers/standard_id/api/oauth/base_controller.rb +1 -0
- 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/mailers/standard_id/passwordless_mailer.rb +20 -2
- 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/passwordless_mailer/step_up_email.html.erb +24 -0
- data/app/views/standard_id/passwordless_mailer/step_up_email.text.erb +12 -0
- data/app/views/standard_id/web/step_up/show.html.erb +65 -0
- data/config/brakeman.ignore +4 -0
- data/config/locales/en.yml +9 -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 +49 -6
- data/lib/standard_id/api/token_manager.rb +7 -4
- data/lib/standard_id/auth_lineage.rb +39 -8
- data/lib/standard_id/config/schema.rb +105 -7
- data/lib/standard_id/email_password/authentication.rb +1 -1
- data/lib/standard_id/email_password.rb +4 -5
- data/lib/standard_id/engine.rb +10 -8
- data/lib/standard_id/errors.rb +58 -0
- data/lib/standard_id/events/definitions.rb +14 -3
- data/lib/standard_id/events/subscribers/passwordless_delivery_subscriber.rb +12 -5
- data/lib/standard_id/http_client.rb +6 -13
- data/lib/standard_id/jwt_service.rb +31 -2
- data/lib/standard_id/keyed_digest.rb +43 -5
- data/lib/standard_id/network_wrapper.rb +78 -0
- data/lib/standard_id/oauth/audiences.rb +42 -0
- data/lib/standard_id/oauth/authorization_code_authorization_flow.rb +1 -1
- 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 +31 -4
- 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 +9 -4
- data/lib/standard_id/version.rb +1 -1
- data/lib/standard_id/web/session_manager.rb +9 -2
- data/lib/standard_id.rb +4 -0
- metadata +12 -1
|
@@ -4,9 +4,10 @@ module StandardId
|
|
|
4
4
|
# Built-in OTP email delivery (`c.passwordless.delivery = :built_in`).
|
|
5
5
|
#
|
|
6
6
|
# Sign-in codes (realm "authentication") get PasswordlessMailer#otp_email;
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
7
|
+
# step-up codes (realm `c.step_up.realm`) get #step_up_email; every other
|
|
8
|
+
# realm — WebEngine email verification, Otp.issue contact verification —
|
|
9
|
+
# gets #verification_email, so neither arrives as "your sign-in code".
|
|
10
|
+
# There is no built-in SMS delivery.
|
|
10
11
|
#
|
|
11
12
|
# On a successful enqueue it marks the code challenge
|
|
12
13
|
# (CodeChallenge#built_in_delivered), which is how the strategy knows to
|
|
@@ -34,7 +35,9 @@ module StandardId
|
|
|
34
35
|
return unless self.class.handles?(event.payload)
|
|
35
36
|
|
|
36
37
|
identifier = event[:identifier]
|
|
37
|
-
|
|
38
|
+
# The record, or (under events.publish_records = false) the one
|
|
39
|
+
# being published, from CodeHandoff.
|
|
40
|
+
challenge = StandardId::Passwordless::CodeHandoff.challenge_for(event)
|
|
38
41
|
code = challenge&.code
|
|
39
42
|
|
|
40
43
|
return if identifier.blank? || code.blank?
|
|
@@ -63,7 +66,11 @@ module StandardId
|
|
|
63
66
|
private
|
|
64
67
|
|
|
65
68
|
def mailer_action(realm)
|
|
66
|
-
|
|
69
|
+
case realm.to_s
|
|
70
|
+
when "", StandardId::Passwordless::DEFAULT_REALM then :otp_email
|
|
71
|
+
when StandardId::StepUp.realm then :step_up_email
|
|
72
|
+
else :verification_email
|
|
73
|
+
end
|
|
67
74
|
end
|
|
68
75
|
|
|
69
76
|
def expires_in_minutes(expires_at)
|
|
@@ -76,20 +76,13 @@ module StandardId
|
|
|
76
76
|
# Private, but provider plugins call it through `send` to fetch a JWKS
|
|
77
77
|
# with a plain GET, so its signature is part of the plugin contract:
|
|
78
78
|
# `provider:` is optional and new keywords must stay optional.
|
|
79
|
+
#
|
|
80
|
+
# The host's `network_wrapper` sees the whole connection (connect,
|
|
81
|
+
# request, response), so a timeout or refused connection raises inside
|
|
82
|
+
# it and reaches the caller as is; an error the wrapper raises itself
|
|
83
|
+
# becomes StandardId::ProviderUnavailableError (see NetworkWrapper).
|
|
79
84
|
def start_connection(uri, resolved_ip: nil, provider: DEFAULT_PROVIDER, &block)
|
|
80
|
-
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# Runs the block through `StandardId.config.network_wrapper`, or
|
|
84
|
-
# directly when none is configured. The wrapper sees the whole
|
|
85
|
-
# connection (connect, request, response), so a timeout or refused
|
|
86
|
-
# connection raises inside it; whatever it raises or returns is the
|
|
87
|
-
# caller's.
|
|
88
|
-
def with_network_wrapper(provider, &block)
|
|
89
|
-
wrapper = StandardId.config.network_wrapper
|
|
90
|
-
return yield unless wrapper.respond_to?(:call)
|
|
91
|
-
|
|
92
|
-
wrapper.call(**StandardId::Utils::CallableParameterFilter.filter(wrapper, { provider: provider&.to_sym }), &block)
|
|
85
|
+
NetworkWrapper.run(provider:) { open_connection(uri, resolved_ip:, &block) }
|
|
93
86
|
end
|
|
94
87
|
|
|
95
88
|
def open_connection(uri, resolved_ip: nil, &block)
|
|
@@ -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.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
module StandardId
|
|
2
|
+
# Runs an outbound provider request through the host's
|
|
3
|
+
# `StandardId.config.network_wrapper`, or directly when none is configured.
|
|
4
|
+
# StandardId::HttpClient sends every request through here; a plugin with its
|
|
5
|
+
# own HTTP code (one that may not use HttpClient's address guard) calls it
|
|
6
|
+
# the same way, so the wrapper and its error contract cover it too:
|
|
7
|
+
#
|
|
8
|
+
# StandardId::NetworkWrapper.run(provider: :example) { http.start { |h| h.request(req) } }
|
|
9
|
+
#
|
|
10
|
+
# With no wrapper, the block runs directly and nothing is rescued. With one,
|
|
11
|
+
# an error that leaves the wrapper is handled by the first rule that matches:
|
|
12
|
+
#
|
|
13
|
+
# 1. A StandardId::OAuthError (a ProviderUnavailableError from a nested
|
|
14
|
+
# call, or a refusal of the host's own) reaches the caller as is.
|
|
15
|
+
# 2. An error the block raised (the request: a timeout, a refused
|
|
16
|
+
# connection), re-raised by the wrapper as the same object, reaches the
|
|
17
|
+
# caller as is, exactly as with no wrapper.
|
|
18
|
+
# 3. If the most recent call of the block returned normally, the request
|
|
19
|
+
# went through (an authorization code may be spent), so the wrapper's
|
|
20
|
+
# error (a failing tracer, say) reaches the caller as is: telling the
|
|
21
|
+
# user to retry would be wrong. With a retrying wrapper, only the last
|
|
22
|
+
# call counts.
|
|
23
|
+
# 4. A programming error in the wrapper (PROGRAMMING_ERRORS: a typo, an
|
|
24
|
+
# unloaded constant, a wrong call) reaches the caller as is, so it is
|
|
25
|
+
# reported as the bug it is, not shown as an outage.
|
|
26
|
+
# 5. Anything else (an open circuit that never ran the block, a breaker
|
|
27
|
+
# that turns the request's failure into its own error) becomes
|
|
28
|
+
# StandardId::ProviderUnavailableError, with the wrapper's error as
|
|
29
|
+
# `cause`. Callers therefore need no knowledge of the host's
|
|
30
|
+
# circuit-breaker library to map an outage.
|
|
31
|
+
#
|
|
32
|
+
# Non-StandardError exceptions (Interrupt, ...) are never touched.
|
|
33
|
+
module NetworkWrapper
|
|
34
|
+
# Errors a host wrapper raises because its own code is wrong, never
|
|
35
|
+
# because the provider is down. NameError covers NoMethodError.
|
|
36
|
+
PROGRAMMING_ERRORS = [NameError, TypeError, ArgumentError].freeze
|
|
37
|
+
|
|
38
|
+
module_function
|
|
39
|
+
|
|
40
|
+
# @param provider [Symbol, String, nil] the remote service (`:google`,
|
|
41
|
+
# `:apple`, ...), handed to the wrapper as `provider:`
|
|
42
|
+
# @return whatever the block (through the wrapper) returns
|
|
43
|
+
# @raise [StandardId::ProviderUnavailableError] the wrapper refused the
|
|
44
|
+
# request (rule 5 above)
|
|
45
|
+
def run(provider:, &block)
|
|
46
|
+
wrapper = StandardId.config.network_wrapper
|
|
47
|
+
return yield unless wrapper.respond_to?(:call)
|
|
48
|
+
|
|
49
|
+
provider = provider&.to_sym
|
|
50
|
+
arguments = StandardId::Utils::CallableParameterFilter.filter(wrapper, { provider: provider })
|
|
51
|
+
# Every error the block raised (rule 2), and whether its latest call
|
|
52
|
+
# returned normally (rule 3).
|
|
53
|
+
raised = []
|
|
54
|
+
returned = false
|
|
55
|
+
request = proc do |*args|
|
|
56
|
+
returned = false
|
|
57
|
+
result = block.call(*args)
|
|
58
|
+
returned = true
|
|
59
|
+
result
|
|
60
|
+
rescue StandardError => e
|
|
61
|
+
raised << e
|
|
62
|
+
raise
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
begin
|
|
66
|
+
wrapper.call(**arguments, &request)
|
|
67
|
+
rescue StandardId::OAuthError
|
|
68
|
+
raise
|
|
69
|
+
rescue StandardError => e
|
|
70
|
+
raise if raised.any? { |error| error.equal?(e) }
|
|
71
|
+
raise if returned
|
|
72
|
+
raise if PROGRAMMING_ERRORS.any? { |klass| e.is_a?(klass) }
|
|
73
|
+
|
|
74
|
+
raise StandardId::ProviderUnavailableError.new(provider: provider), cause: e
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module StandardId
|
|
2
|
+
module Oauth
|
|
3
|
+
# The two audience lists an app works with, resolved from config:
|
|
4
|
+
#
|
|
5
|
+
# * mintable — audiences this app may issue tokens for (every
|
|
6
|
+
# TokenGrantFlow, so /oauth/token for each grant including the
|
|
7
|
+
# authorization-code exchange and client_credentials, plus
|
|
8
|
+
# TokenMinter / DirectMintFlow and the API step-up re-mint).
|
|
9
|
+
# * accepted — audiences the API token manager accepts on a bearer at
|
|
10
|
+
# decode time.
|
|
11
|
+
#
|
|
12
|
+
# Each is `oauth.mintable_audiences` / `oauth.accepted_audiences` when the
|
|
13
|
+
# host set it (nil means unset), else `oauth.allowed_audiences`, so a host
|
|
14
|
+
# that sets neither sees one list doing both jobs, as it always did. An
|
|
15
|
+
# identity provider that mints tokens for other services (aud=harness,
|
|
16
|
+
# aud=inference) lists them in mintable_audiences and keeps
|
|
17
|
+
# accepted_audiences to its own API's audiences, so those service tokens
|
|
18
|
+
# are not also accepted on it.
|
|
19
|
+
#
|
|
20
|
+
# An empty effective list means no restriction on that side, as an empty
|
|
21
|
+
# allowed_audiences always has.
|
|
22
|
+
module Audiences
|
|
23
|
+
module_function
|
|
24
|
+
|
|
25
|
+
# @return [Array<String>]
|
|
26
|
+
def mintable
|
|
27
|
+
resolve(StandardId.config.oauth.mintable_audiences)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @return [Array<String>]
|
|
31
|
+
def accepted
|
|
32
|
+
resolve(StandardId.config.oauth.accepted_audiences)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def resolve(override)
|
|
36
|
+
list = override.nil? ? StandardId.config.oauth.allowed_audiences : override
|
|
37
|
+
Array(list).map(&:to_s)
|
|
38
|
+
end
|
|
39
|
+
private_class_method :resolve
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -5,7 +5,7 @@ module StandardId
|
|
|
5
5
|
# :audience is optional (RFC 6749 / RFC 8707 §2 treats `resource`/`audience`
|
|
6
6
|
# as OPTIONAL at /authorize). Token-time validation in
|
|
7
7
|
# TokenGrantFlow#validate_audience! already no-ops when audience is blank or
|
|
8
|
-
# when no
|
|
8
|
+
# when no mintable audiences are configured, so omitting it is safe and lets
|
|
9
9
|
# standards-compliant clients (e.g. MCP) authorize without it.
|
|
10
10
|
permit_params :audience, :resource, :scope, :redirect_uri, :state, :connection, :prompt, :organization, :invitation, :code_challenge, :code_challenge_method, :nonce
|
|
11
11
|
|
|
@@ -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
|
|
@@ -325,8 +350,10 @@ module StandardId
|
|
|
325
350
|
params[:audience]
|
|
326
351
|
end
|
|
327
352
|
|
|
353
|
+
# Against the audiences this app may mint (oauth.mintable_audiences,
|
|
354
|
+
# else oauth.allowed_audiences), not the ones its API accepts.
|
|
328
355
|
def validate_audience!
|
|
329
|
-
allowed = StandardId.
|
|
356
|
+
allowed = StandardId::Oauth::Audiences.mintable
|
|
330
357
|
return if allowed.blank? # No restriction configured
|
|
331
358
|
return if audience.blank? # Audience not provided (optional)
|
|
332
359
|
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
require "concurrent/delay"
|
|
2
|
+
require "openssl"
|
|
3
|
+
|
|
4
|
+
module StandardId
|
|
5
|
+
# The secret mixed into every password before bcrypt sees it
|
|
6
|
+
# (`c.password.pepper`), so a leaked database alone is not enough to crack
|
|
7
|
+
# the digests: the pepper lives in the app's credentials, not in the rows.
|
|
8
|
+
#
|
|
9
|
+
# A peppered digest is bcrypt(HMAC-SHA256(pepper, password)), hex-encoded,
|
|
10
|
+
# never bcrypt(password + pepper): bcrypt reads only the first 72 bytes, so
|
|
11
|
+
# an appended pepper would be cut off a long password, and a raw HMAC can
|
|
12
|
+
# contain the NUL byte bcrypt stops at. The hex form is 64 bytes of [0-9a-f].
|
|
13
|
+
#
|
|
14
|
+
# Digests are WRITTEN under the current pepper and READ under the current
|
|
15
|
+
# pepper, then each of `c.password.previous_peppers`, then with no pepper at
|
|
16
|
+
# all, so turning the pepper on, or rotating it, locks nobody out: a login
|
|
17
|
+
# that matched anything but the current pepper re-hashes under it
|
|
18
|
+
# (PasswordCredential#rehash_password). With no pepper configured there is
|
|
19
|
+
# one candidate, the bare password, and every caller does exactly what
|
|
20
|
+
# has_secure_password did. The shape mirrors Rodauth's password_pepper.
|
|
21
|
+
#
|
|
22
|
+
# Losing the pepper makes every digest made under it unverifiable: each of
|
|
23
|
+
# those users has to reset their password. Keep it with secret_key_base.
|
|
24
|
+
module PasswordPepper
|
|
25
|
+
# The candidate meaning "no pepper": the bare password.
|
|
26
|
+
UNPEPPERED = ""
|
|
27
|
+
|
|
28
|
+
# A digest to compare against when no credential exists, so a miss costs
|
|
29
|
+
# the same bcrypt work as a wrong password (see .dummy_authenticate).
|
|
30
|
+
DUMMY_DIGEST = Concurrent::Delay.new { BCrypt::Password.create("").freeze }
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
# The pepper new digests are written under; UNPEPPERED when none is set.
|
|
35
|
+
def current
|
|
36
|
+
StandardId.config.password.pepper.to_s
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Whether any pepper is configured, current or previous. When false the
|
|
40
|
+
# engine never looks past has_secure_password's own behaviour.
|
|
41
|
+
def enabled?
|
|
42
|
+
current.present? || previous.any?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The peppers a stored digest may have been made under, current first,
|
|
46
|
+
# then `c.password.previous_peppers`, then UNPEPPERED unless
|
|
47
|
+
# `c.password.accept_unpeppered_passwords` is off. Blank and repeated
|
|
48
|
+
# entries are dropped. The unpeppered candidate is always kept while no
|
|
49
|
+
# current pepper is set, so the setting cannot lock everyone out.
|
|
50
|
+
def candidates
|
|
51
|
+
list = [current, *previous]
|
|
52
|
+
list << UNPEPPERED if current.empty? || StandardId.config.password.accept_unpeppered_passwords
|
|
53
|
+
list.uniq
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# What bcrypt hashes for +password+ under +pepper+.
|
|
57
|
+
def apply(password, pepper)
|
|
58
|
+
password = password.to_s
|
|
59
|
+
pepper.empty? ? password : OpenSSL::HMAC.hexdigest("SHA256", pepper, password)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The candidate +password+ matches +digest+ under, or nil when it matches
|
|
63
|
+
# none. Stops at the first match: once the password is right, how long the
|
|
64
|
+
# check took says nothing a caller did not already know. A wrong password
|
|
65
|
+
# costs one bcrypt comparison per candidate, which .dummy_authenticate
|
|
66
|
+
# matches.
|
|
67
|
+
def matching_pepper(digest, password)
|
|
68
|
+
stored = BCrypt::Password.new(digest)
|
|
69
|
+
candidates.find { |pepper| stored.is_password?(apply(password, pepper)) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The work of a wrong password against a real digest, done against
|
|
73
|
+
# DUMMY_DIGEST, for a login whose credential does not exist: timing must
|
|
74
|
+
# not reveal whether an address is registered. One comparison per
|
|
75
|
+
# candidate, as .matching_pepper makes for a wrong password.
|
|
76
|
+
#
|
|
77
|
+
# @return [nil]
|
|
78
|
+
def dummy_authenticate(password)
|
|
79
|
+
stored = BCrypt::Password.new(DUMMY_DIGEST.value)
|
|
80
|
+
candidates.each { |pepper| stored.is_password?(apply(password, pepper)) }
|
|
81
|
+
nil
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def previous
|
|
85
|
+
Array(StandardId.config.password.previous_peppers).map(&:to_s).reject(&:empty?)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|