Browse Source

Adding otp methods.

master
Matt Mueller 11 years ago
parent
commit
13f33a9445
  1. 4
      lib/two_factor_authentication/hooks/two_factor_authenticatable.rb
  2. 48
      lib/two_factor_authentication/models/two_factor_authenticatable.rb

4
lib/two_factor_authentication/hooks/two_factor_authenticatable.rb

@ -1,9 +1,7 @@
Warden::Manager.after_authentication do |user, auth, options|
if user.respond_to?(:need_two_factor_authentication?)
if auth.session(options[:scope])[:need_two_factor_authentication] = user.need_two_factor_authentication?(auth.request)
code = user.generate_two_factor_code
user.second_factor_pass_code = Digest::MD5.hexdigest(code)
user.save
code = user.otp_code
user.send_two_factor_authentication_code(code)
end
end

48
lib/two_factor_authentication/models/two_factor_authenticatable.rb

@ -5,15 +5,55 @@ module Devise
extend ActiveSupport::Concern
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_column = ROTP::Base32.random_base32 }
if respond_to?(:attributes_protected_by_default)
def self.attributes_protected_by_default #:nodoc:
super + [self.otp_column_name]
end
end
end
::Devise::Models.config(self, :login_code_random_pattern, :max_login_attempts)
end
def need_two_factor_authentication?(request)
true
module InstanceMethodsOnActivation
def authenticate_otp(code, options = {})
totp = ROTP::TOTP.new(self.otp_column)
if drift = options[:drift]
totp.verify_with_drift(code, drift)
else
totp.verify(code)
end
end
def otp_code(time = Time.now)
ROTP::TOTP.new(self.otp_column).at(time)
end
def provisioning_uri(account = nil)
account ||= self.email if self.respond_to?(:email)
ROTP::TOTP.new(self.otp_column).provisioning_uri(account)
end
def otp_column
self.send(self.class.otp_column_name)
end
def otp_column=(attr)
self.send("#{self.class.otp_column_name}=", attr)
end
end
def generate_two_factor_code
self.class.login_code_random_pattern.gen
def need_two_factor_authentication?(request)
true
end
def send_two_factor_authentication_code(code)

Loading…
Cancel
Save