openid_couchdb_store 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sam Schenkman-Moore
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,7 @@
1
+ = Openid Couchdb Store
2
+
3
+ A store for OpenID using CouchDB. Right now depends on CouchRest, will try to to kick that dependency later.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2010 Sam Schenkman-Moore. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "openid_couchdb_store"
8
+ gem.summary = %Q{OpenID store using CouchDB}
9
+ gem.description = %Q{OpenID store using CouchDB. Right now using Couchrest, will make more flexible later, hopefully.}
10
+ gem.email = "samsm@samsm.com"
11
+ gem.homepage = "http://github.com/samsm/openid_couchdb_store"
12
+ gem.authors = ["Sam Schenkman-Moore"]
13
+ gem.add_development_dependency "couchrest", ">= 1.0.0"
14
+ gem.add_development_dependency "couchrest_extended_document", ">= 1.0.0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "openid_couchdb_store #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,135 @@
1
+ # Again, from the OpenID gem
2
+ require 'openid'
3
+ require 'openid/store/interface'
4
+ require "couchrest"
5
+ require "couchrest_extended_document"
6
+
7
+ module OpenidCouchdbStore
8
+
9
+ class Association < CouchRest::ExtendedDocument
10
+
11
+ set_callback :save, :before, :generate_expires_at
12
+
13
+ view_by :server_url
14
+ view_by :expires_at
15
+ view_by :server_url_plus_handle,
16
+ :map =>
17
+ "function(doc) {
18
+ if (doc['couchrest-type'] == 'OpenidCouchdbStore::Association' && doc['handle']) {
19
+ emit(doc['server_url'] + '&' + doc['handle'], 1);
20
+ }
21
+ }"
22
+ def self.by_server_url_and_handle(server, handle)
23
+ by_server_url_plus_handle(:key => "#{server}&#{handle}")
24
+ end
25
+
26
+ def self.expired
27
+ by_expires_at(:endkey => Time.now.to_i)
28
+ end
29
+
30
+ def generate_expires_at
31
+ self['expires_at'] = self['issued'] + self['lifetime']
32
+ end
33
+
34
+ end
35
+
36
+ class Nonce < CouchRest::ExtendedDocument
37
+ view_by :timestamp
38
+ view_by :server_url_salt_timestamp,
39
+ :map =>
40
+ "function(doc) {
41
+ if (doc['couchrest-type'] == 'OpenidCouchdbStore::Nonce' &&
42
+ doc.server_url &&
43
+ doc.salt &&
44
+ doc.timestamp) {
45
+ key = doc.server_url + '&' + doc.salt + '&' + doc.timestamp
46
+ emit(key, 1);
47
+ }
48
+ }"
49
+ def self.first_nonce(server_url,timestamp,salt)
50
+ by_server_url_salt_timestamp(:key => "#{server_url}&#{salt}&#{timestamp}").first
51
+ end
52
+
53
+ def self.expired
54
+ by_timestamp(:endkey => Time.now.to_i)
55
+ end
56
+
57
+ end
58
+
59
+ class Store < OpenID::Store::Interface
60
+
61
+ def initialize(couch_database)
62
+ association.use_database(couch_database)
63
+ nonce.use_database(couch_database)
64
+ end
65
+
66
+ def association
67
+ OpenidCouchdbStore::Association
68
+ end
69
+
70
+ def nonce
71
+ OpenidCouchdbStore::Nonce
72
+ end
73
+
74
+ def store_association(server_url, association)
75
+ remove_association(server_url, association.handle)
76
+
77
+ assoc = OpenidCouchdbStore::Association.create('server_url' => server_url,
78
+ 'handle' => association.handle,
79
+ 'secret' => OpenID::Util.to_base64(association.secret),
80
+ 'issued' => association.issued,
81
+ 'lifetime' => association.lifetime,
82
+ 'assoc_type' => association.assoc_type)
83
+ assoc
84
+ end
85
+
86
+ def get_association(server_url, handle=nil)
87
+ assoc_records = if (handle.nil? or handle.empty?)
88
+ Association.by_server_url(server_url)
89
+ else
90
+ Association.by_server_url_and_handle(server_url,handle)
91
+ end
92
+
93
+ # TODO: Removed .reverse here, make sure that was reasonable.
94
+ assoc_records.each do |a|
95
+ openid_association = OpenID::Association.new(a['handle'],
96
+ OpenID::Util.from_base64(a['secret']),
97
+ a['issued'],
98
+ a['lifetime'],
99
+ a['assoc_type'])
100
+ if openid_association.expires_in == 0
101
+ a.destroy
102
+ else
103
+ return openid_association
104
+ end
105
+ end if assoc_records.any? # <- may not be needed
106
+
107
+ # Fail if there isn't an acceptable association
108
+ return nil
109
+ end
110
+
111
+ def remove_association(server_url, handle)
112
+ Association.by_server_url_and_handle(server_url,handle).each do |association|
113
+ association.destroy
114
+ end
115
+ end
116
+
117
+ def use_nonce(server_url, timestamp, salt)
118
+ return false if Nonce.first_nonce(server_url, timestamp,salt)
119
+ return false if (timestamp - Time.now.to_i).abs > OpenID::Nonce.skew
120
+ Nonce.create({'server_url' => server_url, 'timestamp' => timestamp, 'salt' => salt})
121
+ return true
122
+ end
123
+
124
+ def cleanup_nonces
125
+ nonce.expired.each {|n| n.destroy }
126
+ end
127
+
128
+ def cleanup_associations
129
+ now = Time.now.to_i
130
+ Association.expired.each {|a| a.destroy }
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{openid_couchdb_store}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sam Schenkman-Moore"]
12
+ s.date = %q{2010-09-28}
13
+ s.description = %q{OpenID store using CouchDB. Right now using Couchrest, will make more flexible later, hopefully.}
14
+ s.email = %q{samsm@samsm.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/openid_couchdb_store.rb",
27
+ "openid_couchdb_store.gemspec",
28
+ "test/helper.rb",
29
+ "test/test_openid_couchdb_store.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/samsm/openid_couchdb_store}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.7}
35
+ s.summary = %q{OpenID store using CouchDB}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_openid_couchdb_store.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<couchrest>, [">= 1.0.0"])
47
+ s.add_development_dependency(%q<couchrest_extended_document>, [">= 1.0.0"])
48
+ else
49
+ s.add_dependency(%q<couchrest>, [">= 1.0.0"])
50
+ s.add_dependency(%q<couchrest_extended_document>, [">= 1.0.0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<couchrest>, [">= 1.0.0"])
54
+ s.add_dependency(%q<couchrest_extended_document>, [">= 1.0.0"])
55
+ end
56
+ end
57
+
data/test/helper.rb ADDED
@@ -0,0 +1,111 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'ruby-debug'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require 'openid_couchdb_store'
9
+
10
+ class Test::Unit::TestCase
11
+
12
+ def setup
13
+ timestamp(true)
14
+ end
15
+
16
+ ##
17
+ ## Couch-specific database test configuration here.
18
+ ##
19
+ def teardown
20
+ # Couchrest doesn't cope well with design documents getting deleted mind run, so ...
21
+ # CouchRest.delete "http://127.0.0.1:5984/openid_store_test"
22
+ db.documents['rows'].each do |d|
23
+ unless d['id'].index('_design')
24
+ db.delete_doc({'_rev'=>d['value']['rev'], '_id' => d['id']})
25
+ end
26
+ end
27
+ end
28
+
29
+ def db
30
+ @db ||= CouchRest.database!("http://127.0.0.1:5984/openid_store_test")
31
+ end
32
+
33
+ def store
34
+ @store ||= OpenidCouchdbStore::Store.new(db)
35
+ end
36
+
37
+ def insert_old_association
38
+ doc = {'server_url' => server_url,
39
+ 'handle' => handle,
40
+ 'secret' => OpenID::Util.to_base64(secret),
41
+ 'issued' => too_old_association_timestamp,
42
+ 'lifetime' => lifetime,
43
+ 'assoc_type' => assoc_type,
44
+ 'expire_at' => (too_old_association_timestamp + lifetime)}
45
+ store.association.create(doc)
46
+ end
47
+
48
+ def find_old_association
49
+ OpenidCouchdbStore::Association.all.select {|a| a['issued'] == too_old_association_timestamp}.first
50
+ end
51
+
52
+ def insert_old_nonce
53
+ store.nonce.create({'server_url' => server_url, 'timestamp' => too_old_nonce_timestamp, 'salt' => salt})
54
+ end
55
+
56
+ def find_old_nonce
57
+ store.nonce.all.select {|n| n['server_url'] == server_url && n['timestamp'] == too_old_nonce_timestamp && n['salt'] == salt }.first
58
+ end
59
+
60
+ ##
61
+ ## End of Couch-specific test config
62
+ ##
63
+
64
+ def too_old_nonce_timestamp
65
+ timestamp - (OpenID::Nonce.skew * 1.5).to_i
66
+ end
67
+
68
+ def too_old_association_timestamp
69
+ timestamp - (60*60*24*365) # year old
70
+ end
71
+
72
+ def store_association(opts = {})
73
+ association = OpenID::Association.new(handle, secret, timestamp, lifetime, assoc_type)
74
+ opts.each_pair {|k,v| association.send "#{k}=", v}
75
+ store.store_association(server_url,association)
76
+ end
77
+
78
+ def get_association(server_url = server_url, handle = handle)
79
+ store.get_association(server_url,handle)
80
+ end
81
+
82
+ def server_url
83
+ "http://localhost:98765/"
84
+ end
85
+
86
+ def handle
87
+ "{HMAC-SHA1}{4ca0a54b}{5Sx5CQ==}"
88
+ end
89
+
90
+ def secret
91
+ OpenID::Util.from_base64("5EoS1O4V+x7VkBNEekHsavgRjbk=")
92
+ end
93
+
94
+ def timestamp(refresh = false)
95
+ @timestamp = nil if refresh
96
+ @timestamp ||= (Time.now - 10).to_i # 10 seconds ago
97
+ end
98
+
99
+ def lifetime
100
+ 1209600 # two weeks in seconds
101
+ end
102
+
103
+ def assoc_type
104
+ "HMAC-SHA1"
105
+ end
106
+
107
+ def salt
108
+ "KuIaaq"
109
+ end
110
+
111
+ end
@@ -0,0 +1,67 @@
1
+ require 'helper'
2
+
3
+ class TestOpenidCouchdbStore < Test::Unit::TestCase
4
+ def test_store_and_retrieve_association
5
+ # Make sure there isn't already an association in the database
6
+ assert !get_association
7
+
8
+ # Save an assocaition
9
+ assert store_association
10
+
11
+ # Retrieve the association
12
+ assert assoc = store.get_association(server_url, handle)
13
+
14
+ # Check status of secret
15
+ assert assoc.secret == secret
16
+ end
17
+
18
+ def test_remove_association
19
+ store_association
20
+
21
+ # Ensure an assocaition exists
22
+ assert get_association
23
+
24
+ # Remove a specific association
25
+ assert store.remove_association(server_url,handle)
26
+ # Confirm that the association is gone.
27
+ assert !get_association
28
+ end
29
+
30
+ def test_use_nonce
31
+ # If a timestamp is too old, use_nonce should return false
32
+ assert !store.use_nonce(server_url, too_old_nonce_timestamp, salt)
33
+
34
+ # When no nonces exist, this should return true and create a nonce record
35
+ assert store.use_nonce(server_url, timestamp, salt)
36
+
37
+ # After a nonce is created with a given salt/timestamp, use_nonce should return false
38
+ assert !store.use_nonce(server_url, timestamp, salt)
39
+ end
40
+
41
+ def test_cleanup_nonces
42
+ insert_old_nonce
43
+
44
+ # Verify nonce is inserted
45
+ assert find_old_nonce
46
+
47
+ # Cleanup nonces
48
+ assert store.cleanup_nonces
49
+
50
+ # Verify nonce was cleaned up
51
+ assert !find_old_nonce
52
+ end
53
+
54
+ def test_cleanup_associations
55
+ insert_old_association
56
+
57
+ # Ensure association was saved
58
+ assert find_old_association
59
+
60
+ # Cleanup associations
61
+ assert store.cleanup_associations
62
+
63
+ # Ensure association was removed
64
+ assert !find_old_association
65
+
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openid_couchdb_store
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Sam Schenkman-Moore
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-28 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: couchrest
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: couchrest_extended_document
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 0
50
+ version: 1.0.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: OpenID store using CouchDB. Right now using Couchrest, will make more flexible later, hopefully.
54
+ email: samsm@samsm.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - LICENSE
61
+ - README.rdoc
62
+ files:
63
+ - .document
64
+ - .gitignore
65
+ - LICENSE
66
+ - README.rdoc
67
+ - Rakefile
68
+ - VERSION
69
+ - lib/openid_couchdb_store.rb
70
+ - openid_couchdb_store.gemspec
71
+ - test/helper.rb
72
+ - test/test_openid_couchdb_store.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/samsm/openid_couchdb_store
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: OpenID store using CouchDB
107
+ test_files:
108
+ - test/helper.rb
109
+ - test/test_openid_couchdb_store.rb