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.

104 lines
2.4 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 create
  11. super do |resource|
  12. remember_me(resource)
  13. flash.delete(:notice)
  14. end
  15. end
  16. def destroy
  17. super
  18. flash.delete(:notice)
  19. end
  20. protected
  21. def find_user
  22. if session[:otp_user_id]
  23. User.find(session[:otp_user_id])
  24. elsif user_params[:email]
  25. User.find_for_authentication(email: user_params[:email])
  26. end
  27. end
  28. def user_params
  29. params.require(:user).permit(:email, :password, :otp_attempt)
  30. end
  31. def after_sign_in_path_for(resource)
  32. last_url = stored_location_for(:user)
  33. if home_paths(resource).include?(last_url)
  34. root_path
  35. else
  36. last_url || root_path
  37. end
  38. end
  39. def two_factor_enabled?
  40. find_user.try(:otp_required_for_login?)
  41. end
  42. def valid_otp_attempt?(user)
  43. user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
  44. user.invalidate_otp_backup_code!(user_params[:otp_attempt])
  45. rescue OpenSSL::Cipher::CipherError => _error
  46. false
  47. end
  48. def authenticate_with_two_factor
  49. user = self.resource = find_user
  50. if user_params[:otp_attempt].present? && session[:otp_user_id]
  51. authenticate_with_two_factor_via_otp(user)
  52. elsif user&.valid_password?(user_params[:password])
  53. prompt_for_two_factor(user)
  54. end
  55. end
  56. def authenticate_with_two_factor_via_otp(user)
  57. if valid_otp_attempt?(user)
  58. session.delete(:otp_user_id)
  59. remember_me(user)
  60. sign_in(user)
  61. else
  62. flash.now[:alert] = I18n.t('users.invalid_otp_token')
  63. prompt_for_two_factor(user)
  64. end
  65. end
  66. def prompt_for_two_factor(user)
  67. session[:otp_user_id] = user.id
  68. render :two_factor
  69. end
  70. private
  71. def set_pack
  72. use_pack 'auth'
  73. end
  74. def set_instance_presenter
  75. @instance_presenter = InstancePresenter.new
  76. end
  77. def home_paths(resource)
  78. paths = [about_path]
  79. if single_user_mode? && resource.is_a?(User)
  80. paths << short_account_path(username: resource.account)
  81. end
  82. paths
  83. end
  84. end