simple_token_authentication 1.9.0 → 1.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aff7351e0178a92409d1092d897fba3190bf7931
4
- data.tar.gz: 13cf3934848704cb786c32a88d2811ca957e2907
3
+ metadata.gz: 9f0598cbfab98dc89e4bd6a1ec862ef0c190b11e
4
+ data.tar.gz: 69e2239f4d6d51c230056f61051e6f1cc05a633f
5
5
  SHA512:
6
- metadata.gz: 554b98719aee74a692f8bb5d80089f22ff1ce8ea85f2abdb3071b441a9decf87525d3e3f64aa24726e29f3fe0ed4b148352633ade88ffa0af8f49ede34003244
7
- data.tar.gz: a15f550bac6dd2776aed33aebf543f8ee1806818817c6ed37fb8a965cb2ced54df1f618c6c3445cb6cb3e193341987c6bec1faa8291dfe95b0dd30ff98154fec
6
+ metadata.gz: c0eded2ae10fcb6bed035abb0f0c2715c7914aab698e51bf825e469dcb79e1f499088db73a30bb4ef722129a3ce80714ecf4ceac083face887df7514011d7802
7
+ data.tar.gz: c6ac775ebb4f855edb1799463af719bbf885de8ca7c2f2e8cad1e76a10ffba3f23f106a1c0dfddd87ca31f479efdf9de48e382c2d61394b686bd7210007ccd1d
@@ -11,7 +11,7 @@ module SimpleTokenAuthentication
11
11
 
12
12
  def self.ensure_models_can_act_as_token_authenticatables model_adapters
13
13
  model_adapters.each do |model_adapter|
14
- model_adapter.base_class.send :extend, SimpleTokenAuthentication::ActsAsTokenAuthenticatable
14
+ model_adapter.base_class.send :include, SimpleTokenAuthentication::ActsAsTokenAuthenticatable
15
15
  end
16
16
  end
17
17
 
@@ -1,14 +1,49 @@
1
- require 'simple_token_authentication/token_authenticatable'
1
+ require 'active_support/concern'
2
+ require 'simple_token_authentication/token_generator'
2
3
 
3
4
  module SimpleTokenAuthentication
4
5
  module ActsAsTokenAuthenticatable
6
+ extend ::ActiveSupport::Concern
5
7
 
6
- # This module ensures that no TokenAuthenticatable behaviour
7
- # is added before the class actually `acts_as_token_authenticatable`.
8
+ # Please see https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
9
+ # before editing this file, the discussion is very interesting.
8
10
 
9
- def acts_as_token_authenticatable(options = {})
10
- include SimpleTokenAuthentication::TokenAuthenticatable
11
- before_save :ensure_authentication_token
11
+ included do
12
+ private :generate_authentication_token
13
+ private :token_suitable?
14
+ private :token_generator
15
+ end
16
+
17
+ # Set an authentication token if missing
18
+ #
19
+ # Because it is intended to be used as a filter,
20
+ # this method is -and should be kept- idempotent.
21
+ def ensure_authentication_token
22
+ if authentication_token.blank?
23
+ self.authentication_token = generate_authentication_token(token_generator)
24
+ end
25
+ end
26
+
27
+ def generate_authentication_token(token_generator)
28
+ loop do
29
+ token = token_generator.generate_token
30
+ break token if token_suitable?(token)
31
+ end
32
+ end
33
+
34
+ def token_suitable?(token)
35
+ self.class.where(authentication_token: token).count == 0
36
+ end
37
+
38
+ # Private: Get one (always the same) object which behaves as a token generator
39
+ def token_generator
40
+ @token_generator ||= TokenGenerator.new
41
+ end
42
+
43
+ module ClassMethods
44
+ def acts_as_token_authenticatable(options = {})
45
+ before_save :ensure_authentication_token
46
+ end
12
47
  end
13
48
  end
14
49
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleTokenAuthentication
2
- VERSION = "1.9.0"
2
+ VERSION = "1.9.1"
3
3
  end
@@ -15,22 +15,14 @@ describe DummyTokenGenerator do
15
15
  it_behaves_like 'a token generator'
16
16
  end
17
17
 
18
- describe 'Any class which extends SimpleTokenAuthentication::ActsAsTokenAuthenticatable (or any if its children)' do
18
+ describe 'A token authenticatable class (or one of its children)' do
19
19
 
20
20
  after(:each) do
21
21
  ensure_examples_independence
22
22
  end
23
23
 
24
24
  before(:each) do
