devise_jwt_auth 0.1.5 → 0.1.6

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.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/app/controllers/devise_jwt_auth/application_controller.rb +14 -11
  4. data/app/controllers/devise_jwt_auth/concerns/resource_finder.rb +2 -6
  5. data/app/controllers/devise_jwt_auth/concerns/set_user_by_token.rb +17 -19
  6. data/app/controllers/devise_jwt_auth/confirmations_controller.rb +10 -19
  7. data/app/controllers/devise_jwt_auth/omniauth_callbacks_controller.rb +32 -33
  8. data/app/controllers/devise_jwt_auth/passwords_controller.rb +29 -19
  9. data/app/controllers/devise_jwt_auth/refresh_token_controller.rb +4 -1
  10. data/app/controllers/devise_jwt_auth/registrations_controller.rb +40 -21
  11. data/app/controllers/devise_jwt_auth/sessions_controller.rb +18 -12
  12. data/app/controllers/devise_jwt_auth/unlocks_controller.rb +5 -4
  13. data/app/models/devise_jwt_auth/concerns/active_record_support.rb +3 -0
  14. data/app/models/devise_jwt_auth/concerns/confirmable_support.rb +7 -4
  15. data/app/models/devise_jwt_auth/concerns/mongoid_support.rb +3 -0
  16. data/app/models/devise_jwt_auth/concerns/tokens_serialization.rb +4 -1
  17. data/app/models/devise_jwt_auth/concerns/user.rb +18 -9
  18. data/app/models/devise_jwt_auth/concerns/user_omniauth_callbacks.rb +11 -3
  19. data/app/validators/devise_jwt_auth_email_validator.rb +4 -3
  20. data/lib/devise_jwt_auth/blacklist.rb +2 -0
  21. data/lib/devise_jwt_auth/controllers/url_helpers.rb +1 -2
  22. data/lib/devise_jwt_auth/engine.rb +4 -4
  23. data/lib/devise_jwt_auth/rails/routes.rb +35 -24
  24. data/lib/devise_jwt_auth/token_factory.rb +3 -2
  25. data/lib/devise_jwt_auth/url.rb +2 -4
  26. data/lib/devise_jwt_auth/version.rb +1 -1
  27. data/lib/generators/devise_jwt_auth/install_generator.rb +7 -6
  28. data/lib/generators/devise_jwt_auth/install_generator_helpers.rb +14 -7
  29. data/lib/generators/devise_jwt_auth/install_mongoid_generator.rb +3 -2
  30. data/lib/generators/devise_jwt_auth/templates/devise_jwt_auth.rb +2 -3
  31. data/test/controllers/custom/custom_confirmations_controller_test.rb +2 -2
  32. data/test/controllers/custom/custom_passwords_controller_test.rb +4 -4
  33. data/test/controllers/custom/custom_refresh_token_controller_test.rb +2 -3
  34. data/test/controllers/custom/custom_registrations_controller_test.rb +2 -2
  35. data/test/controllers/demo_mang_controller_test.rb +206 -210
  36. data/test/controllers/demo_user_controller_test.rb +358 -374
  37. data/test/controllers/devise_jwt_auth/confirmations_controller_test.rb +5 -5
  38. data/test/controllers/devise_jwt_auth/omniauth_callbacks_controller_test.rb +6 -7
  39. data/test/controllers/devise_jwt_auth/passwords_controller_test.rb +11 -13
  40. data/test/controllers/devise_jwt_auth/refresh_token_controller_test.rb +8 -12
  41. data/test/controllers/devise_jwt_auth/registrations_controller_test.rb +23 -25
  42. data/test/controllers/devise_jwt_auth/sessions_controller_test.rb +30 -32
  43. data/test/controllers/devise_jwt_auth/unlocks_controller_test.rb +2 -2
  44. data/test/controllers/overrides/confirmations_controller_test.rb +1 -1
  45. data/test/controllers/overrides/passwords_controller_test.rb +1 -1
  46. data/test/controllers/overrides/refresh_token_controller_test.rb +1 -2
  47. data/test/controllers/overrides/registrations_controller_test.rb +1 -1
  48. data/test/dummy/app/controllers/custom/refresh_token_controller.rb +2 -1
  49. data/test/dummy/app/controllers/custom/registrations_controller.rb +1 -1
  50. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +4 -4
  51. data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +4 -4
  52. data/test/dummy/app/controllers/overrides/passwords_controller.rb +4 -4
  53. data/test/dummy/app/controllers/overrides/refresh_token_controller.rb +1 -1
  54. data/test/dummy/app/controllers/overrides/registrations_controller.rb +2 -2
  55. data/test/dummy/app/controllers/overrides/sessions_controller.rb +2 -2
  56. data/test/dummy/app/models/concerns/favorite_color.rb +11 -9
  57. data/test/dummy/config.ru +2 -2
  58. data/test/dummy/config/application.rb +1 -0
  59. data/test/dummy/config/boot.rb +1 -1
  60. data/test/dummy/config/environments/test.rb +11 -7
  61. data/test/dummy/config/initializers/figaro.rb +1 -1
  62. data/test/dummy/config/initializers/omniauth.rb +2 -2
  63. data/test/dummy/config/routes.rb +8 -8
  64. data/test/dummy/db/migrate/20141222035835_devise_jwt_auth_create_only_email_users.rb +9 -9
  65. data/test/dummy/db/migrate/20190924101113_devise_jwt_auth_create_confirmable_users.rb +6 -5
  66. data/test/dummy/db/schema.rb +170 -170
  67. data/test/dummy/tmp/generators/app/controllers/application_controller.rb +6 -0
  68. data/test/dummy/tmp/generators/config/initializers/devise_jwt_auth.rb +2 -3
  69. data/test/dummy/tmp/generators/db/migrate/{20200228012905_devise_jwt_auth_create_users.rb → 20201006030349_devise_jwt_auth_create_users.rb} +0 -0
  70. data/test/factories/users.rb +5 -3
  71. data/test/lib/devise_jwt_auth/token_factory_test.rb +6 -6
  72. data/test/lib/generators/devise_jwt_auth/install_generator_test.rb +3 -20
  73. data/test/lib/generators/devise_jwt_auth/install_generator_with_namespace_test.rb +4 -21
  74. data/test/models/concerns/tokens_serialization_test.rb +68 -68
  75. data/test/models/user_test.rb +35 -37
  76. data/test/support/controllers/routes.rb +7 -5
  77. data/test/test_helper.rb +1 -1
  78. metadata +50 -54
  79. data/test/dummy/tmp/generators/app/models/mang.rb +0 -9
  80. data/test/dummy/tmp/generators/config/routes.rb +0 -9
  81. data/test/dummy/tmp/generators/db/migrate/20200228012905_devise_jwt_auth_create_mangs.rb +0 -54
