standard_id 0.58.1 → 0.60.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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +103 -0
  3. data/README.md +80 -3
  4. data/app/controllers/concerns/standard_id/web_authentication.rb +2 -0
  5. data/app/controllers/standard_id/web/login_verify_controller.rb +7 -1
  6. data/app/forms/standard_id/web/reset_password_confirm_form.rb +1 -0
  7. data/app/mailers/standard_id/account_notice_mailer.rb +11 -0
  8. data/app/models/standard_id/authorization_code.rb +6 -3
  9. data/app/models/standard_id/client_application.rb +7 -6
  10. data/app/models/standard_id/code_challenge.rb +69 -1
  11. data/app/models/standard_id/email_identifier.rb +14 -0
  12. data/app/models/standard_id/password_credential.rb +141 -0
  13. data/app/models/standard_id/session.rb +22 -12
  14. data/app/views/standard_id/account_notice_mailer/password_changed.html.erb +22 -0
  15. data/app/views/standard_id/account_notice_mailer/password_changed.text.erb +10 -0
  16. data/app/views/standard_id/web/login_verify/show.html.erb +9 -1
  17. data/config/locales/en.yml +7 -0
  18. data/db/migrate/20261122000000_add_code_digest_to_standard_id_code_challenges.rb +24 -0
  19. data/lib/generators/standard_id/install/templates/standard_id_reference.rb +21 -1
  20. data/lib/standard_id/config/schema.rb +48 -0
  21. data/lib/standard_id/email_domain.rb +169 -0
  22. data/lib/standard_id/email_password/authentication.rb +8 -2
  23. data/lib/standard_id/email_password/password_reset.rb +1 -0
  24. data/lib/standard_id/email_password/verification.rb +5 -1
  25. data/lib/standard_id/engine.rb +1 -0
  26. data/lib/standard_id/events/definitions.rb +5 -0
  27. data/lib/standard_id/events/subscribers/password_changed_notice_subscriber.rb +42 -0
  28. data/lib/standard_id/keyed_digest.rb +65 -0
  29. data/lib/standard_id/oauth/password_flow.rb +1 -0
  30. data/lib/standard_id/passwordless/base_strategy.rb +8 -0
  31. data/lib/standard_id/passwordless/email_strategy.rb +7 -0
  32. data/lib/standard_id/passwordless/verification_service.rb +9 -7
  33. data/lib/standard_id/testing/otp_helpers.rb +58 -0
  34. data/lib/standard_id/testing.rb +1 -0
  35. data/lib/standard_id/version.rb +1 -1
  36. data/lib/standard_id.rb +3 -0
  37. metadata +30 -3
@@ -11,6 +11,13 @@ module StandardId
11
11
  raise StandardId::InvalidRequestError, "Invalid email format" unless email.to_s.match?(/\A[^@\s]+@[^@\s]+\z/)
12
12
  end
13
13
 
14
+ # A mistyped TLD (rarebit.ome) is accepted by the ESP and bounces hours
15
+ # later, so the user just never gets a code. Refuse it before sending.
16
+ def validate_recipient!(email)
17
+ error = StandardId::EmailDomain.validation_error(email)
18
+ raise StandardId::InvalidRequestError, error if error
19
+ end
20
+
14
21
  def find_or_create_account!(email)
15
22
  Account.find_or_create_by_verified_email!(email)
16
23
  end
@@ -115,21 +115,23 @@ module StandardId
115
115
  challenge = lock_active_challenge
116
116
 
117
117
  unless challenge
118
- # Constant-time compare even when no challenge exists to prevent
119
- # timing-based enumeration of valid target+realm pairs. We compare
120
- # the submitted code against a random value of the same digit
121
- # length so an observer cannot distinguish "no challenge" from
122
- # "wrong code" by response time.
118
+ # Same comparison even when no challenge exists to prevent
119
+ # timing-based enumeration of valid target+realm pairs. We check
120
+ # the submitted code against an unsaved challenge for a random
121
+ # value of the same digit length, so the work (an HMAC per accepted
122
+ # secret and a constant-time compare) matches the wrong-code path
123
+ # and an observer cannot distinguish "no challenge" from "wrong
124
+ # code" by response time.
123
125
  padded_length = @code.length.clamp(4, 10)
124
126
  fabricated_code = SecureRandom.random_number(10**padded_length).to_s.rjust(padded_length, "0")
125
- secure_compare(fabricated_code, @code)
127
+ StandardId::CodeChallenge.new(code: fabricated_code).code_matches?(@code)
126
128
  # No event — matches prior behavior: we do not publish failure
127
129
  # events for probes that never triggered a real challenge.
