diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index c8b44c20a..7c196ed69 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -201,7 +201,7 @@ class Users::RegistrationsController < Devise::RegistrationsController end def two_factor_qr_code - current_user.ensure_2fa_token! + current_user.assign_2fa_token! qr_code_url = ROTP::TOTP.new(current_user.otp_secret, issuer: 'SciNote').provisioning_uri(current_user.email) qr_code = RQRCode::QRCode.new(qr_code_url) render json: { qr_code: qr_code.as_svg } diff --git a/app/models/user.rb b/app/models/user.rb index b281dd57b..89609fadc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -628,9 +628,7 @@ class User < ApplicationRecord totp.verify(otp, drift_behind: 10) end - def ensure_2fa_token! - return if otp_secret - + def assign_2fa_token! self.otp_secret = ROTP::Base32.random save! end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2cd07954b..376050d13 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -335,7 +335,7 @@ describe User, type: :model do describe 'valid_otp?' do let(:user) { create :user } before do - user.ensure_2fa_token! + user.assign_2fa_token! allow_any_instance_of(ROTP::TOTP).to receive(:verify).and_return(nil) end