class OpenID::Store::Redis
Attributes
Public Class Methods
# File lib/open_id/store/redis.rb, line 11 def initialize(cache_client, key_prefix = 'openid-store:') @cache_client = cache_client @key_prefix = key_prefix end
Public Instance Methods
# File lib/open_id/store/redis.rb, line 71 def assoc_key(server_url, assoc_handle=nil) key = key_prefix + 'A' + server_url if assoc_handle key += '|' + assoc_handle end key end
# File lib/open_id/store/redis.rb, line 84 def cleanup end
# File lib/open_id/store/redis.rb, line 87 def cleanup_associations end
# File lib/open_id/store/redis.rb, line 81 def cleanup_nonces end
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.)
# File lib/open_id/store/redis.rb, line 31 def get_association(server_url, handle=nil) value = @cache_client.get(assoc_key(server_url, handle)) value ? deserialize(value) : nil end
If there is a matching association, remove it from the store and return true, otherwise return false.
# File lib/open_id/store/redis.rb, line 39 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 deleted end
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.
# File lib/open_id/store/redis.rb, line 20 def store_association(server_url, association) key = assoc_key(server_url, association.handle) value = serialize(association) @cache_client.setex(key, association.lifetime, value) end
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
# File lib/open_id/store/redis.rb, line 59 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 if result = @cache_client.setnx(nonce_key, '') @cache_client.expire(nonce_key, Nonce.skew + 5) end result end
Protected Instance Methods
# File lib/open_id/store/redis.rb, line 92 def delete(key) @cache_client.del(key) end
# File lib/open_id/store/redis.rb, line 100 def deserialize(assoc_str) Marshal.load(assoc_str) rescue ArgumentError nil end
# File lib/open_id/store/redis.rb, line 96 def serialize(assoc) Marshal.dump(assoc) end