rails_authentication 0.6.0 → 0.7.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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +73 -7
  3. data/lib/generators/authentication/authentication_generator.rb +70 -8
  4. data/lib/generators/authentication/features/confirmable.rb +5 -5
  5. data/lib/generators/authentication/features/invitable.rb +5 -5
  6. data/lib/generators/authentication/features/lockable.rb +5 -5
  7. data/lib/generators/authentication/features/magic_link.rb +5 -5
  8. data/lib/generators/authentication/features/ott.rb +5 -5
  9. data/lib/generators/authentication/features/recoverable.rb +8 -6
  10. data/lib/generators/authentication/features/rememberable.rb +8 -3
  11. data/lib/generators/authentication/templates/app/controllers/concerns/authentication.rb.tt +61 -14
  12. data/lib/generators/authentication/templates/app/controllers/confirmations_controller.rb.tt +9 -5
  13. data/lib/generators/authentication/templates/app/controllers/invitations_controller.rb.tt +13 -4
  14. data/lib/generators/authentication/templates/app/controllers/magic_links_controller.rb.tt +9 -7
  15. data/lib/generators/authentication/templates/app/controllers/otts_controller.rb.tt +12 -9
  16. data/lib/generators/authentication/templates/app/controllers/passkey_sessions_controller.rb.tt +9 -7
  17. data/lib/generators/authentication/templates/app/controllers/passwords_controller.rb.tt +21 -8
  18. data/lib/generators/authentication/templates/app/controllers/registrations_controller.rb.tt +7 -5
  19. data/lib/generators/authentication/templates/app/controllers/sessions_controller.rb.tt +9 -7
  20. data/lib/generators/authentication/templates/app/controllers/unlocks_controller.rb.tt +7 -5
  21. data/lib/generators/authentication/templates/app/controllers/webauthn_credentials_controller.rb.tt +7 -3
  22. data/lib/generators/authentication/templates/app/mailers/confirmations_mailer.rb.tt +4 -2
  23. data/lib/generators/authentication/templates/app/mailers/invitations_mailer.rb.tt +3 -1
  24. data/lib/generators/authentication/templates/app/mailers/magic_links_mailer.rb.tt +3 -1
  25. data/lib/generators/authentication/templates/app/mailers/otts_mailer.rb.tt +3 -1
  26. data/lib/generators/authentication/templates/app/mailers/passwords_mailer.rb.tt +3 -1
  27. data/lib/generators/authentication/templates/app/mailers/unlocks_mailer.rb.tt +3 -1
  28. data/lib/generators/authentication/templates/app/models/concerns/confirmable_concern.rb.tt +7 -2
  29. data/lib/generators/authentication/templates/app/models/concerns/invitable_concern.rb.tt +12 -1
  30. data/lib/generators/authentication/templates/app/models/concerns/lockable_concern.rb.tt +8 -0
  31. data/lib/generators/authentication/templates/app/models/concerns/magic_link_concern.rb.tt +2 -0
  32. data/lib/generators/authentication/templates/app/models/concerns/ott_concern.rb.tt +2 -0
  33. data/lib/generators/authentication/templates/app/models/concerns/passkey_concern.rb.tt +2 -0
  34. data/lib/generators/authentication/templates/app/models/concerns/recoverable_concern.rb.tt +8 -0
  35. data/lib/generators/authentication/templates/app/models/concerns/timeoutable_concern.rb.tt +2 -0
  36. data/lib/generators/authentication/templates/app/models/concerns/trackable_concern.rb.tt +2 -0
  37. data/lib/generators/authentication/templates/app/models/concerns/validatable_concern.rb.tt +5 -3
  38. data/lib/generators/authentication/templates/app/models/session.rb.tt +45 -0
  39. data/lib/generators/authentication/templates/app/models/user_auth.rb.tt +5 -3
  40. data/lib/generators/authentication/templates/app/models/webauthn_credential.rb.tt +2 -0
  41. data/lib/generators/authentication/templates/app/views/confirmations/new.html.erb.tt +3 -3
  42. data/lib/generators/authentication/templates/app/views/confirmations_mailer/confirmation_instructions.html.erb.tt +3 -3
  43. data/lib/generators/authentication/templates/app/views/confirmations_mailer/confirmation_instructions.text.erb.tt +2 -2
  44. data/lib/generators/authentication/templates/app/views/invitations/edit.html.erb.tt +4 -4
  45. data/lib/generators/authentication/templates/app/views/invitations/new.html.erb.tt +3 -3
  46. data/lib/generators/authentication/templates/app/views/invitations_mailer/invitation_instructions.html.erb.tt +3 -3
  47. data/lib/generators/authentication/templates/app/views/invitations_mailer/invitation_instructions.text.erb.tt +2 -2
  48. data/lib/generators/authentication/templates/app/views/magic_links/new.html.erb.tt +4 -4
  49. data/lib/generators/authentication/templates/app/views/magic_links_mailer/magic_link.html.erb.tt +3 -3
  50. data/lib/generators/authentication/templates/app/views/magic_links_mailer/magic_link.text.erb.tt +2 -3
  51. data/lib/generators/authentication/templates/app/views/otts/edit.html.erb.tt +7 -7
  52. data/lib/generators/authentication/templates/app/views/otts_mailer/ott.html.erb.tt +3 -3
  53. data/lib/generators/authentication/templates/app/views/otts_mailer/ott.text.erb.tt +3 -3
  54. data/lib/generators/authentication/templates/app/views/passwords/edit.html.erb.tt +9 -0
  55. data/lib/generators/authentication/templates/app/views/passwords/new.html.erb.tt +8 -0
  56. data/lib/generators/authentication/templates/app/views/passwords_mailer/reset.html.erb.tt +3 -3
  57. data/lib/generators/authentication/templates/app/views/passwords_mailer/reset.text.erb.tt +2 -2
  58. data/lib/generators/authentication/templates/app/views/registrations/edit.html.erb.tt +6 -6
  59. data/lib/generators/authentication/templates/app/views/registrations/new.html.erb.tt +6 -6
  60. data/lib/generators/authentication/templates/app/views/sessions/new.html.erb.tt +20 -16
  61. data/lib/generators/authentication/templates/app/views/unlocks/new.html.erb.tt +3 -3
  62. data/lib/generators/authentication/templates/app/views/unlocks_mailer/unlock_instructions.html.erb.tt +3 -3
  63. data/lib/generators/authentication/templates/app/views/unlocks_mailer/unlock_instructions.text.erb.tt +3 -3
  64. data/lib/generators/authentication/templates/app/views/webauthn_credentials/index.html.erb.tt +5 -5
  65. data/lib/generators/authentication/templates/app/views/webauthn_credentials/new.html.erb.tt +12 -6
  66. data/lib/generators/authentication/templates/config/initializers/webauthn.rb.tt +2 -0
  67. data/lib/generators/authentication/templates/config/locales/authentication.en.yml.tt +421 -0
  68. data/lib/generators/authentication/templates/db/migrate/add_rememberable_to_sessions.rb.tt +5 -0
  69. data/lib/generators/authentication/templates/db/migrate/remove_remember_created_at_from_users.rb.tt +11 -0
  70. data/lib/rails_authentication/version.rb +1 -1
  71. metadata +8 -3
  72. data/lib/generators/authentication/templates/app/models/concerns/rememberable_concern.rb.tt +0 -15
  73. data/lib/generators/authentication/templates/db/migrate/add_rememberable_to_users.rb.tt +0 -5
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class MagicLinksController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_magic_link_path, alert: "Try again later." }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_magic_link_path, alert: t("flash.magic_links.create.alert.rate_limited") }
4
6
 
