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

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

Instance Method Summary collapse

Instance Method Details

#authenticate_direct_otp(code) ⇒ Object



42
43
44
45
46
47
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 42

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



36
37
38
39
40
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 36

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 49

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(
    without_spaces(code),
    drift_ahead: drift, drift_behind: drift, after: totp_timestamp
  )
  return false unless new_timestamp

  self.totp_timestamp = new_timestamp
  true
end

#create_direct_otp(options = {}) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 113

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

#enroll_totp!(otp_secret_key, code) ⇒ Object



74
75
76
77
78
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 74

def enroll_totp!(otp_secret_key, code)
  return false unless authenticate_totp(code, { otp_secret_key: otp_secret_key })

  update_columns(totp_timestamp: totp_timestamp, otp_secret_key: otp_secret_key)
end

#generate_totp_secretObject



109
110
111
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 109

def generate_totp_secret
  self.class.generate_totp_secret
end

#max_login_attemptsObject



101
102
103
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 101

def 
  self.class.
end

#max_login_attempts?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 97

def 
  second_factor_attempts_count.to_i >= .to_i
end

#need_two_factor_authentication?(request) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 80

def need_two_factor_authentication?(request)
  totp_enabled?
end

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



66
67
68
69
70
71
72
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 66

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



84
85
86
87
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 84

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

#send_new_otp_after_login?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 89

def send_new_otp_after_login?
  !totp_enabled?
end

#send_two_factor_authentication_code(code) ⇒ Object

Raises:

  • (NotImplementedError)


93
94
95
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 93

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

#totp_enabled?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/devise_multi_factor/models/two_factor_authenticatable.rb', line 105

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