Class: OpenID::Store::Memory

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

Overview

An in-memory implementation of Store. This class is mainly used for testing, though it may be useful for long-running single process apps. Note that this store is NOT thread-safe.

You should probably be looking at OpenID::Store::Filesystem

Instance Method Summary collapse

Methods inherited from Interface

#cleanup

Constructor Details

#initializeMemory

Returns a new instance of Memory.



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

def initialize
  @associations = Hash.new { |hash, key| hash[key] = {} }
  @nonces = {}
end

Instance Method Details

#cleanup_associationsObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/openid/store/memory.rb', line 47

def cleanup_associations
  count = 0
  @associations.each do |_server_url, assocs|
    assocs.each do |handle, assoc|
      if assoc.expires_in == 0
        assocs.delete(handle)
        count += 1
      end
    end
  end
  count
end

#cleanup_noncesObject



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

def cleanup_nonces
  count = 0
  now = Time.now.to_i
  @nonces.each do |nonce, timestamp|
    if (timestamp - now).abs > Nonce.skew
      @nonces.delete(nonce)
      count += 1
    end
  end
  count
end

#get_association(server_url, handle = nil) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/openid/store/memory.rb', line 21

def get_association(server_url, handle = nil)
  assocs = @associations[server_url]
  if handle
    assocs[handle]
  else
    assocs.values.sort_by(&:issued)[-1]
  end
end

#remove_association(server_url, handle) ⇒ Object



30
31
32
33
34
35
# File 'lib/openid/store/memory.rb', line 30

def remove_association(server_url, handle)
  assocs = @associations[server_url]
  return true if assocs.delete(handle)

  false
end

#store_association(server_url, assoc) ⇒ Object



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

def store_association(server_url, assoc)
  assocs = @associations[server_url]
  @associations[server_url] = assocs.merge({assoc.handle => deepcopy(assoc)})
end

#use_nonce(server_url, timestamp, salt) ⇒ Object



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

def use_nonce(server_url, timestamp, salt)
  return false if (timestamp - Time.now.to_i).abs > Nonce.skew

  nonce = [server_url, timestamp, salt].join("")
  return false if @nonces[nonce]

  @nonces[nonce] = timestamp
  true
end