5
7
  def new
6
8
  end
@@ -9,34 +11,34 @@ class MagicLinksController < ApplicationController
9
11
  user = User.find_by(email_address: params[:email_address])
10
12
  user&.send_magic_link
11
13
 
12
- redirect_to new_session_path, notice: "Sign-in link sent (if a user with that email address exists)."
14
+ redirect_to new_session_path, notice: t("flash.magic_links.create.notice")
13
15
  end
14
16
 
15
17
  def show
16
18
  user = User.find_by_valid_magic_link_token(params[:token])
17
19
 
18
20
  if user.nil?
19
- redirect_to new_magic_link_path, alert: "Sign-in link is invalid or has expired."
21
+ redirect_to new_magic_link_path, alert: t("flash.magic_links.show.alert.invalid_token")
20
22
  <% if lockable? -%>
21
23
  elsif user.locked?
22
24
  <% if trackable? -%>
23
25
  record_authentication_attempt(user, success: false, failure_reason: "locked")
24
26
  <% end -%>
25
- redirect_to new_session_path, alert: "Your account is locked. Check your email for unlock instructions."
27
+ redirect_to new_session_path, alert: t("flash.magic_links.show.alert.locked")
26
28
  <% end -%>
27
29
  <% if confirmable? -%>
28
30
  elsif !user.confirmed?
