Class: OpenID::Store::Memcache

Inherits:
Interface show all
Defined in:
lib/openid/store/memcache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_client, key_prefix = "openid-store:") ⇒ Memcache

Returns a new instance of Memcache.



14
15
16
17
# File 'lib/openid/store/memcache.rb', line 14

def initialize(cache_client, key_prefix = "openid-store:")
  @cache_client = cache_client
  self.key_prefix = key_prefix
end

Instance Attribute Details

#key_prefixObject

Returns the value of attribute key_prefix.



12
13
14
# File 'lib/openid/store/memcache.rb', line 12

def key_prefix
  @key_prefix
end

Instance Method Details

#assoc_key(server_url, assoc_handle = nil) ⇒ Object



71
72
73
74
75
# File 'lib/openid/store/memcache.rb', line 71

def assoc_key(server_url, assoc_handle = nil)
  key = key_prefix + "A" + server_url
  key += "|" + assoc_handle if assoc_handle
  key
end

#cleanupObject



80
81
# File 'lib/openid/store/memcache.rb', line 80

def cleanup
end

#cleanup_associationsObject



83
84
# File 'lib/openid/store/memcache.rb', line 83

def cleanup_associations
end

#cleanup_noncesObject



77
78
# File 'lib/openid/store/memcache.rb', line 77

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.)



35
36
37
38
39
40
# File 'lib/openid/store/memcache.rb', line 35

def get_association(server_url, handle = nil)
  serialized = @cache_client.get(assoc_key(server_url, handle))
  return deserialize(serialized) if serialized

  nil
end

#remove_association(server_url, handle) ⇒ Object

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



44
45
46
47
48
49
# File 'lib/openid/store/memcache.rb', line 44

def remove_association(server_url, handle)
  deleted = delete(assoc_key(server_url, handle))
  server_assoc = get_association(server_url)
  deleted = delete(assoc_key(server_url)) | deleted if server_assoc && server_assoc.handle == handle
  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.



23
24
25
26
27
28
29
# File 'lib/openid/store/memcache.rb', line 23

def store_association(server_url, association)
  serialized = serialize(association)
  [nil, association.handle].each do |handle|
    key = assoc_key(server_url, handle)
    @cache_client.set(key, serialized, 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


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

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 = key_prefix + "N" + server_url + "|" + ts + "|" + salt
  result = @cache_client.add(nonce_key, "", expiry(Nonce.skew + 5))
  return !!(result =~ /^STORED/) if result.is_a?(String)

  !!result
end