Module: Devise::Models::TwoFactorAuthenticatable

Extended by:
ActiveSupport::Concern
Includes:
DatabaseAuthenticatable
Defined in:
lib/devise_two_factor/models/two_factor_authenticatable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.required_fields(klass) ⇒ Object



24
25
26
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 24

def self.required_fields(klass)
  [:encrypted_otp_secret, :encrypted_otp_secret_iv, :encrypted_otp_secret_salt, :consumed_timestep]
end

Instance Method Details

#clean_up_passwordsObject



58
59
60
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 58

def clean_up_passwords
  self.otp_attempt = nil
end

#current_otpObject



44
45
46
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 44

def current_otp
  otp.at(Time.now)
end

#current_otp_timestepObject

ROTP’s TOTP#timecode is private, so we duplicate it here



49
50
51
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 49

def current_otp_timestep
   Time.now.utc.to_i / otp.interval
end

#otp(otp_secret = self.otp_secret) ⇒ Object



40
41
42
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 40

def otp(otp_secret = self.otp_secret)
  ROTP::TOTP.new(otp_secret)
end

#otp_provisioning_uri(account, options = {}) ⇒ Object



53
54
55
56
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 53

def otp_provisioning_uri(, options = {})
  otp_secret = options[:otp_secret] || self.otp_secret
  ROTP::TOTP.new(otp_secret, options).provisioning_uri()
end

#validate_and_consume_otp!(code, options = {}) ⇒ Object

This defaults to the model’s otp_secret If this hasn’t been generated yet, pass a secret as an option



30
31
32
33
34
35
36
37
38
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 30

def validate_and_consume_otp!(code, options = {})
  otp_secret = options[:otp_secret] || self.otp_secret
  return false unless code.present? && otp_secret.present?

  totp = self.otp(otp_secret)
  return consume_otp! if totp.verify_with_drift(code, self.class.otp_allowed_drift)

  false
end