29
31
  <% if trackable? -%>
30
32
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
31
33
  <% end -%>
32
- redirect_to new_session_path, alert: "You must confirm your email address before signing in."
34
+ redirect_to new_session_path, alert: t("flash.magic_links.show.alert.unconfirmed")
33
35
  <% end -%>
34
36
  <% if invitable? -%>
35
37
  elsif user.invitation_pending?
36
38
  <% if trackable? -%>
37
39
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
38
40
  <% end -%>
39
- redirect_to new_session_path, alert: "You must accept your invitation before signing in."
41
+ redirect_to new_session_path, alert: t("flash.magic_links.show.alert.invitation_pending")
40
42
  <% end -%>
41
43
  # Add your own authentication restrictions specific to your application
42
44
  # elsif user.deleted_at.present?
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class OttsController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: %i[ create update ], with: -> { redirect_to new_session_path, alert: "Try again later." }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: t("flash.otts.create.alert.rate_limited") }
6
+ rate_limit to: 10, within: 3.minutes, only: :update, with: -> { redirect_to new_session_path, alert: t("flash.otts.update.alert.rate_limited") }
4
7
 
5
8
  def create
6
9
  email_address = params[:email_address].presence || session[:ott_email_address]
@@ -8,7 +11,7 @@ class OttsController < ApplicationController
8
11
  user&.send_ott
9
12
  session[:ott_email_address] = email_address
10
13
 
11
- redirect_to edit_ott_path, notice: "We've emailed you a sign-in code (if a user with that email address exists)."
14
+ redirect_to edit_ott_path, notice: t("flash.otts.create.notice")
12
15
  end
13
16
 
14
17
  def edit
@@ -20,17 +23,17 @@ class OttsController < ApplicationController
20
23
 
21
24
  case user && user.verify_ott(params[:code])
22
25
  when nil, :expired
23
- redirect_to new_session_path, alert: "Your sign-in code is invalid or has expired. Request a new one."
26
+ redirect_to new_session_path, alert: t("flash.otts.update.alert.invalid_code")
24
27
  when :exhausted
25
28
  <% if trackable? -%>
26
29
  record_authentication_attempt(user, success: false, failure_reason: "ott_attempts_exhausted")
27
30
  <% end -%>
28
- redirect_to new_session_path, alert: "Too many incorrect attempts. Request a new code."
31
+ redirect_to new_session_path, alert: t("flash.otts.update.alert.too_many_attempts")
29
32
  when :invalid
30
33
  <% if trackable? -%>
31
34
  record_authentication_attempt(user, success: false, failure_reason: "invalid_ott")
32
35
  <% end -%>
33
- redirect_to edit_ott_path, alert: "Incorrect code. Try again."
36
+ redirect_to edit_ott_path, alert: t("flash.otts.update.alert.incorrect_code")
34
37
  else
35
38
  complete_ott_sign_in(user)
36
39
  end
@@ -44,7 +47,7 @@ class OttsController < ApplicationController
44
47
  <% if trackable? -%>
45
48
  record_authentication_attempt(user, success: false, failure_reason: "locked")
46
49
  <% end -%>
47
- return redirect_to new_session_path, alert: "Your account is locked. Check your email for unlock instructions."
50
+ return redirect_to new_session_path, alert: t("flash.otts.update.alert.locked")
48
51
  end
49
52
  <% end -%>
50
53
  <% if confirmable? -%>
@@ -52,7 +55,7 @@ class OttsController < ApplicationController
52
55
  <% if trackable? -%>
53
56
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
54
57
  <% end -%>
55
- return redirect_to new_session_path, alert: "You must confirm your email address before signing in."
58
+ return redirect_to new_session_path, alert: t("flash.otts.update.alert.unconfirmed")
56
59
  end
57
60
  <% end -%>
58
61
  <% if invitable? -%>
