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.

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