josh-memcache_openid_store 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Joshua Peek
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,16 @@
1
+ = OpenID::Store::Memcache
2
+
3
+ Store OpenID associations, nonces, and auth keys in MemCache.
4
+
5
+ === Usage
6
+
7
+ OpenID::Store::Memcache is standardized to work with any 'ruby-openid' library.
8
+
9
+ You can use it directly with the OpenID::Consumer:
10
+
11
+ store = OpenID::Store::Memcache.new
12
+ OpenID::Consumer.new(session, store)
13
+
14
+ Or with another wrapper library like Rack::OpenID:
15
+
16
+ use Rack::OpenID, OpenID::Store::Memcache.new
@@ -0,0 +1,83 @@
1
+ require 'memcache'
2
+
3
+ require 'openid/store/interface'
4
+ require 'openid/store/nonce'
5
+
6
+ require 'digest/sha1'
7
+
8
+ module OpenID
9
+ module Store
10
+ class Memcache < Interface
11
+ def initialize(*addresses)
12
+ addresses = addresses.flatten
13
+ addresses = ['localhost'] if addresses.empty?
14
+ @addresses = addresses
15
+ @connection = MemCache.new(@addresses)
16
+ raise 'No memcache servers' unless @connection.servers.any? { |s| s.alive? }
17
+ end
18
+
19
+ def store_association(server_url, assoc)
20
+ server_key = association_server_key(server_url)
21
+ assoc_key = association_key(server_url, assoc.handle)
22
+
23
+ assocs = @connection.get(server_key) || {}
24
+ assocs[assoc.issued] = assoc_key
25
+
26
+ @connection.set(server_key, assocs)
27
+ @connection.set(assoc_key, assoc, assoc.lifetime)
28
+ end
29
+
30
+ def get_association(server_url, handle = nil)
31
+ if handle
32
+ @connection.get(association_key(server_url, handle))
33
+ else
34
+ server_key = association_server_key(server_url)
35
+ assocs = @connection.get(server_key)
36
+ return if assocs.nil? || assocs.empty?
37
+
38
+ last_key = assocs[assocs.keys.sort.last]
39
+ @connection.get(last_key)
40
+ end
41
+ end
42
+
43
+ def remove_association(server_url, handle)
44
+ server_key = association_server_key(server_url)
45
+ assoc_key = association_key(server_url, handle)
46
+ assocs = @connection.get(server_key)
47
+
48
+ return false unless assocs && assocs.has_value?(assoc_key)
49
+
50
+ assocs = assocs.delete_if { |key, value| value == assoc_key }
51
+
52
+ @connection.set(server_key, assocs)
53
+ @connection.delete(assoc_key)
54
+
55
+ return true
56
+ end
57
+
58
+ def use_nonce(server_url, timestamp, salt)
59
+ return false if @connection.get(nonce_key(server_url, salt))
60
+ return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
61
+ @connection.set(nonce_key(server_url, salt), timestamp, OpenID::Nonce.skew)
62
+ return true
63
+ end
64
+
65
+ private
66
+ def association_key(server_url, handle = nil)
67
+ "openid_association_#{digest(server_url)}_#{digest(handle)}"
68
+ end
69
+
70
+ def association_server_key(server_url)
71
+ "openid_association_server_#{digest(server_url)}"
72
+ end
73
+
74
+ def nonce_key(server_url, salt)
75
+ "openid_nonce_#{digest(server_url)}_#{digest(salt)}"
76
+ end
77
+
78
+ def digest(text)
79
+ Digest::SHA1.hexdigest(text)
80
+ end
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: josh-memcache_openid_store
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Peek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-openid
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.1.6
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: memcache-client
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.0
34
+ version:
35
+ description: MemCache OpenID Store allows you to store OpenID associations, nonces, and auth keys in MemCache.
36
+ email: josh@joshpeek.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ - MIT-LICENSE
44
+ files:
45
+ - lib/openid/store/memcache.rb
46
+ - README.rdoc
47
+ - MIT-LICENSE
48
+ has_rdoc: false
49
+ homepage: http://github.com/josh/memcache_openid_store
50
+ post_install_message:
51
+ rdoc_options: []
52
+
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.2.0
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: Store OpenID associations, nonces, and auth keys in MemCache
74
+ test_files: []
75
+