Sha256: 847e84fad3e4b559699f22b819f2487259cebccfc31d54d3ea4666b72ca97cc8

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'two_factor_authentication/hooks/two_factor_authenticatable'
module Devise
  module Models
    module TwoFactorAuthenticatable
      extend ActiveSupport::Concern

      module ClassMethods
        def has_one_time_password(options = {})

          cattr_accessor :otp_column_name
          self.otp_column_name = "otp_secret_key"

          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, :max_login_attempts, :allowed_otp_drift_seconds)
      end

      module InstanceMethodsOnActivation
        def authenticate_otp(code, options = {})
          totp = ROTP::TOTP.new(self.otp_column)
          drift = options[:drift] || self.class.allowed_otp_drift_seconds

          totp.verify_with_drift(code, drift)
        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

        def need_two_factor_authentication?(request)
          true
        end

        def send_two_factor_authentication_code
          raise NotImplementedError.new("No default implementation - please define in your class.")
        end

        def max_login_attempts?
          second_factor_attempts_count >= self.class.max_login_attempts
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
two_factor_authentication-1.0 lib/two_factor_authentication/models/two_factor_authenticatable.rb