Module: ActiveModel::OneTimePassword::InstanceMethodsOnActivation

Defined in:
lib/active_model/one_time_password.rb

Instance Method Summary collapse

Instance Method Details

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_model/one_time_password.rb', line 43

def authenticate_otp(code, options = {})
  if otp_counter_based
    hotp = ROTP::HOTP.new(otp_column, digits: otp_digits)
    result = hotp.verify(code, otp_counter)
    if result && options[:auto_increment]
      self.otp_counter += 1
      save if !new_record?
    end
    result
  else
    totp = ROTP::TOTP.new(otp_column, digits: otp_digits)
    if drift = options[:drift]
      totp.verify_with_drift(code, drift)
    else
      totp.verify(code)
    end
  end
end

#otp_code(options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_model/one_time_password.rb', line 62

def otp_code(options = {})
  if otp_counter_based
    if options[:auto_increment]
      self.otp_counter += 1
      save if !new_record?
    end
    ROTP::HOTP.new(otp_column, digits: otp_digits).at(self.otp_counter)
  else
    if options.is_a? Hash
      time = options.fetch(:time, Time.now)
      padding = options.fetch(:padding, true)
    else
      time = options
      padding = true
    end
    ROTP::TOTP.new(otp_column, digits: otp_digits).at(time, padding)
  end
end

#otp_columnObject



91
92
93
# File 'lib/active_model/one_time_password.rb', line 91

def otp_column
  self.send(self.class.otp_column_name)
end

#otp_column=(attr) ⇒ Object



95
96
97
# File 'lib/active_model/one_time_password.rb', line 95

def otp_column=(attr)
  self.send("#{self.class.otp_column_name}=", attr)
end

#otp_counterObject



99
100
101
# File 'lib/active_model/one_time_password.rb', line 99

def otp_counter
  self.send(self.class.otp_counter_column_name)
end

#otp_counter=(attr) ⇒ Object



103
104
105
# File 'lib/active_model/one_time_password.rb', line 103

def otp_counter=(attr)
  self.send("#{self.class.otp_counter_column_name}=", attr)
end

#otp_regenerate_counterObject



39
40
41
# File 'lib/active_model/one_time_password.rb', line 39

def otp_regenerate_counter
  self.otp_counter = 1
end

#otp_regenerate_secretObject



35
36
37
# File 'lib/active_model/one_time_password.rb', line 35

def otp_regenerate_secret
  self.otp_column = ROTP::Base32.random_base32
end

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



81
82
83
84
85
86
87
88
89
# File 'lib/active_model/one_time_password.rb', line 81

def provisioning_uri( = nil, options = {})
   ||= self.email if self.respond_to?(:email)

  if otp_counter_based
    ROTP::HOTP.new(otp_column, options).provisioning_uri()
  else
    ROTP::TOTP.new(otp_column, options).provisioning_uri()
  end
end