Class: OpenID::Store::Mongo

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

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mongo

Returns a new instance of Mongo.



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

def initialize(options = {})
  self.collection = options[:collection]
  self.collection.ensure_index('expiry',{ :expireAfterSeconds => 0 })
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



16
17
18
# File 'lib/openid/store/mongo.rb', line 16

def collection
  @collection
end

Instance Method Details

#assoc_key(server_url, assoc_handle = nil) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/openid/store/mongo.rb', line 79

def assoc_key(server_url, assoc_handle=nil)
  key = 'A' + server_url
  if assoc_handle
    key += '|' + assoc_handle
  end
  return key
end

#cleanupObject



90
91
# File 'lib/openid/store/mongo.rb', line 90

def cleanup
end

#cleanup_associationsObject



93
94
# File 'lib/openid/store/mongo.rb', line 93

def cleanup_associations
end

#cleanup_noncesObject



87
88
# File 'lib/openid/store/mongo.rb', line 87

def cleanup_nonces
end

#get_association(server_url, handle = nil) ⇒ Object

Returns a Association object from storage that matches the server_url. Returns nil if no such association is found or if the one matching association is expired. (Is allowed to GC expired associations when found.)



38
39
40
41
42
43
44
45
# File 'lib/openid/store/mongo.rb', line 38

def get_association(server_url, handle=nil)
  doc = collection.find_one({:_id => assoc_key(server_url, handle)})
  if doc 
    return OpenID::Association.deserialize(doc['value'])
  else
    return nil
  end
end

#remove_association(server_url, handle) ⇒ Object

If there is a matching association, remove it from the store and return true, otherwise return false.



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

def remove_association(server_url, handle)
  deleted = delete(assoc_key(server_url, handle))
  server_assoc = get_association(server_url)
  if server_assoc && server_assoc.handle == handle
    deleted = delete(assoc_key(server_url)) | deleted
  end
  return deleted
end

#store_association(server_url, association) ⇒ Object

Put a Association object into storage. When implementing a store, don’t assume that there are any limitations on the character set of the server_url. In particular, expect to see unescaped non-url-safe characters in the server_url field.



27
28
29
30
31
32
# File 'lib/openid/store/mongo.rb', line 27

def store_association(server_url, association)
  [nil, association.handle].each do |handle|
    key = assoc_key(server_url, handle)
    collection.save({:_id => key, :value => association.serialize, :expiry => expiry(association.lifetime)})
  end
end

#use_nonce(server_url, timestamp, salt) ⇒ Object

Return true if the nonce has not been used before, and store it for a while to make sure someone doesn’t try to use the same value again. Return false if the nonce has already been used or if the timestamp is not current. You can use OpenID::Store::Nonce::SKEW for your timestamp window. server_url: URL of the server from which the nonce originated timestamp: time the nonce was created in seconds since unix epoch salt: A random string that makes two nonces issued by a server in

the same second unique


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openid/store/mongo.rb', line 67

def use_nonce(server_url, timestamp, salt)
  return false if (timestamp - Time.now.to_i).abs > Nonce.skew
  ts = timestamp.to_s # base 10 seconds since epoch
  nonce_key = 'N' + server_url + '|' + ts + '|' + salt
  begin 
    result = collection.insert({:_id=>nonce_key, :expiry => expiry(Nonce.skew + 5)})
    return true
  rescue
    return false
  end
end