@@ -0,0 +1,6 @@
1
+ class ApplicationController < ActionController::Base
2
+ include DeviseJwtAuth::Concerns::SetUserByToken
3
+ def whatever
4
+ 'whatever'
5
+ end
6
+ end
@@ -6,7 +6,7 @@ DeviseJwtAuth.setup do |config|
6
6
  # use the HTTP only refresh cookie that is sent during the authentication
7
7
  # process and make refresh token requests.
8
8
  # config.send_new_access_token_on_each_request = false
9
-
9
+
10
10
  # By default, refresh token HTTP Only cookies last for 2 weeks. These tokens
11
11
  # are used for requesting shorter-lived acccess tokens.
12
12
  # config.refresh_token_lifespan = 2.weeks
@@ -32,7 +32,7 @@ DeviseJwtAuth.setup do |config|
32
32
  # environment variable or secret key base that isn't store in a repository.
33
33
  # Also, its a good idea to NOT use the same key for access tokens.
34
34
  config.refresh_token_encryption_key = 'your-refresh-token-secret-key-here'
35
-
35
+
36
36
  # This is the refresh token encryption key. You should set this in an
37
37
  # environment variable or secret key base that isn't store in a repository.
38
38
  # Also, its a good idea to NOT use the same key for access tokens.
@@ -70,5 +70,4 @@ DeviseJwtAuth.setup do |config|
70
70
  # config.update_token_version_after_password_reset = true