25
- define_test_subjects_for_extension_of(SimpleTokenAuthentication::ActsAsTokenAuthenticatable)
26
- end
27
-
28
- it 'doesn\'t behave like a token authenticatable', public: true do
29
- stub_const('SimpleTokenAuthentication::TokenAuthenticatable', Module.new)
30
-
31
- @subjects.each do |subject|
32
- expect(subject).not_to be_include SimpleTokenAuthentication::TokenAuthenticatable
33
- end
25
+ define_test_subjects_for_inclusion_of(SimpleTokenAuthentication::ActsAsTokenAuthenticatable)
34
26
  end
35
27
 
36
28
  it 'responds to :acts_as_token_authenticatable', public: true do
@@ -39,46 +31,113 @@ describe 'Any class which extends SimpleTokenAuthentication::ActsAsTokenAuthenti
39
31
  end
40
32
  end
41
33
 
42
- context 'when it explicitely acts as a token authenticatable' do
34
+ describe 'which supports the :before_save hook' do
43
35
 
44
- it 'behaves like a token authenticatable (1)', rspec_3_error: true, public: true do
45
- stub_const('SimpleTokenAuthentication::TokenAuthenticatable', Module.new)
36
+ context 'when it acts as token authenticatable' do
37
+ it 'ensures its instances have an authentication token before being saved (1)', rspec_3_error: true, public: true do
38
+ some_class = @subjects.first
46
39
 
47
- some_class = @subjects.first
48
- allow(some_class).to receive(:before_save)
40
+ expect(some_class).to receive(:before_save).with(:ensure_authentication_token)
41
+ some_class.acts_as_token_authenticatable
42
+ end
49
43
 
50
- some_class.acts_as_token_authenticatable
51
- expect(some_class).to be_include SimpleTokenAuthentication::TokenAuthenticatable
52
- end
44
+ it 'ensures its instances have an authentication token before being saved (2)', rspec_3_error: true, public: true do
45
+ some_child_class = @subjects.last
53
46
 
54
- it 'behaves like a token authenticatable (2)', rspec_3_error: true, public: true do
55
- stub_const('SimpleTokenAuthentication::TokenAuthenticatable', Module.new)
47
+ expect(some_child_class).to receive(:before_save).with(:ensure_authentication_token)
48
+ some_child_class.acts_as_token_authenticatable
49
+ end
50
+ end
51
+ end
56
52
 
57
- some_child_class = @subjects.last
58
- allow(some_child_class).to receive(:before_save)
53
+ describe 'instance' do
59
54
 
60
- some_child_class.acts_as_token_authenticatable
61
- expect(some_child_class).to be_include SimpleTokenAuthentication::TokenAuthenticatable
55
+ it 'responds to :ensure_authentication_token', protected: true do
56
+ @subjects.map!{ |subject| subject.new }
57
+ @subjects.each do |subject|
58
+ expect(subject).to respond_to :ensure_authentication_token
59
+ end
62
60
  end
63
- end
64
61
 
65
- describe '.acts_as_token_authenticatable' do
62
+ context 'when some authentication tokens are already in use' do
66
63
 
67
- context 'when the class supports the :before_save hook' do
64
+ before(:each) do
65
+ TOKENS_IN_USE = ['ExampleTok3n', '4notherTokeN']
68
66
 
69
- it 'ensures its instances have an authentication token before being saved (1)', rspec_3_error: true, public: true do
70
- some_class = @subjects.first
67
+ @subjects.each do |k|
68
+ k.class_eval do
71
69
 
72
- expect(some_class).to receive(:before_save).with(:ensure_authentication_token)
73
- some_class.acts_as_token_authenticatable
70
+ def initialize(args={})
71
+ @authentication_token = args[:authentication_token]
72
+ @token_generator = DummyTokenGenerator.new(
73
+ tokens_to_be_generated: TOKENS_IN_USE + ['Dist1nCt-Tok3N'])
74
+ end
75
+
76
+ def authentication_token=(value)
77
+ @authentication_token = value
78
+ end
79
+
80
+ def authentication_token
81
+ @authentication_token
82
+ end
83
+
84
+ # the 'ExampleTok3n' is already in use
85
+ def token_suitable?(token)
86
+ not TOKENS_IN_USE.include? token
87
+ end
88
+
89
+ def token_generator
90
+ @token_generator
91
+ end
92
+ end
93
+ end
94
+ @subjects.map!{ |subject| subject.new }
74
95
  end
75
96
 
