Module: Devise::Models::TwoFactorAuthenticatable::InstanceMethodsOnActivation

Defined in:
lib/two_factor_authentication/models/two_factor_authenticatable.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_direct_otp(code) ⇒ Object



29
30
31
32
33
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 29

def authenticate_direct_otp(code)
  return false if direct_otp.nil? || direct_otp != code || direct_otp_expired?
  clear_direct_otp
  true
end

#authenticate_otp(code, options = {}) ⇒ Object



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

def authenticate_otp(code, options = {})
  return true if direct_otp && authenticate_direct_otp(code)
  return true if totp_enabled? && authenticate_totp(code, options)
  false
end

#authenticate_totp(code, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 35

def authenticate_totp(code, options = {})
  totp_secret = options[:otp_secret_key] || otp_secret_key
  digits = options[:otp_length] || self.class.otp_length
  drift = options[:drift] || self.class.allowed_otp_drift_seconds
  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_with_drift_and_prior(code, drift, totp_timestamp)
  return false unless new_timestamp
  self.totp_timestamp = new_timestamp
  true
end

#confirm_totp_secret(secret, code, options = {}) ⇒ Object



80
81
82
83
84
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 80

def confirm_totp_secret(secret, code, options = {})
  return false unless authenticate_totp(code, {otp_secret_key: secret})
  self.otp_secret_key = secret
  true
end

#create_direct_otp(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 90

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(
    direct_otp: random_base10(digits),
    direct_otp_sent_at: Time.now.utc
  )
end

#generate_totp_secretObject



86
87
88
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 86

def generate_totp_secret
  ROTP::Base32.random_base32
end

#max_login_attemptsObject



72
73
74
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 72

def 
  self.class.
end

#max_login_attempts?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 68

def 
  second_factor_attempts_count.to_i >= .to_i
end

#need_two_factor_authentication?(request) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 55

def need_two_factor_authentication?(request)
  true
end

#provisioning_uri(account = nil, options = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 47

def provisioning_uri( = nil, options = {})
  totp_secret = options[:otp_secret_key] || otp_secret_key
  options[:digits] ||= options[:otp_length] || self.class.otp_length
  raise "provisioning_uri called with no otp_secret_key set" if totp_secret.nil?
   ||= email if respond_to?(:email)
  ROTP::TOTP.new(totp_secret, options).provisioning_uri()
end

#send_new_otp(options = {}) ⇒ Object



59
60
61
62
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 59

def send_new_otp(options = {})
  create_direct_otp options
  send_two_factor_authentication_code(direct_otp)
end

#send_two_factor_authentication_code(code) ⇒ Object

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 64

def send_two_factor_authentication_code(code)
  raise NotImplementedError.new("No default implementation - please define in your class.")
end

#totp_enabled?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/two_factor_authentication/models/two_factor_authenticatable.rb', line 76

def totp_enabled?
  respond_to?(:otp_secret_key) && !otp_secret_key.nil?
end