71
71
  # config.bypass_sign_in = true
72
72
  # config.require_client_password_reset_token = false
73
-
74
73
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  factory :user do
3
5
  email { Faker::Internet.unique.safe_email }
@@ -13,13 +15,13 @@ FactoryBot.define do
13
15
  end
14
16
 
15
17
  trait :confirmed do
16
- after(:create) { |user| user.confirm }
18
+ after(:create, &:confirm)
17
19
  end
18
20
 
19
21
  # confirmation period is expired
20
22
  trait :unconfirmed do
21
23
  after(:create) do |user, evaluator|
22
- user.update_attribute(:confirmation_sent_at, evaluator.allow_unconfirmed_period - 1.day )
24
+ user.update_attribute(:confirmation_sent_at, evaluator.allow_unconfirmed_period - 1.day)
23
25
  end
24
26
  end
25
27
 
@@ -29,7 +31,7 @@ FactoryBot.define do
29
31
  end
30
32
 
31
33
  trait :locked do
32
- after(:create) { |user| user.lock_access! }
34
+ after(:create, &:lock_access!)
33
35
  end
34
36
 
35
37
  factory :lockable_user, class: 'LockableUser'
@@ -5,15 +5,15 @@ require 'test_helper'
5
5
  class DeviseJwtAuth::TokenFactoryTest < ActiveSupport::TestCase
6
6
  describe 'TokenFactory module' do
7
7
  let(:tf) { DeviseJwtAuth::TokenFactory }
8
- let(:token_regexp) { /^[A-Za-z0-9\-_=]+\.[A-Za-z0-9\-_=]+\.?[A-Za-z0-9\-_.+\/=]*$/ }
9
-
8
+ let(:token_regexp) { %r{^[A-Za-z0-9\-_=]+\.[A-Za-z0-9\-_=]+\.?[A-Za-z0-9\-_.+/=]*$} }
9
+
10
10
  it 'should be defined' do
11
11
  assert_equal(tf.present?, true)
12
12
  assert_kind_of(Module, tf)
13
13
  end
14
-
14
+
15
15
  describe 'interface' do
16
- let(:payload) { { foo: 'test'} }
16
+ let(:payload) { { foo: 'test' } }
17
17
  let(:future_exp) { (Time.zone.now + 1.hour).to_i }
18
18
  let(:past_exp) { (Time.zone.now - 1.hour).to_i }
19
19
 
@@ -50,7 +50,7 @@ class DeviseJwtAuth::TokenFactoryTest < ActiveSupport::TestCase
50
50
  result = tf.decode_refresh_token(token)
51
51
  assert_nil result['exp']
52
52
  end
53
-
53
+
54
54
  it 'invalidates bogus token' do
55
55
  result = tf.decode_refresh_token('bogus token')
56
56
  assert_empty result
@@ -91,7 +91,7 @@ class DeviseJwtAuth::TokenFactoryTest < ActiveSupport::TestCase
91
91
  result = tf.decode_access_token(token)
92
92
  assert_nil result['exp']
93
93
  end
94
-
94
+
95
95
  it 'invalidates bogus token' do
96
96
  result = tf.decode_access_token('bogus token')
97
97
  assert_empty result
@@ -44,17 +44,6 @@ module DeviseJwtAuth
44
44
  assert_migration 'db/migrate/devise_jwt_auth_create_users.rb'
45
45
  end
46
46
  end
47
-
48
- =begin
49
- test 'add primary key type with rails 5 when specified in rails generator' do
50
- run_generator %w[--primary_key_type=uuid --force]
51
- if Rails::VERSION::MAJOR >= 5
52
- assert_migration 'db/migrate/devise_jwt_auth_create_users.rb', /create_table\(:users, id: :uuid\) do/
53
- else
54
- assert_migration 'db/migrate/devise_jwt_auth_create_users.rb', /create_table\(:users\) do/
55
- end
56
- end
57
- =end
58
47
  end
