From 19d5f7bf3cb2c65458129caa48bfc9fa4a53582e Mon Sep 17 00:00:00 2001 From: Kalpesh Fulpagare Date: Tue, 30 Jul 2019 17:05:51 +0530 Subject: [PATCH 1/2] Change method name 'decode_salt_if_encoded' to 'decode_otp_salt_if_encoded' for compatibility with attr_encrypted --- .../models/two_factor_authenticatable.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 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..0fd5d339 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/models/two_factor_authenticatable.rb @@ -40,7 +40,7 @@ def authenticate_totp(code, options = {}) raise "authenticate_totp called with no otp_secret_key set" if totp_secret.nil? totp = ROTP::TOTP.new(totp_secret, digits: digits) new_timestamp = totp.verify( - without_spaces(code), + without_spaces(code), drift_ahead: drift, drift_behind: drift, after: totp_timestamp ) return false unless new_timestamp @@ -189,7 +189,7 @@ def salt_for_attribute salt = encrypted_otp_secret_key_salt || self.encrypted_otp_secret_key_salt = generate_random_base64_encoded_salt - decode_salt_if_encoded(salt) + decode_otp_salt_if_encoded(salt) end def generate_random_base64_encoded_salt @@ -197,7 +197,7 @@ def generate_random_base64_encoded_salt prefix + [SecureRandom.random_bytes].pack('m') end - def decode_salt_if_encoded(salt) + def decode_otp_salt_if_encoded(salt) salt.slice(0).eql?('_') ? salt.slice(1..-1).unpack('m').first : salt end end From 1cc85708167a84ad04193f92cc51a5c96a8ae6c3 Mon Sep 17 00:00:00 2001 From: Kalpesh Fulpagare Date: Thu, 14 Apr 2022 11:18:58 +0530 Subject: [PATCH 2/2] Rails 6 changes --- .../models/two_factor_authenticatable.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/two_factor_authentication/models/two_factor_authenticatable.rb b/lib/two_factor_authentication/models/two_factor_authenticatable.rb index 0fd5d339..405b3705 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/models/two_factor_authenticatable.rb @@ -101,7 +101,7 @@ 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( + update( direct_otp: random_base10(digits), direct_otp_sent_at: Time.now.utc ) @@ -122,7 +122,7 @@ def direct_otp_expired? end def clear_direct_otp - update_attributes(direct_otp: nil, direct_otp_sent_at: nil) + update(direct_otp: nil, direct_otp_sent_at: nil) end end