Skip to content

Commit

Permalink
Custom column name in has_one_time_password
Browse files Browse the repository at this point in the history
  • Loading branch information
robertomiranda committed Apr 6, 2013
1 parent e56e8d3 commit 95d0587
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/active_model/otp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ module Otp

module ClassMethods
def has_one_time_password(options = {})

cattr_accessor :otp_column_name

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

include InstanceMethodsOnActivation

before_create { self.otp_secret_key = ROTP::Base32.random_base32 }

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

module InstanceMethodsOnActivation
def authenticate_otp(code)
ROTP::TOTP.new(self.otp_secret_key).verify(code)
ROTP::TOTP.new(self.otp_column).verify(code)
end

def provisioning_uri
ROTP::TOTP.new(self.otp_secret_key).provisioning_url(self.email)
ROTP::TOTP.new(self.otp_column).provisioning_url(self.email)
end

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

end
end
end

0 comments on commit 95d0587

Please sign in to comment.