simple_token_authentication 1.5.2 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e7a77a49ad9d0cecdf975b21c733087091a4671
4
- data.tar.gz: 6ea3dea47399dc595603ebbe97bc859417bfe775
3
+ metadata.gz: a858d56cf65a5595155ab5b4e593d90c8c52e4b3
4
+ data.tar.gz: 48005f8c531276fe56de66bb371ad76ae61cd334
5
5
  SHA512:
6
- metadata.gz: c8f34f63afbf988faccb9d952740ef54283846e1d9163d937323102e793948bed2567fd0cacca4c311cb6243d97b42223874045fad0f39b611d3e7bf29e889eb
7
- data.tar.gz: 9409b4492ef48a1ceb2f3c4ed00a509e37cf0b9de0338486474fb5101c4eccb685eb09017d0a215944c6cafe453eeea9bb4bde06e1ec9bae5cc5f834437abdf0
6
+ metadata.gz: 21405a3b9a7b2728a7de067aa21aac10ab17217c1946bf4b2c980406a56e0af9b815e4f28cfe7f9136ebbfe8890e611d932ef0193a7e110dc296ce4731ff6e0a
7
+ data.tar.gz: baf60814272483318583370c68e9f21583c81929a4e5c3506cf58c609afe526e2581eaab68c21e3b8975c6928ea3cc1468ec6e03c103037503ac4d12469faaf7
data/README.md CHANGED
@@ -32,6 +32,10 @@ Install [Devise][devise] with any modules you want, then add the gem to your `Ge
32
32
  gem 'simple_token_authentication'
33
33
  ```
34
34
 
35
+ ### Make models token authenticatable
36
+
37
+ #### ActiveRecord
38
+
35
39
  First define which model or models will be token authenticatable (typ. `User`):
36
40
 
37
41
  ```ruby
@@ -61,7 +65,31 @@ rails g migration add_authentication_token_to_users authentication_token:string:
61
65
  rake db:migrate
62
66
  ```
63
67
 
64
- Finally define which controller will handle authentication (typ. `ApplicationController`) for which _token authenticatable_ model:
68
+ #### Mongoid
69
+
70
+ Define which model or models will be token authenticatable (typ. `User`):
71
+
72
+ ```ruby
73
+ # app/models/user.rb
74
+
75
+ class User
76
+ include Mongoid::Document
77
+ # Include default devise modules. Others available are:
78
+ # :confirmable, :lockable, :timeoutable and :omniauthable
79
+ devise :database_authenticatable, :registerable,
80
+ :recoverable, :rememberable, :trackable, :validatable
81
+
82
+ ## Token Authenticatable
83
+ acts_as_token_authenticatable
84
+ field :authentication_token
85
+
86
+ # ...
87
+ end
88
+ ```
89
+
90
+ ### Allow controllers to handle token authentication
91
+
92
+ Finally define which controllers will handle token authentication (typ. `ApplicationController`) for which _token authenticatable_ models:
65
93
 
