devise_token_auth 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise_token_auth might be problematic. Click here for more details.

Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +26 -29
  4. data/app/controllers/devise_token_auth/confirmations_controller.rb +54 -7
  5. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +7 -7
  6. data/app/controllers/devise_token_auth/passwords_controller.rb +4 -4
  7. data/app/controllers/devise_token_auth/registrations_controller.rb +2 -2
  8. data/app/controllers/devise_token_auth/sessions_controller.rb +5 -5
  9. data/app/controllers/devise_token_auth/unlocks_controller.rb +3 -3
  10. data/app/models/devise_token_auth/concerns/active_record_support.rb +3 -21
  11. data/app/models/devise_token_auth/concerns/tokens_serialization.rb +19 -0
  12. data/app/models/devise_token_auth/concerns/user.rb +36 -45
  13. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +1 -1
  14. data/app/validators/{devise_token_auth/email_validator.rb → devise_token_auth_email_validator.rb} +1 -1
  15. data/config/locales/en.yml +5 -0
  16. data/lib/devise_token_auth.rb +1 -0
  17. data/lib/devise_token_auth/engine.rb +2 -0
  18. data/lib/devise_token_auth/rails/routes.rb +1 -1
  19. data/lib/devise_token_auth/token_factory.rb +126 -0
  20. data/lib/devise_token_auth/version.rb +1 -1
  21. data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +5 -0
  22. data/test/controllers/demo_user_controller_test.rb +2 -2
  23. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +39 -0
  24. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +3 -3
  25. data/test/dummy/app/controllers/overrides/passwords_controller.rb +3 -3
  26. data/test/dummy/app/controllers/overrides/registrations_controller.rb +1 -1
  27. data/test/dummy/app/controllers/overrides/sessions_controller.rb +1 -1
  28. data/test/dummy/config/initializers/devise.rb +275 -2
  29. data/test/dummy/config/initializers/devise_token_auth.rb +35 -4
  30. data/test/dummy/tmp/generators/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  31. data/test/dummy/tmp/generators/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  32. data/test/factories/users.rb +1 -1
  33. data/test/lib/devise_token_auth/token_factory_test.rb +191 -0
  34. data/test/models/concerns/tokens_serialization_test.rb +70 -0
  35. data/test/models/user_test.rb +0 -32
  36. metadata +29 -13
  37. data/test/dummy/tmp/generators/app/models/azpire/v1/human_resource/user.rb +0 -9
  38. data/test/dummy/tmp/generators/config/initializers/devise_token_auth.rb +0 -50
  39. data/test/dummy/tmp/generators/config/routes.rb +0 -4
  40. data/test/dummy/tmp/generators/db/migrate/20190112150327_devise_token_auth_create_azpire_v1_human_resource_users.rb +0 -56
@@ -5,20 +5,51 @@ DeviseTokenAuth.setup do |config|
5
5
  # client is responsible for keeping track of the changing tokens. Change
6
6
  # this to false to prevent the Authorization header from changing after
7
7
  # each request.
8
- #config.change_headers_on_each_request = true
8
+ # config.change_headers_on_each_request = true
9
9
 
10
10
  # By default, users will need to re-authenticate after 2 weeks. This setting
11
11
  # determines how long tokens will remain valid after they are issued.
12
- #config.token_lifespan = 2.weeks
12
+ # config.token_lifespan = 2.weeks
13
+
14
+ # Limiting the token_cost to just 4 in testing will increase the performance of
15
+ # your test suite dramatically. The possible cost value is within range from 4
16
+ # to 31. It is recommended to not use a value more than 10 in other environments.
17
+ config.token_cost = Rails.env.test? ? 4 : 10
18
+
19
+ # Sets the max number of concurrent devices per user, which is 10 by default.
20
+ # After this limit is reached, the oldest tokens will be removed.
21
+ # config.max_number_of_devices = 10
13
22
 
14
23
  # Sometimes it's necessary to make several requests to the API at the same
15
24
  # time. In this case, each request in the batch will need to share the same
16
25
  # auth token. This setting determines how far apart the requests can be while
17
26
  # still using the same auth token.
18
- #config.batch_request_buffer_throttle = 5.seconds
27
+ # config.batch_request_buffer_throttle = 5.seconds
19
28
 
20
29
  # This route will be the prefix for all oauth2 redirect callbacks. For
21
30
  # example, using the default '/omniauth', the github oauth2 provider will
22
31
  # redirect successful authentications to '/omniauth/github/callback'
