Chris@909: require 'openid/store/interface' Chris@909: Chris@909: module OpenIdAuthentication Chris@909: class DbStore < OpenID::Store::Interface Chris@909: def self.cleanup_nonces Chris@909: now = Time.now.to_i Chris@909: Nonce.delete_all(["timestamp > ? OR timestamp < ?", now + OpenID::Nonce.skew, now - OpenID::Nonce.skew]) Chris@909: end Chris@909: Chris@909: def self.cleanup_associations Chris@909: now = Time.now.to_i Chris@909: Association.delete_all(['issued + lifetime > ?',now]) Chris@909: end Chris@909: Chris@909: def store_association(server_url, assoc) Chris@909: remove_association(server_url, assoc.handle) Chris@909: Association.create(:server_url => server_url, Chris@909: :handle => assoc.handle, Chris@909: :secret => assoc.secret, Chris@909: :issued => assoc.issued, Chris@909: :lifetime => assoc.lifetime, Chris@909: :assoc_type => assoc.assoc_type) Chris@909: end Chris@909: Chris@909: def get_association(server_url, handle = nil) Chris@909: assocs = if handle.blank? Chris@909: Association.find_all_by_server_url(server_url) Chris@909: else Chris@909: Association.find_all_by_server_url_and_handle(server_url, handle) Chris@909: end Chris@909: Chris@909: assocs.reverse.each do |assoc| Chris@909: a = assoc.from_record Chris@909: if a.expires_in == 0 Chris@909: assoc.destroy Chris@909: else Chris@909: return a Chris@909: end Chris@909: end if assocs.any? Chris@909: Chris@909: return nil Chris@909: end Chris@909: Chris@909: def remove_association(server_url, handle) Chris@909: Association.delete_all(['server_url = ? AND handle = ?', server_url, handle]) > 0 Chris@909: end Chris@909: Chris@909: def use_nonce(server_url, timestamp, salt) Chris@909: return false if Nonce.find_by_server_url_and_timestamp_and_salt(server_url, timestamp, salt) Chris@909: return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew Chris@909: Nonce.create(:server_url => server_url, :timestamp => timestamp, :salt => salt) Chris@909: return true Chris@909: end Chris@909: end Chris@909: end