66
94
  ```ruby
67
95
  # app/controllers/application_controller.rb
@@ -5,6 +5,8 @@ require 'simple_token_authentication/configuration'
5
5
  module SimpleTokenAuthentication
6
6
  extend Configuration
7
7
 
8
+ NoAdapterAvailableError = Class.new(LoadError)
9
+
8
10
  private
9
11
 
10
12
  def self.ensure_models_can_act_as_token_authenticatables model_adapters
@@ -30,12 +32,18 @@ module SimpleTokenAuthentication
30
32
  #
31
33
  # Returns an Array of available adapters
32
34
  def self.load_available_adapters adapters_short_names
33
- adapters_short_names.collect do |short_name|
35
+ available_adapters = adapters_short_names.collect do |short_name|
34
36
  adapter_name = "simple_token_authentication/adapters/#{short_name}_adapter"
35
- if require adapter_name
37
+ if const_defined?(short_name.camelize) && require(adapter_name)
36
38
  adapter_name.camelize.constantize
37
39
  end
38
40
  end
41
+ available_adapters.compact!
42
+
43
+ # stop here if no constants are defined or no adequate adapters are present
44
+ raise SimpleTokenAuthentication::NoAdapterAvailableError if available_adapters.empty?
45
+
46
+ available_adapters
39
47
  end
40
48
 
41
49
  available_model_adapters = load_available_adapters SimpleTokenAuthentication.model_adapters
@@ -28,7 +28,7 @@ module SimpleTokenAuthentication
28
28
  end
29
29
 
30
30
  def token_suitable?(token)
31
- not self.class.exists?(authentication_token: token)
31
+ self.class.where(authentication_token: token).count == 0
32
32
  end
33
33
 
34
34
  def token_generator
@@ -0,0 +1,14 @@
1
+ require 'mongoid'
2
+ require 'simple_token_authentication/adapter'
3
+
4
+ module SimpleTokenAuthentication
5
+ module Adapters
6
+ class MongoidAdapter
7
+ extend SimpleTokenAuthentication::Adapter
8
+
9
+ def self.base_class
10
+ ::Mongoid::Document
11
+ end
12
+ end
13
+ end
14
+ end
@@ -12,7 +12,7 @@ module SimpleTokenAuthentication
12
12
  @@header_names = {}
13
13
  @@sign_in_token = false
14
14
  @@controller_adapters = ['rails']
15
- @@model_adapters = ['active_record']
15
+ @@model_adapters = ['active_record', 'mongoid']
16
16
 
17
17
  # Allow the default configuration to be overwritten from initializers
18
18
  def configure
@@ -4,7 +4,6 @@ require 'active_support/concern'
4
4
  require 'simple_token_authentication/entities_manager'
5
5
  require 'simple_token_authentication/fallback_authentication_handler'
6
6
  require 'simple_token_authentication/sign_in_handler'
7
- require 'simple_token_authentication/token_authentication_handler'
8
7
  require 'simple_token_authentication/token_comparator'
9
8
 
10
9
  module SimpleTokenAuthentication
@@ -23,11 +22,6 @@ module SimpleTokenAuthentication
23
22
  private :token_comparator
24
23
  private :sign_in_handler
25
24
  private :find_record_from_identifier
26
-
27
- # This is necessary to test which arguments were passed to sign_in
28
- # from authenticate_entity_from_token!
29
- # See https://github.com/gonzalo-bulnes/simple_token_authentication/pull/32
30
- ::ActionController::Base.send :include, Devise::Controllers::SignInOut if Rails.env.test?
31
25
  end
32
26
 
33
27
  def authenticate_entity_from_token!(entity)
@@ -61,11 +55,7 @@ module SimpleTokenAuthentication
61
55
  # Rails 3 and 4 finder methods are supported,
62
56
  # see https://github.com/ryanb/cancan/blob/1.6.10/lib/cancan/controller_resource.rb#L108-L111
63
57
  record = nil
64
- if entity.model.respond_to? "find_by"
65
- record = email && entity.model.find_by(email: email)
66
- elsif entity.model.respond_to? "find_by_email"
67
- record = email && entity.model.find_by_email(email)
68
- end
58
+ record = email && entity.model.where(email: email).first
69
59
  end
70
60
 
71
61
  def token_comparator
@@ -113,13 +103,13 @@ module SimpleTokenAuthentication
113
103
 
114
104
  class_eval do
115
105
  define_method method_name.to_sym do
116
- lambda { |entity| authenticate_entity_from_token!(entity) }.call(entity)
106
+ lambda { |_entity| authenticate_entity_from_token!(_entity) }.call(entity)
117
107
  end
118
108
 
119
109
  define_method method_name_bang.to_sym do
120
- lambda do |entity|
121
- authenticate_entity_from_token!(entity)
122
- authenticate_entity_from_fallback!(entity, fallback_authentication_handler)
110
+ lambda do |_entity|
111
+ authenticate_entity_from_token!(_entity)
112
+ authenticate_entity_from_fallback!(_entity, fallback_authentication_handler)
123
113
  end.call(entity)
124
114
  end
125
115
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleTokenAuthentication
2
- VERSION = "1.5.2"
2
+ VERSION = "1.6.0"
3
3
  end
@@ -18,14 +18,14 @@ describe 'Simple Token Authentication' do
18
18
  # given one *c*orrect record (which is supposed to get signed in)
19
19
  @charles_record = double()
20
20
  [user, admin].each do |model|
21
- allow(model).to receive(:find_by).with(email: 'charles@example.com').and_return(@charles_record)
21
+ allow(model).to receive(:where).with(email: 'charles@example.com').and_return([@charles_record])
22
22
  end
23
23
  allow(@charles_record).to receive(:authentication_token).and_return('ch4rlEs_toKeN')
24
24
 
25
25
  # and one *w*rong record (which should not be signed in)
26
26
  @waldo_record = double()
27
27
  [user, admin].each do |model|
28
- allow(model).to receive(:find_by).with(email: 'waldo@example.com').and_return(@waldo_record)
28
+ allow(model).to receive(:where).with(email: 'waldo@example.com').and_return([@waldo_record])
29
29
  end
30
30
  allow(@waldo_record).to receive(:authentication_token).and_return('w4LdO_toKeN')
31
31
 
@@ -394,12 +394,12 @@ describe 'Simple Token Authentication' do
394
394
 
395
395
  # given one *c*orrect record (which is supposed to get signed in)
396
396
  @charles_record = double()
397
- allow(user).to receive(:find_by).with(email: 'charles@example.com').and_return(@charles_record)
397
+ allow(user).to receive(:where).with(email: 'charles@example.com').and_return([@charles_record])
398
398
  allow(@charles_record).to receive(:authentication_token).and_return('ch4rlEs_toKeN')
399
399
 
400
400
  # and one *w*rong record (which should not be signed in)
401
401
  @waldo_record = double()
402
- allow(user).to receive(:find_by).with(email: 'waldo@example.com').and_return(@waldo_record)
402
+ allow(user).to receive(:where).with(email: 'waldo@example.com').and_return([@waldo_record])
403
403
  allow(@waldo_record).to receive(:authentication_token).and_return('w4LdO_toKeN')
404
404
 
405
405
  # given a controller class which acts as token authentication handler
@@ -10,8 +10,6 @@ describe 'Any class which extends SimpleTokenAuthentication::Adapter' do
10
10
  @subject = define_dummy_class_which_extends(SimpleTokenAuthentication::Adapter)
11
11
  end
12
12
 
13
- it_behaves_like 'an adapter'
14
-
15
13
  describe '.base_class' do
16
14
 
17
15
  it 'raises an error if not overwritten', public: true do
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ require 'simple_token_authentication/adapters/mongoid_adapter'
3
+
4
+ describe 'SimpleTokenAuthentication::Adapters::MongoidAdapter' do
5
+
6
+ before(:each) do
7
+ stub_const('Mongoid', Module.new)
8
+ stub_const('Mongoid::Document', double())
9
+
10
+ @subject = SimpleTokenAuthentication::Adapters::MongoidAdapter
11
+ end
12
+
13
+ it_behaves_like 'an adapter'
14
+
15
+ describe '.base_class' do
16
+
17
+ it 'is Mongoid::Document', private: true do
18
+ expect(@subject.base_class).to eq Mongoid::Document
19
+ end
20
+ end
21
+ end
@@ -29,8 +29,8 @@ describe SimpleTokenAuthentication::Configuration do
29
29
 
30
30
  it_behaves_like 'a configuration option', 'model_adapters'
31
31
 
32
- it "defauts to ['active_record']", private: true do
33
- expect(@subject.model_adapters).to eq ['active_record']
32
+ it "defauts to ['active_record', 'mongoid']", private: true do
33
+ expect(@subject.model_adapters).to eq ['active_record', 'mongoid']
34
34
  end
35
35
  end
36
36
 
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleTokenAuthentication::NoAdapterAvailableError do
4
+
5
+ it 'is a kind of LoadError', public: true do
6
+ expect(subject).to be_kind_of LoadError
7
+ end
8
+ end
@@ -48,6 +48,65 @@ describe SimpleTokenAuthentication do
48
48
  end
49
49
  end
50
50
 
51
+ context 'when Mongoid is available' do
52
+
53
+ before(:each) do
54
+ stub_const('Mongoid', Module.new)
55
+ stub_const('Mongoid::Document', Class.new)
56
+
57
+ # define a dummy Mongoid adapter
58
+ dummy_mongoid_adapter = double()
59
+ allow(dummy_mongoid_adapter).to receive(:base_class).and_return(Mongoid::Document)
60
+ stub_const('SimpleTokenAuthentication::Adapters::DummyMongoidAdapter',
61
+ dummy_mongoid_adapter)
62
+ end
63
+
64
+ describe '#ensure_models_can_act_as_token_authenticatables' do
65
+
66
+ before(:each) do
67
+ class SimpleTokenAuthentication::DummyModel < Mongoid::Document; end
68
+ @dummy_model = SimpleTokenAuthentication::DummyModel
69
+
70
+ expect(@dummy_model.new).to be_instance_of SimpleTokenAuthentication::DummyModel
71
+ expect(@dummy_model.new).to be_kind_of Mongoid::Document
72
+ end
73
+
74
+ after(:each) do
75
+ SimpleTokenAuthentication.send(:remove_const, :DummyModel)
76
+ end
77
+
78
+ it 'allows any kind of Mongoid::Document to act as token authenticatable', private: true do
79
+ expect(@dummy_model).not_to respond_to :acts_as_token_authenticatable
80
+
81
+ subject.ensure_models_can_act_as_token_authenticatables [
82
+ SimpleTokenAuthentication::Adapters::DummyMongoidAdapter]
83
+
84
+ expect(@dummy_model).to respond_to :acts_as_token_authenticatable
85
+ end
86
+ end
87
+ end
88
+
89
+ context 'when no ORM, ODM or OxM is available' do
90
+
91
+ before(:each) do
92
+ stub_const('ActiveRecord', Module.new)
93
+ stub_const('Mongoid', Module.new)
94
+ end
95
+
96
+ describe '#load_available_adapters' do
97
+
98
+ it 'raises NoAdapterAvailableError', private: true do
99
+ allow(subject).to receive(:require).and_return(true)
100
+ hide_const('ActiveRecord')
101
+ hide_const('Mongoid')
102
+
103
+ expect do
104
+ subject.load_available_adapters SimpleTokenAuthentication.model_adapters
105
+ end.to raise_error SimpleTokenAuthentication::NoAdapterAvailableError
106
+ end
107
+ end
108
+ end
109
+
51
110
  context 'when ActionController::Base is available' do
52
111
 
53
112
  before(:each) do
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,8 @@ require 'bundler/setup'
2
2
  Bundler.setup
3
3
 
4
4
  require 'action_controller'
5
+ require 'active_record'
6
+ require 'mongoid'
5
7
  require 'active_support'
6
8
 
7
9
  require 'simple_token_authentication'
@@ -3,4 +3,8 @@ RSpec.shared_examples 'an adapter' do
3
3
  it 'responds to :base_class', public: true do
4
4
  expect(@subject).to respond_to :base_class
5
5
  end
6
+
7
+ it 'defines :base_class', public: true do
8
+ expect { @subject.base_class }.not_to raise_error
9
+ end
6
10
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_token_authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gonzalo Bulnes Guilpain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-21 00:00:00.000000000 Z
11
+ date: 2014-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activerecord
14
+ name: actionmailer
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -31,7 +31,7 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5'
33
33
  - !ruby/object:Gem::Dependency
34
- name: actionmailer
34
+ name: actionpack
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">="
@@ -92,6 +92,46 @@ dependencies:
92
92
  - - "~>"
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0.4'
95
+ - !ruby/object:Gem::Dependency
96
+ name: activerecord
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 3.2.6
102
+ - - "<"
103
+ - !ruby/object:Gem::Version
104
+ version: '5'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 3.2.6
112
+ - - "<"
113
+ - !ruby/object:Gem::Version
114
+ version: '5'
115
+ - !ruby/object:Gem::Dependency
116
+ name: mongoid
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: 3.1.0
122
+ - - "<"
123
+ - !ruby/object:Gem::Version
124
+ version: '5'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 3.1.0
132
+ - - "<"
133
+ - !ruby/object:Gem::Version
134
+ version: '5'
95
135
  description:
96
136
  email:
97
137
  - gon.bulnes@gmail.com
@@ -108,6 +148,7 @@ files:
108
148
  - lib/simple_token_authentication/acts_as_token_authentication_handler.rb
109
149
  - lib/simple_token_authentication/adapter.rb
110
150
  - lib/simple_token_authentication/adapters/active_record_adapter.rb
151
+ - lib/simple_token_authentication/adapters/mongoid_adapter.rb
111
152
  - lib/simple_token_authentication/adapters/rails_adapter.rb
112
153
  - lib/simple_token_authentication/configuration.rb
113
154
  - lib/simple_token_authentication/entities_manager.rb
@@ -127,10 +168,12 @@ files:
127
168
  - spec/lib/simple_token_authentication/acts_as_token_authentication_handler_spec.rb
128
169
  - spec/lib/simple_token_authentication/adapter_spec.rb
129
170
  - spec/lib/simple_token_authentication/adapters/active_record_adapter_spec.rb
171
+ - spec/lib/simple_token_authentication/adapters/mongoid_adapter_spec.rb
130
172
  - spec/lib/simple_token_authentication/adapters/rails_adapter_spec.rb
131
173
  - spec/lib/simple_token_authentication/configuration_spec.rb
132
174
  - spec/lib/simple_token_authentication/entities_manager_spec.rb
133
175
  - spec/lib/simple_token_authentication/entity_spec.rb
176
+ - spec/lib/simple_token_authentication/errors_spec.rb
134
177
  - spec/lib/simple_token_authentication/fallback_authentication_handler_spec.rb
135
178
  - spec/lib/simple_token_authentication/sign_in_handler_spec.rb
136
179
  - spec/lib/simple_token_authentication/token_authentication_handler_spec.rb
@@ -182,7 +225,9 @@ test_files:
182
225
  - spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb
183
226
  - spec/lib/simple_token_authentication/acts_as_token_authentication_handler_spec.rb
184
227
  - spec/lib/simple_token_authentication/token_authentication_handler_spec.rb
228
+ - spec/lib/simple_token_authentication/errors_spec.rb
185
229
  - spec/lib/simple_token_authentication/adapters/active_record_adapter_spec.rb
230
+ - spec/lib/simple_token_authentication/adapters/mongoid_adapter_spec.rb
186
231
  - spec/lib/simple_token_authentication/adapters/rails_adapter_spec.rb
187
232
  - spec/lib/simple_token_authentication/entity_spec.rb
188
233
  - spec/lib/simple_token_authentication/adapter_spec.rb