otp_authentication_controller.rb 1.1 KB

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