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.

47 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. module Settings
  3. class TwoFactorAuthenticationsController < BaseController
  4. layout 'admin'
  5. before_action :authenticate_user!
  6. before_action :verify_otp_required, only: [:create]
  7. def show
  8. @confirmation = Form::TwoFactorConfirmation.new
  9. end
  10. def create
  11. current_user.otp_secret = User.generate_otp_secret(32)
  12. current_user.save!
  13. redirect_to new_settings_two_factor_authentication_confirmation_path
  14. end
  15. def destroy
  16. if acceptable_code?
  17. current_user.otp_required_for_login = false
  18. current_user.save!
  19. redirect_to settings_two_factor_authentication_path
  20. else
  21. flash.now[:alert] = I18n.t('two_factor_authentication.wrong_code')
  22. @confirmation = Form::TwoFactorConfirmation.new
  23. render :show
  24. end
  25. end
  26. private
  27. def confirmation_params
  28. params.require(:form_two_factor_confirmation).permit(:code)
  29. end
  30. def verify_otp_required
  31. redirect_to settings_two_factor_authentication_path if current_user.otp_required_for_login?
  32. end
  33. def acceptable_code?
  34. current_user.validate_and_consume_otp!(confirmation_params[:code]) ||
  35. current_user.invalidate_otp_backup_code!(confirmation_params[:code])
  36. end
  37. end
  38. end