logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

sessions_controller.rb (2947B)


  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_suspension, 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. 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. super do |resource|
  17. remember_me(resource)
  18. flash.delete(:notice)
  19. end
  20. end
  21. def destroy
  22. super
  23. flash.delete(:notice)
  24. end
  25. protected
  26. def find_user
  27. if session[:otp_user_id]
  28. User.find(session[:otp_user_id])
  29. elsif user_params[:email]
  30. if use_seamless_external_login? && Devise.check_at_sign && user_params[:email].index('@').nil?
  31. User.joins(:account).find_by(accounts: { username: user_params[:email] })
  32. else
  33. User.find_for_authentication(email: user_params[:email])
  34. end
  35. end
  36. end
  37. def user_params
  38. params.require(:user).permit(:email, :password, :otp_attempt)
  39. end
  40. def after_sign_in_path_for(resource)
  41. last_url = stored_location_for(:user)
  42. if home_paths(resource).include?(last_url)
  43. root_path
  44. else
  45. last_url || root_path
  46. end
  47. end
  48. def after_sign_out_path_for(_resource_or_scope)
  49. Devise.omniauth_configs.each_value do |config|
  50. return root_path if config.strategy.redirect_at_sign_in
  51. end
  52. super
  53. end
  54. def two_factor_enabled?
  55. find_user.try(:otp_required_for_login?)
  56. end
  57. def valid_otp_attempt?(user)
  58. user.validate_and_consume_otp!(user_params[:otp_attempt]) ||
  59. user.invalidate_otp_backup_code!(user_params[:otp_attempt])
  60. rescue OpenSSL::Cipher::CipherError => _error
  61. false
  62. end
  63. def authenticate_with_two_factor
  64. user = self.resource = find_user
  65. if user_params[:otp_attempt].present? && session[:otp_user_id]
  66. authenticate_with_two_factor_via_otp(user)
  67. elsif user&.valid_password?(user_params[:password])
  68. prompt_for_two_factor(user)
  69. end
  70. end
  71. def authenticate_with_two_factor_via_otp(user)
  72. if valid_otp_attempt?(user)
  73. session.delete(:otp_user_id)
  74. remember_me(user)
  75. sign_in(user)
  76. else
  77. flash.now[:alert] = I18n.t('users.invalid_otp_token')
  78. prompt_for_two_factor(user)
  79. end
  80. end
  81. def prompt_for_two_factor(user)
  82. session[:otp_user_id] = user.id
  83. render :two_factor
  84. end
  85. private
  86. def set_instance_presenter
  87. @instance_presenter = InstancePresenter.new
  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. end