Class: OpenID::Store::MongoidStore

Inherits:
Interface
  • Object
show all
Defined in:
lib/openid/store/mongoid_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanup_associationsObject



23
24
25
26
# File 'lib/openid/store/mongoid_store.rb', line 23

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

.cleanup_noncesObject



18
19
20
21
# File 'lib/openid/store/mongoid_store.rb', line 18

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

Instance Method Details

#get_association(server_url, handle = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openid/store/mongoid_store.rb', line 41

def get_association(server_url, handle = nil)
  assocs = if handle.blank?
    Association.find :all, :conditions => { :server_url => server_url }
  else
    Association.find :all, :conditions => { :server_url => server_url, :handle => handle }
  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



60
61
62
63
64
# File 'lib/openid/store/mongoid_store.rb', line 60

def remove_association(server_url, handle)
  Association.find(:all, :conditions => { :server_url => server_url, :handle => handle }).each do |assoc|
    assoc.destroy!
  end
end

#store_association(server_url, assoc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/openid/store/mongoid_store.rb', line 28

def store_association(server_url, assoc)
  remove_association(server_url, assoc.handle)

  # BSON::Binary is used because secrets raise an exception
  # due to character encoding
  Association.create(:server_url => server_url,
                     :handle     => assoc.handle,
                     :secret     => BSON::Binary.new(assoc.secret),
                     :issued     => assoc.issued,
                     :lifetime   => assoc.lifetime,
                     :assoc_type => assoc.assoc_type)
end

#use_nonce(server_url, timestamp, salt) ⇒ Object



66
67
68
69
70
71
# File 'lib/openid/store/mongoid_store.rb', line 66

def use_nonce(server_url, timestamp, salt)
  return false if Nonce.find(:first, :conditions => { :server_url => server_url, :timestamp => timestamp, :salt => salt})
  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