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.

176 lines
4.4 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
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
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 Auth::SessionsController < Devise::SessionsController
  3. include Devise::Controllers::Rememberable
  4. layout 'auth'
  5. skip_before_action :require_no_authentication, only: [:create]
  6. skip_before_action :require_functional!
  7. skip_before_action :update_user_sign_in
  8. include TwoFactorAuthenticationConcern
  9. include SignInTokenAuthenticationConcern
  10. before_action :set_instance_presenter, only: [:new]
  11. before_action :set_body_classes
  12. def new
  13. Devise.omniauth_configs.each do |provider, config|
  14. return redirect_to(omniauth_authorize_path(resource_name, provider)) if config.strategy.redirect_at_sign_in
  15. end
  16. super
  17. end
  18. def create
  19. super do |resource|
  20. # We only need to call this if this hasn't already been
  21. # called from one of the two-factor or sign-in token
  22. # authentication methods
  23. on_authentication_success(resource, :password) unless @on_authentication_success_called
  24. end
  25. end
  26. def destroy
  27. tmp_stored_location = stored_location_for(:user)
  28. super
  29. session.delete(:challenge_passed_at)
  30. flash.delete(:notice)
  31. store_location_for(:user, tmp_stored_location) if continue_after?
  32. end
  33. def webauthn_options
  34. user = find_user
  35. if user&.webauthn_enabled?
  36. options_for_get = WebAuthn::Credential.options_for_get(
  37. allow: user.webauthn_credentials.pluck(:external_id),
  38. user_verification: 'discouraged'
  39. )
  40. session[:webauthn_challenge] = options_for_get.challenge
  41. render json: options_for_get, status: :ok
  42. else
  43. render json: { error: t('webauthn_credentials.not_enabled') }, status: :unauthorized
  44. end
  45. end
  46. protected
  47. def find_user
  48. if user_params[:email].present?
  49. find_user_from_params
  50. elsif session[:attempt_user_id]
  51. User.find_by(id: session[:attempt_user_id])
  52. end
  53. end
  54. def find_user_from_params
  55. user = User.authenticate_with_ldap(user_params) if Devise.ldap_authentication
  56. user ||= User.authenticate_with_pam(user_params) if Devise.pam_authentication
  57. user ||= User.find_for_authentication(email: user_params[:email])
  58. user
  59. end
  60. def user_params
  61. params.require(:user).permit(:email, :password, :otp_attempt, :sign_in_token_attempt, credential: {})
  62. end
  63. def after_sign_in_path_for(resource)
  64. last_url = stored_location_for(:user)
  65. if home_paths(resource).include?(last_url)
  66. root_path
  67. else
  68. last_url || root_path
  69. end
  70. end
  71. def after_sign_out_path_for(_resource_or_scope)
  72. Devise.omniauth_configs.each_value do |config|
  73. return root_path if config.strategy.redirect_at_sign_in
  74. end
  75. super
  76. end
  77. def require_no_authentication
  78. super
  79. # Delete flash message that isn't entirely useful and may be confusing in
  80. # most cases because /web doesn't display/clear flash messages.
  81. flash.delete(:alert) if flash[:alert] == I18n.t('devise.failure.already_authenticated')
  82. end
  83. private
  84. def set_instance_presenter
  85. @instance_presenter = InstancePresenter.new
  86. end
  87. def set_body_classes
  88. @body_classes = 'lighter'
  89. end
  90. def home_paths(resource)
  91. paths = [about_path]
  92. if single_user_mode? && resource.is_a?(User)
  93. paths << short_account_path(username: resource.account)
  94. end
  95. paths
  96. end
  97. def continue_after?
  98. truthy_param?(:continue)
  99. end
  100. def restart_session
  101. clear_attempt_from_session
  102. redirect_to new_user_session_path, alert: I18n.t('devise.failure.timeout')
  103. end
  104. def set_attempt_session(user)
  105. session[:attempt_user_id] = user.id
  106. session[:attempt_user_updated_at] = user.updated_at.to_s
  107. end
  108. def clear_attempt_from_session
  109. session.delete(:attempt_user_id)
  110. session.delete(:attempt_user_updated_at)
  111. end
  112. def on_authentication_success(user, security_measure)
  113. @on_authentication_success_called = true
  114. clear_attempt_from_session
  115. user.update_sign_in!(request, new_sign_in: true)
  116. remember_me(user)
  117. sign_in(user)
  118. flash.delete(:notice)
  119. LoginActivity.create(
  120. user: user,
  121. success: true,
  122. authentication_method: security_measure,
  123. ip: request.remote_ip,
  124. user_agent: request.user_agent
  125. )
  126. end
  127. def on_authentication_failure(user, security_measure, failure_reason)
  128. LoginActivity.create(
  129. user: user,
  130. success: false,
  131. authentication_method: security_measure,
  132. failure_reason: failure_reason,
  133. ip: request.remote_ip,
  134. user_agent: request.user_agent
  135. )
  136. end
  137. end