diff --git a/lib/two_factor_authentication.rb b/lib/two_factor_authentication.rb index d39ab37..3e6e67e 100644 --- a/lib/two_factor_authentication.rb +++ b/lib/two_factor_authentication.rb @@ -10,6 +10,9 @@ require "rotp" module Devise mattr_accessor :max_login_attempts @@max_login_attempts = 3 + + mattr_accessor :allowed_otp_drift_seconds + @@allowed_otp_drift_seconds = 30 end module TwoFactorAuthentication diff --git a/lib/two_factor_authentication/models/two_factor_authenticatable.rb b/lib/two_factor_authentication/models/two_factor_authenticatable.rb index 6ec57b4..01009a6 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/two_factor_authentication/models/two_factor_authenticatable.rb @@ -20,17 +20,15 @@ module Devise end end end - ::Devise::Models.config(self, :max_login_attempts) + ::Devise::Models.config(self, :max_login_attempts, :allowed_otp_drift_seconds) end module InstanceMethodsOnActivation def authenticate_otp(code, options = {}) totp = ROTP::TOTP.new(self.otp_column) - if drift = options[:drift] - totp.verify_with_drift(code, drift) - else - totp.verify(code) - end + drift = options[:drift] || self.class.allowed_otp_drift_seconds + + totp.verify_with_drift(code, drift) end def otp_code(time = Time.now)