sessions_controller.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # frozen_string_literal: true
  2. class Auth::SessionsController < Devise::SessionsController
  3. layout 'auth'
  4. skip_before_action :require_no_authentication, only: [:create]
  5. skip_before_action :require_functional!
  6. skip_before_action :update_user_sign_in
  7. include TwoFactorAuthenticationConcern
  8. before_action :set_instance_presenter, only: [:new]
  9. before_action :set_body_classes
  10. content_security_policy only: :new do |p|
  11. p.form_action(false)
  12. end
  13. def create
  14. super do |resource|
  15. # We only need to call this if this hasn't already been
  16. # called from one of the two-factor or sign-in token
  17. # authentication methods
  18. on_authentication_success(resource, :password) unless @on_authentication_success_called
  19. end
  20. end
  21. def destroy
  22. tmp_stored_location = stored_location_for(:user)
  23. super
  24. session.delete(:challenge_passed_at)
  25. flash.delete(:notice)
  26. store_location_for(:user, tmp_stored_location) if continue_after?
  27. end
  28. def webauthn_options
  29. user = User.find_by(id: session[:attempt_user_id])
  30. if user&.webauthn_enabled?
  31. options_for_get = WebAuthn::Credential.options_for_get(
  32. allow: user.webauthn_credentials.pluck(:external_id),
  33. user_verification: 'discouraged'
  34. )
  35. session[:webauthn_challenge] = options_for_get.challenge
  36. render json: options_for_get, status: :ok
  37. else
  38. render json: { error: t('webauthn_credentials.not_enabled') }, status: :unauthorized
  39. end
  40. end
  41. protected
  42. def find_user
  43. if user_params[:email].present?
  44. find_user_from_params
  45. elsif session[:attempt_user_id]
  46. User.find_by(id: session[:attempt_user_id])
  47. end
  48. end
  49. def find_user_from_params
  50. user = User.authenticate_with_ldap(user_params) if Devise.ldap_authentication
  51. user ||= User.authenticate_with_pam(user_params) if Devise.pam_authentication
  52. user ||= User.find_for_authentication(email: user_params[:email])
  53. user
  54. end
  55. def user_params
  56. params.require(:user).permit(:email, :password, :otp_attempt, credential: {})
  57. end
  58. def after_sign_in_path_for(resource)
  59. last_url = stored_location_for(:user)
  60. if home_paths(resource).include?(last_url)
  61. root_path
  62. else
  63. last_url || root_path
  64. end
  65. end
  66. def require_no_authentication
  67. super
  68. # Delete flash message that isn't entirely useful and may be confusing in
  69. # most cases because /web doesn't display/clear flash messages.
  70. flash.delete(:alert) if flash[:alert] == I18n.t('devise.failure.already_authenticated')
  71. end
  72. private
  73. def set_instance_presenter
  74. @instance_presenter = InstancePresenter.new
  75. end
  76. def set_body_classes
  77. @body_classes = 'lighter'
  78. end
  79. def home_paths(resource)
  80. paths = [about_path]
  81. if single_user_mode? && resource.is_a?(User)
  82. paths << short_account_path(username: resource.account)
  83. end
  84. paths
  85. end
  86. def continue_after?
  87. truthy_param?(:continue)
  88. end
  89. def restart_session
  90. clear_attempt_from_session
  91. redirect_to new_user_session_path, alert: I18n.t('devise.failure.timeout')
  92. end
  93. def set_attempt_session(user)
  94. session[:attempt_user_id] = user.id
  95. session[:attempt_user_updated_at] = user.updated_at.to_s
  96. end
  97. def clear_attempt_from_session
  98. session.delete(:attempt_user_id)
  99. session.delete(:attempt_user_updated_at)
  100. end
  101. def on_authentication_success(user, security_measure)
  102. @on_authentication_success_called = true
  103. clear_attempt_from_session
  104. user.update_sign_in!(new_sign_in: true)
  105. sign_in(user)
  106. flash.delete(:notice)
  107. LoginActivity.create(
  108. user: user,
  109. success: true,
  110. authentication_method: security_measure,
  111. ip: request.remote_ip,
  112. user_agent: request.user_agent
  113. )
  114. UserMailer.suspicious_sign_in(user, request.remote_ip, request.user_agent, Time.now.utc).deliver_later! if suspicious_sign_in?(user)
  115. end
  116. def suspicious_sign_in?(user)
  117. SuspiciousSignInDetector.new(user).suspicious?(request)
  118. end
  119. def on_authentication_failure(user, security_measure, failure_reason)
  120. LoginActivity.create(
  121. user: user,
  122. success: false,
  123. authentication_method: security_measure,
  124. failure_reason: failure_reason,
  125. ip: request.remote_ip,
  126. user_agent: request.user_agent
  127. )
  128. end
  129. end