@@ -60,7 +63,7 @@ class OttsController < ApplicationController
60
63
  <% if trackable? -%>
61
64
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
62
65
  <% end -%>
63
- return redirect_to new_session_path, alert: "You must accept your invitation before signing in."
66
+ return redirect_to new_session_path, alert: t("flash.otts.update.alert.invitation_pending")
64
67
  end
65
68
  <% end -%>
66
69
  # Add your own authentication restrictions specific to your application
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class PasskeySessionsController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { render json: { error: "Try again later." }, status: :too_many_requests }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { render json: { error: t("json.passkey_sessions.create.alert.rate_limited") }, status: :too_many_requests }
4
6
 
5
7
  def create
6
8
  webauthn_credential = WebAuthn::Credential.from_get(params)
7
9
  stored_credential = WebauthnCredential.find_by(external_id: webauthn_credential.id)
8
10
 
9
11
  if stored_credential.nil?
10
- return render json: { error: "Passkey not recognized." }, status: :unprocessable_content
12
+ return render json: { error: t("json.passkey_sessions.create.alert.not_recognized") }, status: :unprocessable_content
11
13
  end
12
14
 
13
15
  begin
@@ -17,7 +19,7 @@ class PasskeySessionsController < ApplicationController
17
19
  sign_count: stored_credential.sign_count
18
20
  )
19
21
  rescue WebAuthn::Error
20
- return render json: { error: "Passkey verification failed." }, status: :unprocessable_content
22
+ return render json: { error: t("json.passkey_sessions.create.alert.verification_failed") }, status: :unprocessable_content
21
23
  end
22
24
 
23
25
  stored_credential.update!(sign_count: webauthn_credential.sign_count)
@@ -29,7 +31,7 @@ class PasskeySessionsController < ApplicationController
29
31
  <% if trackable? -%>
30
32
  record_authentication_attempt(user, success: false, failure_reason: "locked")
31
33
  <% end -%>
32
- return render json: { error: "Your account is locked. Check your email for unlock instructions." }, status: :unprocessable_content
34
+ return render json: { error: t("json.passkey_sessions.create.alert.locked") }, status: :unprocessable_content
33
35
  end
34
36
  <% end -%>
35
37
  <% if confirmable? -%>
@@ -37,7 +39,7 @@ class PasskeySessionsController < ApplicationController
37
39
  <% if trackable? -%>
38
40
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
39
41
  <% end -%>
40
- return render json: { error: "You must confirm your email address before signing in." }, status: :unprocessable_content
42
+ return render json: { error: t("json.passkey_sessions.create.alert.unconfirmed") }, status: :unprocessable_content
41
43
  end
42
44
  <% end -%>
43
45
  <% if invitable? -%>
@@ -45,7 +47,7 @@ class PasskeySessionsController < ApplicationController
45
47
  <% if trackable? -%>
46
48
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
47
49
  <% end -%>
48
- return render json: { error: "You must accept your invitation before signing in." }, status: :unprocessable_content
50
+ return render json: { error: t("json.passkey_sessions.create.alert.invitation_pending") }, status: :unprocessable_content
49
51
  end
50
52
  <% end -%>
51
53
  <% if lockable? -%>
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class PasswordsController < ApplicationController
2
- allow_unauthenticated_access
4
+ require_unauthenticated_access
3
5
  before_action :set_user_by_token, only: %i[ edit update ]
4
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_password_path, alert: "Try again later." }
6
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_password_path, alert: t("flash.passwords.create.alert.rate_limited") }
5
7
 
6
8
  def new
7
9
  end
@@ -11,22 +13,26 @@ class PasswordsController < ApplicationController
11
13
  user.send_reset_password_instructions
12
14
  end
13
15
 
14
- redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)."
16
+ redirect_to new_session_path, notice: t("flash.passwords.create.notice")
15
17
  end
16
18
 
17
19
  def edit
18
20
  end
19
21
 
20
22
  def update
21
- if @user.update(params.permit(:password, :password_confirmation))
23
+ @user.assign_attributes(params.permit(:password, :password_confirmation))
24
+
25
+ if @user.save(context: :password_reset)
22
26
  @user.clear_reset_password_token!
23
27
  <% if lockable? -%>
24
28
  @user.unlock!
25
29
  <% end -%>
