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.

44 lines
1.2 KiB

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