Module: Devise::Models::GoogleAuthenticatable::InstanceMethods

Defined in:
lib/devise_google_authenticatable/models/google_authenticatable.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#assign_tmpObject



27
28
29
30
# File 'lib/devise_google_authenticatable/models/google_authenticatable.rb', line 27

def assign_tmp
  self.update_attributes(:gauth_tmp => ROTP::Base32.random_base32(32), :gauth_tmp_datetime => DateTime.now)
  self.gauth_tmp
end

#gauth_enabled?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/devise_google_authenticatable/models/google_authenticatable.rb', line 53

def gauth_enabled?
  # Active_record seems to handle determining the status better this way
  if self.gauth_enabled.respond_to?("to_i")
    if self.gauth_enabled.to_i != 0
      return true
    else
      return false
    end
  # Mongoid does NOT have a .to_i for the Boolean return value, hence, we can just return it
  else
    return self.gauth_enabled
  end
end

#get_qrObject



18
19
20
# File 'lib/devise_google_authenticatable/models/google_authenticatable.rb', line 18

def get_qr
  self.gauth_secret
end

#require_token?(cookie) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/devise_google_authenticatable/models/google_authenticatable.rb', line 67

def require_token?(cookie)
  if self.class.ga_remembertime.nil? || cookie.blank?
    return true
  end
  array = cookie.to_s.split ','
  if array.count != 2
    return true
  end
  last_logged_in_email = array[0]
  last_logged_in_time = array[1].to_i
  return last_logged_in_email != self.email || (Time.now.to_i - last_logged_in_time) > self.class.ga_remembertime.to_i
end

#set_gauth_enabled(param) ⇒ Object



22
23
24
25
# File 'lib/devise_google_authenticatable/models/google_authenticatable.rb', line 22

def set_gauth_enabled(param)
  #self.update_without_password(params[gauth_enabled])
  self.update_attributes(:gauth_enabled => param)
end

#validate_token(token) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/devise_google_authenticatable/models/google_authenticatable.rb', line 32

def validate_token(token)
  return false if self.gauth_tmp_datetime.nil?
  if self.gauth_tmp_datetime < self.class.ga_timeout.ago
    return false
  else

    valid_vals = []
    valid_vals << ROTP::TOTP.new(self.get_qr).at(Time.now)
    (1..self.class.ga_timedrift).each do |cc|
      valid_vals << ROTP::TOTP.new(self.get_qr).at(Time.now.ago(30*cc))
      valid_vals << ROTP::TOTP.new(self.get_qr).at(Time.now.in(30*cc))
    end

    if valid_vals.include?(token.to_i)
      return true
    else
      return false
    end
  end
end