sessions_controller.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # frozen_string_literal: true
  2. class Auth::SessionsController < Devise::SessionsController
  3. layout 'auth'
  4. skip_before_action :check_self_destruct!
  5. skip_before_action :require_no_authentication, only: [:create]
  6. skip_before_action :require_functional!
  7. skip_before_action :update_user_sign_in
  8. prepend_before_action :check_suspicious!, only: [:create]
  9. include Auth::TwoFactorAuthenticationConcern
  10. before_action :set_body_classes
  11. content_security_policy only: :new do |p|
  12. p.form_action(false)
  13. end
  14. def check_suspicious!
  15. user = find_user
  16. @login_is_suspicious = suspicious_sign_in?(user) unless user.nil?
  17. end
  18. def create
  19. super do |resource|
  20. # We only need to call this if this hasn't already been
  21. # called from one of the two-factor or sign-in token
  22. # authentication methods
  23. on_authentication_success(resource, :password) unless @on_authentication_success_called
  24. end
  25. end
  26. def destroy
  27. tmp_stored_location = stored_location_for(:user)
  28. super
  29. session.delete(:challenge_passed_at)
  30. flash.delete(:notice)
  31. store_location_for(:user, tmp_stored_location) if continue_after?
  32. end
  33. def webauthn_options
  34. user = User.find_by(id: session[:attempt_user_id])
  35. if user&.webauthn_enabled?
  36. options_for_get = WebAuthn::Credential.options_for_get(
  37. allow: user.webauthn_credentials.pluck(:external_id),
  38. user_verification: 'discouraged'
  39. )
  40. session[:webauthn_challenge] = options_for_get.challenge
  41. render json: options_for_get, status: 200
  42. else
  43. render json: { error: t('webauthn_credentials.not_enabled') }, status: 401
  44. end
  45. end
  46. protected
  47. def find_user
  48. if user_params[:email].present?
  49. find_user_from_params
  50. elsif session[:attempt_user_id]
  51. User.find_by(id: session[:attempt_user_id])
  52. end
  53. end
  54. def find_user_from_params
  55. user = User.authenticate_with_ldap(user_params) if Devise.ldap_authentication
  56. user ||= User.authenticate_with_pam(user_params) if Devise.pam_authentication
  57. user ||= User.find_for_authentication(email: user_params[:email])
  58. user
  59. end
  60. def user_params
  61. params.require(:user).permit(:email, :password, :otp_attempt, credential: {})
  62. end
  63. def after_sign_in_path_for(resource)
  64. last_url = stored_location_for(:user)
  65. if home_paths(resource).include?(last_url)
  66. root_path
  67. else
  68. last_url || root_path
  69. end
  70. end
  71. def require_no_authentication
  72. super
  73. # Delete flash message that isn't entirely useful and may be confusing in
  74. # most cases because /web doesn't display/clear flash messages.
  75. flash.delete(:alert) if flash[:alert] == I18n.t('devise.failure.already_authenticated')
  76. end
  77. private
  78. def set_body_classes
  79. @body_classes = 'lighter'
  80. end
  81. def home_paths(resource)
  82. paths = [about_path, '/explore']
  83. paths << short_account_path(username: resource.account) if single_user_mode? && resource.is_a?(User)
  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 register_attempt_in_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 @login_is_suspicious
  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