class OpenID::Store::Redis

Attributes

prefix[R]

Public Class Methods

new(client = ::Redis.current, prefix = "openid-store") click to toggle source
# File lib/openid/store/redis.rb, line 9
def initialize(client = ::Redis.current, prefix = "openid-store")
  @redis = client
  @prefix = prefix
end

Public Instance Methods

cleanup() click to toggle source

Cleanup all OpenID data from Redis This iterates over Redis keys, so it’s better to rely on TTLs in production environments.

# File lib/openid/store/redis.rb, line 78
def cleanup
  remove_keys(':*')
end
cleanup_associations() click to toggle source

Cleanup associations from Redis This iterates over Redis keys, so it’s better to rely on TTLs in production environments.

# File lib/openid/store/redis.rb, line 64
def cleanup_associations
  remove_keys(':a:*')
end
cleanup_nonces() click to toggle source

Cleanup nonces from Redis This iterates over Redis keys, so it’s better to rely on TTLs in production environments.

# File lib/openid/store/redis.rb, line 71
def cleanup_nonces
  remove_keys(':n:*')
end
get_association(server_url, handle=nil) click to toggle source

Fetch and deserialize an Association object from Redis

# File lib/openid/store/redis.rb, line 24
def get_association(server_url, handle=nil)
  if serialized = @redis.get(assoc_key(server_url, handle))
    deserialize(serialized)
  else
    nil
  end
end
remove_association(url, handle) click to toggle source

Remove matching association from Redis

return true when data is removed, otherwise false

# File lib/openid/store/redis.rb, line 35
def remove_association(url, handle)
  deleted = @redis.del(assoc_key(url, handle))
  assoc = get_association(url)
  if assoc && assoc.handle == handle
    deleted + @redis.del(assoc_key(url))
  else
    deleted
  end > 0
end
store_association(server_url, association) click to toggle source

Store an Association in Redis

# File lib/openid/store/redis.rb, line 15
def store_association(server_url, association)
  serialized = serialize(association)
  [nil, association.handle].each do |handle|
    key = assoc_key(server_url, handle)
    @redis.setex(key, association.lifetime, serialized)
  end
end
use_nonce(server_url, timestamp, salt) click to toggle source

Use nonce and store that it has been used in Redis temporarily

Returns true if nonce has not been used before and is still usable,

# File lib/openid/store/redis.rb, line 49
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 = prefix + ':n:' + server_url + ':' + ts + ':' + salt
  if @redis.setnx(nonce_key, '')
    @redis.expire(nonce_key, Nonce.skew + 5)
    true
  else
    false
  end
end

Private Instance Methods

assoc_key(server_url, assoc_handle=nil) click to toggle source
# File lib/openid/store/redis.rb, line 90
def assoc_key(server_url, assoc_handle=nil)
  key = prefix + ':a:' + server_url
  if assoc_handle
    key + ':' + assoc_handle
  else
    key
  end
end
deserialize(assoc_str) click to toggle source
# File lib/openid/store/redis.rb, line 103
def deserialize(assoc_str)
  Marshal.load(assoc_str)
end
remove_keys(pattern) click to toggle source
# File lib/openid/store/redis.rb, line 84
def remove_keys(pattern)
  @redis.keys(prefix + pattern).each do |key|
    @redis.del(key)
  end
end
serialize(assoc) click to toggle source
# File lib/openid/store/redis.rb, line 99
def serialize(assoc)
  Marshal.dump(assoc)
end