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.

55 lines
1.6 KiB

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