59
48
  end
60
49
 
@@ -72,15 +61,12 @@ module DeviseJwtAuth
72
61
  case DEVISE_JWT_AUTH_ORM
73
62
  when :active_record
74
63
  # account for rails version 5
75
- active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
64
+ active_record_needle = Rails::VERSION::MAJOR == 5 ? 'ApplicationRecord' : 'ActiveRecord::Base'
76
65
 
77
66
  @f = File.open(@fname, 'w') do |f|
78
67
  f.write <<-RUBY
79
68
  class User < #{active_record_needle}
80
-
81
- def whatever
82
- puts 'whatever'
83
- end
69
+ def whatever; puts 'whatever'; end
84
70
  end
85
71
  RUBY
86
72
  end
@@ -88,10 +74,7 @@ module DeviseJwtAuth
88
74
  @f = File.open(@fname, 'w') do |f|
89
75
  f.write <<-'RUBY'
90
76
  class User
91
-
92
- def whatever
93
- puts 'whatever'
94
- end
77
+ def whatever; puts 'whatever'; end
95
78
  end
96
79
  RUBY
97
80
  end
@@ -14,7 +14,7 @@ module DeviseJwtAuth
14
14
  # The namespaced user model for testing
15
15
  let(:user_class) { 'Azpire::V1::HumanResource::User' }
16
16
  let(:namespace_path) { user_class.underscore }
17
- let(:table_name) { user_class.pluralize.underscore.gsub('/','_') }
17
+ let(:table_name) { user_class.pluralize.underscore.gsub('/', '_') }
18
18
 
19
19
  describe 'user model with namespace, clean install' do
20
20
  setup :prepare_destination
@@ -49,17 +49,6 @@ module DeviseJwtAuth
49
49
  assert_migration "db/migrate/devise_jwt_auth_create_#{table_name}.rb"
50
50
  end
51
51
  end
