annotate .svn/pristine/38/38d3fc6a695b34cdb3895e43b2a6149999c95fa1.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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