The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

140 lines
3.5 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_user_permissions, only: [:destroy]
  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. after_action :clear_site_data, only: [:destroy]
  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. tmp_stored_location = stored_location_for(:user)
  25. super
  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. elsif user_params[:email]
  34. if use_seamless_external_login? && Devise.check_at_sign && user_params[:email].index('@').nil?
  35. User.joins(:account).find_by(accounts: { username: user_params[:email] })
  36. else
  37. User.find_for_authentication(email: user_params[:email])
  38. end
  39. end
  40. end
  41. def user_params
  42. params.require(:user).permit(:email, :password, :otp_attempt)
  43. end
  44. def after_sign_in_path_for(resource)
  45. last_url = stored_location_for(:user)
  46. if home_paths(resource).include?(last_url)
  47. root_path
  48. else
  49. last_url || root_path
  50. end
  51. end
  52. def after_sign_out_path_for(_resource_or_scope)
  53. Devise.omniauth_configs.each_value do |config|
  54. return root_path if config.strategy.redirect_at_sign_in
  55. end
  56. super
  57. end
  58. def two_factor_enabled?
  59. find_user.try(:otp_required_for_login?)
  60. end
  61. def valid_otp_attempt?(user)
  62. user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
  63. user.invalidate_otp_backup_code!(user_params[:otp_attempt])
  64. rescue OpenSSL::Cipher::CipherError => _error
  65. false
  66. end
  67. def authenticate_with_two_factor
  68. user = self.resource = find_user
  69. if user_params[:otp_attempt].present? && session[:otp_user_id]
  70. authenticate_with_two_factor_via_otp(user)
  71. elsif user&.valid_password?(user_params[:password])
  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. render :two_factor
  88. end
  89. private
  90. def set_instance_presenter
  91. @instance_presenter = InstancePresenter.new
  92. end
  93. def set_body_classes
  94. @body_classes = 'lighter'
  95. end
  96. def home_paths(resource)
  97. paths = [about_path]
  98. if single_user_mode? && resource.is_a?(User)
  99. paths << short_account_path(username: resource.account)
  100. end
  101. paths
  102. end
  103. def clear_site_data
  104. return if continue_after?
  105. # Should be '"*"' but that doen't work in Chrome (neither does '"executionContexts"')
  106. # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Clear-Site-Data
  107. response.headers['Clear-Site-Data'] = '"cache", "cookies", "storage"'
  108. end
  109. def continue_after?
  110. truthy_param?(:continue)
  111. end
  112. end