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



17
18
19
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 17

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

Instance Method Details

#clean_up_passwords ⇒ Object



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

def clean_up_passwords
  self.otp_attempt = nil
end

#current_otp ⇒ Object



35
36
37
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 35

def current_otp
  otp.at(Time.now)
end

#otp(otp_secret = self.otp_secret) ⇒ Object



31
32
33
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 31

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

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



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

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

#valid_otp?(code, options = {}) ⇒ Boolean

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

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/devise_two_factor/models/two_factor_authenticatable.rb', line 23

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

  totp = self.otp(otp_secret)
  totp.verify_with_drift(code, self.class.otp_allowed_drift)
end