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



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

def assign_tmp
  self.update_attributes(:gauth_tmp => ROTP::Base32.random_base32(32), :gauth_tmp_datetime => DateTime.now)
  self.gauth_tmp
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)


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

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(params) ⇒ Object



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

def set_gauth_enabled(params)
  self.update_without_password(params)
end

#validate_token(token) ⇒ Object



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

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