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.

124 lines
3.0 KiB

  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 :check_suspension, only: [:destroy]
  7. prepend_before_action :authenticate_with_two_factor, if: :two_factor_enabled?, only: [:create]
  8. prepend_before_action :set_pack
  9. before_action :set_instance_presenter, only: [:new]
  10. def new
  11. Devise.omniauth_configs.each do |provider, config|
  12. return redirect_to(omniauth_authorize_path(resource_name, provider)) if config.strategy.redirect_at_sign_in
  13. end
  14. super
  15. end
  16. def create
  17. super do |resource|
  18. remember_me(resource)
  19. flash.delete(:notice)
  20. end
  21. end
  22. def destroy
  23. super
  24. flash.delete(:notice)
  25. end
  26. protected
  27. def find_user
  28. if session[:otp_user_id]
  29. User.find(session[:otp_user_id])
  30. elsif user_params[:email]
  31. if use_seamless_external_login? && Devise.check_at_sign && user_params[:email].index('@').nil?
  32. User.joins(:account).find_by(accounts: { username: user_params[:email] })
  33. else
  34. User.find_for_authentication(email: user_params[:email])
  35. end
  36. end
  37. end
  38. def user_params
  39. params.require(:user).permit(:email, :password, :otp_attempt)
  40. end
  41. def after_sign_in_path_for(resource)
  42. last_url = stored_location_for(:user)
  43. if home_paths(resource).include?(last_url)
  44. root_path
  45. else
  46. last_url || root_path
  47. end
  48. end
  49. def after_sign_out_path_for(_resource_or_scope)
  50. Devise.omniauth_configs.each_value do |config|
  51. return root_path if config.strategy.redirect_at_sign_in
  52. end
  53. super
  54. end
  55. def two_factor_enabled?
  56. find_user.try(:otp_required_for_login?)
  57. end
  58. def valid_otp_attempt?(user)
  59. user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
  60. user.invalidate_otp_backup_code!(user_params[:otp_attempt])
  61. rescue OpenSSL::Cipher::CipherError => _error
  62. false
  63. end
  64. def authenticate_with_two_factor
  65. user = self.resource = find_user
  66. if user_params[:otp_attempt].present? && session[:otp_user_id]
  67. authenticate_with_two_factor_via_otp(user)
  68. elsif user&.valid_password?(user_params[:password])
  69. prompt_for_two_factor(user)
  70. end
  71. end
  72. def authenticate_with_two_factor_via_otp(user)
  73. if valid_otp_attempt?(user)
  74. session.delete(:otp_user_id)
  75. remember_me(user)
  76. sign_in(user)
  77. else
  78. flash.now[:alert] = I18n.t('users.invalid_otp_token')
  79. prompt_for_two_factor(user)
  80. end
  81. end
  82. def prompt_for_two_factor(user)
  83. session[:otp_user_id] = user.id
  84. render :two_factor
  85. end
  86. private
  87. def set_pack
  88. use_pack 'auth'
  89. end
  90. def set_instance_presenter
  91. @instance_presenter = InstancePresenter.new
  92. end
  93. def home_paths(resource)
  94. paths = [about_path]
  95. if single_user_mode? && resource.is_a?(User)
  96. paths << short_account_path(username: resource.account)
  97. end
  98. paths
  99. end
  100. end