Class: OpenID::Store::Mongoid

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

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Mongoid

Returns a new instance of Mongoid.



6
7
8
# File 'lib/openid/store/mongoid.rb', line 6

def initialize(connection)
  ::Mongoid.database = connection 
end

Instance Method Details

#cleanup_associationsObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/openid/store/mongoid.rb', line 60

def cleanup_associations
  count = 0
  # Not sure how to do this in Mongo, maybe someone can show me?
  OpenIDStoreMongoid::Association.all(:conditions => { :issued.gt => 0 }).each do |association|
    if association.lifetime + association.issued > Time.now.to_i
      association.destroy
      count += 1
    end
  end
  count
end

#cleanup_noncesObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/openid/store/mongoid.rb', line 49

def cleanup_nonces
  now = Time.now.to_i
  count = 0
  # Some weird bug with .destroy_all(:conditions => {:timestamp.gt => (now + OpenID::Nonce.skew), :timestamp.lt => (now - OpenID::Nonce.skew)})
  OpenIDStoreMongoid::Nonce.where(:timestamp.gt => (now + OpenID::Nonce.skew)).and(:timestamp.lt => (now - OpenID::Nonce.skew)).each do |nonce|
    nonce.destroy
    count += 1
  end
  count
end

#get_association(server_url, handle = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/openid/store/mongoid.rb', line 20

def get_association(server_url, handle=nil)
  assocs = if handle.blank?
      OpenIDStoreMongoid::Association.all(:conditions => { :server_url => server_url })
    else
      OpenIDStoreMongoid::Association.all(:conditions => { :server_url => server_url, :handle => handle })
    end

  assocs.to_a.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



39
40
41
# File 'lib/openid/store/mongoid.rb', line 39

def remove_association(server_url, handle)
  OpenIDStoreMongoid::Association.destroy_all(:conditions => { :server_url => server_url, :handle => handle }) > 0 ? true : false
end

#store_association(server_url, assoc) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/openid/store/mongoid.rb', line 10

def store_association(server_url, assoc)
  remove_association(server_url, assoc.handle)    
  OpenIDStoreMongoid::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



43
44
45
46
47
# File 'lib/openid/store/mongoid.rb', line 43

def use_nonce(server_url, timestamp, salt)
  return false if OpenIDStoreMongoid::Nonce.first(:conditions => { :server_url => server_url, :timestamp => timestamp, :salt => salt }) or (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
  OpenIDStoreMongoid::Nonce.create!(:server_url => server_url, :timestamp => timestamp, :salt => salt)
  return true
end