1 module OpenIdAuthentication
2 class DbStore < OpenID::Store
7 nonces = Nonce.find(:all)
8 nonces.each {|n| n.destroy if now - n.created > 6.hours} unless nonces.nil?
10 # remove expired assocs
11 assocs = Association.find(:all)
12 assocs.each { |a| a.destroy if a.from_record.expired? } unless assocs.nil?
17 unless setting = Setting.find_by_setting('auth_key')
18 auth_key = OpenID::Util.random_string(20)
19 setting = Setting.create(:setting => 'auth_key', :value => auth_key)
25 def store_association(server_url, assoc)
26 remove_association(server_url, assoc.handle)
27 Association.create(:server_url => server_url,
28 :handle => assoc.handle,
29 :secret => assoc.secret,
30 :issued => assoc.issued,
31 :lifetime => assoc.lifetime,
32 :assoc_type => assoc.assoc_type)
35 def get_association(server_url, handle=nil)
36 assocs = handle.blank? ?
37 Association.find_all_by_server_url(server_url) :
38 Association.find_all_by_server_url_and_handle(server_url, handle)
40 assocs.reverse.each do |assoc|
52 def remove_association(server_url, handle)
53 assoc = Association.find_by_server_url_and_handle(server_url, handle)
61 def store_nonce(nonce)
63 Nonce.create :nonce => nonce, :created => Time.now.to_i
67 nonce = Nonce.find_by_nonce(nonce)
68 return false if nonce.nil?
70 age = Time.now.to_i - nonce.created
73 age < 6.hours # max nonce age of 6 hours