26
- @user.sessions.destroy_all
27
- redirect_to new_session_path, notice: "Password has been reset."
30
+ terminate_all_sessions_for(@user)
31
+ redirect_to new_session_path, notice: t("flash.passwords.update.notice")
32
+ elsif @user.errors.of_kind?(:password, :blank)
33
+ redirect_to edit_password_path(params[:token]), alert: t("flash.passwords.update.alert.password_blank")
28
34
  else
29
- redirect_to edit_password_path(params[:token]), alert: "Passwords did not match."
35
+ redirect_to edit_password_path(params[:token]), alert: t("flash.passwords.update.alert.password_mismatch")
30
36
  end
31
37
  end
32
38
 
@@ -34,6 +40,13 @@ class PasswordsController < ApplicationController
34
40
 
35
41
  def set_user_by_token
36
42
  @user = User.find_by_valid_reset_password_token(params[:token])
37
- redirect_to new_password_path, alert: "Password reset link is invalid or has expired." unless @user
43
+ return if @user
44
+
45
+ alert = if action_name == "edit"
46
+ t("flash.passwords.edit.alert.invalid_token")
47
+ else
48
+ t("flash.passwords.update.alert.invalid_token")
49
+ end
50
+ redirect_to new_password_path, alert: alert
38
51
  end
39
52
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class RegistrationsController < ApplicationController
2
- allow_unauthenticated_access only: %i[ new create ]
4
+ require_unauthenticated_access only: %i[ new create ]
3
5
 
4
6
  def new
5
7
  @user = User.new
@@ -11,10 +13,10 @@ class RegistrationsController < ApplicationController
11
13
  if @user.save
12
14
  <% if confirmable? -%>
13
15
  @user.send_confirmation_instructions
14
- redirect_to new_session_path, notice: "Welcome! Check your email to confirm your account."
16
+ redirect_to new_session_path, notice: t("flash.registrations.create.notice")
15
17
  <% else -%>
16
18
  start_new_session_for @user
17
- redirect_to after_authentication_url, notice: "Welcome! You have signed up successfully."
19
+ redirect_to after_authentication_url, notice: t("flash.registrations.create.notice")
18
20
  <% end -%>
19
21
  else
20
22
  render :new, status: :unprocessable_content
@@ -29,7 +31,7 @@ class RegistrationsController < ApplicationController
29
31
  @user = Current.user
30
32
 
31
33
  if @user.update(user_params_for_update)
32
- redirect_to edit_registration_path, notice: "Your account has been updated."
34
+ redirect_to edit_registration_path, notice: t("flash.registrations.update.notice")
33
35
  else
34
36
  render :edit, status: :unprocessable_content
35
37
  end
@@ -39,7 +41,7 @@ class RegistrationsController < ApplicationController
39
41
  user = Current.user
40
42
  terminate_session
41
43
  user.destroy
42
- redirect_to new_session_path, notice: "Your account has been deleted.", status: :see_other
44
+ redirect_to new_session_path, notice: t("flash.registrations.destroy.notice"), status: :see_other
43
45
  end
44
46
 
45
47
  private
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class SessionsController < ApplicationController
2
- allow_unauthenticated_access only: %i[ new create ]
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: "Try again later." }
4
+ require_unauthenticated_access only: %i[ new create ]
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_path, alert: t("flash.sessions.create.alert.rate_limited") }
4
6
 
5
7
  def new
6
8
  <% if passkey? -%>
@@ -22,27 +24,27 @@ class SessionsController < ApplicationController
22
24
  <% if trackable? -%>
23
25
  record_authentication_attempt(account, success: false, failure_reason: "invalid_credentials")
24
26
  <% end -%>
25
- redirect_to new_session_path, alert: "Try another email address or password."
27
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.invalid")
26
28
  <% if lockable? -%>
27
29
  elsif user.locked?
28
30
  <% if trackable? -%>
29
31
  record_authentication_attempt(user, success: false, failure_reason: "locked")
30
32
  <% end -%>
31
- redirect_to new_session_path, alert: "Your account is locked. Check your email for unlock instructions."
33
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.locked")
32
34
  <% end -%>
33
35
  <% if confirmable? -%>
34
36
  elsif !user.confirmed?
35
37
  <% if trackable? -%>
36
38
  record_authentication_attempt(user, success: false, failure_reason: "unconfirmed")
37
39
  <% end -%>
