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



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

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 respond_to?(:new_record) && !new_record?
    end
    result
  else
    totp = ROTP::TOTP.new(otp_column, digits: otp_digits)
    if drift = options[:drift]
      totp.verify(code, drift_behind: drift)
    else
      totp.verify(code)
    end
  end
end

#otp_code(options = {}) ⇒ Object



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

def otp_code(options = {})
  if otp_counter_based
    if options[:auto_increment]
      self.otp_counter += 1
      save if respond_to?(:new_record) && !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)
    else
      time = options
    end
    ROTP::TOTP.new(otp_column, digits: otp_digits).at(time)
  end
end

#otp_columnObject



93
94
95
# File 'lib/active_model/one_time_password.rb', line 93

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

#otp_column=(attr) ⇒ Object



97
98
99
# File 'lib/active_model/one_time_password.rb', line 97

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

#otp_counterObject



101
102
103
104
105
106
107
# File 'lib/active_model/one_time_password.rb', line 101

def otp_counter
  if self.class.otp_counter_column_name != "otp_counter"
    self.public_send(self.class.otp_counter_column_name)
  else
    super
  end
end

#otp_counter=(attr) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/active_model/one_time_password.rb', line 109

def otp_counter=(attr)
  if self.class.otp_counter_column_name != "otp_counter"
    self.public_send("#{self.class.otp_counter_column_name}=", attr)
  else
    super
  end
end

#otp_regenerate_counterObject



42
43
44
# File 'lib/active_model/one_time_password.rb', line 42

def otp_regenerate_counter
  self.otp_counter = 1
end

#otp_regenerate_secretObject



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

def otp_regenerate_secret
  self.otp_column = self.class.otp_random_secret
end

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



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

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

#serializable_hash(options = nil) ⇒ Object



117
118
119
120
121
122
# File 'lib/active_model/one_time_password.rb', line 117

def serializable_hash(options = nil)
  options ||= {}
  options[:except] = Array(options[:except])
  options[:except] << self.class.otp_column_name
  super(options)
end