Class: OpenID::Store::Redis

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = ::Redis.current, prefix = "openid-store") ⇒ Redis

Returns a new instance of Redis.



9
10
11
12
# File 'lib/openid/store/redis.rb', line 9

def initialize(client = ::Redis.current, prefix = "openid-store")
  @redis = client
  @prefix = prefix
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/openid/store/redis.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#cleanupObject

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



78
79
80
# File 'lib/openid/store/redis.rb', line 78

def cleanup
  remove_keys(':*')
end

#cleanup_associationsObject

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



64
65
66
# File 'lib/openid/store/redis.rb', line 64

def cleanup_associations
  remove_keys(':a:*')
end

#cleanup_noncesObject

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



71
72
73
# File 'lib/openid/store/redis.rb', line 71

def cleanup_nonces
  remove_keys(':n:*')
end

#get_association(server_url, handle = nil) ⇒ Object

Fetch and deserialize an Association object from Redis



24
25
26
27
28
29
30
# 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) ⇒ Object

Remove matching association from Redis

return true when data is removed, otherwise false



35
36
37
38
39
40
41
42
43
# 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) ⇒ Object

Store an Association in Redis



15
16
17
18
19
20
21
# 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) ⇒ Object

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,



49
50
51
52
53
54
55
56
57
58
59
# 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