38
- redirect_to new_session_path, alert: "You must confirm your email address before signing in."
40
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.unconfirmed")
39
41
  <% end -%>
40
42
  <% if invitable? -%>
41
43
  elsif user.invitation_pending?
42
44
  <% if trackable? -%>
43
45
  record_authentication_attempt(user, success: false, failure_reason: "invitation_pending")
44
46
  <% end -%>
45
- redirect_to new_session_path, alert: "You must accept your invitation before signing in."
47
+ redirect_to new_session_path, alert: t("flash.sessions.create.alert.invitation_pending")
46
48
  <% end -%>
47
49
  # Add your own authentication restrictions specific to your application
48
50
  # elsif user.deleted_at.present?
@@ -68,7 +70,7 @@ class SessionsController < ApplicationController
68
70
 
69
71
  def destroy
70
72
  terminate_session
71
- redirect_to new_session_path, status: :see_other
73
+ redirect_to new_session_path, status: :see_other, notice: t("flash.sessions.destroy.notice")
72
74
  end
73
75
  <% if trackable? -%>
74
76
 
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class UnlocksController < ApplicationController
2
- allow_unauthenticated_access
3
- rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_unlock_path, alert: "Try again later." }
4
+ require_unauthenticated_access
5
+ rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_unlock_path, alert: t("flash.unlocks.create.alert.rate_limited") }
4
6
 
5
7
  def new
6
8
  end
@@ -9,15 +11,15 @@ class UnlocksController < ApplicationController
9
11
  user = User.find_by(email_address: params[:email_address])
10
12
  user.send_unlock_instructions if user&.locked?
11
13
 
12
- redirect_to new_session_path, notice: "Unlock instructions sent (if user with that email address exists and is locked)."
14
+ redirect_to new_session_path, notice: t("flash.unlocks.create.notice")
13
15
  end
14
16
 
15
17
  def show
16
18
  if user = User.find_by_valid_unlock_token(params[:token])
17
19
  user.unlock!
18
- redirect_to new_session_path, notice: "Your account has been unlocked. Please sign in."
20
+ redirect_to new_session_path, notice: t("flash.unlocks.show.notice")
19
21
  else
20
- redirect_to new_unlock_path, alert: "Unlock link is invalid or has expired."
22
+ redirect_to new_unlock_path, alert: t("flash.unlocks.show.alert.invalid_token")
21
23
  end
22
24
  end
23
25
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class WebauthnCredentialsController < ApplicationController
2
4
  before_action :set_webauthn_credential, only: :destroy
3
5
 
@@ -21,18 +23,20 @@ class WebauthnCredentialsController < ApplicationController
21
23
  external_id: webauthn_credential.id,
22
24
  public_key: webauthn_credential.public_key,
23
25
  sign_count: webauthn_credential.sign_count,
24
- nickname: params[:nickname].presence || "Passkey"
26
+ nickname: params[:nickname].presence || t("webauthn_credentials.create.default_nickname")
25
27
  )
26
28
  session.delete(:webauthn_registration_challenge)
27
29
 
28
30
  render json: { redirect_to: webauthn_credentials_path }
29
31
  rescue WebAuthn::Error => e
30
- render json: { error: e.message }, status: :unprocessable_content
32
+ # The gem's message is technical and English-only, so log it and show a translated one.
33
+ logger.info "Passkey registration failed: #{e.message}"
34
+ render json: { error: t("json.webauthn_credentials.create.alert.save_failed") }, status: :unprocessable_content
31
35
  end
32
36
 
33
37
  def destroy
34
38
  @webauthn_credential.destroy
35
- redirect_to webauthn_credentials_path, notice: "Passkey removed."
39
+ redirect_to webauthn_credentials_path, notice: t("flash.webauthn_credentials.destroy.notice")
36
40
  end
37
41
 
38
42
  private
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ConfirmationsMailer < ApplicationMailer
2
4
  def confirmation_instructions(user)
3
5
  @user = user
4
6
  <% if reconfirmable? -%>
5
- mail subject: "Confirm your email address", to: user.unconfirmed_email.presence || user.email_address
7
+ mail subject: t('confirmations_mailer.confirmation_instructions.subject'), to: user.unconfirmed_email.presence || user.email_address
6
8
  <% else -%>