76
- it 'ensures its instances have an authentication token before being saved (2)', rspec_3_error: true, public: true do
77
- some_child_class = @subjects.last
97
+ it 'ensures its authentication token is unique', public: true do
98
+ @subjects.each do |subject|
99
+ subject.ensure_authentication_token
78
100
 
79
- expect(some_child_class).to receive(:before_save).with(:ensure_authentication_token)
80
- some_child_class.acts_as_token_authenticatable
101
+ expect(subject.authentication_token).not_to eq 'ExampleTok3n'
102
+ expect(subject.authentication_token).not_to eq '4notherTokeN'
103
+ expect(subject.authentication_token).to eq 'Dist1nCt-Tok3N'
104
+ end
81
105
  end
82
106
  end
83
107
  end
84
108
  end
109
+
110
+ describe 'A class which includes a module which includes ActsAsTokenAuthenticatable and ActiveSupport::Concern (a.k.a Adapters::MongoidAdapter)' do
111
+
112
+ before(:each) do
113
+ base_module = Module.new do
114
+ extend ActiveSupport::Concern
115
+ include SimpleTokenAuthentication::ActsAsTokenAuthenticatable
116
+ end
117
+ stub_const('BaseModule', base_module)
118
+
119
+ @subject = Class.new do
120
+ include BaseModule
121
+ end
122
+ end
123
+
124
+ it 'responds to :acts_as_token_authenticatable', protected: true do
125
+ expect(@subject).to respond_to :acts_as_token_authenticatable
126
+ end
127
+ end
128
+
129
+ describe 'A class that inherits from a class which includes ActsAsTokenAuthenticatable (a.k.a Adapters::ActiveRecordAdapter)' do
130
+
131
+ before(:each) do
132
+ base_class = Class.new do
133
+ include SimpleTokenAuthentication::ActsAsTokenAuthenticatable
134
+ end
135
+ stub_const('BaseClass', base_class)
136
+
137
+ @subject = Class.new(BaseClass)
138
+ end
139
+
140
+ it 'responds to :acts_as_token_authenticatable', protected: true do
141
+ expect(@subject).to respond_to :acts_as_token_authenticatable
142
+ end
143
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_token_authentication
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.9.1
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: 2015-04-24 00:00:00.000000000 Z
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionmailer
@@ -156,7 +156,6 @@ files:
156
156
  - lib/simple_token_authentication/entity.rb
157
157
  - lib/simple_token_authentication/fallback_authentication_handler.rb
158
158
  - lib/simple_token_authentication/sign_in_handler.rb
159
- - lib/simple_token_authentication/token_authenticatable.rb
160
159
  - lib/simple_token_authentication/token_authentication_handler.rb
161
160
  - lib/simple_token_authentication/token_comparator.rb
162
161
  - lib/simple_token_authentication/token_generator.rb
@@ -179,7 +178,6 @@ files:
179
178
  - spec/lib/simple_token_authentication/errors_spec.rb
180
179
  - spec/lib/simple_token_authentication/fallback_authentication_handler_spec.rb
181
180
  - spec/lib/simple_token_authentication/sign_in_handler_spec.rb
182
- - spec/lib/simple_token_authentication/token_authenticatable_spec.rb
183
181
  - spec/lib/simple_token_authentication/token_authentication_handler_spec.rb
184
182
  - spec/lib/simple_token_authentication/token_comparator_spec.rb
185
183
  - spec/lib/simple_token_authentication/token_generator_spec.rb
@@ -191,7 +189,6 @@ files:
191
189
  - spec/support/spec_for_configuration_option_interface.rb
192
190
  - spec/support/spec_for_entities_manager_interface.rb
193
191
  - spec/support/spec_for_sign_in_handler_interface.rb
194
- - spec/support/spec_for_token_authenticatable_interface.rb
195
192
  - spec/support/spec_for_token_comparator_interface.rb
196
193
  - spec/support/spec_for_token_generator_interface.rb
197
194
  - spec/support/specs_for_token_authentication_handler_interface.rb
@@ -229,7 +226,6 @@ test_files:
229
226
  - spec/lib/simple_token_authentication/token_generator_spec.rb
230
227
  - spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb
231
228
  - spec/lib/simple_token_authentication/acts_as_token_authentication_handler_spec.rb
232
- - spec/lib/simple_token_authentication/token_authenticatable_spec.rb
233
229
  - spec/lib/simple_token_authentication/token_authentication_handler_spec.rb
