闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.8 KiB

Add WebAuthn as an alternative 2FA method (#14466) * feat: add possibility of adding WebAuthn security keys to use as 2FA This adds a basic UI for enabling WebAuthn 2FA. We did a little refactor to the Settings page for editing the 2FA methods – now it will list the methods that are available to the user (TOTP and WebAuthn) and from there they'll be able to add or remove any of them. Also, it's worth mentioning that for enabling WebAuthn it's required to have TOTP enabled, so the first time that you go to the 2FA Settings page, you'll be asked to set it up. This work was inspired by the one donde by Github in their platform, and despite it could be approached in different ways, we decided to go with this one given that we feel that this gives a great UX. Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: add request for WebAuthn as second factor at login if enabled This commits adds the feature for using WebAuthn as a second factor for login when enabled. If users have WebAuthn enabled, now a page requesting for the use of a WebAuthn credential for log in will appear, although a link redirecting to the old page for logging in using a two-factor code will also be present. Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: add possibility of deleting WebAuthn Credentials Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: disable WebAuthn when an Admin disables 2FA for a user Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * feat: remove ability to disable TOTP leaving only WebAuthn as 2FA Following examples form other platforms like Github, we decided to make Webauthn 2FA secondary to 2FA with TOTP, so that we removed the possibility of removing TOTP authentication only, leaving users with just WEbAuthn as 2FA. Instead, users will have to click on 'Disable 2FA' in order to remove second factor auth. The reason for WebAuthn being secondary to TOPT is that in that way, users will still be able to log in using their code from their phone's application if they don't have their security keys with them – or maybe even lost them. * We had to change a little the flow for setting up TOTP, given that now it's possible to setting up again if you already had TOTP, in order to let users modify their authenticator app – given that now it's not possible for them to disable TOTP and set it up again with another authenticator app. So, basically, now instead of storing the new `otp_secret` in the user, we store it in the session until the process of set up is finished. This was because, as it was before, when users clicked on 'Edit' in the new two-factor methods lists page, but then went back without finishing the flow, their `otp_secret` had been changed therefore invalidating their previous authenticator app, making them unable to log in again using TOTP. Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com> * refactor: fix eslint errors The PR build was failing given that linting returning some errors. This commit attempts to fix them. * refactor: normalize i18n translations The build was failing given that i18n translations files were not normalized. This commits fixes that. * refactor: avoid having the webauthn gem locked to a specific version * refactor: use symbols for routes without '/' * refactor: avoid sending webauthn disabled email when 2FA is disabled When an admins disable 2FA for users, we were sending two mails to them, one notifying that 2FA was disabled and the other to notify that WebAuthn was disabled. As the second one is redundant since the first email includes it, we can remove it and send just one email to users. * refactor: avoid creating new env variable for webauthn_origin config * refactor: improve flash error messages for webauthn pages Co-authored-by: Facundo Padula <facundo.padula@cedarcode.com>
4 years ago
  1. # frozen_string_literal: true
  2. class UserMailer < Devise::Mailer
  3. layout 'mailer'
  4. helper :accounts
  5. helper :application
  6. helper :instance
  7. helper :statuses
  8. add_template_helper RoutingHelper
  9. def confirmation_instructions(user, token, **)
  10. @resource = user
  11. @token = token
  12. @instance = Rails.configuration.x.local_domain
  13. return if @resource.disabled?
  14. I18n.with_locale(@resource.locale || I18n.default_locale) do
  15. mail to: @resource.unconfirmed_email.presence || @resource.email,
  16. subject: I18n.t(@resource.pending_reconfirmation? ? 'devise.mailer.reconfirmation_instructions.subject' : 'devise.mailer.confirmation_instructions.subject', instance: @instance),
  17. template_name: @resource.pending_reconfirmation? ? 'reconfirmation_instructions' : 'confirmation_instructions'
  18. end
  19. end
  20. def reset_password_instructions(user, token, **)
  21. @resource = user
  22. @token = token
  23. @instance = Rails.configuration.x.local_domain
  24. return if @resource.disabled?
  25. I18n.with_locale(@resource.locale || I18n.default_locale) do
  26. mail to: @resource.email, subject: I18n.t('devise.mailer.reset_password_instructions.subject')
  27. end
  28. end
  29. def password_change(user, **)
  30. @resource = user
  31. @instance = Rails.configuration.x.local_domain
  32. return if @resource.disabled?
  33. I18n.with_locale(@resource.locale || I18n.default_locale) do
  34. mail to: @resource.email, subject: I18n.t('devise.mailer.password_change.subject')
  35. end
  36. end
  37. def email_changed(user, **)
  38. @resource = user
  39. @instance = Rails.configuration.x.local_domain
  40. return if @resource.disabled?
  41. I18n.with_locale(@resource.locale || I18n.default_locale) do
  42. mail to: @resource.email, subject: I18n.t('devise.mailer.email_changed.subject')
  43. end
  44. end
  45. def two_factor_enabled(user, **)
  46. @resource = user
  47. @instance = Rails.configuration.x.local_domain
  48. return if @resource.disabled?
  49. I18n.with_locale(@resource.locale || I18n.default_locale) do
  50. mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_enabled.subject')
  51. end
  52. end
  53. def two_factor_disabled(user, **)
  54. @resource = user
  55. @instance = Rails.configuration.x.local_domain
  56. return if @resource.disabled?
  57. I18n.with_locale(@resource.locale || I18n.default_locale) do
  58. mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_disabled.subject')
  59. end
  60. end
  61. def two_factor_recovery_codes_changed(user, **)
  62. @resource = user
  63. @instance = Rails.configuration.x.local_domain
  64. return if @resource.disabled?
  65. I18n.with_locale(@resource.locale || I18n.default_locale) do
  66. mail to: @resource.email, subject: I18n.t('devise.mailer.two_factor_recovery_codes_changed.subject')
  67. end
  68. end
  69. def webauthn_enabled(user, **)
  70. @resource = user
  71. @instance = Rails.configuration.x.local_domain
  72. return if @resource.disabled?
  73. I18n.with_locale(@resource.locale || I18n.default_locale) do
  74. mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_enabled.subject')
  75. end
  76. end
  77. def webauthn_disabled(user, **)
  78. @resource = user
  79. @instance = Rails.configuration.x.local_domain
  80. return if @resource.disabled?
  81. I18n.with_locale(@resource.locale || I18n.default_locale) do
  82. mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_disabled.subject')
  83. end
  84. end
  85. def webauthn_credential_added(user, webauthn_credential)
  86. @resource = user
  87. @instance = Rails.configuration.x.local_domain
  88. @webauthn_credential = webauthn_credential
  89. return if @resource.disabled?
  90. I18n.with_locale(@resource.locale || I18n.default_locale) do
  91. mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.added.subject')
  92. end
  93. end
  94. def webauthn_credential_deleted(user, webauthn_credential)
  95. @resource = user
  96. @instance = Rails.configuration.x.local_domain
  97. @webauthn_credential = webauthn_credential
  98. return if @resource.disabled?
  99. I18n.with_locale(@resource.locale || I18n.default_locale) do
  100. mail to: @resource.email, subject: I18n.t('devise.mailer.webauthn_credential.deleted.subject')
  101. end
  102. end
  103. def welcome(user)
  104. @resource = user
  105. @instance = Rails.configuration.x.local_domain
  106. return if @resource.disabled?
  107. I18n.with_locale(@resource.locale || I18n.default_locale) do
  108. mail to: @resource.email, subject: I18n.t('user_mailer.welcome.subject')
  109. end
  110. end
  111. def backup_ready(user, backup)
  112. @resource = user
  113. @instance = Rails.configuration.x.local_domain
  114. @backup = backup
  115. return if @resource.disabled?
  116. I18n.with_locale(@resource.locale || I18n.default_locale) do
  117. mail to: @resource.email, subject: I18n.t('user_mailer.backup_ready.subject')
  118. end
  119. end
  120. def warning(user, warning, status_ids = nil)
  121. @resource = user
  122. @warning = warning
  123. @instance = Rails.configuration.x.local_domain
  124. @statuses = Status.where(id: status_ids).includes(:account) if status_ids.is_a?(Array)
  125. I18n.with_locale(@resource.locale || I18n.default_locale) do
  126. mail to: @resource.email,
  127. subject: I18n.t("user_mailer.warning.subject.#{@warning.action}", acct: "@#{user.account.local_username_and_domain}"),
  128. reply_to: Setting.site_contact_email
  129. end
  130. end
  131. def sign_in_token(user, remote_ip, user_agent, timestamp)
  132. @resource = user
  133. @instance = Rails.configuration.x.local_domain
  134. @remote_ip = remote_ip
  135. @user_agent = user_agent
  136. @detection = Browser.new(user_agent)
  137. @timestamp = timestamp.to_time.utc
  138. return if @resource.disabled?
  139. I18n.with_locale(@resource.locale || I18n.default_locale) do
  140. mail to: @resource.email,
  141. subject: I18n.t('user_mailer.sign_in_token.subject'),
  142. reply_to: Setting.site_contact_email
  143. end
  144. end
  145. end