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.

45 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. module Settings
  3. module TwoFactorAuthentication
  4. class ConfirmationsController < BaseController
  5. before_action :ensure_otp_secret
  6. def new
  7. prepare_two_factor_form
  8. end
  9. def create
  10. if current_user.validate_and_consume_otp!(confirmation_params[:code])
  11. flash[:notice] = I18n.t('two_factor_authentication.enabled_success')
  12. current_user.otp_required_for_login = true
  13. @recovery_codes = current_user.generate_otp_backup_codes!
  14. current_user.save!
  15. render 'settings/two_factor_authentication/recovery_codes/index'
  16. else
  17. flash.now[:alert] = I18n.t('two_factor_authentication.wrong_code')
  18. prepare_two_factor_form
  19. render :new
  20. end
  21. end
  22. private
  23. def confirmation_params
  24. params.require(:form_two_factor_confirmation).permit(:code)
  25. end
  26. def prepare_two_factor_form
  27. @confirmation = Form::TwoFactorConfirmation.new
  28. @provision_url = current_user.otp_provisioning_uri(current_user.email, issuer: Rails.configuration.x.local_domain)
  29. @qrcode = RQRCode::QRCode.new(@provision_url)
  30. end
  31. def ensure_otp_secret
  32. redirect_to settings_two_factor_authentication_path unless current_user.otp_secret
  33. end
  34. end
  35. end
  36. end