From 91e8eb40d7d2bab34d77a90a816dec56195c5ffb Mon Sep 17 00:00:00 2001 From: Ryan McGeary Date: Mon, 29 Jan 2018 14:26:23 -0700 Subject: [PATCH] Delegate logic of send_new_otp to user#send_new_otp_after_login? Instead of keeping this logic nested in the Warden hook, call a method on the user object to determine if a new OTP code should be delivered. --- .../hooks/two_factor_authenticatable.rb | 2 +- .../models/two_factor_authenticatable.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb b/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb index 254df84..3ff0341 100644 --- a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb @@ -7,7 +7,7 @@ Warden::Manager.after_authentication do |user, auth, options| if user.respond_to?(:need_two_factor_authentication?) && !bypass_by_cookie if auth.session(options[:scope])[TwoFactorAuthentication::NEED_AUTHENTICATION] = user.need_two_factor_authentication?(auth.request) - user.send_new_otp unless user.totp_enabled? + user.send_new_otp if user.send_new_otp_after_login? end end end diff --git a/lib/two_factor_authentication/models/two_factor_authenticatable.rb b/lib/two_factor_authentication/models/two_factor_authenticatable.rb index 68cf7ae..3c7de70 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/models/two_factor_authenticatable.rb @@ -62,6 +62,10 @@ module Devise send_two_factor_authentication_code(direct_otp) end + def send_new_otp_after_login? + !totp_enabled? + end + def send_two_factor_authentication_code(code) raise NotImplementedError.new("No default implementation - please define in your class.") end