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.
 
 
 
 

127 lines
2.9 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 :require_functional!
  7. before_action :set_instance_presenter, only: [:new]
  8. before_action :set_body_classes
  9. def new
  10. Devise.omniauth_configs.each do |provider, config|
  11. return redirect_to(omniauth_authorize_path(resource_name, provider)) if config.strategy.redirect_at_sign_in
  12. end
  13. super
  14. end
  15. def create
  16. self.resource = begin
  17. if user_params[:email].blank? && session[:otp_user_id].present?
  18. User.find(session[:otp_user_id])
  19. else
  20. warden.authenticate!(auth_options)
  21. end
  22. end
  23. if resource.otp_required_for_login?
  24. if user_params[:otp_attempt].present? && session[:otp_user_id].present?
  25. authenticate_with_two_factor_via_otp(resource)
  26. else
  27. prompt_for_two_factor(resource)
  28. end
  29. else
  30. authenticate_and_respond(resource)
  31. end
  32. end
  33. def destroy
  34. tmp_stored_location = stored_location_for(:user)
  35. super
  36. session.delete(:challenge_passed_at)
  37. flash.delete(:notice)
  38. store_location_for(:user, tmp_stored_location) if continue_after?
  39. end
  40. protected
  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 valid_otp_attempt?(user)
  59. user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
  60. user.invalidate_otp_backup_code!(user_params[:otp_attempt])
  61. rescue OpenSSL::Cipher::CipherError
  62. false
  63. end
  64. def authenticate_with_two_factor_via_otp(user)
  65. if valid_otp_attempt?(user)
  66. session.delete(:otp_user_id)
  67. authenticate_and_respond(user)
  68. else
  69. flash.now[:alert] = I18n.t('users.invalid_otp_token')
  70. prompt_for_two_factor(user)
  71. end
  72. end
  73. def prompt_for_two_factor(user)
  74. session[:otp_user_id] = user.id
  75. render :two_factor
  76. end
  77. def authenticate_and_respond(user)
  78. sign_in(user)
  79. remember_me(user)
  80. respond_with user, location: after_sign_in_path_for(user)
  81. end
  82. private
  83. def set_instance_presenter
  84. @instance_presenter = InstancePresenter.new
  85. end
  86. def set_body_classes
  87. @body_classes = 'lighter'
  88. end
  89. def home_paths(resource)
  90. paths = [about_path]
  91. if single_user_mode? && resource.is_a?(User)
  92. paths << short_account_path(username: resource.account)
  93. end
  94. paths
  95. end
  96. def continue_after?
  97. truthy_param?(:continue)
  98. end
  99. end