sorcery-couchbase 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7f5de4d99d22bfa0dc36201f1dc9bee7bdcdcb95
4
+ data.tar.gz: 135945680d6adbd81aa6a46ea5c007cff6cba8b6
5
+ SHA512:
6
+ metadata.gz: e68bef69ed2471d9f64d89aae0a4fd0898f7214023f6e53759dbed550cf3f67f40da8f57ec5c1e3c8f11140066f7dad5ddfdb8cafc7eb371ecec17139d4e4487
7
+ data.tar.gz: 2df7c1d4582ca124da8cef1c17d55d34a8b5df2d797dfce6f2008f791a9dce6207d9a30ca0e89af28d3966fd9e0668ffb45021045cb80357d4453a2371d18c6a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sorcery-couchbase.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mike Evans
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Sorcery::Couchbase
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sorcery-couchbase'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sorcery-couchbase
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,94 @@
1
+ module Sorcery
2
+
3
+ module CouchbaseModel
4
+ def self.included(klass)
5
+ klass.class_eval do
6
+ class << self
7
+ alias :orig_authenticates_with_sorcery! :authenticates_with_sorcery!
8
+
9
+ def authenticates_with_sorcery!
10
+ orig_authenticates_with_sorcery!
11
+ init_couchbase_support! if defined?(Couchbase) && self.ancestors.include?(Couchbase::Model)
12
+ end
13
+
14
+ protected
15
+
16
+ def init_couchbase_support!
17
+ self.class_eval do
18
+ sorcery_config.username_attribute_names.each do |username|
19
+ attribute username
20
+ end
21
+ attribute sorcery_config.email_attribute_name unless sorcery_config.username_attribute_names.include?(sorcery_config.email_attribute_name)
22
+ attribute sorcery_config.crypted_password_attribute_name
23
+ attribute sorcery_config.salt_attribute_name
24
+ attribute sorcery_config.activation_token_attribute_name if sorcery_config.respond_to? :activation_token_attribute_name
25
+ attribute sorcery_config.activation_state_attribute_name if sorcery_config.respond_to? :activation_token_attribute_name
26
+ attribute sorcery_config.remember_me_token_attribute_name if sorcery_config.respond_to? :remember_me_token_attribute_name
27
+
28
+ if sorcery_config.respond_to? :remember_me_token_expires_at_attribute_name
29
+ attribute sorcery_config.remember_me_token_expires_at_attribute_name
30
+ define_method sorcery_config.remember_me_token_expires_at_attribute_name do
31
+ time = read_attribute(sorcery_config.remember_me_token_expires_at_attribute_name)
32
+ case time
33
+ when Array
34
+ Time.utc *time
35
+ when String
36
+ Time.new time
37
+ else
38
+ raise ArgumentError, 'Remember me token stored incorrectly'
39
+ end
40
+ end
41
+ end
42
+
43
+ view :all
44
+ view *_sorcery_view_attributes.map { |attr| "by_#{attr}" }
45
+ ensure_sorcery_design_document!
46
+ end
47
+ end
48
+
49
+ def ensure_sorcery_design_document!
50
+ bucket.save_design_doc(_sorcery_design_doc)
51
+ end
52
+
53
+ def _sorcery_design_doc
54
+ doc = {
55
+ '_id' => "_design/#{design_document}",
56
+ 'language' => 'javascript',
57
+ 'views' => {
58
+ 'all' => {
59
+ 'map' => <<-JS
60
+ function (doc, meta) {
61
+ if (doc.type && doc.type == '#{design_document}')
62
+ emit(meta.id, null);
63
+ }
64
+ JS
65
+ }
66
+ }
67
+ }
68
+
69
+ _sorcery_view_attributes.each do |attribute|
70
+ doc['views']["by_#{attribute}"] = {
71
+ 'map' => <<-JS
72
+ function (doc, meta) {
73
+ if (doc.type && doc.type == '#{design_document}')
74
+ emit(doc.#{attribute}, null);
75
+ }
76
+ JS
77
+ }
78
+ end
79
+
80
+ doc
81
+ end
82
+
83
+ def _sorcery_view_attributes
84
+ attributes = sorcery_config.username_attribute_names
85
+ attributes << sorcery_config.activation_token_attribute_name if sorcery_config.respond_to? :activation_token_attribute_name
86
+ attributes << sorcery_config.remember_me_token_attribute_name if sorcery_config.respond_to? :remember_me_token_attribute_name
87
+ attributes << sorcery_config.email_attribute_name
88
+ attributes.uniq
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,79 @@
1
+ module Sorcery
2
+ module Model
3
+ module Adapters
4
+ module Couchbase
5
+ def self.included(klass)
6
+ klass.extend ClassMethods
7
+ klass.send(:include, InstanceMethods)
8
+ end
9
+
10
+ module InstanceMethods
11
+ def increment(attr)
12
+ self.write_attribute(attr, self.read_attribute(attr).to_i + 1 )
13
+ end
14
+
15
+ def update_many_attributes(attrs)
16
+ self.update(attrs)
17
+ end
18
+
19
+ def update_single_attribute(name, value)
20
+ update_many_attributes(name => value)
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+
26
+ def sorcery_view(view)
27
+ ::Couchbase::View.new(bucket, "_design/#{design_document}/_view/by_#{view}", { wrapper_class: self, include_docs: true })
28
+ end
29
+
30
+ def credential_regex(credential)
31
+ @sorcery_config.downcase_username_before_authenticating ? credential.downcase : credential
32
+ end
33
+
34
+ def find_by_credentials(credentials)
35
+ user = nil
36
+ sorcery_config.username_attribute_names.each do |attribute|
37
+ user = sorcery_view(attribute).fetch(key: credential_regex(credentials[0]), stale: false).first
38
+ break if user
39
+ end
40
+ user
41
+ end
42
+
43
+ def find_by_provider_and_uid(provider, uid)
44
+ @user_klass ||= ::Sorcery::Controller::Config.user_class.to_s.constantize
45
+ where(@user_klass.sorcery_config.provider_attribute_name => provider, @user_klass.sorcery_config.provider_uid_attribute_name => uid).first
46
+ end
47
+
48
+ def find_by_activation_token(token)
49
+ sorcery_view(sorcery_config.activation_token_attribute_name).fetch(key: token, stale: false).first
50
+ end
51
+
52
+ def find_by_remember_me_token(token)
53
+ sorcery_view(sorcery_config.remember_me_token_attribute_name).fetch(key: token, stale: false).first
54
+ end
55
+
56
+ def find_by_username(username)
57
+ find_by_credentials(username)
58
+ end
59
+
60
+
61
+ def find_by_sorcery_token(token_attr_name, token)
62
+ where(token_attr_name => token).first
63
+ end
64
+
65
+ def find_by_email(email)
66
+ where(sorcery_config.email_attribute_name => email).first
67
+ end
68
+
69
+ # def get_current_users
70
+ # config = sorcery_config
71
+ # where(config.last_activity_at_attribute_name.ne => nil) \
72
+ # .where("this.#{config.last_logout_at_attribute_name} == null || this.#{config.last_activity_at_attribute_name} > this.#{config.last_logout_at_attribute_name}") \
73
+ # .where(config.last_activity_at_attribute_name.gt => config.activity_timeout.seconds.ago.utc).order_by([:_id,:asc])
74
+ # end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ module SorceryCouchbase
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'active_model'
2
+ require 'couchbase'
3
+ require 'couchbase/model'
4
+ require 'sorcery_couchbase/version'
5
+ require 'sorcery'
6
+ require 'sorcery/model/adapters/couchbase'
7
+ require 'sorcery/couchbase_model'
8
+
9
+ Couchbase::Model.send(:include, Sorcery::Model)
10
+ Couchbase::Model.send(:include, Sorcery::CouchbaseModel)
11
+ Couchbase::Model.send(:include, Sorcery::Model::Adapters::Couchbase)
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sorcery_couchbase/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sorcery-couchbase'
8
+ spec.version = SorceryCouchbase::VERSION
9
+ spec.authors = ['Mike Evans']
10
+ spec.email = ['mike@urlgonomics.com']
11
+ spec.description = %q{Couchbase backend for the Sorcery authentication framework}
12
+ spec.summary = %q{Couchbase backend for the Sorcery authentication framework}
13
+ spec.homepage = 'https://github.com/mje113/sorcery-couchbase'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+ spec.platform = 'java' if defined?(JRUBY_VERSION)
21
+
22
+ spec.add_dependency 'sorcery'
23
+ spec.add_dependency 'activemodel'
24
+ if defined?(JRUBY_VERSION)
25
+ spec.add_dependency 'couchbase-jruby-model', '>= 0.1.0'
26
+ else
27
+ spec.add_dependency 'couchbase-model', '>= 0.5.3'
28
+ end
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.3'
31
+ spec.add_development_dependency 'rake'
32
+ spec.add_development_dependency 'minitest', '>= 5.0'
33
+ spec.add_development_dependency 'pry'
34
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,46 @@
1
+ gem 'minitest'
2
+ require 'minitest/autorun'
3
+ require 'sorcery_couchbase'
4
+ require 'pry'
5
+
6
+ class MockMailer
7
+ def self.activation_needed_email(user)
8
+ end
9
+
10
+ def self.activation_success_email(user)
11
+ end
12
+ end
13
+
14
+ Sorcery::Controller::Config.submodules = [ :user_activation, :remember_me, :reset_password ]
15
+ Sorcery::Controller::Config.user_config do |user|
16
+ user.user_activation_mailer = MockMailer
17
+ user.reset_password_mailer = MockMailer
18
+ end
19
+
20
+ class TestUser < Couchbase::Model
21
+ authenticates_with_sorcery!
22
+
23
+ attribute :role, default: 'user'
24
+ end
25
+
26
+ Couchbase.bucket.flush
27
+
28
+ Minitest.after_run { Couchbase.disconnect; TestUser.bucket.disconnect }
29
+
30
+ class Minitest::Test
31
+
32
+ def password
33
+ 's3cr3t'
34
+ end
35
+
36
+ def user
37
+ @@user ||= begin
38
+ TestUser.bucket.connect unless TestUser.bucket.connected?
39
+ user = TestUser.create(email: 'bar@example.com')
40
+ user.password = password
41
+ fail unless user.save
42
+ user.activate!
43
+ user
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ require 'helper'
2
+
3
+ class TestCouchbaseAdapter < Minitest::Test
4
+
5
+ def setup
6
+ user
7
+ end
8
+
9
+ def test_initialization
10
+ assert TestUser.new
11
+ end
12
+
13
+ def test_find_by_credentials
14
+ assert_equal user.email, TestUser.find_by_credentials([ user.email ]).email
15
+ end
16
+
17
+ def test_not_found_credentials
18
+ assert_nil TestUser.find_by_credentials([ 'fu@example.com' ])
19
+ end
20
+
21
+ def test_can_authenticate
22
+ TestUser.authenticate(user.email, password)
23
+ assert_equal user.id, TestUser.authenticate(user.email, password).id
24
+ end
25
+
26
+ def test_can_update_atributes
27
+ assert user.update_many_attributes(role: 'admin')
28
+ assert_equal 'admin', user.role
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sorcery-couchbase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sorcery
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: couchbase-model
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Couchbase backend for the Sorcery authentication framework
112
+ email:
113
+ - mike@urlgonomics.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - lib/sorcery/couchbase_model.rb
124
+ - lib/sorcery/model/adapters/couchbase.rb
125
+ - lib/sorcery_couchbase.rb
126
+ - lib/sorcery_couchbase/version.rb
127
+ - sorcery-couchbase.gemspec
128
+ - test/helper.rb
129
+ - test/test_couchbase_adapter.rb
130
+ homepage: https://github.com/mje113/sorcery-couchbase
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.2.0.rc.1
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Couchbase backend for the Sorcery authentication framework
154
+ test_files:
155
+ - test/helper.rb
156
+ - test/test_couchbase_adapter.rb