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.

49 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. skip_before_action :require_functional!
  8. def show
  9. @confirmation = Form::TwoFactorConfirmation.new
  10. end
  11. def create
  12. current_user.otp_secret = User.generate_otp_secret(32)
  13. current_user.save!
  14. redirect_to new_settings_two_factor_authentication_confirmation_path
  15. end
  16. def destroy
  17. if acceptable_code?
  18. current_user.otp_required_for_login = false
  19. current_user.save!
  20. redirect_to settings_two_factor_authentication_path
  21. else
  22. flash.now[:alert] = I18n.t('two_factor_authentication.wrong_code')
  23. @confirmation = Form::TwoFactorConfirmation.new
  24. render :show
  25. end
  26. end
  27. private
  28. def confirmation_params
  29. params.require(:form_two_factor_confirmation).permit(:code)
  30. end
  31. def verify_otp_required
  32. redirect_to settings_two_factor_authentication_path if current_user.otp_required_for_login?
  33. end
  34. def acceptable_code?
  35. current_user.validate_and_consume_otp!(confirmation_params[:code]) ||
  36. current_user.invalidate_otp_backup_code!(confirmation_params[:code])
  37. end
  38. end
  39. end