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



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

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

Instance Method Details

#clean_up_passwordsObject



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

def clean_up_passwords
  self.otp_attempt = nil
end

#current_otpObject



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

def current_otp
  otp.at(Time.now)
end

#otp(otp_secret = self.otp_secret) ⇒ Object



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

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

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



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

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)


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

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