7
- mail subject: "Confirm your email address", to: user.email_address
9
+ mail subject: t('confirmations_mailer.confirmation_instructions.subject'), to: user.email_address
8
10
  <% end -%>
9
11
  end
10
12
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class InvitationsMailer < ApplicationMailer
2
4
  def invitation_instructions(user)
3
5
  @user = user
4
- mail subject: "You have been invited", to: user.email_address
6
+ mail subject: t('invitations_mailer.invitation_instructions.subject'), to: user.email_address
5
7
  end
6
8
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class MagicLinksMailer < ApplicationMailer
2
4
  def magic_link(user)
3
5
  @user = user
4
- mail subject: "Your sign-in link", to: user.email_address
6
+ mail subject: t('magic_links_mailer.magic_link.subject'), to: user.email_address
5
7
  end
6
8
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class OttsMailer < ApplicationMailer
2
4
  def ott(user)
3
5
  @user = user
4
- mail subject: "Your sign-in code", to: user.email_address
6
+ mail subject: t('otts_mailer.ott.subject'), to: user.email_address
5
7
  end
6
8
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class PasswordsMailer < ApplicationMailer
2
4
  def reset(user)
3
5
  @user = user
4
- mail subject: "Reset your password", to: user.email_address
6
+ mail subject: t('passwords_mailer.reset.subject'), to: user.email_address
5
7
  end
6
8
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class UnlocksMailer < ApplicationMailer
2
4
  def unlock_instructions(user)
3
5
  @user = user
4
- mail subject: "Unlock your account", to: user.email_address
6
+ mail subject: t('unlocks_mailer.unlock_instructions.subject'), to: user.email_address
5
7
  end
6
8
  end
@@ -1,14 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ConfirmableConcern
2
4
  extend ActiveSupport::Concern
3
5
 
4
6
  CONFIRMATION_TOKEN_EXPIRES_IN = 24.hours
5
- <% if reconfirmable? -%>
6
7
 
7
8
  included do
9
+ <% if reconfirmable? -%>
8
10
  before_update :postpone_email_address_change, if: :postponing_email_address_change?
9
11
  after_update_commit :deliver_reconfirmation_instructions, if: -> { saved_change_to_unconfirmed_email? && unconfirmed_email.present? }
10
- end
12
+
11
13
  <% end -%>
14
+ scope :confirmed, -> { where.not(confirmed_at: nil) }
15
+ scope :unconfirmed, -> { where(confirmed_at: nil) }
16
+ end
12
17
 
13
18
  class_methods do
14
19
  def find_by_valid_confirmation_token(token)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module InvitableConcern
2
4
  extend ActiveSupport::Concern
3
5
 
@@ -5,6 +7,15 @@ module InvitableConcern
5
7
 
6
8
  included do
7
9
  belongs_to :invited_by, polymorphic: true, optional: true
10
+
11
+ # has_secure_password treats a blank password as "unchanged", so without this a blank
12
+ # acceptance would accept the invitation while keeping the generated temporary password.
13
+ validates :password, presence: true, on: :invitation_acceptance
14
+
15
+ # Match invitation_pending?: a user who signed up without an invitation is neither.
16
+ scope :accepted, -> { where.not(invitation_accepted_at: nil) }
17
+ scope :pending, -> { where.not(invitation_token: nil).where(invitation_accepted_at: nil) }
18
+ scope :not_pending, -> { where(invitation_token: nil).or(where.not(invitation_accepted_at: nil)) }
8
19
  end
9
20
 
10
21
  class_methods do
@@ -82,6 +93,6 @@ module InvitableConcern
82
93
  <% if confirmable? -%>
83
94
  self.confirmed_at ||= Time.current # accepting the invite proves the email address
84
95
  <% end -%>
85
- save
96
+ save(context: :invitation_acceptance)
86
97
  end
87
98
  end
@@ -1,9 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LockableConcern
2
4
  extend ActiveSupport::Concern
3
5
 
4
6
  MAXIMUM_ATTEMPTS = 5
5
7
  UNLOCK_IN = 1.hour
6
8
 
9
+ included do
10
+ # Match locked?: a lock expires on its own after UNLOCK_IN.
11
+ scope :locked, -> { where(locked_at: UNLOCK_IN.ago..) }
12
+ scope :not_locked, -> { where(locked_at: nil).or(where(locked_at: ...UNLOCK_IN.ago)) }
13
+ end
14
+
7
15
  class_methods do