234
230
  - spec/lib/simple_token_authentication/errors_spec.rb
235
231
  - spec/lib/simple_token_authentication/adapters/active_record_adapter_spec.rb
@@ -247,7 +243,6 @@ test_files:
247
243
  - spec/support/spec_for_authentication_handler_interface.rb
248
244
  - spec/support/spec_for_entities_manager_interface.rb
249
245
  - spec/support/spec_for_adapter.rb
250
- - spec/support/spec_for_token_authenticatable_interface.rb
251
246
  - spec/support/specs_for_token_authentication_handler_interface.rb
252
247
  - spec/support/spec_for_token_comparator_interface.rb
253
248
  - spec/support/spec_for_configuration_option_interface.rb
@@ -1,43 +0,0 @@
1
- require 'active_support/concern'
2
- require 'simple_token_authentication/token_generator'
3
-
4
- module SimpleTokenAuthentication
5
- module TokenAuthenticatable
6
- extend ::ActiveSupport::Concern
7
-
8
- # Please see https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
9
- # before editing this file, the discussion is very interesting.
10
-
11
- included do
12
- private :generate_authentication_token
13
- private :token_suitable?
14
- private :token_generator
15
- end
16
-
17
- # Set an authentication token if missing
18
- #
19
- # Because it is intended to be used as a filter,
20
- # this method is -and should be kept- idempotent.
21
- def ensure_authentication_token
22
- if authentication_token.blank?
23
- self.authentication_token = generate_authentication_token(token_generator)
24
- end
25
- end
26
-
27
- def generate_authentication_token(token_generator)
28
- loop do
29
- token = token_generator.generate_token
30
- break token if token_suitable?(token)
31
- end
32
- end
33
-
34
- def token_suitable?(token)
35
- self.class.where(authentication_token: token).count == 0
36
- end
37
-
38
- # Private: Get one (always the same) object which behaves as a token generator
39
- def token_generator
40
- @token_generator ||= TokenGenerator.new
41
- end
42
- end
43
- end
@@ -1,68 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class DummyTokenGenerator
4
- def initialize(args={})
5
- @tokens_to_be_generated = args[:tokens_to_be_generated]
6
- end
7
-
8
- def generate_token
9
- @tokens_to_be_generated.shift
10
- end
11
- end
12
-
13
- describe DummyTokenGenerator do
14
- it_behaves_like 'a token generator'
15
- end
16
-
17
- describe 'Any instance of a class which includes SimpleTokenAuthentication::TokenAuthenticatable' do
18
-
19
- let(:described_class) do
20
- define_dummy_class_which_includes SimpleTokenAuthentication::TokenAuthenticatable
21
- end
22
-
23
- after(:each) do
24
- # ensure_examples_independence
25
- SimpleTokenAuthentication.send(:remove_const, :SomeClass)
26
- end
27
-
28
- it_behaves_like 'a token authenticatable'
29
-
30
- let(:subject) { described_class.new() }
31
-
32
- describe '#ensure_authentication_token' do
33
-
34
- context 'when some authentication tokens are already in use' do
35
-
36
- before(:each) do
37
- TOKENS_IN_USE = ['ExampleTok3n', '4notherTokeN']
38
-
39
- subject.instance_eval do
40
-
41
- @token_generator = DummyTokenGenerator.new(
42
- tokens_to_be_generated: TOKENS_IN_USE + ['Dist1nCt-Tok3N'])
43
-
44
- def authentication_token=(value)
45
- @authentication_token = value
46
- end
47
-
48
- def authentication_token
49
- @authentication_token
50
- end
51
-
52
- # the 'ExampleTok3n' is already in use
53
- def token_suitable?(token)
54
- not TOKENS_IN_USE.include? token
55
- end
56
- end
57
- end
58
-
59
- it 'ensures its authentication token is unique', public: true do
60
- subject.ensure_authentication_token
61
-
62
- expect(subject.authentication_token).not_to eq 'ExampleTok3n'
63
- expect(subject.authentication_token).not_to eq '4notherTokeN'
64
- expect(subject.authentication_token).to eq 'Dist1nCt-Tok3N'
65
- end
66
- end
67
- end
68
- end
@@ -1,8 +0,0 @@
1
- RSpec.shared_examples 'a token authenticatable' do
2
-
3
- let(:token_authenticatable) { described_class.new() }
4
-
5
- it 'responds to :ensure_authentication_token', private: true do
6
- expect(token_authenticatable).to respond_to :ensure_authentication_token
7
- end
8
- end