]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/open_id_authentication/lib/open_id_authentication/db_store.rb
Merge branch 'master' into openid
[rails.git] / vendor / plugins / open_id_authentication / lib / open_id_authentication / db_store.rb
1 require 'openid/store/interface'
2
3 module OpenIdAuthentication
4   class DbStore < OpenID::Store::Interface
5     def self.cleanup_nonces
6       now = Time.now.to_i
7       Nonce.delete_all(["timestamp > ? OR timestamp < ?", now + OpenID::Nonce.skew, now - OpenID::Nonce.skew])
8     end
9
10     def self.cleanup_associations
11       now = Time.now.to_i
12       Association.delete_all(['issued + lifetime > ?',now])
13     end
14
15     def store_association(server_url, assoc)
16       remove_association(server_url, assoc.handle)
17       Association.create(:server_url => server_url,
18                          :handle     => assoc.handle,
19                          :secret     => assoc.secret,
20                          :issued     => assoc.issued,
21                          :lifetime   => assoc.lifetime,
22                          :assoc_type => assoc.assoc_type)
23     end
24
25     def get_association(server_url, handle = nil)
26       assocs = if handle.blank?
27           Association.find_all_by_server_url(server_url)
28         else
29           Association.find_all_by_server_url_and_handle(server_url, handle)
30         end
31
32       assocs.reverse.each do |assoc|
33         a = assoc.from_record
34         if a.expires_in == 0
35           assoc.destroy
36         else
37           return a
38         end
39       end if assocs.any?
40
41       return nil
42     end
43
44     def remove_association(server_url, handle)
45       Association.delete_all(['server_url = ? AND handle = ?', server_url, handle]) > 0
46     end
47
48     def use_nonce(server_url, timestamp, salt)
49       return false if Nonce.find_by_server_url_and_timestamp_and_salt(server_url, timestamp, salt)
50       return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
51       Nonce.create(:server_url => server_url, :timestamp => timestamp, :salt => salt)
52       return true
53     end
54   end
55 end