The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

57 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class Settings::TwoFactorAuthsController < ApplicationController
  3. layout 'admin'
  4. before_action :authenticate_user!
  5. def show; end
  6. def new
  7. redirect_to settings_two_factor_auth_path if current_user.otp_required_for_login
  8. @confirmation = Form::TwoFactorConfirmation.new
  9. current_user.otp_secret = User.generate_otp_secret(32)
  10. current_user.save!
  11. set_qr_code
  12. end
  13. def create
  14. if current_user.validate_and_consume_otp!(confirmation_params[:code])
  15. current_user.otp_required_for_login = true
  16. @codes = current_user.generate_otp_backup_codes!
  17. current_user.save!
  18. flash[:notice] = I18n.t('two_factor_auth.enabled_success')
  19. else
  20. @confirmation = Form::TwoFactorConfirmation.new
  21. set_qr_code
  22. flash.now[:alert] = I18n.t('two_factor_auth.wrong_code')
  23. render action: :new
  24. end
  25. end
  26. def recovery_codes
  27. @codes = current_user.generate_otp_backup_codes!
  28. current_user.save!
  29. flash[:notice] = I18n.t('two_factor_auth.recovery_codes_regenerated')
  30. end
  31. def disable
  32. current_user.otp_required_for_login = false
  33. current_user.save!
  34. redirect_to settings_two_factor_auth_path
  35. end
  36. private
  37. def set_qr_code
  38. @provision_url = current_user.otp_provisioning_uri(current_user.email, issuer: Rails.configuration.x.local_domain)
  39. @qrcode = RQRCode::QRCode.new(@provision_url)
  40. end
  41. def confirmation_params
  42. params.require(:form_two_factor_confirmation).permit(:code)
  43. end
  44. end