Class: PasaporteStore

Inherits:
OpenID::Store::Interface
  • Object
show all
Includes:
Pasaporte::Models
Defined in:
lib/pasaporte/pasaporte_store.rb

Constant Summary

Constants included from Pasaporte::Models

Pasaporte::Models::MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pasaporte_domainObject

Returns the value of attribute pasaporte_domain.



4
5
6
# File 'lib/pasaporte/pasaporte_store.rb', line 4

def pasaporte_domain
  @pasaporte_domain
end

Instance Method Details

#cleanup_associationsObject



56
57
58
59
# File 'lib/pasaporte/pasaporte_store.rb', line 56

def cleanup_associations
  now = Time.now.to_i
  Association.delete_all(['issued + lifetime > ?',now])
end

#cleanup_noncesObject



51
52
53
54
# File 'lib/pasaporte/pasaporte_store.rb', line 51

def cleanup_nonces
  now = Time.now.to_i
  Nonce.delete_all(["timestamp > ? OR timestamp < ?", now + OpenID::Nonce.skew, now - OpenID::Nonce.skew])
end

#get_association(server_url, handle = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pasaporte/pasaporte_store.rb', line 18

def get_association(server_url, handle=nil)
  raise "Cannot load association without my own domain being set" unless @pasaporte_domain
  assocs = if handle.blank?
      Association.find_all_by_server_url_and_pasaporte_domain(server_url, pasaporte_domain)
    else
      Association.find_all_by_server_url_and_handle_and_pasaporte_domain(server_url, handle, pasaporte_domain)
    end
    
  assocs.reverse.each do |assoc|
    a = assoc.from_record    
    if a.expires_in == 0
      assoc.destroy
    else
      return a
    end
  end if assocs.any?
  
  return nil
end

#remove_association(server_url, handle) ⇒ Object



38
39
40
41
# File 'lib/pasaporte/pasaporte_store.rb', line 38

def remove_association(server_url, handle)
  raise "Cannot remove association without my own domain being set" unless pasaporte_domain
  Association.delete_all(['server_url = ? AND handle = ? AND pasaporte_domain = ?', server_url, handle, pasaporte_domain]) > 0
end

#store_association(server_url, assoc) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/pasaporte/pasaporte_store.rb', line 6

def store_association(server_url, assoc)
  raise "Cannot save association without my own domain being set" unless @pasaporte_domain
  remove_association(server_url, assoc.handle)    
  Association.create(:server_url => server_url,
                     :handle     => assoc.handle,
                     :secret     => assoc.secret,
                     :issued     => assoc.issued,
                     :lifetime   => assoc.lifetime,
                     :assoc_type => assoc.assoc_type,
                     :pasaporte_domain => pasaporte_domain)
end

#use_nonce(server_url, timestamp, salt) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/pasaporte/pasaporte_store.rb', line 43

def use_nonce(server_url, timestamp, salt)
  raise "Cannot look for nonce without my own domain being set" unless pasaporte_domain
  return false if Nonce.find_by_server_url_timestamp_salt_and_pasaporte_domain(server_url, timestamp, salt, pasaporte_domain)
  return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
  Nonce.create(:server_url => server_url, :timestamp => timestamp, :salt => salt)
  return true
end