闭社主体 forked from https://github.com/tootsuite/mastodon
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.

50 lines
1.3 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. current_user.save!
  17. redirect_to settings_two_factor_auth_path, notice: I18n.t('two_factor_auth.enabled_success')
  18. else
  19. @confirmation = Form::TwoFactorConfirmation.new
  20. set_qr_code
  21. flash.now[:alert] = I18n.t('two_factor_auth.wrong_code')
  22. render action: :new
  23. end
  24. end
  25. def disable
  26. current_user.otp_required_for_login = false
  27. current_user.save!
  28. redirect_to settings_two_factor_auth_path
  29. end
  30. private
  31. def set_qr_code
  32. @provision_url = current_user.otp_provisioning_uri(current_user.email, issuer: Rails.configuration.x.local_domain)
  33. @qrcode = RQRCode::QRCode.new(@provision_url)
  34. end
  35. def confirmation_params
  36. params.require(:form_two_factor_confirmation).permit(:code)
  37. end
  38. end