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



62
63
64
65
66
67
68
69
70
71
# File 'lib/active_model/one_time_password.rb', line 62

def authenticate_otp(code, options = {})
  return false if code.nil? || code.empty?
  return true if backup_codes_enabled? && authenticate_backup_code(code)

  if otp_counter_based
    otp_counter == authenticate_hotp(code, options)
  else
    authenticate_totp(code, options).present?
  end
end

#backup_codes_enabled?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/active_model/one_time_password.rb', line 133

def backup_codes_enabled?
  self.class.attribute_method?(self.class.otp_backup_codes_column_name)
end

#otp_code(options = {}) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/active_model/one_time_password.rb', line 73

def otp_code(options = {})
  if otp_counter_based
    hotp_code(options)
  else
    totp_code(options)
  end
end

#otp_columnObject



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

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

#otp_column=(attr) ⇒ Object



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

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

#otp_counterObject



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

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



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

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_backup_codesObject



124
125
126
127
128
129
130
131
# File 'lib/active_model/one_time_password.rb', line 124

def otp_regenerate_backup_codes
  otp = ROTP::OTP.new(otp_column)
  backup_codes = Array.new(self.class.otp_backup_codes_count) do
    otp.generate_otp((SecureRandom.random_number(9e5) + 1e5).to_i)
  end

  public_send("#{self.class.otp_backup_codes_column_name}=", backup_codes)
end

#otp_regenerate_counterObject



58
59
60
# File 'lib/active_model/one_time_password.rb', line 58

def otp_regenerate_counter
  self.otp_counter = 1
end

#otp_regenerate_secretObject



54
55
56
# File 'lib/active_model/one_time_password.rb', line 54

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

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



81
82
83
84
85
86
87
88
89
90
# 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(, self.otp_counter)
  else
    ROTP::TOTP.new(otp_column, options).provisioning_uri()
  end
end

#serializable_hash(options = nil) ⇒ Object



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

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

  super(options)
end