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.

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