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.

139 lines
3.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>
3 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>
3 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. resource.update_sign_in!(request, new_sign_in: true)
  21. remember_me(resource)
  22. flash.delete(:notice)
  23. end
  24. end
  25. def destroy
  26. tmp_stored_location = stored_location_for(:user)
  27. super
  28. session.delete(:challenge_passed_at)
  29. flash.delete(:notice)
  30. store_location_for(:user, tmp_stored_location) if continue_after?
  31. end
  32. def webauthn_options
  33. user = find_user
  34. if user.webauthn_enabled?
  35. options_for_get = WebAuthn::Credential.options_for_get(
  36. allow: user.webauthn_credentials.pluck(:external_id)
  37. )
  38. session[:webauthn_challenge] = options_for_get.challenge
  39. render json: options_for_get, status: :ok
  40. else
  41. render json: { error: t('webauthn_credentials.not_enabled') }, status: :unauthorized
  42. end
  43. end
  44. protected
  45. def find_user
  46. if session[:attempt_user_id]
  47. User.find_by(id: session[:attempt_user_id])
  48. else
  49. user = User.authenticate_with_ldap(user_params) if Devise.ldap_authentication
  50. user ||= User.authenticate_with_pam(user_params) if Devise.pam_authentication
  51. user ||= User.find_for_authentication(email: user_params[:email])
  52. user
  53. end
  54. end
  55. def user_params
  56. params.require(:user).permit(:email, :password, :otp_attempt, :sign_in_token_attempt, credential: {})
  57. end
  58. def after_sign_in_path_for(resource)
  59. last_url = stored_location_for(:user)
  60. if home_paths(resource).include?(last_url)
  61. root_path
  62. else
  63. last_url || root_path
  64. end
  65. end
  66. def after_sign_out_path_for(_resource_or_scope)
  67. Devise.omniauth_configs.each_value do |config|
  68. return root_path if config.strategy.redirect_at_sign_in
  69. end
  70. super
  71. end
  72. def require_no_authentication
  73. super
  74. # Delete flash message that isn't entirely useful and may be confusing in
  75. # most cases because /web doesn't display/clear flash messages.
  76. flash.delete(:alert) if flash[:alert] == I18n.t('devise.failure.already_authenticated')
  77. end
  78. private
  79. def set_instance_presenter
  80. @instance_presenter = InstancePresenter.new
  81. end
  82. def set_body_classes
  83. @body_classes = 'lighter'
  84. end
  85. def home_paths(resource)
  86. paths = [about_path]
  87. if single_user_mode? && resource.is_a?(User)
  88. paths << short_account_path(username: resource.account)
  89. end
  90. paths
  91. end
  92. def continue_after?
  93. truthy_param?(:continue)
  94. end
  95. def restart_session
  96. clear_attempt_from_session
  97. redirect_to new_user_session_path, alert: I18n.t('devise.failure.timeout')
  98. end
  99. def set_attempt_session(user)
  100. session[:attempt_user_id] = user.id
  101. session[:attempt_user_updated_at] = user.updated_at.to_s
  102. end
  103. def clear_attempt_from_session
  104. session.delete(:attempt_user_id)
  105. session.delete(:attempt_user_updated_at)
  106. end
  107. end