128
130
  result = failure("Invalid or expired verification code", error_code: :not_found, attempts: 0)
129
131
  next
130
132
  end
131
133
 
132
- code_matches = secure_compare(challenge.code, @code)
134
+ code_matches = challenge.code_matches?(@code)
133
135
 
134
136
  unless code_matches
135
137
  attempts = record_failed_attempt!(challenge)
@@ -0,0 +1,58 @@
1
+ module StandardId
2
+ module Testing
3
+ # Raw one-time codes, for specs that need to submit one.
4
+ #
5
+ # A challenge stores only an HMAC of its code (migration 20261122000000),
6
+ # so `StandardId::CodeChallenge.last.code` is nil once the row is read back
7
+ # from the database. The raw code exists only on the record the engine has
8
+ # just created, which it publishes as PASSWORDLESS_CODE_GENERATED's
9
+ # `code_challenge:`; these helpers capture it from there, exactly as a
10
+ # delivery subscriber does. (With `config.events.publish_records = false`
11
+ # the record is not published, and there is nothing to capture.)
12
+ #
13
+ # Usage in rails_helper.rb:
14
+ #
15
+ # require "standard_id/testing"
16
+ #
17
+ # RSpec.configure do |config|
18
+ # config.include StandardId::Testing::OtpHelpers, type: :request
19
+ # end
20
+ #
21
+ # Example:
22
+ # code = capture_otp_code { post "/login", params: { email: email } }
23
+ # patch "/login_verify", params: { code: code }
24
+ #
25
+ module OtpHelpers
26
+ IssuedCode = Data.define(:identifier, :channel, :realm, :code)
27
+
28
+ # Every code issued while the block runs, oldest first.
29
+ #
30
+ # @return [Array<IssuedCode>]
31
+ def capture_otp_codes
32
+ codes = []
33
+ subscription = StandardId::Events.subscribe(StandardId::Events::PASSWORDLESS_CODE_GENERATED) do |event|
34
+ code = event[:code_challenge]&.code
35
+ next if code.blank?
36
+
37
+ codes << IssuedCode.new(identifier: event[:identifier], channel: event[:channel], realm: event[:realm], code: code)
38
+ end
39
+
40
+ yield
41
+ codes
42
+ ensure
43
+ ActiveSupport::Notifications.unsubscribe(subscription) if subscription
44
+ end
45
+
46
+ # The last code issued while the block runs, to +identifier+ when given.
47
+ #
48
+ # @return [String]
49
+ # @raise [RuntimeError] when no code was issued
50
+ def capture_otp_code(identifier = nil, &block)
51
+ codes = capture_otp_codes(&block)
52
+ codes = codes.select { |issued| issued.identifier == identifier } if identifier
53
+
54
+ codes.last&.code || raise("No one-time code was issued#{" to #{identifier}" if identifier}")
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,6 +1,7 @@
1
1
  require "standard_id/testing/authentication_helpers"
2
2
  require "standard_id/testing/request_helpers"
3
3
  require "standard_id/testing/config_helpers"
4
+ require "standard_id/testing/otp_helpers"
4
5
  require "standard_id/testing/provider_examples" if defined?(RSpec)
5
6
 
6
7
  module StandardId
@@ -1,3 +1,3 @@
1
1
  module StandardId
2
- VERSION = "0.58.1"
2
+ VERSION = "0.60.0"
3
3
  end
data/lib/standard_id.rb CHANGED
@@ -15,6 +15,7 @@ require "standard_id/migration_check"
15
15
  require "standard_id/migration_helpers"
16
16
  require "standard_id/checks/migrations"
17
17
  require "standard_id/errors"
18
+ require "standard_id/keyed_digest"
18
19
  require "standard_id/events"
19
20
  require "standard_id/events/subscribers/base"
20
21
  require "standard_id/events/subscribers/logging_subscriber"
@@ -22,6 +23,7 @@ require "standard_id/events/subscribers/account_status_subscriber"
22
23
  require "standard_id/events/subscribers/account_locking_subscriber"
23
24
  require "standard_id/events/subscribers/passwordless_delivery_subscriber"
24
25
  require "standard_id/events/subscribers/password_reset_delivery_subscriber"
26
+ require "standard_id/events/subscribers/password_changed_notice_subscriber"
25
27
  require "standard_id/account_status"
26
28
  require "standard_id/account_locking"
27
29
  require "standard_id/http_client"
@@ -67,6 +69,7 @@ require "standard_id/oauth/protected_resource_document"
67
69
  require "standard_id/routing"
68
70
  require "standard_id/oauth/consent_payload"
69
71
  require "standard_id/oauth/client_registration"
72
+ require "standard_id/email_domain"
70
73
  require "standard_id/passwordless/base_strategy"
71
74
  require "standard_id/passwordless/email_strategy"
72
75
  require "standard_id/passwordless/sms_strategy"
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.58.1
4
+ version: 0.60.0
5
5
  platform: ruby
6
6
  authors:
7
- - Jaryl Sim
7
+ - Rarebit One
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
@@ -71,6 +71,26 @@ dependencies:
71
71
  - - "~>"
72
72
  - !ruby/object:Gem::Version
73
73
  version: '1.3'
74
+ - !ruby/object:Gem::Dependency
75
+ name: public_suffix
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '5.0'
81
+ - - "<"
82
+ - !ruby/object:Gem::Version
83
+ version: '8'
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '5.0'
91
+ - - "<"
92
+ - !ruby/object:Gem::Version
93
+ version: '8'
74
94
  - !ruby/object:Gem::Dependency
75
95
  name: factory_bot
76
96
  requirement: !ruby/object:Gem::Requirement
@@ -205,6 +225,8 @@ files:
205
225
  - app/models/standard_id/username_identifier.rb
206
226
  - app/views/standard_id/account_notice_mailer/existing_account.html.erb
207
227
  - app/views/standard_id/account_notice_mailer/existing_account.text.erb
228
+ - app/views/standard_id/account_notice_mailer/password_changed.html.erb
229
+ - app/views/standard_id/account_notice_mailer/password_changed.text.erb
208
230
  - app/views/standard_id/password_reset_mailer/reset_email.html.erb
209
231
  - app/views/standard_id/password_reset_mailer/reset_email.text.erb
210
232
  - app/views/standard_id/passwordless_mailer/otp_email.html.erb
@@ -251,6 +273,7 @@ files:
251
273
  - db/migrate/20261022000000_add_signup_source_to_standard_id_identifiers.rb
252
274
  - db/migrate/20261028000000_add_value_lookup_indexes_to_standard_id_tables.rb
253
275
  - db/migrate/20261028100000_remove_redundant_standard_id_indexes.rb
276
+ - db/migrate/20261122000000_add_code_digest_to_standard_id_code_challenges.rb
254
277
  - lib/generators/standard_id/install/install_generator.rb
255
278
  - lib/generators/standard_id/install/templates/standard_id.rb
256
279
  - lib/generators/standard_id/install/templates/standard_id_reference.rb
@@ -276,6 +299,7 @@ files:
276
299
  - lib/standard_id/config_schema.rb
277
300
  - lib/standard_id/current_attributes.rb
278
301
  - lib/standard_id/deprecator.rb
302
+ - lib/standard_id/email_domain.rb
279
303
  - lib/standard_id/email_password.rb
280
304
  - lib/standard_id/email_password/authentication.rb
281
305
  - lib/standard_id/email_password/daily_limit.rb
@@ -292,11 +316,13 @@ files:
292
316
  - lib/standard_id/events/subscribers/account_status_subscriber.rb
293
317
  - lib/standard_id/events/subscribers/base.rb
294
318
  - lib/standard_id/events/subscribers/logging_subscriber.rb
319
+ - lib/standard_id/events/subscribers/password_changed_notice_subscriber.rb
295
320
  - lib/standard_id/events/subscribers/password_reset_delivery_subscriber.rb
296
321
  - lib/standard_id/events/subscribers/passwordless_delivery_subscriber.rb
297
322
  - lib/standard_id/http_client.rb
298
323
  - lib/standard_id/instrumentation.rb
299
324
  - lib/standard_id/jwt_service.rb
325
+ - lib/standard_id/keyed_digest.rb
300
326
  - lib/standard_id/login_method_policy.rb
301
327
  - lib/standard_id/migration_check.rb
302
328
  - lib/standard_id/migration_helpers.rb
@@ -349,6 +375,7 @@ files:
349
375
  - lib/standard_id/testing/factories/oauth.rb
350
376
  - lib/standard_id/testing/factories/sessions.rb
351
377
  - lib/standard_id/testing/factory_bot.rb
378
+ - lib/standard_id/testing/otp_helpers.rb
352
379
  - lib/standard_id/testing/provider_examples.rb
353
380
  - lib/standard_id/testing/request_helpers.rb
354
381
  - lib/standard_id/token_minter.rb
@@ -382,7 +409,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
382
409
  - !ruby/object:Gem::Version
383
410
  version: '0'
384
411
  requirements: []
385
- rubygems_version: 4.0.10
412
+ rubygems_version: 4.0.20
386
413
  specification_version: 4
387
414
  summary: A comprehensive authentication engine for Rails, built on the security primitives
388
415
  introduced in Rails 8.