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.

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