From e00d5a971aaf8742246f6a7f17f59942fa0fd453 Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Mon, 9 Sep 2019 15:21:05 -0400 Subject: [PATCH 1/2] Skip validations when setting direct_otp --- .../models/two_factor_authenticatable.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/two_factor_authentication/models/two_factor_authenticatable.rb b/lib/two_factor_authentication/models/two_factor_authenticatable.rb index 6d73a0fb..dff056b1 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/models/two_factor_authenticatable.rb @@ -101,10 +101,9 @@ def generate_totp_secret def create_direct_otp(options = {}) # Create a new random OTP and store it in the database digits = options[:length] || self.class.direct_otp_length || 6 - update_attributes( - direct_otp: random_base10(digits), - direct_otp_sent_at: Time.now.utc - ) + self.direct_otp = random_base10(digits) + self.direct_otp_sent_at = Time.now.utc + save(validate: false) end private @@ -122,7 +121,9 @@ def direct_otp_expired? end def clear_direct_otp - update_attributes(direct_otp: nil, direct_otp_sent_at: nil) + self.direct_otp = nil + self.direct_otp_sent_at = nil + save(validate: false) end end From a4601014ec6193592d679912e835608c1dcebb92 Mon Sep 17 00:00:00 2001 From: Joel Schneider Date: Tue, 15 Oct 2019 09:35:42 -0400 Subject: [PATCH 2/2] Update DB directly --- .../models/two_factor_authenticatable.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/two_factor_authentication/models/two_factor_authenticatable.rb b/lib/two_factor_authentication/models/two_factor_authenticatable.rb index dff056b1..ab7dd8ee 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/models/two_factor_authenticatable.rb @@ -61,7 +61,7 @@ def need_two_factor_authentication?(request) end def send_new_otp(options = {}) - create_direct_otp options + create_direct_otp(options) send_two_factor_authentication_code(direct_otp) end @@ -101,9 +101,10 @@ def generate_totp_secret def create_direct_otp(options = {}) # Create a new random OTP and store it in the database digits = options[:length] || self.class.direct_otp_length || 6 - self.direct_otp = random_base10(digits) - self.direct_otp_sent_at = Time.now.utc - save(validate: false) + update_columns( + direct_otp: random_base10(digits), + direct_otp_sent_at: Time.now.utc + ) end private @@ -121,9 +122,10 @@ def direct_otp_expired? end def clear_direct_otp - self.direct_otp = nil - self.direct_otp_sent_at = nil - save(validate: false) + update_columns( + direct_otp: nil, + direct_otp_sent_at: nil + ) end end