devise-jwt 0.6.0 → 0.7.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 +2 -3
- data/CHANGELOG.md +5 -1
- data/README.md +31 -22
- data/devise-jwt.gemspec +1 -1
- data/lib/devise/jwt/revocation_strategies.rb +2 -2
- data/lib/devise/jwt/revocation_strategies/{whitelist.rb → allowlist.rb} +8 -8
- data/lib/devise/jwt/revocation_strategies/{blacklist.rb → denylist.rb} +1 -1
- data/lib/devise/jwt/test_helpers.rb +1 -1
- data/lib/devise/jwt/version.rb +1 -1
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a4a4e4cfd349ee9e76533374b8269516152d37ddafd77b04c55a1d6d53b49c7
|
4
|
+
data.tar.gz: 06d6b7627bbbf01ce30796856236e8e854ad083bf35bfabb3972b1744f6fe8b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2574faee8bb3ca9f7481335360534104e04308772c6c0a1e19b4d2c0fafeb3d075d2faae2e23f98aee7115d9e64f9105b1020cc30825d8e40a633da1404b29de
|
7
|
+
data.tar.gz: bba859af422238968a66f01e13771db8efedf3626c5e21d67f16c192467c4ed1213d9844a6a0e615754c657155ea8b8dc972ac7bacd4c6b7984ce6c70e7c9f4b
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
6
|
|
7
|
-
## [0.
|
7
|
+
## [0.7.0] - 2020-06-03
|
8
|
+
### Fixed
|
9
|
+
- Replace whitelist/blacklist terminology with allowlist/denylist
|
10
|
+
|
11
|
+
## [0.6.0] - 2019-08-01
|
8
12
|
### Fixed
|
9
13
|
- Update warden-jwt_auth dependency to v0.4.0 so that now it is possible to configure algorithm.
|
10
14
|
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@ You can read about which security concerns this library takes into account and a
|
|
26
26
|
Add this line to your application's Gemfile:
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
gem 'devise-jwt', '~> 0.
|
29
|
+
gem 'devise-jwt', '~> 0.6.0'
|
30
30
|
```
|
31
31
|
|
32
32
|
And then execute:
|
@@ -76,7 +76,7 @@ An example configuration:
|
|
76
76
|
```ruby
|
77
77
|
class User < ApplicationRecord
|
78
78
|
devise :database_authenticatable,
|
79
|
-
:jwt_authenticatable, jwt_revocation_strategy:
|
79
|
+
:jwt_authenticatable, jwt_revocation_strategy: Denylist
|
80
80
|
end
|
81
81
|
```
|
82
82
|
|
@@ -132,7 +132,7 @@ This is so because of the following default devise workflow:
|
|
132
132
|
in the session without even reaching to any strategy (`:jwt_authenticatable`
|
133
133
|
in our case).
|
134
134
|
|
135
|
-
So, if you want to avoid this caveat you have
|
135
|
+
So, if you want to avoid this caveat you have three options:
|
136
136
|
|
137
137
|
- Disable the session. If you are developing an API, probably you don't need
|
138
138
|
it. In order to disable it, change `config/initializers/session_store.rb` to:
|
@@ -146,6 +146,15 @@ So, if you want to avoid this caveat you have two options:
|
|
146
146
|
```ruby
|
147
147
|
config.skip_session_storage = [:http_auth, :params_auth]
|
148
148
|
```
|
149
|
+
- If you are using Devise for another model (e.g. `AdminUser`) and doesn't want
|
150
|
+
to disable session storage for devise entirely, you can disable it on a
|
151
|
+
per-model basis:
|
152
|
+
```ruby
|
153
|
+
class User < ApplicationRecord
|
154
|
+
devise :database_authenticatable #, your other enabled modules...
|
155
|
+
self.skip_session_storage = [:http_auth, :params_auth]
|
156
|
+
end
|
157
|
+
```
|
149
158
|
|
150
159
|
### Revocation strategies
|
151
160
|
|
@@ -157,7 +166,7 @@ Here, the model class acts itself as the revocation strategy. It needs a new str
|
|
157
166
|
|
158
167
|
It works like the following:
|
159
168
|
|
160
|
-
-
|
169
|
+
- When a token is dispatched for a user, the `jti` claim is taken from the `jti` column in the model (which has been initialized when the record has been created).
|
161
170
|
- At every authenticated action, the incoming token `jti` claim is matched against the `jti` column for that user. The authentication only succeeds if they are the same.
|
162
171
|
- When the user requests to sign out its `jti` column changes, so that provided token won't be valid anymore.
|
163
172
|
|
@@ -196,29 +205,29 @@ def jwt_payload
|
|
196
205
|
end
|
197
206
|
```
|
198
207
|
|
199
|
-
####
|
208
|
+
#### Denylist
|
200
209
|
|
201
|
-
In this strategy, a database table is used as a
|
210
|
+
In this strategy, a database table is used as a list of revoked JWT tokens. The `jti` claim, which uniquely identifies a token, is persisted. The `exp` claim is also stored to allow the clean-up of staled tokens.
|
202
211
|
|
203
|
-
In order to use it, you need to create the
|
212
|
+
In order to use it, you need to create the denylist table in a migration:
|
204
213
|
|
205
214
|
```ruby
|
206
215
|
def change
|
207
|
-
create_table :
|
216
|
+
create_table :jwt_denylist do |t|
|
208
217
|
t.string :jti, null: false
|
209
218
|
t.datetime :exp, null: false
|
210
219
|
end
|
211
|
-
add_index :
|
220
|
+
add_index :jwt_denylist, :jti
|
212
221
|
end
|
213
222
|
```
|
214
223
|
For performance reasons, it is better if the `jti` column is an index.
|
215
224
|
|
216
|
-
Note: if you used the
|
225
|
+
Note: if you used the denylist strategy before vesion 0.4.0 you may not have the field *exp.* If not, run the following migration:
|
217
226
|
|
218
227
|
```ruby
|
219
|
-
class
|
228
|
+
class AddExpirationTimeToJWTDenylist < ActiveRecord::Migration
|
220
229
|
def change
|
221
|
-
add_column :
|
230
|
+
add_column :jwt_denylist, :exp, :datetime, null: false
|
222
231
|
end
|
223
232
|
end
|
224
233
|
|
@@ -227,10 +236,10 @@ end
|
|
227
236
|
Then, you need to create the corresponding model and include the strategy:
|
228
237
|
|
229
238
|
```ruby
|
230
|
-
class
|
231
|
-
include Devise::JWT::RevocationStrategies::
|
239
|
+
class JwtDenylist < ApplicationRecord
|
240
|
+
include Devise::JWT::RevocationStrategies::Denylist
|
232
241
|
|
233
|
-
self.table_name = '
|
242
|
+
self.table_name = 'jwt_denylist'
|
234
243
|
end
|
235
244
|
```
|
236
245
|
|
@@ -239,11 +248,11 @@ Last, configure the user model to use it:
|
|
239
248
|
```ruby
|
240
249
|
class User < ApplicationRecord
|
241
250
|
devise :database_authenticatable,
|
242
|
-
:jwt_authenticatable, jwt_revocation_strategy:
|
251
|
+
:jwt_authenticatable, jwt_revocation_strategy: JwtDenylist
|
243
252
|
end
|
244
253
|
```
|
245
254
|
|
246
|
-
####
|
255
|
+
#### Allowlist
|
247
256
|
|
248
257
|
Here, the model itself acts also as a revocation strategy, but it needs to have
|
249
258
|
a one-to-many association with another table which stores the tokens (in fact
|
@@ -266,11 +275,11 @@ devices for the same user.
|
|
266
275
|
The `exp` claim is also stored to allow the clean-up of staled tokens.
|
267
276
|
|
268
277
|
In order to use it, you have to create yourself the associated table and model.
|
269
|
-
The association table must be called `
|
278
|
+
The association table must be called `allowlisted_jwts`:
|
270
279
|
|
271
280
|
```ruby
|
272
281
|
def change
|
273
|
-
create_table :
|
282
|
+
create_table :allowlisted_jwts do |t|
|
274
283
|
t.string :jti, null: false
|
275
284
|
t.string :aud
|
276
285
|
# If you want to leverage the `aud` claim, add to it a `NOT NULL` constraint:
|
@@ -279,7 +288,7 @@ def change
|
|
279
288
|
t.references :your_user_table, foreign_key: { on_delete: :cascade }, null: false
|
280
289
|
end
|
281
290
|
|
282
|
-
add_index :
|
291
|
+
add_index :allowlisted_jwts, :jti, unique: true
|
283
292
|
end
|
284
293
|
```
|
285
294
|
Important: You are encouraged to set a unique index in the jti column. This way we can be sure at the database level that there aren't two valid tokens with same jti at the same time. Definining `foreign_key: { on_delete: :cascade }, null: false` on `t.references :your_user_table` helps to keep referential integrity of your database.
|
@@ -287,7 +296,7 @@ Important: You are encouraged to set a unique index in the jti column. This way
|
|
287
296
|
And then, the model:
|
288
297
|
|
289
298
|
```ruby
|
290
|
-
class
|
299
|
+
class AllowlistedJwt < ApplicationRecord
|
291
300
|
end
|
292
301
|
```
|
293
302
|
|
@@ -295,7 +304,7 @@ Finally, include the strategy in the model and configure it:
|
|
295
304
|
|
296
305
|
```ruby
|
297
306
|
class User < ApplicationRecord
|
298
|
-
include Devise::JWT::RevocationStrategies::
|
307
|
+
include Devise::JWT::RevocationStrategies::Allowlist
|
299
308
|
|
300
309
|
devise :database_authenticatable,
|
301
310
|
:jwt_authenticatable, jwt_revocation_strategy: self
|
data/devise-jwt.gemspec
CHANGED
@@ -33,6 +33,6 @@ Gem::Specification.new do |spec|
|
|
33
33
|
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
34
34
|
spec.add_development_dependency 'rspec-rails', '~> 3.5'
|
35
35
|
# Test reporting
|
36
|
-
spec.add_development_dependency 'simplecov', '
|
36
|
+
spec.add_development_dependency 'simplecov', '0.17'
|
37
37
|
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
|
38
38
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'devise/jwt/revocation_strategies/jti_matcher'
|
4
|
-
require 'devise/jwt/revocation_strategies/
|
5
|
-
require 'devise/jwt/revocation_strategies/
|
4
|
+
require 'devise/jwt/revocation_strategies/denylist'
|
5
|
+
require 'devise/jwt/revocation_strategies/allowlist'
|
6
6
|
require 'devise/jwt/revocation_strategies/null'
|
7
7
|
|
8
8
|
module Devise
|
@@ -7,32 +7,32 @@ module Devise
|
|
7
7
|
module RevocationStrategies
|
8
8
|
# This strategy must be included in the user model.
|
9
9
|
#
|
10
|
-
# The
|
10
|
+
# The JwtAllowlist table must include `jti`, `aud`, `exp` and `user_id`
|
11
11
|
# columns
|
12
12
|
#
|
13
13
|
# In order to tell whether a token is revoked, it just tries to find the
|
14
|
-
# `jti` and `aud` values from the token on the `
|
14
|
+
# `jti` and `aud` values from the token on the `allowlisted_jwts`
|
15
15
|
# table for the respective user.
|
16
16
|
#
|
17
17
|
# If the values don't exist means the token was revoked.
|
18
18
|
# On revocation, it deletes the matching record from the
|
19
|
-
# `
|
19
|
+
# `allowlisted_jwts` table.
|
20
20
|
#
|
21
21
|
# On sign in, it creates a new record with the `jti` and `aud` values.
|
22
|
-
module
|
22
|
+
module Allowlist
|
23
23
|
extend ActiveSupport::Concern
|
24
24
|
|
25
25
|
included do
|
26
|
-
has_many :
|
26
|
+
has_many :allowlisted_jwts, dependent: :destroy
|
27
27
|
|
28
28
|
# @see Warden::JWTAuth::Interfaces::RevocationStrategy#jwt_revoked?
|
29
29
|
def self.jwt_revoked?(payload, user)
|
30
|
-
!user.
|
30
|
+
!user.allowlisted_jwts.exists?(payload.slice('jti', 'aud'))
|
31
31
|
end
|
32
32
|
|
33
33
|
# @see Warden::JWTAuth::Interfaces::RevocationStrategy#revoke_jwt
|
34
34
|
def self.revoke_jwt(payload, user)
|
35
|
-
jwt = user.
|
35
|
+
jwt = user.allowlisted_jwts.find_by(payload.slice('jti', 'aud'))
|
36
36
|
jwt.destroy! if jwt
|
37
37
|
end
|
38
38
|
end
|
@@ -40,7 +40,7 @@ module Devise
|
|
40
40
|
# Warden::JWTAuth::Interfaces::User#on_jwt_dispatch
|
41
41
|
# :reek:FeatureEnvy
|
42
42
|
def on_jwt_dispatch(_token, payload)
|
43
|
-
|
43
|
+
allowlisted_jwts.create!(
|
44
44
|
jti: payload['jti'],
|
45
45
|
aud: payload['aud'],
|
46
46
|
exp: Time.at(payload['exp'].to_i)
|
@@ -9,7 +9,7 @@ module Devise
|
|
9
9
|
#
|
10
10
|
# Side effects could happen if you have implemented
|
11
11
|
# `on_jwt_dispatch` method on the user model (as it happens in
|
12
|
-
# the
|
12
|
+
# the allowlist revocation strategy).
|
13
13
|
#
|
14
14
|
# Be aware that a fresh copy of `headers` is returned with the new
|
15
15
|
# key/value pair added, instead of modifying given argument.
|
data/lib/devise/jwt/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devise-jwt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marc Busqué
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: devise
|
@@ -140,16 +140,16 @@ dependencies:
|
|
140
140
|
name: simplecov
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
|
-
- -
|
143
|
+
- - '='
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: '0.
|
145
|
+
version: '0.17'
|
146
146
|
type: :development
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
|
-
- -
|
150
|
+
- - '='
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: '0.
|
152
|
+
version: '0.17'
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: codeclimate-test-reporter
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -198,10 +198,10 @@ files:
|
|
198
198
|
- lib/devise/jwt/models/jwt_authenticatable.rb
|
199
199
|
- lib/devise/jwt/railtie.rb
|
200
200
|
- lib/devise/jwt/revocation_strategies.rb
|
201
|
-
- lib/devise/jwt/revocation_strategies/
|
201
|
+
- lib/devise/jwt/revocation_strategies/allowlist.rb
|
202
|
+
- lib/devise/jwt/revocation_strategies/denylist.rb
|
202
203
|
- lib/devise/jwt/revocation_strategies/jti_matcher.rb
|
203
204
|
- lib/devise/jwt/revocation_strategies/null.rb
|
204
|
-
- lib/devise/jwt/revocation_strategies/whitelist.rb
|
205
205
|
- lib/devise/jwt/test_helpers.rb
|
206
206
|
- lib/devise/jwt/version.rb
|
207
207
|
homepage: https://github.com/waiting-for-dev/devise-jwt
|
@@ -223,8 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
223
223
|
- !ruby/object:Gem::Version
|
224
224
|
version: '0'
|
225
225
|
requirements: []
|
226
|
-
|
227
|
-
rubygems_version: 2.7.8
|
226
|
+
rubygems_version: 3.1.2
|
228
227
|
signing_key:
|
229
228
|
specification_version: 4
|
230
229
|
summary: JWT authentication for devise
|