jwt-auth 4.2.0 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/README.md +119 -18
- data/bin/build +22 -0
- data/bin/release +40 -0
- data/jwt-auth.gemspec +18 -15
- data/lib/jwt/auth.rb +2 -0
- data/lib/jwt/auth/access_token.rb +20 -0
- data/lib/jwt/auth/authenticatable.rb +16 -0
- data/lib/jwt/auth/authentication.rb +63 -22
- data/lib/jwt/auth/configuration.rb +4 -1
- data/lib/jwt/auth/refresh_token.rb +20 -0
- data/lib/jwt/auth/token.rb +49 -41
- data/lib/jwt/auth/version.rb +3 -1
- data/spec/controllers/content_controller_spec.rb +95 -0
- data/spec/controllers/tokens_controller_spec.rb +140 -0
- data/spec/dummy/Rakefile +2 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +2 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +2 -0
- data/spec/dummy/app/controllers/application_controller.rb +6 -1
- data/spec/dummy/app/controllers/content_controller.rb +29 -0
- data/spec/dummy/app/controllers/tokens_controller.rb +53 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/helpers/authentication_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +3 -1
- data/spec/dummy/app/models/application_record.rb +2 -0
- data/spec/dummy/app/models/user.rb +3 -6
- data/spec/dummy/bin/bundle +2 -0
- data/spec/dummy/bin/rails +2 -0
- data/spec/dummy/bin/rake +2 -0
- data/spec/dummy/bin/setup +2 -0
- data/spec/dummy/bin/update +2 -0
- data/spec/dummy/bin/yarn +7 -7
- data/spec/dummy/config.ru +2 -0
- data/spec/dummy/config/application.rb +2 -0
- data/spec/dummy/config/boot.rb +3 -1
- data/spec/dummy/config/environment.rb +2 -0
- data/spec/dummy/config/environments/development.rb +3 -1
- data/spec/dummy/config/environments/production.rb +4 -2
- data/spec/dummy/config/environments/test.rb +2 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +2 -0
- data/spec/dummy/config/initializers/assets.rb +2 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +2 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +2 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
- data/spec/dummy/config/initializers/inflections.rb +2 -0
- data/spec/dummy/config/initializers/jwt_auth.rb +9 -2
- data/spec/dummy/config/initializers/mime_types.rb +2 -0
- data/spec/dummy/config/initializers/new_framework_defaults_5_2.rb +2 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +3 -1
- data/spec/dummy/config/puma.rb +5 -3
- data/spec/dummy/config/routes.rb +5 -4
- data/spec/dummy/config/spring.rb +4 -2
- data/spec/dummy/db/migrate/20170726110751_create_users.rb +2 -0
- data/spec/dummy/db/migrate/20170726110825_add_token_version_to_user.rb +2 -0
- data/spec/dummy/db/migrate/20170726112117_add_activated_to_user.rb +2 -0
- data/spec/dummy/db/migrate/20190221100103_add_password_to_user.rb +7 -0
- data/spec/dummy/db/schema.rb +10 -9
- data/spec/jwt/auth/access_token_spec.rb +35 -0
- data/spec/jwt/auth/configuration_spec.rb +36 -0
- data/spec/jwt/auth/refresh_token_spec.rb +35 -0
- data/spec/jwt/auth/token_spec.rb +144 -0
- data/spec/models/user_spec.rb +24 -0
- data/spec/rails_helper.rb +8 -0
- data/spec/spec_helper.rb +51 -53
- data/spec/support/database_cleaner.rb +22 -0
- data/spec/support/matchers/return_token.rb +33 -0
- data/version.yml +1 -0
- metadata +119 -54
- data/spec/authentication_spec.rb +0 -136
- data/spec/configuration_spec.rb +0 -18
- data/spec/dummy/app/controllers/authentication_controller.rb +0 -22
- data/spec/token_spec.rb +0 -125
data/spec/configuration_spec.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'securerandom'
|
4
|
-
|
5
|
-
require 'rails_helper'
|
6
|
-
|
7
|
-
RSpec.describe JWT::Auth do
|
8
|
-
it 'configures correctly' do
|
9
|
-
JWT::Auth.configure do |config|
|
10
|
-
config.token_lifetime = 24.hours
|
11
|
-
config.secret = 'mysecret'
|
12
|
-
end
|
13
|
-
|
14
|
-
expect(subject.token_lifetime).to eq 24.hours
|
15
|
-
expect(subject.secret).to eq 'mysecret'
|
16
|
-
expect(subject.model).to eq 'User'
|
17
|
-
end
|
18
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
class AuthenticationController < ApplicationController
|
2
|
-
# Authenticates user from request header
|
3
|
-
before_action :authenticate_user, :only => :private
|
4
|
-
|
5
|
-
# Validate token
|
6
|
-
before_action :validate_token, :only => :validate
|
7
|
-
|
8
|
-
# Renew token and set response header
|
9
|
-
after_action :renew_token
|
10
|
-
|
11
|
-
def public
|
12
|
-
head :no_content
|
13
|
-
end
|
14
|
-
|
15
|
-
def private
|
16
|
-
head :no_content
|
17
|
-
end
|
18
|
-
|
19
|
-
def validate
|
20
|
-
head :no_content
|
21
|
-
end
|
22
|
-
end
|
data/spec/token_spec.rb
DELETED
@@ -1,125 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe JWT::Auth::Token do
|
4
|
-
let(:user) { User.create! :activated => true }
|
5
|
-
let(:token) { JWT::Auth::Token.from_user user }
|
6
|
-
|
7
|
-
describe 'properties' do
|
8
|
-
let(:token) { JWT::Auth::Token.from_user user }
|
9
|
-
|
10
|
-
it 'has an issued at' do
|
11
|
-
expect(token).to respond_to :issued_at
|
12
|
-
expect(token.issued_at).to be_nil
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'has a subject' do
|
16
|
-
expect(token).to respond_to :subject
|
17
|
-
expect(token.subject).to eq user
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'has a token_version' do
|
21
|
-
expect(token).to respond_to :token_version
|
22
|
-
expect(token.token_version).to be_nil
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
describe 'valid?' do
|
27
|
-
it 'is invalid without subject' do
|
28
|
-
jwt = token.to_jwt
|
29
|
-
|
30
|
-
user.destroy
|
31
|
-
|
32
|
-
t = JWT::Auth::Token.from_token jwt
|
33
|
-
|
34
|
-
expect(t).not_to be_valid
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'is invalid without subject 2' do
|
38
|
-
t = JWT::Auth::Token.from_token token.to_jwt
|
39
|
-
|
40
|
-
user.destroy
|
41
|
-
|
42
|
-
expect(t).not_to be_valid
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'is invalid on token_version increment' do
|
46
|
-
t = JWT::Auth::Token.from_token token.to_jwt
|
47
|
-
|
48
|
-
expect(t).to be_valid
|
49
|
-
|
50
|
-
user.increment_token_version!
|
51
|
-
user.reload
|
52
|
-
|
53
|
-
expect(t).not_to be_valid
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'is invalid on past date' do
|
57
|
-
token.issued_at = (JWT::Auth.token_lifetime + 1.second).ago.to_i
|
58
|
-
|
59
|
-
t = JWT::Auth::Token.from_token token.to_jwt
|
60
|
-
|
61
|
-
expect(t).not_to be_valid
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'is invalid after expiry date' do
|
65
|
-
token.issued_at = JWT::Auth.token_lifetime.ago.to_i
|
66
|
-
sleep 2
|
67
|
-
|
68
|
-
t = JWT::Auth::Token.from_token token.to_jwt
|
69
|
-
|
70
|
-
expect(t).not_to be_valid
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'is invalid on future tokens' do
|
74
|
-
token.issued_at = 1.year.from_now.to_i
|
75
|
-
|
76
|
-
t = JWT::Auth::Token.from_token token.to_jwt
|
77
|
-
|
78
|
-
expect(t).not_to be_valid
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
describe 'renew!' do
|
83
|
-
it 'renews a token' do
|
84
|
-
old_jwt = token.to_jwt
|
85
|
-
old_token = JWT::Auth::Token.from_token old_jwt
|
86
|
-
|
87
|
-
expect(old_token).to be_valid
|
88
|
-
|
89
|
-
sleep 2
|
90
|
-
|
91
|
-
old_token.renew!
|
92
|
-
|
93
|
-
new_jwt = old_token.to_jwt
|
94
|
-
new_token = JWT::Auth::Token.from_token new_jwt
|
95
|
-
|
96
|
-
expect(new_token).to be_valid
|
97
|
-
expect(new_jwt).not_to eq old_jwt
|
98
|
-
expect(new_token.issued_at).not_to eq old_token.issued_at
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe 'from token' do
|
103
|
-
let(:issued_at) { 1.second.ago.to_i }
|
104
|
-
|
105
|
-
let(:jwt) do
|
106
|
-
payload = {
|
107
|
-
:iat => issued_at,
|
108
|
-
:sub => user.id,
|
109
|
-
:ver => user.token_version
|
110
|
-
}
|
111
|
-
JWT.encode payload, JWT::Auth.secret
|
112
|
-
end
|
113
|
-
|
114
|
-
let(:token) { JWT::Auth::Token.from_token jwt }
|
115
|
-
|
116
|
-
it 'matches issued at' do
|
117
|
-
expect(token.issued_at).to eq issued_at
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'matches subject' do
|
121
|
-
expect(token.subject.id).to eq user.id
|
122
|
-
expect(token.subject.token_version).to eq user.token_version
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|