8
16
  def find_by_valid_unlock_token(token)
9
17
  return if token.blank?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MagicLinkConcern
2
4
  extend ActiveSupport::Concern
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OttConcern
2
4
  extend ActiveSupport::Concern
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module PasskeyConcern
2
4
  extend ActiveSupport::Concern
3
5
 
@@ -1,8 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RecoverableConcern
2
4
  extend ActiveSupport::Concern
3
5
 
4
6
  RESET_PASSWORD_TOKEN_EXPIRES_IN = 4.hours
5
7
 
8
+ included do
9
+ # has_secure_password treats a blank password as "unchanged", so without this a blank reset
10
+ # form would "succeed" (token cleared, sessions ended) while keeping the old password.
11
+ validates :password, presence: true, on: :password_reset
12
+ end
13
+
6
14
  class_methods do
7
15
  def find_by_valid_reset_password_token(token)
8
16
  return if token.blank?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TimeoutableConcern
2
4
  extend ActiveSupport::Concern
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module TrackableConcern
2
4
  extend ActiveSupport::Concern
3
5
 
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ValidatableConcern
2
4
  extend ActiveSupport::Concern
3
5
 
4
6
  EMAIL_ADDRESS_REGEXP = /\A[^@\s]+@[^@\s]+\z/
5
- PASSWORD_LENGTH = 6..72
7
+ PASSWORD_LENGTH = 6..ActiveModel::SecurePassword::MAX_PASSWORD_LENGTH_ALLOWED # bcrypt can't hash more than 72 bytes
6
8
  PASSWORD_DIGIT_REGEXP = /\d+/
7
9
  PASSWORD_UPPER_REGEXP = /[A-Z]+/
8
10
  PASSWORD_LOWER_REGEXP = /[a-z]+/
9
11
  PASSWORD_SPECIAL_REGEXP = /[^a-zA-Z\d\s]+/
10
- PASSWORD_MINIMUM_COMPLEXITY = 2 # Minumum number of complexities to match to be valid
12
+ PASSWORD_MINIMUM_COMPLEXITY = 2 # Minimum number of complexities to match to be valid
11
13
 
12
14
  included do
13
15
  validates :email_address, presence: true,
@@ -23,7 +25,7 @@ module ValidatableConcern
23
25
  def password_complexity
24
26
  return if password.blank? || valid_password_complexity?
25
27
 
26
- errors.add(:password, "must meet password complexity standards")
28
+ errors.add(:password, :complexity)
27
29
  end
28
30
 
29
31
  def valid_password_complexity?
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Session < ApplicationRecord
4
+ <% if rememberable? -%>
5
+ # How long a browser session lasts without activity even when it's remembered (the cookie is permanent)
6
+ REMEMBERED_TIMEOUT_IN = 90.days
7
+
8
+ <% end -%>
9
+ belongs_to :user
10
+ <% if timeoutable? && rememberable? -%>
11
+
12
+ scope :expired, lambda {
13
+ where(updated_at: ...REMEMBERED_TIMEOUT_IN.ago)
14
+ .or(where(remembered_at: nil, updated_at: ...TimeoutableConcern::TIMEOUT_IN.ago))
15
+ }
16
+
17
+ def remembered?
18
+ remembered_at.present?
19
+ end
20
+
21
+ def expired?
22
+ return false if updated_at.blank?
23
+
24
+ updated_at.before?(REMEMBERED_TIMEOUT_IN.ago) || (!remembered? && user.timedout?(updated_at))
25
+ end
26
+ <% elsif timeoutable? -%>
27
+
28
+ scope :expired, -> { where(updated_at: ...TimeoutableConcern::TIMEOUT_IN.ago) }
29
+
30
+ def expired?
31
+ user.timedout?(updated_at)
32
+ end
33
+ <% elsif rememberable? -%>
34
+
35
+ scope :expired, -> { where(updated_at: ...REMEMBERED_TIMEOUT_IN.ago) }
36
+
37
+ def remembered?
38
+ remembered_at.present?
39
+ end
40
+
41
+ def expired?
42
+ updated_at.present? && updated_at.before?(REMEMBERED_TIMEOUT_IN.ago)
43
+ end
44
+ <% end -%>
45
+ end