Module: ActiveModel::OneTimePassword::ClassMethods

Defined in:
lib/active_model/one_time_password.rb

Instance Method Summary collapse

Instance Method Details

#has_one_time_password(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_model/one_time_password.rb', line 6

def has_one_time_password(options = {})
  cattr_accessor :otp_column_name, :otp_counter_column_name
  class_attribute :otp_digits, :otp_counter_based

  self.otp_column_name = (options[:column_name] || "otp_secret_key").to_s
  self.otp_digits = options[:length] || 6

  self.otp_counter_based = (options[:counter_based] || false)
  self.otp_counter_column_name = (options[:counter_column_name] || "otp_counter").to_s

  include InstanceMethodsOnActivation

  before_create do
    self.otp_regenerate_secret if !otp_column
    self.otp_regenerate_counter if otp_counter_based && !otp_counter
  end

  if respond_to?(:attributes_protected_by_default)
    def self.attributes_protected_by_default #:nodoc:
      super + [otp_column_name, otp_counter_column_name]
    end
  end
end

#otp_random_secret(length = 20) ⇒ Object

Defaults to 160 bit long secret (meaning a 32 character long base32 secret)



32
33
34
# File 'lib/active_model/one_time_password.rb', line 32

def otp_random_secret(length = 20)
  ROTP::Base32.random(length)
end