Class: TrustedDevice

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/trusted_device.rb

Overview

This is a list of devices that are trusted A one time key is used to authenticate and re-generated for the next logon

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.try_to_login(key, gen = false) ⇒ Object

Attempts to login using a one time key and generates a new one if it is accepted



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/trusted_device.rb', line 32

def self.(key, gen = false)
	entry = TrustedDevice.where("one_time_key = ? or next_key = ?", key, key).first
	if entry.nil?
		return nil
	else
		if !entry.expires.nil? && (entry.expires <=> Time.now) > 0	# We are past the expired time
			entry.destroy
			return nil
		end
		if entry.one_time_key != entry.next_key && entry.next_key == key	# so the accept failed?
			entry.one_time_key = entry.next_key # Perform an implicit accept
			entry.save							# Prevents the same key being used again
		end
		
		entry.generate_key if gen		# Key accepted, generate a new one
		return entry
	end
end

Instance Method Details

#accept_keyObject



24
25
26
27
# File 'app/models/trusted_device.rb', line 24

def accept_key
	self[:one_time_key] = self[:next_key]
	save!
end

#generate_keyObject

Generates a one-time key that will allow this device to login without interaction



18
19
20
21
22
# File 'app/models/trusted_device.rb', line 18

def generate_key
	self[:last_authenticated] = Time.now
	self[:next_key] = Digest::SHA1.hexdigest "#{Time.now.to_f.to_s}#{id}"
	save!
end