sessions_controller.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 :require_functional!
  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. session.delete(:challenge_passed_at)
  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. else
  34. user = User.authenticate_with_ldap(user_params) if Devise.ldap_authentication
  35. user ||= User.authenticate_with_pam(user_params) if Devise.pam_authentication
  36. user ||= User.find_for_authentication(email: user_params[:email])
  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&.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
  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.present? && (user.encrypted_password.blank? || user.valid_password?(user_params[:password]))
  70. # If encrypted_password is blank, we got the user from LDAP or PAM,
  71. # so credentials are already valid
  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. @body_classes = 'lighter'
  88. render :two_factor
  89. end
  90. def require_no_authentication
  91. super
  92. # Delete flash message that isn't entirely useful and may be confusing in
  93. # most cases because /web doesn't display/clear flash messages.
  94. flash.delete(:alert) if flash[:alert] == I18n.t('devise.failure.already_authenticated')
  95. end
  96. private
  97. def set_instance_presenter
  98. @instance_presenter = InstancePresenter.new
  99. end
  100. def set_body_classes
  101. @body_classes = 'lighter'
  102. end
  103. def home_paths(resource)
  104. paths = [about_path]
  105. if single_user_mode? && resource.is_a?(User)
  106. paths << short_account_path(username: resource.account)
  107. end
  108. paths
  109. end
  110. def continue_after?
  111. truthy_param?(:continue)
  112. end
  113. end