otp_authentication_controller.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. module Settings
  3. module TwoFactorAuthentication
  4. class OtpAuthenticationController < BaseController
  5. include ChallengableConcern
  6. skip_before_action :require_functional!
  7. before_action :verify_otp_not_enabled, only: [:show]
  8. before_action :require_challenge!, only: [:create]
  9. def show
  10. @confirmation = Form::TwoFactorConfirmation.new
  11. end
  12. def create
  13. session[:new_otp_secret] = User.generate_otp_secret(32)
  14. redirect_to new_settings_two_factor_authentication_confirmation_path
  15. end
  16. private
  17. def confirmation_params
  18. params.require(:form_two_factor_confirmation).permit(:otp_attempt)
  19. end
  20. def verify_otp_not_enabled
  21. redirect_to settings_two_factor_authentication_methods_path if current_user.otp_enabled?
  22. end
  23. def acceptable_code?
  24. current_user.validate_and_consume_otp!(confirmation_params[:otp_attempt]) ||
  25. current_user.invalidate_otp_backup_code!(confirmation_params[:otp_attempt])
  26. end
  27. end
  28. end
  29. end