52
-
53
- =begin
54
- test 'add primary key type with rails 5 when specified in rails generator' do
55
- run_generator %W[#{user_class} auth --primary_key_type=uuid --force]
56
- if Rails::VERSION::MAJOR >= 5
57
- assert_migration "db/migrate/devise_jwt_auth_create_#{table_name}.rb", /create_table\(:#{table_name}, id: :uuid\) do/
58
- else
59
- assert_migration "db/migrate/devise_jwt_auth_create_#{table_name}.rb", /create_table\(:#{table_name}\) do/
60
- end
61
- end
62
- =end
63
52
  end
64
53
  end
65
54
 
@@ -77,15 +66,12 @@ module DeviseJwtAuth
77
66
  case DEVISE_JWT_AUTH_ORM
78
67
  when :active_record
79
68
  # account for rails version 5
80
- active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
69
+ active_record_needle = Rails::VERSION::MAJOR == 5 ? 'ApplicationRecord' : 'ActiveRecord::Base'
81
70
 
82
71
  @f = File.open(@fname, 'w') do |f|
83
72
  f.write <<-RUBY
84
73
  class User < #{active_record_needle}
85
-
86
- def whatever
87
- puts 'whatever'
88
- end
74
+ def whatever; puts 'whatever'; end
89
75
  end
90
76
  RUBY
91
77
  end
@@ -93,10 +79,7 @@ module DeviseJwtAuth
93
79
  @f = File.open(@fname, 'w') do |f|
94
80
  f.write <<-'RUBY'
95
81
  class User
96
-
97
- def whatever
98
- puts 'whatever'
99
- end
82
+ def whatever; puts 'whatever'; end
100
83
  end
101
84
  RUBY
102
85
  end
@@ -1,72 +1,72 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  if DEVISE_JWT_AUTH_ORM == :active_record
4
- =begin
5
- describe 'DeviseJwtAuth::Concerns::TokensSerialization' do
6
- let(:ts) { DeviseJwtAuth::Concerns::TokensSerialization }
7
- let(:user) { FactoryBot.create(:user) }
8
- let(:tokens) do
9
- # Сreate all possible token's attributes combinations
10
- user.create_token
11
- 2.times { user.create_new_auth_token(user.tokens.first[0]) }
12
- user.create_new_auth_token
13
- user.create_token
14
-
15
- user.tokens
16
- end
17
- let(:json) { JSON.generate(tokens) }
18
-
19
- it 'is defined' do
20
- assert_equal(ts.present?, true)
21
- assert_kind_of(Module, ts)
22
- end
23
-
24
- describe '.load(json)' do
25
- let(:default) { {} }
26
-
27
- it 'is defined' do
28
- assert_respond_to(ts, :load)
29
- end
30
-
31
- it 'handles nil' do
32
- assert_equal(ts.load(nil), default)
33
- end
34
-
35
- it 'handles string' do
36
- assert_equal(ts.load(json), JSON.parse(json))
37
- end
38
-
39
- it 'returns object of undesirable class' do
40
- assert_equal(ts.load([]), [])
41
- end
42
- end
43
-
44
- describe '.dump(object)' do
45
- let(:default) { 'null' }
46
-
47
- it 'is defined' do
48
- assert_respond_to(ts, :dump)
49
- end
50
-
51
- it 'handles nil' do
52
- assert_equal(ts.dump(nil), default)
53
- end
54
-
55
- it 'handles empty hash' do
56
- assert_equal(ts.dump({}), '{}')
57
- end
58
-
59
- it 'deserialize tokens' do
60
- assert_equal(ts.dump(tokens), json)
61
- end
62
-
63
- it 'removes nil values' do
64
- new_tokens = tokens.dup
65
- new_tokens[new_tokens.first[0]][:kos] = nil
66
-
67
- assert_equal(ts.dump(tokens), ts.dump(new_tokens))
68
- end
69
- end
70
- end
71
- =end
6
+ # describe 'DeviseJwtAuth::Concerns::TokensSerialization' do
7
+ # let(:ts) { DeviseJwtAuth::Concerns::TokensSerialization }
8
+ # let(:user) { FactoryBot.create(:user) }
9
+ # let(:tokens) do
10
+ # # Create all possible token's attributes combinations
11
+ # user.create_token
12
+ # 2.times { user.create_new_auth_token(user.tokens.first[0]) }
13
+ # user.create_new_auth_token
14
+ # user.create_token
15
+ #
16
+ # user.tokens
17
+ # end
18
+ # let(:json) { JSON.generate(tokens) }
19
+ #
20
+ # it 'is defined' do
21
+ # assert_equal(ts.present?, true)
22
+ # assert_kind_of(Module, ts)
23
+ # end
24
+ #
25
+ # describe '.load(json)' do
26
+ # let(:default) { {} }
27
+ #
28
+ # it 'is defined' do
29
+ # assert_respond_to(ts, :load)
30
+ # end
31
+ #
32
+ # it 'handles nil' do
33
+ # assert_equal(ts.load(nil), default)
34
+ # end
35
+ #
36
+ # it 'handles string' do
37
+ # assert_equal(ts.load(json), JSON.parse(json))
38
+ # end
39
+ #
40
+ # it 'returns object of undesirable class' do
41
+ # assert_equal(ts.load([]), [])
42
+ # end
43
+ # end
44
+ #
45
+ # describe '.dump(object)' do
46
+ # let(:default) { 'null' }
47
+ #
48
+ # it 'is defined' do
49
+ # assert_respond_to(ts, :dump)
50
+ # end
51
+ #
52
+ # it 'handles nil' do
53
+ # assert_equal(ts.dump(nil), default)
54
+ # end
55
+ #
56
+ # it 'handles empty hash' do
57
+ # assert_equal(ts.dump({}), '{}')
58
+ # end
59
+ #
60
+ # it 'deserialize tokens' do
61
+ # assert_equal(ts.dump(tokens), json)
62
+ # end
63
+ #
64
+ # it 'removes nil values' do
65
+ # new_tokens = tokens.dup
66
+ # new_tokens[new_tokens.first[0]][:kos] = nil
67
+ #
68
+ # assert_equal(ts.dump(tokens), ts.dump(new_tokens))
69
+ # end
70
+ # end
71
+ # end
72
72
  end
@@ -58,43 +58,41 @@ class UserTest < ActiveSupport::TestCase
58
58
  end
59
59
  end
60
60
 
61
- =begin
62
- describe 'token expiry' do
63
- before do
64
- @resource = create(:user, :confirmed)
65
- # TODO: expire this token?
66
- @auth_headers = @resource.create_named_token_pair
67
-
68
- # @token = @auth_headers['access-token']
69
- # @client_id = @auth_headers['client']
70
- end
71
-
72
- test 'should properly indicate whether token is current' do
73
- assert @resource.token_is_current?(@token, @client_id)
74
- # we want to update the expiry without forcing a cleanup (see below)
75
- @resource.tokens[@client_id]['expiry'] = Time.zone.now.to_i - 10.seconds
76
- refute @resource.token_is_current?(@token, @client_id)
77
- end
78
- end
79
-
80
- describe 'expired tokens are destroyed on save' do
81
- before do
82
- @resource = create(:user, :confirmed)
83
-
84
- @old_auth_headers = @resource.create_new_auth_token
85
- @new_auth_headers = @resource.create_new_auth_token
86
- expire_token(@resource, @old_auth_headers['client'])
87
- end
88
-
89
- test 'expired token was removed' do
90
- refute @resource.tokens[@old_auth_headers[:client]]
91
- end
92
-
93
- test 'current token was not removed' do
94
- assert @resource.tokens[@new_auth_headers['client']]
95
- end
96
- end
97
- =end
61
+ # describe 'token expiry' do
62
+ # before do
63
+ # @resource = create(:user, :confirmed)
64
+ # # TODO: expire this token?
65
+ # @auth_headers = @resource.create_named_token_pair
66
+ #
67
+ # # @token = @auth_headers['access-token']
68
+ # # @client_id = @auth_headers['client']
69
+ # end
70
+ #
71
+ # test 'should properly indicate whether token is current' do
72
+ # assert @resource.token_is_current?(@token, @client_id)
73
+ # # we want to update the expiry without forcing a cleanup (see below)
74
+ # @resource.tokens[@client_id]['expiry'] = Time.zone.now.to_i - 10.seconds
75
+ # refute @resource.token_is_current?(@token, @client_id)
76
+ # end
77
+ # end
78
+ #
79
+ # describe 'expired tokens are destroyed on save' do
80
+ # before do
81
+ # @resource = create(:user, :confirmed)
82
+ #
83
+ # @old_auth_headers = @resource.create_new_auth_token
84
+ # @new_auth_headers = @resource.create_new_auth_token
85
+ # expire_token(@resource, @old_auth_headers['client'])
86
+ # end
87
+ #
88
+ # test 'expired token was removed' do
89
+ # refute @resource.tokens[@old_auth_headers[:client]]
90
+ # end
91
+ #
92
+ # test 'current token was not removed' do
93
+ # assert @resource.tokens[@new_auth_headers['client']]
94
+ # end
95
+ # end
98
96
 
99
97
  describe 'nil tokens are handled properly' do
100
98
  before do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Module
2
4
  include Minitest::Spec::DSL
3
5
  end
@@ -31,12 +33,12 @@ module OverridesControllersRoutes
31
33
  before do
32
34
  Rails.application.routes.draw do
33
35
  mount_devise_jwt_auth_for 'User', at: 'evil_user_auth', controllers: {
34
- confirmations: 'overrides/confirmations',
35
- passwords: 'overrides/passwords',
36
+ confirmations: 'overrides/confirmations',
37
+ passwords: 'overrides/passwords',
36
38
  omniauth_callbacks: 'overrides/omniauth_callbacks',
37
- registrations: 'overrides/registrations',
38
- sessions: 'overrides/sessions',
39
- refresh_token: 'overrides/refresh_token'
39
+ registrations: 'overrides/registrations',
40
+ sessions: 'overrides/sessions',
41
+ refresh_token: 'overrides/refresh_token'
40
42
  }
41
43
  end
42
44
  end