public
Fork of rails/open_id_authentication
Description: OpenID authentication plugin
Homepage: http://rubyonrails.org
Clone URL: git://github.com/technoweenie/open_id_authentication.git
100644 74 lines (57 sloc) 2.202 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'digest/sha1'
require 'openid/store/interface'
 
module OpenIdAuthentication
  class MemCacheStore < OpenID::Store::Interface
    def initialize(*addresses)
      @connection = ActiveSupport::Cache::MemCacheStore.new(addresses)
    end
 
    def store_association(server_url, assoc)
      server_key = association_server_key(server_url)
      assoc_key = association_key(server_url, assoc.handle)
 
      assocs = @connection.read(server_key) || {}
      assocs[assoc.issued] = assoc_key
 
      @connection.write(server_key, assocs)
      @connection.write(assoc_key, assoc, :expires_in => assoc.lifetime)
    end
 
    def get_association(server_url, handle = nil)
      if handle
        @connection.read(association_key(server_url, handle))
      else
        server_key = association_server_key(server_url)
        assocs = @connection.read(server_key)
        return if assocs.nil?
 
        last_key = assocs[assocs.keys.sort.last]
        @connection.read(last_key)
      end
    end
 
    def remove_association(server_url, handle)
      server_key = association_server_key(server_url)
      assoc_key = association_key(server_url, handle)
      assocs = @connection.read(server_key)
 
      return false unless assocs && assocs.has_value?(assoc_key)
 
      assocs = assocs.delete_if { |key, value| value == assoc_key }
 
      @connection.write(server_key, assocs)
      @connection.delete(assoc_key)
 
      return true
    end
 
    def use_nonce(server_url, timestamp, salt)
      return false if @connection.read(nonce_key(server_url, salt))
      return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
      @connection.write(nonce_key(server_url, salt), timestamp, :expires_in => OpenID::Nonce.skew)
      return true
    end
 
    private
      def association_key(server_url, handle = nil)
        "openid_association_#{digest(server_url)}_#{digest(handle)}"
      end
 
      def association_server_key(server_url)
        "openid_association_server_#{digest(server_url)}"
      end
 
      def nonce_key(server_url, salt)
        "openid_nonce_#{digest(server_url)}_#{digest(salt)}"
      end
 
      def digest(text)
        Digest::SHA1.hexdigest(text)
      end
  end
end