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 +4 -4
- data/lib/simple_token_authentication.rb +1 -1
- data/lib/simple_token_authentication/acts_as_token_authenticatable.rb +41 -6
- data/lib/simple_token_authentication/version.rb +1 -1
- data/spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb +94 -35
- metadata +2 -7
- data/lib/simple_token_authentication/token_authenticatable.rb +0 -43
- data/spec/lib/simple_token_authentication/token_authenticatable_spec.rb +0 -68
- data/spec/support/spec_for_token_authenticatable_interface.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f0598cbfab98dc89e4bd6a1ec862ef0c190b11e
|
4
|
+
data.tar.gz: 69e2239f4d6d51c230056f61051e6f1cc05a633f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 :
|
14
|
+
model_adapter.base_class.send :include, SimpleTokenAuthentication::ActsAsTokenAuthenticatable
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -1,14 +1,49 @@
|
|
1
|
-
require '
|
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
|
-
#
|
7
|
-
#
|
8
|
+
# Please see https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
|
9
|
+
# before editing this file, the discussion is very interesting.
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
@@ -15,22 +15,14 @@ describe DummyTokenGenerator do
|
|
15
15
|
it_behaves_like 'a token generator'
|
16
16
|
end
|
17
17
|
|
18
|
-
describe '
|
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
|
-
|
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
|
-
|
34
|
+
describe 'which supports the :before_save hook' do
|
43
35
|
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
40
|
+
expect(some_class).to receive(:before_save).with(:ensure_authentication_token)
|
41
|
+
some_class.acts_as_token_authenticatable
|
42
|
+
end
|
49
43
|
|
50
|
-
|
51
|
-
|
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
|
-
|
55
|
-
|
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
|
-
|
58
|
-
allow(some_child_class).to receive(:before_save)
|
53
|
+
describe 'instance' do
|
59
54
|
|
60
|
-
|
61
|
-
|
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
|
-
|
62
|
+
context 'when some authentication tokens are already in use' do
|
66
63
|
|
67
|
-
|
64
|
+
before(:each) do
|
65
|
+
TOKENS_IN_USE = ['ExampleTok3n', '4notherTokeN']
|
68
66
|
|
69
|
-
|
70
|
-
|
67
|
+
@subjects.each do |k|
|
68
|
+
k.class_eval do
|
71
69
|
|
72
|
-
|
73
|
-
|
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
|
77
|
-
|
97
|
+
it 'ensures its authentication token is unique', public: true do
|
98
|
+
@subjects.each do |subject|
|
99
|
+
subject.ensure_authentication_token
|
78
100
|
|
79
|
-
|
80
|
-
|
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.
|
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-
|
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
|