23
- #config.omniauth_prefix = "/omniauth"
32
+ # config.omniauth_prefix = "/omniauth"
33
+
34
+ # By default sending current password is not needed for the password update.
35
+ # Uncomment to enforce current_password param to be checked before all
36
+ # attribute updates. Set it to :password if you want it to be checked only if
37
+ # password is updated.
38
+ # config.check_current_password_before_update = :attributes
39
+
40
+ # By default we will use callbacks for single omniauth.
41
+ # It depends on fields like email, provider and uid.
42
+ # config.default_callbacks = true
43
+
44
+ # Makes it possible to change the headers names
45
+ # config.headers_names = {:'access-token' => 'access-token',
46
+ # :'client' => 'client',
47
+ # :'expiry' => 'expiry',
48
+ # :'uid' => 'uid',
49
+ # :'token-type' => 'token-type' }
50
+
51
+ # By default, only Bearer Token authentication is implemented out of the box.
52
+ # If, however, you wish to integrate with legacy Devise authentication, you can
53
+ # do so by enabling this flag. NOTE: This feature is highly experimental!
54
+ # config.enable_standard_devise_support = false
24
55
  end
@@ -0,0 +1,5 @@
1
+ <p><%= t(:welcome).capitalize + ' ' + @email %>!</p>
2
+
3
+ <p><%= t '.confirm_link_msg' %> </p>
4
+
5
+ <p><%= link_to t('.confirm_account_link'), confirmation_url(@resource, {confirmation_token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url']}).html_safe %></p>
@@ -0,0 +1,8 @@
1
+ <p><%= t(:hello).capitalize %> <%= @resource.email %>!</p>
2
+
3
+ <p><%= t '.request_reset_link_msg' %></p>
4
+
5
+ <p><%= link_to t('.password_change_link'), edit_password_url(@resource, reset_password_token: @token, config: message['client-config'].to_s, redirect_url: message['redirect-url'].to_s).html_safe %></p>
6
+
7
+ <p><%= t '.ignore_mail_msg' %></p>
8
+ <p><%= t '.no_changes_msg' %></p>
@@ -24,7 +24,7 @@ FactoryBot.define do
24
24
  end
25
25
 
26
26
  trait :facebook do
27
- uid { Faker::Number.number(10) }
27
+ uid { Faker::Number.number }
28
28
  provider { 'facebook' }
29
29
  end
30
30
 
@@ -0,0 +1,191 @@
1
+ require 'test_helper'
2
+
3
+ class DeviseTokenAuth::TokenFactoryTest < ActiveSupport::TestCase
4
+ describe 'TokenFactory module' do
5
+ let(:tf) { DeviseTokenAuth::TokenFactory }
6
+ let(:token_regexp) { /^[-_A-Za-z0-9]{22}$/ }
7
+
8
+ it 'should be defined' do
9
+ assert_equal(tf.present?, true)
10
+ assert_kind_of(Module, tf)
11
+ end
12
+
13
+ describe 'interface' do
14
+ let(:token_hash_cost_regexp) { /\$[\w]+\$([\d]+)\$/ }
15
+ let(:lifespan) { 10 }
16
+ let(:cost) { DeviseTokenAuth.token_cost }
17
+
18
+ it '::secure_string' do
19
+ assert_respond_to(tf, :secure_string)
20
+
21
+ secure_string = tf.secure_string
22
+ assert_equal(secure_string.size, 22)
23
+ assert_match(token_regexp, secure_string)
24
+
25
+ SecureRandom.stub(:urlsafe_base64, secure_string) do
26
+ assert_equal(tf.secure_string, secure_string)
27
+ end
28
+ end
29
+
30
+ it '::client' do
31
+ assert_respond_to(tf, :client)
32
+
33
+ client = tf.client
34
+ assert_equal(client.size, 22)
35
+ assert_match(token_regexp, client)
36
+
37
+ secure_string = tf.secure_string
38
+ tf.stub(:secure_string, secure_string) do
39
+ assert_equal(tf.client, secure_string)
40
+ end
41
+ end
42
+
43
+ it '::token' do
44
+ assert_respond_to(tf, :token)
45
+
46
+ token = tf.token
47
+ assert_kind_of(String, token)
48
+ assert_equal(token.size, 22)
49
+ assert_match(token_regexp, token)
50
+
51
+ secure_string = tf.secure_string
52
+ tf.stub(:secure_string, secure_string) do
53
+ assert_equal(tf.token, secure_string)
54
+ end
55
+ end
56
+
57
+ it '::token_hash(args)' do
58
+ assert_respond_to(tf, :token_hash)
59
+
60
+ token_hash = tf.token_hash(tf.token)
61
+ assert_equal(token_hash.size, 60)
62
+ assert_kind_of(String, token_hash)
63
+
64
+ token_cost = token_hash_cost_regexp.match(token_hash)[1].to_i
65
+ assert_equal(token_cost, cost)
66
+
67
+ cost = DeviseTokenAuth.token_cost == 4 ? 10 : 4
68
+ token_hash = tf.token_hash(tf.token, cost)
69
+ token_cost = token_hash_cost_regexp.match(token_hash)[1].to_i
70
+ assert_equal(token_cost, cost)
71
+
72
+ cost = nil
73
+ token_hash = tf.token_hash(tf.token, cost)
74
+ token_cost = token_hash_cost_regexp.match(token_hash)[1].to_i
75
+ assert_equal(token_cost, DeviseTokenAuth.token_cost)
76
+ end
77
+
78
+ it '::expiry' do
79
+ assert_respond_to(tf, :expiry)
80
+
81
+ assert_kind_of(Integer, tf.expiry)
82
+ assert tf.expiry > Time.now.to_i
83
+ end
84
+
85
+ it '::expiry(args)' do
86
+ time = Time.now
87
+ Time.stub(:now, time) do
88
+ assert_equal(tf.expiry(lifespan), (time + lifespan).to_i)
89
+
90
+ lifespan = nil
91
+ assert_equal(tf.expiry(lifespan), (time + DeviseTokenAuth.token_lifespan).to_i)
92
+ end
93
+ end
94
+
95
+ it '::create' do
96
+ assert_respond_to(tf, :create)
97
+
98
+ token = tf.create
99
+ assert token
100
+ token.members.each { |m| refute_nil token[m] }
101
+ end
102
+
103
+ it '::create(args)' do
104
+ client = tf.client
105
+ token = tf.create(client: client)
106
+ assert_equal(token.client, client)
107
+
108
+ time = Time.now
109
+ Time.stub(:now, time) do
110
+ token = tf.create(lifespan: lifespan)
111
+ assert_equal(token.expiry, (time + lifespan).to_i)
112
+ end
113
+
114
+ token = tf.create(cost: cost)
115
+ token_cost = token_hash_cost_regexp.match(token.token_hash)[1].to_i
116
+ assert_equal(token_cost, cost)
117
+ end
118
+
119
+ it '::new' do
120
+ assert_respond_to(tf, :new)
121
+
122
+ token = tf.new
123
+ token.each { |v| assert_nil v }
124
+ end
125
+
126
+ it '::valid_token_hash?' do
127
+ assert_respond_to(tf, :valid_token_hash?)
128
+
129
+ refute tf.valid_token_hash?('koskoskos')
130
+ assert tf.valid_token_hash?(tf.create.token_hash)
131
+ end
132
+
133
+ it '::token_hash_is_token?' do
134
+ assert_respond_to(tf, :token_hash_is_token?)
135
+
136
+ token = tf.create
137
+ refute tf.token_hash_is_token?(token.token_hash, 'koskoskos')
138
+ refute tf.token_hash_is_token?('koskoskos', token.token)
139
+ assert tf.token_hash_is_token?(token.token_hash, token.token)
140
+ end
141
+ end
142
+
143
+ describe 'token object implements' do
144
+ let(:object) { tf.create }
145
+
146
+ it '#client' do
147
+ assert_respond_to(object, :client)
148
+
149
+ assert_kind_of(String, object.client)
150
+ assert_equal(object.client.size, 22)
151
+ assert_match(token_regexp, object.client)
152
+ end
153
+
154
+ it '#token' do
155
+ assert_respond_to(object, :token)
156
+
157
+ assert_kind_of(String, object.token)
158
+ assert_equal(object.token.size, 22)
159
+ assert_match(token_regexp, object.token)
160
+ end
161
+
162
+ it '#token_hash' do
163
+ assert_respond_to(object, :token_hash)
164
+
165
+ assert_kind_of(String, object.token_hash)
166
+ assert_equal(object.token_hash.size, 60)
167
+ end
168
+
169
+ it '#expiry' do
170
+ assert_respond_to(object, :expiry)
171
+ assert_kind_of(Integer, object.expiry)
172
+ end
173
+
174
+ it '#clear!' do
175
+ assert_respond_to(object, :clear!)
176
+
177
+ assert object.clear!
178
+ object.each { |v| assert_nil v }
179
+ end
180
+
181
+ it '#present?' do
182
+ assert_respond_to(object, :present?)
183
+
184
+ assert object.present?
185
+
186
+ object.token = nil
187
+ refute object.present?
188
+ end
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+
3
+ if DEVISE_TOKEN_AUTH_ORM == :active_record
4
+ describe 'DeviseTokenAuth::TokensSerialization' do
5
+ let(:ts) { DeviseTokenAuth::TokensSerialization }
6
+ let(:user) { FactoryBot.create(:user) }
7
+ let(:tokens) do
8
+ # Сreate all possible token's attributes combinations
9
+ user.create_token
10
+ 2.times { user.create_new_auth_token(user.tokens.first[0]) }
11
+ user.create_new_auth_token
12
+ user.create_token
13
+
14
+ user.tokens
15
+ end
16
+ let(:json) { JSON.generate(tokens) }
17
+
18
+ it 'is defined' do
19
+ assert_equal(ts.present?, true)
20
+ assert_kind_of(Module, ts)
21
+ end
22
+
23
+ describe '.load(json)' do
24
+ let(:default) { {} }
25
+
26
+ it 'is defined' do
27
+ assert_respond_to(ts, :load)
28
+ end
29
+
30
+ it 'handles nil' do
31
+ assert_equal(ts.load(nil), default)
32
+ end
33
+
34
+ it 'handles string' do
35
+ assert_equal(ts.load(json), JSON.parse(json))
36
+ end
37
+
38
+ it 'returns object of undesirable class' do
39
+ assert_equal(ts.load([]), [])
40
+ end
41
+ end
42
+
43
+ describe '.dump(object)' do
44
+ let(:default) { 'null' }
45
+
46
+ it 'is defined' do
47
+ assert_respond_to(ts, :dump)
48
+ end
49
+
50
+ it 'handles nil' do
51
+ assert_equal(ts.dump(nil), default)
52
+ end
53
+
54
+ it 'handles empty hash' do
55
+ assert_equal(ts.dump({}), '{}')
56
+ end
57
+
58
+ it 'deserialize tokens' do
59
+ assert_equal(ts.dump(tokens), json)
60
+ end
61
+
62
+ it 'removes nil values' do
63
+ new_tokens = tokens.dup
64
+ new_tokens[new_tokens.first[0]][:kos] = nil
65
+
66
+ assert_equal(ts.dump(tokens), ts.dump(new_tokens))
67
+ end
68
+ end
69
+ end
70
+ end
@@ -76,38 +76,6 @@ class UserTest < ActiveSupport::TestCase
76
76
  end
77
77
  end
78
78
 
79
- describe 'user specific token lifespan' do
80
- before do
81
- @resource = create(:user, :confirmed)
82
-
83
- auth_headers = @resource.create_new_auth_token
84
- @token_global = auth_headers['access-token']
85
- @client_id_global = auth_headers['client']
86
-
87
- def @resource.token_lifespan
88
- 1.minute
89
- end
90
-
91
- auth_headers = @resource.create_new_auth_token
92
- @token_specific = auth_headers['access-token']
93
- @client_id_specific = auth_headers['client']
94
- end
95
-
96
- test 'works per user' do
97
- assert @resource.token_is_current?(@token_global, @client_id_global)
98
-
99
- time = Time.zone.now.to_i
100
- expiry_global = @resource.tokens[@client_id_global]['expiry'] || @resource.tokens[@client_id_global][:expiry]
101
-
102
- assert expiry_global > time + DeviseTokenAuth.token_lifespan - 5.seconds
103
- assert expiry_global < time + DeviseTokenAuth.token_lifespan + 5.seconds
104
-
105
- expiry_specific = @resource.tokens[@client_id_specific]['expiry'] || @resource.tokens[@client_id_specific][:expiry]
106
- assert expiry_specific > time + 55.seconds
107
- assert expiry_specific < time + 65.seconds
108
- end
109
- end
110
-
111
79
  describe 'expired tokens are destroyed on save' do
112
80
  before do
113
81
  @resource = create(:user, :confirmed)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_token_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lynn Hurley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-18 00:00:00.000000000 Z
11
+ date: 2019-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: 4.2.0
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6'
22
+ version: '6.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: 4.2.0
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6'
32
+ version: '6.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: devise
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -50,6 +50,20 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '4.7'
53
+ - !ruby/object:Gem::Dependency
54
+ name: bcrypt
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '3.0'
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '3.0'
53
67
  - !ruby/object:Gem::Dependency
54
68
  name: appraisal
55
69
  requirement: !ruby/object:Gem::Requirement
@@ -162,9 +176,10 @@ files:
162
176
  - app/controllers/devise_token_auth/unlocks_controller.rb
163
177
  - app/models/devise_token_auth/concerns/active_record_support.rb
164
178
  - app/models/devise_token_auth/concerns/mongoid_support.rb
179
+ - app/models/devise_token_auth/concerns/tokens_serialization.rb
165
180
  - app/models/devise_token_auth/concerns/user.rb
166
181
  - app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb
167
- - app/validators/devise_token_auth/email_validator.rb
182
+ - app/validators/devise_token_auth_email_validator.rb
168
183
  - app/views/devise/mailer/confirmation_instructions.html.erb
169
184
  - app/views/devise/mailer/reset_password_instructions.html.erb
170
185
  - app/views/devise/mailer/unlock_instructions.html.erb
@@ -197,6 +212,7 @@ files:
197
212
  - lib/devise_token_auth/engine.rb
198
213
  - lib/devise_token_auth/errors.rb
199
214
  - lib/devise_token_auth/rails/routes.rb
215
+ - lib/devise_token_auth/token_factory.rb
200
216
  - lib/devise_token_auth/url.rb
201
217
  - lib/devise_token_auth/version.rb
202
218
  - lib/generators/devise_token_auth/USAGE
@@ -299,17 +315,17 @@ files:
299
315
  - test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb
300
316
  - test/dummy/db/schema.rb
301
317
  - test/dummy/lib/migration_database_helper.rb
302
- - test/dummy/tmp/generators/app/models/azpire/v1/human_resource/user.rb
303
- - test/dummy/tmp/generators/config/initializers/devise_token_auth.rb
304
- - test/dummy/tmp/generators/config/routes.rb
305
- - test/dummy/tmp/generators/db/migrate/20190112150327_devise_token_auth_create_azpire_v1_human_resource_users.rb
318
+ - test/dummy/tmp/generators/app/views/devise/mailer/confirmation_instructions.html.erb
319
+ - test/dummy/tmp/generators/app/views/devise/mailer/reset_password_instructions.html.erb
306
320
  - test/factories/users.rb
307
321
  - test/lib/devise_token_auth/blacklist_test.rb
322
+ - test/lib/devise_token_auth/token_factory_test.rb
308
323
  - test/lib/devise_token_auth/url_test.rb
309
324
  - test/lib/generators/devise_token_auth/install_generator_test.rb
310
325
  - test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb
311
326
  - test/lib/generators/devise_token_auth/install_views_generator_test.rb
312
327
  - test/models/concerns/mongoid_support_test.rb
328
+ - test/models/concerns/tokens_serialization_test.rb
313
329
  - test/models/only_email_user_test.rb
314
330
  - test/models/user_test.rb
315
331
  - test/support/controllers/routes.rb
@@ -406,18 +422,18 @@ test_files:
406
422
  - test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb
407
423
  - test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb
408
424
  - test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb
409
- - test/dummy/tmp/generators/app/models/azpire/v1/human_resource/user.rb
410
- - test/dummy/tmp/generators/config/routes.rb
411
- - test/dummy/tmp/generators/config/initializers/devise_token_auth.rb
412
- - test/dummy/tmp/generators/db/migrate/20190112150327_devise_token_auth_create_azpire_v1_human_resource_users.rb
425
+ - test/dummy/tmp/generators/app/views/devise/mailer/confirmation_instructions.html.erb
426
+ - test/dummy/tmp/generators/app/views/devise/mailer/reset_password_instructions.html.erb
413
427
  - test/dummy/README.rdoc
414
428
  - test/models/only_email_user_test.rb
415
429
  - test/models/concerns/mongoid_support_test.rb
430
+ - test/models/concerns/tokens_serialization_test.rb
416
431
  - test/models/user_test.rb
417
432
  - test/support/controllers/routes.rb
418
433
  - test/factories/users.rb
419
434
  - test/lib/devise_token_auth/url_test.rb
420
435
  - test/lib/devise_token_auth/blacklist_test.rb
436
+ - test/lib/devise_token_auth/token_factory_test.rb
421
437
  - test/lib/generators/devise_token_auth/install_generator_test.rb
422
438
  - test/lib/generators/devise_token_auth/install_views_generator_test.rb
423
439
  - test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb