jwt 1.5.0 → 2.5.0
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.
- checksums.yaml +5 -5
- data/.codeclimate.yml +8 -0
- data/.github/workflows/coverage.yml +27 -0
- data/.github/workflows/test.yml +67 -0
- data/.gitignore +13 -0
- data/.reek.yml +22 -0
- data/.rspec +2 -0
- data/.rubocop.yml +67 -0
- data/.sourcelevel.yml +17 -0
- data/AUTHORS +119 -0
- data/Appraisals +13 -0
- data/CHANGELOG.md +786 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/CONTRIBUTING.md +99 -0
- data/Gemfile +7 -0
- data/LICENSE +7 -0
- data/README.md +639 -0
- data/Rakefile +13 -14
- data/lib/jwt/algos/ecdsa.rb +64 -0
- data/lib/jwt/algos/eddsa.rb +35 -0
- data/lib/jwt/algos/hmac.rb +36 -0
- data/lib/jwt/algos/none.rb +17 -0
- data/lib/jwt/algos/ps.rb +43 -0
- data/lib/jwt/algos/rsa.rb +22 -0
- data/lib/jwt/algos/unsupported.rb +19 -0
- data/lib/jwt/algos.rb +44 -0
- data/lib/jwt/base64.rb +19 -0
- data/lib/jwt/claims_validator.rb +37 -0
- data/lib/jwt/configuration/container.rb +21 -0
- data/lib/jwt/configuration/decode_configuration.rb +46 -0
- data/lib/jwt/configuration/jwk_configuration.rb +27 -0
- data/lib/jwt/configuration.rb +15 -0
- data/lib/jwt/decode.rb +145 -0
- data/lib/jwt/encode.rb +69 -0
- data/lib/jwt/error.rb +22 -0
- data/lib/jwt/json.rb +10 -22
- data/lib/jwt/jwk/ec.rb +199 -0
- data/lib/jwt/jwk/hmac.rb +67 -0
- data/lib/jwt/jwk/key_base.rb +35 -0
- data/lib/jwt/jwk/key_finder.rb +62 -0
- data/lib/jwt/jwk/kid_as_key_digest.rb +15 -0
- data/lib/jwt/jwk/rsa.rb +138 -0
- data/lib/jwt/jwk/thumbprint.rb +26 -0
- data/lib/jwt/jwk.rb +52 -0
- data/lib/jwt/security_utils.rb +59 -0
- data/lib/jwt/signature.rb +35 -0
- data/lib/jwt/verify.rb +113 -0
- data/lib/jwt/version.rb +28 -0
- data/lib/jwt/x5c_key_finder.rb +55 -0
- data/lib/jwt.rb +20 -215
- data/ruby-jwt.gemspec +35 -0
- metadata +138 -30
- data/Manifest +0 -6
- data/jwt.gemspec +0 -34
- data/spec/helper.rb +0 -2
- data/spec/jwt_spec.rb +0 -434
data/README.md
ADDED
@@ -0,0 +1,639 @@
|
|
1
|
+
# JWT
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/jwt)
|
4
|
+
[](https://github.com/jwt/ruby-jwt/actions)
|
5
|
+
[](https://codeclimate.com/github/jwt/ruby-jwt)
|
6
|
+
[](https://codeclimate.com/github/jwt/ruby-jwt/coverage)
|
7
|
+
[](https://codeclimate.com/github/jwt/ruby-jwt)
|
8
|
+
[](https://app.sourcelevel.io/github/jwt/-/ruby-jwt)
|
9
|
+
|
10
|
+
A ruby implementation of the [RFC 7519 OAuth JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) standard.
|
11
|
+
|
12
|
+
If you have further questions related to development or usage, join us: [ruby-jwt google group](https://groups.google.com/forum/#!forum/ruby-jwt).
|
13
|
+
|
14
|
+
## Announcements
|
15
|
+
* Ruby 2.4 support was dropped in version 2.4.0
|
16
|
+
* Ruby 1.9.3 support was dropped at December 31st, 2016.
|
17
|
+
* Version 1.5.3 yanked. See: [#132](https://github.com/jwt/ruby-jwt/issues/132) and [#133](https://github.com/jwt/ruby-jwt/issues/133)
|
18
|
+
|
19
|
+
See [CHANGELOG.md](CHANGELOG.md) for a complete set of changes.
|
20
|
+
|
21
|
+
## Sponsors
|
22
|
+
|
23
|
+
|Logo|Message|
|
24
|
+
|-|-|
|
25
|
+
||If you want to quickly add secure token-based authentication to Ruby projects, feel free to check Auth0's Ruby SDK and free plan at [auth0.com/developers](https://auth0.com/developers?utm_source=GHsponsor&utm_medium=GHsponsor&utm_campaign=rubyjwt&utm_content=auth)|
|
26
|
+
|
27
|
+
## Installing
|
28
|
+
|
29
|
+
### Using Rubygems:
|
30
|
+
```bash
|
31
|
+
gem install jwt
|
32
|
+
```
|
33
|
+
|
34
|
+
### Using Bundler:
|
35
|
+
Add the following to your Gemfile
|
36
|
+
```
|
37
|
+
gem 'jwt'
|
38
|
+
```
|
39
|
+
And run `bundle install`
|
40
|
+
|
41
|
+
## Algorithms and Usage
|
42
|
+
|
43
|
+
The JWT spec supports NONE, HMAC, RSASSA, ECDSA and RSASSA-PSS algorithms for cryptographic signing. Currently the jwt gem supports NONE, HMAC, RSASSA and ECDSA. If you are using cryptographic signing, you need to specify the algorithm in the options hash whenever you call JWT.decode to ensure that an attacker [cannot bypass the algorithm verification step](https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/). **It is strongly recommended that you hard code the algorithm, as you may leave yourself vulnerable by dynamically picking the algorithm**
|
44
|
+
|
45
|
+
See: [ JSON Web Algorithms (JWA) 3.1. "alg" (Algorithm) Header Parameter Values for JWS](https://tools.ietf.org/html/rfc7518#section-3.1)
|
46
|
+
|
47
|
+
### **NONE**
|
48
|
+
|
49
|
+
* none - unsigned token
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'jwt'
|
53
|
+
|
54
|
+
payload = { data: 'test' }
|
55
|
+
|
56
|
+
# IMPORTANT: set nil as password parameter
|
57
|
+
token = JWT.encode payload, nil, 'none'
|
58
|
+
|
59
|
+
# eyJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.
|
60
|
+
puts token
|
61
|
+
|
62
|
+
# Set password to nil and validation to false otherwise this won't work
|
63
|
+
decoded_token = JWT.decode token, nil, false
|
64
|
+
|
65
|
+
# Array
|
66
|
+
# [
|
67
|
+
# {"data"=>"test"}, # payload
|
68
|
+
# {"alg"=>"none"} # header
|
69
|
+
# ]
|
70
|
+
puts decoded_token
|
71
|
+
```
|
72
|
+
|
73
|
+
### **HMAC**
|
74
|
+
|
75
|
+
* HS256 - HMAC using SHA-256 hash algorithm
|
76
|
+
* HS512256 - HMAC using SHA-512-256 hash algorithm (only available with RbNaCl; see note below)
|
77
|
+
* HS384 - HMAC using SHA-384 hash algorithm
|
78
|
+
* HS512 - HMAC using SHA-512 hash algorithm
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# The secret must be a string. A JWT::DecodeError will be raised if it isn't provided.
|
82
|
+
hmac_secret = 'my$ecretK3y'
|
83
|
+
|
84
|
+
token = JWT.encode payload, hmac_secret, 'HS256'
|
85
|
+
|
86
|
+
# eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.pNIWIL34Jo13LViZAJACzK6Yf0qnvT_BuwOxiMCPE-Y
|
87
|
+
puts token
|
88
|
+
|
89
|
+
decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
|
90
|
+
|
91
|
+
# Array
|
92
|
+
# [
|
93
|
+
# {"data"=>"test"}, # payload
|
94
|
+
# {"alg"=>"HS256"} # header
|
95
|
+
# ]
|
96
|
+
puts decoded_token
|
97
|
+
```
|
98
|
+
|
99
|
+
Note: If [RbNaCl](https://github.com/cryptosphere/rbnacl) is loadable, ruby-jwt will use it for HMAC-SHA256, HMAC-SHA512-256, and HMAC-SHA512. RbNaCl enforces a maximum key size of 32 bytes for these algorithms.
|
100
|
+
|
101
|
+
[RbNaCl](https://github.com/cryptosphere/rbnacl) requires
|
102
|
+
[libsodium](https://github.com/jedisct1/libsodium), it can be installed
|
103
|
+
on MacOS with `brew install libsodium`.
|
104
|
+
|
105
|
+
### **RSA**
|
106
|
+
|
107
|
+
* RS256 - RSA using SHA-256 hash algorithm
|
108
|
+
* RS384 - RSA using SHA-384 hash algorithm
|
109
|
+
* RS512 - RSA using SHA-512 hash algorithm
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
rsa_private = OpenSSL::PKey::RSA.generate 2048
|
113
|
+
rsa_public = rsa_private.public_key
|
114
|
+
|
115
|
+
token = JWT.encode payload, rsa_private, 'RS256'
|
116
|
+
|
117
|
+
# eyJhbGciOiJSUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.GplO4w1spRgvEJQ3-FOtZr-uC8L45Jt7SN0J4woBnEXG_OZBSNcZjAJWpjadVYEe2ev3oUBFDYM1N_-0BTVeFGGYvMewu8E6aMjSZvOpf1cZBew-Vt4poSq7goG2YRI_zNPt3af2lkPqXD796IKC5URrEvcgF5xFQ-6h07XRDpSRx1ECrNsUOt7UM3l1IB4doY11GzwQA5sHDTmUZ0-kBT76ZMf12Srg_N3hZwphxBtudYtN5VGZn420sVrQMdPE_7Ni3EiWT88j7WCr1xrF60l8sZT3yKCVleG7D2BEXacTntB7GktBv4Xo8OKnpwpqTpIlC05dMowMkz3rEAAYbQ
|
118
|
+
puts token
|
119
|
+
|
120
|
+
decoded_token = JWT.decode token, rsa_public, true, { algorithm: 'RS256' }
|
121
|
+
|
122
|
+
# Array
|
123
|
+
# [
|
124
|
+
# {"data"=>"test"}, # payload
|
125
|
+
# {"alg"=>"RS256"} # header
|
126
|
+
# ]
|
127
|
+
puts decoded_token
|
128
|
+
```
|
129
|
+
|
130
|
+
### **ECDSA**
|
131
|
+
|
132
|
+
* ES256 - ECDSA using P-256 and SHA-256
|
133
|
+
* ES384 - ECDSA using P-384 and SHA-384
|
134
|
+
* ES512 - ECDSA using P-521 and SHA-512
|
135
|
+
* ES256K - ECDSA using P-256K and SHA-256
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
ecdsa_key = OpenSSL::PKey::EC.generate('prime256v1')
|
139
|
+
|
140
|
+
token = JWT.encode payload, ecdsa_key, 'ES256'
|
141
|
+
|
142
|
+
# eyJhbGciOiJFUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.AlLW--kaF7EX1NMX9WJRuIW8NeRJbn2BLXHns7Q5TZr7Hy3lF6MOpMlp7GoxBFRLISQ6KrD0CJOrR8aogEsPeg
|
143
|
+
puts token
|
144
|
+
|
145
|
+
decoded_token = JWT.decode token, ecdsa_key, true, { algorithm: 'ES256' }
|
146
|
+
|
147
|
+
# Array
|
148
|
+
# [
|
149
|
+
# {"test"=>"data"}, # payload
|
150
|
+
# {"alg"=>"ES256"} # header
|
151
|
+
# ]
|
152
|
+
puts decoded_token
|
153
|
+
```
|
154
|
+
|
155
|
+
### **EDDSA**
|
156
|
+
|
157
|
+
In order to use this algorithm you need to add the `RbNaCl` gem to you `Gemfile`.
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
gem 'rbnacl'
|
161
|
+
```
|
162
|
+
|
163
|
+
For more detailed installation instruction check the official [repository](https://github.com/cryptosphere/rbnacl) on GitHub.
|
164
|
+
|
165
|
+
* ED25519
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF')
|
169
|
+
public_key = private_key.verify_key
|
170
|
+
token = JWT.encode payload, private_key, 'ED25519'
|
171
|
+
|
172
|
+
# eyJhbGciOiJFRDI1NTE5In0.eyJkYXRhIjoidGVzdCJ9.6xIztXyOupskddGA_RvKU76V9b2dCQUYhoZEVFnRimJoPYIzZ2Fm47CWw8k2NTCNpgfAuxg9OXjaiVK7MvrbCQ
|
173
|
+
puts token
|
174
|
+
|
175
|
+
decoded_token = JWT.decode token, public_key, true, { algorithm: 'ED25519' }
|
176
|
+
# Array
|
177
|
+
# [
|
178
|
+
# {"test"=>"data"}, # payload
|
179
|
+
# {"alg"=>"ED25519"} # header
|
180
|
+
# ]
|
181
|
+
|
182
|
+
```
|
183
|
+
|
184
|
+
### **RSASSA-PSS**
|
185
|
+
|
186
|
+
In order to use this algorithm you need to add the `openssl` gem to your `Gemfile` with a version greater or equal to `2.1`.
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
gem 'openssl', '~> 2.1'
|
190
|
+
```
|
191
|
+
|
192
|
+
* PS256 - RSASSA-PSS using SHA-256 hash algorithm
|
193
|
+
* PS384 - RSASSA-PSS using SHA-384 hash algorithm
|
194
|
+
* PS512 - RSASSA-PSS using SHA-512 hash algorithm
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
rsa_private = OpenSSL::PKey::RSA.generate 2048
|
198
|
+
rsa_public = rsa_private.public_key
|
199
|
+
|
200
|
+
token = JWT.encode payload, rsa_private, 'PS256'
|
201
|
+
|
202
|
+
# eyJhbGciOiJQUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.KEmqagMUHM-NcmXo6818ZazVTIAkn9qU9KQFT1c5Iq91n0KRpAI84jj4ZCdkysDlWokFs3Dmn4MhcXP03oJKLFgnoPL40_Wgg9iFr0jnIVvnMUp1kp2RFUbL0jqExGTRA3LdAhuvw6ZByGD1bkcWjDXygjQw-hxILrT1bENjdr0JhFd-cB0-ps5SB0mwhFNcUw-OM3Uu30B1-mlFaelUY8jHJYKwLTZPNxHzndt8RGXF8iZLp7dGb06HSCKMcVzhASGMH4ZdFystRe2hh31cwcvnl-Eo_D4cdwmpN3Abhk_8rkxawQJR3duh8HNKc4AyFPo7SabEaSu2gLnLfN3yfg
|
203
|
+
puts token
|
204
|
+
|
205
|
+
decoded_token = JWT.decode token, rsa_public, true, { algorithm: 'PS256' }
|
206
|
+
|
207
|
+
# Array
|
208
|
+
# [
|
209
|
+
# {"data"=>"test"}, # payload
|
210
|
+
# {"alg"=>"PS256"} # header
|
211
|
+
# ]
|
212
|
+
puts decoded_token
|
213
|
+
```
|
214
|
+
|
215
|
+
## Support for reserved claim names
|
216
|
+
JSON Web Token defines some reserved claim names and defines how they should be
|
217
|
+
used. JWT supports these reserved claim names:
|
218
|
+
|
219
|
+
- 'exp' (Expiration Time) Claim
|
220
|
+
- 'nbf' (Not Before Time) Claim
|
221
|
+
- 'iss' (Issuer) Claim
|
222
|
+
- 'aud' (Audience) Claim
|
223
|
+
- 'jti' (JWT ID) Claim
|
224
|
+
- 'iat' (Issued At) Claim
|
225
|
+
- 'sub' (Subject) Claim
|
226
|
+
|
227
|
+
## Add custom header fields
|
228
|
+
Ruby-jwt gem supports custom [header fields](https://tools.ietf.org/html/rfc7519#section-5)
|
229
|
+
To add custom header fields you need to pass `header_fields` parameter
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
token = JWT.encode payload, key, algorithm='HS256', header_fields={}
|
233
|
+
```
|
234
|
+
|
235
|
+
**Example:**
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
require 'jwt'
|
239
|
+
|
240
|
+
payload = { data: 'test' }
|
241
|
+
|
242
|
+
# IMPORTANT: set nil as password parameter
|
243
|
+
token = JWT.encode payload, nil, 'none', { typ: 'JWT' }
|
244
|
+
|
245
|
+
# eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJkYXRhIjoidGVzdCJ9.
|
246
|
+
puts token
|
247
|
+
|
248
|
+
# Set password to nil and validation to false otherwise this won't work
|
249
|
+
decoded_token = JWT.decode token, nil, false
|
250
|
+
|
251
|
+
# Array
|
252
|
+
# [
|
253
|
+
# {"data"=>"test"}, # payload
|
254
|
+
# {"typ"=>"JWT", "alg"=>"none"} # header
|
255
|
+
# ]
|
256
|
+
puts decoded_token
|
257
|
+
```
|
258
|
+
|
259
|
+
### Expiration Time Claim
|
260
|
+
|
261
|
+
From [Oauth JSON Web Token 4.1.4. "exp" (Expiration Time) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.4):
|
262
|
+
|
263
|
+
> The `exp` (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of the `exp` claim requires that the current date/time MUST be before the expiration date/time listed in the `exp` claim. Implementers MAY provide for some small `leeway`, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
|
264
|
+
|
265
|
+
**Handle Expiration Claim**
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
exp = Time.now.to_i + 4 * 3600
|
269
|
+
exp_payload = { data: 'data', exp: exp }
|
270
|
+
|
271
|
+
token = JWT.encode exp_payload, hmac_secret, 'HS256'
|
272
|
+
|
273
|
+
begin
|
274
|
+
decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
|
275
|
+
rescue JWT::ExpiredSignature
|
276
|
+
# Handle expired token, e.g. logout user or deny access
|
277
|
+
end
|
278
|
+
```
|
279
|
+
|
280
|
+
The Expiration Claim verification can be disabled.
|
281
|
+
```ruby
|
282
|
+
# Decode token without raising JWT::ExpiredSignature error
|
283
|
+
JWT.decode token, hmac_secret, true, { verify_expiration: false, algorithm: 'HS256' }
|
284
|
+
```
|
285
|
+
|
286
|
+
**Adding Leeway**
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
exp = Time.now.to_i - 10
|
290
|
+
leeway = 30 # seconds
|
291
|
+
|
292
|
+
exp_payload = { data: 'data', exp: exp }
|
293
|
+
|
294
|
+
# build expired token
|
295
|
+
token = JWT.encode exp_payload, hmac_secret, 'HS256'
|
296
|
+
|
297
|
+
begin
|
298
|
+
# add leeway to ensure the token is still accepted
|
299
|
+
decoded_token = JWT.decode token, hmac_secret, true, { exp_leeway: leeway, algorithm: 'HS256' }
|
300
|
+
rescue JWT::ExpiredSignature
|
301
|
+
# Handle expired token, e.g. logout user or deny access
|
302
|
+
end
|
303
|
+
```
|
304
|
+
|
305
|
+
### Not Before Time Claim
|
306
|
+
|
307
|
+
From [Oauth JSON Web Token 4.1.5. "nbf" (Not Before) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.5):
|
308
|
+
|
309
|
+
> The `nbf` (not before) claim identifies the time before which the JWT MUST NOT be accepted for processing. The processing of the `nbf` claim requires that the current date/time MUST be after or equal to the not-before date/time listed in the `nbf` claim. Implementers MAY provide for some small `leeway`, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
|
310
|
+
|
311
|
+
**Handle Not Before Claim**
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
nbf = Time.now.to_i - 3600
|
315
|
+
nbf_payload = { data: 'data', nbf: nbf }
|
316
|
+
|
317
|
+
token = JWT.encode nbf_payload, hmac_secret, 'HS256'
|
318
|
+
|
319
|
+
begin
|
320
|
+
decoded_token = JWT.decode token, hmac_secret, true, { algorithm: 'HS256' }
|
321
|
+
rescue JWT::ImmatureSignature
|
322
|
+
# Handle invalid token, e.g. logout user or deny access
|
323
|
+
end
|
324
|
+
```
|
325
|
+
|
326
|
+
The Not Before Claim verification can be disabled.
|
327
|
+
```ruby
|
328
|
+
# Decode token without raising JWT::ImmatureSignature error
|
329
|
+
JWT.decode token, hmac_secret, true, { verify_not_before: false, algorithm: 'HS256' }
|
330
|
+
```
|
331
|
+
|
332
|
+
**Adding Leeway**
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
nbf = Time.now.to_i + 10
|
336
|
+
leeway = 30
|
337
|
+
|
338
|
+
nbf_payload = { data: 'data', nbf: nbf }
|
339
|
+
|
340
|
+
# build expired token
|
341
|
+
token = JWT.encode nbf_payload, hmac_secret, 'HS256'
|
342
|
+
|
343
|
+
begin
|
344
|
+
# add leeway to ensure the token is valid
|
345
|
+
decoded_token = JWT.decode token, hmac_secret, true, { nbf_leeway: leeway, algorithm: 'HS256' }
|
346
|
+
rescue JWT::ImmatureSignature
|
347
|
+
# Handle invalid token, e.g. logout user or deny access
|
348
|
+
end
|
349
|
+
```
|
350
|
+
|
351
|
+
### Issuer Claim
|
352
|
+
|
353
|
+
From [Oauth JSON Web Token 4.1.1. "iss" (Issuer) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.1):
|
354
|
+
|
355
|
+
> The `iss` (issuer) claim identifies the principal that issued the JWT. The processing of this claim is generally application specific. The `iss` value is a case-sensitive string containing a ***StringOrURI*** value. Use of this claim is OPTIONAL.
|
356
|
+
|
357
|
+
You can pass multiple allowed issuers as an Array, verification will pass if one of them matches the `iss` value in the payload.
|
358
|
+
|
359
|
+
```ruby
|
360
|
+
iss = 'My Awesome Company Inc. or https://my.awesome.website/'
|
361
|
+
iss_payload = { data: 'data', iss: iss }
|
362
|
+
|
363
|
+
token = JWT.encode iss_payload, hmac_secret, 'HS256'
|
364
|
+
|
365
|
+
begin
|
366
|
+
# Add iss to the validation to check if the token has been manipulated
|
367
|
+
decoded_token = JWT.decode token, hmac_secret, true, { iss: iss, verify_iss: true, algorithm: 'HS256' }
|
368
|
+
rescue JWT::InvalidIssuerError
|
369
|
+
# Handle invalid token, e.g. logout user or deny access
|
370
|
+
end
|
371
|
+
```
|
372
|
+
|
373
|
+
You can also pass a Regexp or Proc (with arity 1), verification will pass if the regexp matches or the proc returns truthy.
|
374
|
+
On supported ruby versions (>= 2.5) you can also delegate to methods, on older versions you will have
|
375
|
+
to convert them to proc (using `to_proc`)
|
376
|
+
|
377
|
+
```ruby
|
378
|
+
JWT.decode token, hmac_secret, true,
|
379
|
+
iss: %r'https://my.awesome.website/',
|
380
|
+
verify_iss: true,
|
381
|
+
algorithm: 'HS256'
|
382
|
+
```
|
383
|
+
|
384
|
+
```ruby
|
385
|
+
JWT.decode token, hmac_secret, true,
|
386
|
+
iss: ->(issuer) { issuer.start_with?('My Awesome Company Inc') },
|
387
|
+
verify_iss: true,
|
388
|
+
algorithm: 'HS256'
|
389
|
+
```
|
390
|
+
|
391
|
+
```ruby
|
392
|
+
JWT.decode token, hmac_secret, true,
|
393
|
+
iss: method(:valid_issuer?),
|
394
|
+
verify_iss: true,
|
395
|
+
algorithm: 'HS256'
|
396
|
+
|
397
|
+
# somewhere in the same class:
|
398
|
+
def valid_issuer?(issuer)
|
399
|
+
# custom validation
|
400
|
+
end
|
401
|
+
```
|
402
|
+
|
403
|
+
### Audience Claim
|
404
|
+
|
405
|
+
From [Oauth JSON Web Token 4.1.3. "aud" (Audience) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.3):
|
406
|
+
|
407
|
+
> The `aud` (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the `aud` claim when this claim is present, then the JWT MUST be rejected. In the general case, the `aud` value is an array of case-sensitive strings, each containing a ***StringOrURI*** value. In the special case when the JWT has one audience, the `aud` value MAY be a single case-sensitive string containing a ***StringOrURI*** value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.
|
408
|
+
|
409
|
+
```ruby
|
410
|
+
aud = ['Young', 'Old']
|
411
|
+
aud_payload = { data: 'data', aud: aud }
|
412
|
+
|
413
|
+
token = JWT.encode aud_payload, hmac_secret, 'HS256'
|
414
|
+
|
415
|
+
begin
|
416
|
+
# Add aud to the validation to check if the token has been manipulated
|
417
|
+
decoded_token = JWT.decode token, hmac_secret, true, { aud: aud, verify_aud: true, algorithm: 'HS256' }
|
418
|
+
rescue JWT::InvalidAudError
|
419
|
+
# Handle invalid token, e.g. logout user or deny access
|
420
|
+
puts 'Audience Error'
|
421
|
+
end
|
422
|
+
```
|
423
|
+
|
424
|
+
### JWT ID Claim
|
425
|
+
|
426
|
+
From [Oauth JSON Web Token 4.1.7. "jti" (JWT ID) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.7):
|
427
|
+
|
428
|
+
> The `jti` (JWT ID) claim provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner that ensures that there is a negligible probability that the same value will be accidentally assigned to a different data object; if the application uses multiple issuers, collisions MUST be prevented among values produced by different issuers as well. The `jti` claim can be used to prevent the JWT from being replayed. The `jti` value is a case-sensitive string. Use of this claim is OPTIONAL.
|
429
|
+
|
430
|
+
```ruby
|
431
|
+
# Use the secret and iat to create a unique key per request to prevent replay attacks
|
432
|
+
jti_raw = [hmac_secret, iat].join(':').to_s
|
433
|
+
jti = Digest::MD5.hexdigest(jti_raw)
|
434
|
+
jti_payload = { data: 'data', iat: iat, jti: jti }
|
435
|
+
|
436
|
+
token = JWT.encode jti_payload, hmac_secret, 'HS256'
|
437
|
+
|
438
|
+
begin
|
439
|
+
# If :verify_jti is true, validation will pass if a JTI is present
|
440
|
+
#decoded_token = JWT.decode token, hmac_secret, true, { verify_jti: true, algorithm: 'HS256' }
|
441
|
+
# Alternatively, pass a proc with your own code to check if the JTI has already been used
|
442
|
+
decoded_token = JWT.decode token, hmac_secret, true, { verify_jti: proc { |jti| my_validation_method(jti) }, algorithm: 'HS256' }
|
443
|
+
# or
|
444
|
+
decoded_token = JWT.decode token, hmac_secret, true, { verify_jti: proc { |jti, payload| my_validation_method(jti, payload) }, algorithm: 'HS256' }
|
445
|
+
rescue JWT::InvalidJtiError
|
446
|
+
# Handle invalid token, e.g. logout user or deny access
|
447
|
+
puts 'Error'
|
448
|
+
end
|
449
|
+
```
|
450
|
+
|
451
|
+
### Issued At Claim
|
452
|
+
|
453
|
+
From [Oauth JSON Web Token 4.1.6. "iat" (Issued At) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.6):
|
454
|
+
|
455
|
+
> The `iat` (issued at) claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT. The `leeway` option is not taken into account when verifying this claim. The `iat_leeway` option was removed in version 2.2.0. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
|
456
|
+
|
457
|
+
**Handle Issued At Claim**
|
458
|
+
|
459
|
+
```ruby
|
460
|
+
iat = Time.now.to_i
|
461
|
+
iat_payload = { data: 'data', iat: iat }
|
462
|
+
|
463
|
+
token = JWT.encode iat_payload, hmac_secret, 'HS256'
|
464
|
+
|
465
|
+
begin
|
466
|
+
# Add iat to the validation to check if the token has been manipulated
|
467
|
+
decoded_token = JWT.decode token, hmac_secret, true, { verify_iat: true, algorithm: 'HS256' }
|
468
|
+
rescue JWT::InvalidIatError
|
469
|
+
# Handle invalid token, e.g. logout user or deny access
|
470
|
+
end
|
471
|
+
```
|
472
|
+
|
473
|
+
### Subject Claim
|
474
|
+
|
475
|
+
From [Oauth JSON Web Token 4.1.2. "sub" (Subject) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.2):
|
476
|
+
|
477
|
+
> The `sub` (subject) claim identifies the principal that is the subject of the JWT. The Claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique. The processing of this claim is generally application specific. The sub value is a case-sensitive string containing a ***StringOrURI*** value. Use of this claim is OPTIONAL.
|
478
|
+
|
479
|
+
```ruby
|
480
|
+
sub = 'Subject'
|
481
|
+
sub_payload = { data: 'data', sub: sub }
|
482
|
+
|
483
|
+
token = JWT.encode sub_payload, hmac_secret, 'HS256'
|
484
|
+
|
485
|
+
begin
|
486
|
+
# Add sub to the validation to check if the token has been manipulated
|
487
|
+
decoded_token = JWT.decode token, hmac_secret, true, { sub: sub, verify_sub: true, algorithm: 'HS256' }
|
488
|
+
rescue JWT::InvalidSubError
|
489
|
+
# Handle invalid token, e.g. logout user or deny access
|
490
|
+
end
|
491
|
+
```
|
492
|
+
|
493
|
+
### Finding a Key
|
494
|
+
|
495
|
+
To dynamically find the key for verifying the JWT signature, pass a block to the decode block. The block receives headers and the original payload as parameters. It should return with the key to verify the signature that was used to sign the JWT.
|
496
|
+
|
497
|
+
```ruby
|
498
|
+
issuers = %w[My_Awesome_Company1 My_Awesome_Company2]
|
499
|
+
iss_payload = { data: 'data', iss: issuers.first }
|
500
|
+
|
501
|
+
secrets = { issuers.first => hmac_secret, issuers.last => 'hmac_secret2' }
|
502
|
+
|
503
|
+
token = JWT.encode iss_payload, hmac_secret, 'HS256'
|
504
|
+
|
505
|
+
begin
|
506
|
+
# Add iss to the validation to check if the token has been manipulated
|
507
|
+
decoded_token = JWT.decode(token, nil, true, { iss: issuers, verify_iss: true, algorithm: 'HS256' }) do |_headers, payload|
|
508
|
+
secrets[payload['iss']]
|
509
|
+
end
|
510
|
+
rescue JWT::InvalidIssuerError
|
511
|
+
# Handle invalid token, e.g. logout user or deny access
|
512
|
+
end
|
513
|
+
```
|
514
|
+
|
515
|
+
### Required Claims
|
516
|
+
|
517
|
+
You can specify claims that must be present for decoding to be successful. JWT::MissingRequiredClaim will be raised if any are missing
|
518
|
+
```ruby
|
519
|
+
# Will raise a JWT::MissingRequiredClaim error if the 'exp' claim is absent
|
520
|
+
JWT.decode token, hmac_secret, true, { required_claims: ['exp'], algorithm: 'HS256' }
|
521
|
+
```
|
522
|
+
|
523
|
+
### X.509 certificates in x5c header
|
524
|
+
|
525
|
+
A JWT signature can be verified using certificate(s) given in the `x5c` header. Before doing that, the trustworthiness of these certificate(s) must be established. This is done in accordance with RFC 5280 which (among other things) verifies the certificate(s) are issued by a trusted root certificate, the timestamps are valid, and none of the certificate(s) are revoked (i.e. being present in the root certificate's Certificate Revocation List).
|
526
|
+
|
527
|
+
```ruby
|
528
|
+
root_certificates = [] # trusted `OpenSSL::X509::Certificate` objects
|
529
|
+
crl_uris = root_certificates.map(&:crl_uris)
|
530
|
+
crls = crl_uris.map do |uri|
|
531
|
+
# look up cached CRL by `uri` and return it if found, otherwise continue
|
532
|
+
crl = Net::HTTP.get(uri)
|
533
|
+
crl = OpenSSL::X509::CRL.new(crl)
|
534
|
+
# cache `crl` using `uri` as the key, expiry set to `crl.next_update` timestamp
|
535
|
+
end
|
536
|
+
|
537
|
+
begin
|
538
|
+
JWT.decode(token, nil, true, { x5c: { root_certificates: root_certificates, crls: crls })
|
539
|
+
rescue JWT::DecodeError
|
540
|
+
# Handle error, e.g. x5c header certificate revoked or expired
|
541
|
+
end
|
542
|
+
```
|
543
|
+
|
544
|
+
### JSON Web Key (JWK)
|
545
|
+
|
546
|
+
JWK is a JSON structure representing a cryptographic key. Currently only supports RSA, EC and HMAC keys. The `jwks` option can be given as a lambda that evaluates every time a kid is resolved.
|
547
|
+
|
548
|
+
If the kid is not found from the given set the loader will be called a second time with the `kid_not_found` option set to `true`. The application can choose to implement some kind of JWK cache invalidation or other mechanism to handle such cases.
|
549
|
+
|
550
|
+
```ruby
|
551
|
+
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), 'optional-kid')
|
552
|
+
payload = { data: 'data' }
|
553
|
+
headers = { kid: jwk.kid }
|
554
|
+
|
555
|
+
token = JWT.encode(payload, jwk.keypair, 'RS512', headers)
|
556
|
+
|
557
|
+
# The jwk loader would fetch the set of JWKs from a trusted source,
|
558
|
+
# to avoid malicious requests triggering cache invalidations there needs to be some kind of grace time or other logic for determining the validity of the invalidation.
|
559
|
+
# This example only allows cache invalidations every 5 minutes.
|
560
|
+
jwk_loader = ->(options) do
|
561
|
+
if options[:kid_not_found] && @cache_last_update < Time.now.to_i - 300
|
562
|
+
logger.info("Invalidating JWK cache. #{options[:kid]} not found from previous cache")
|
563
|
+
@cached_keys = nil
|
564
|
+
end
|
565
|
+
@cached_keys ||= begin
|
566
|
+
@cache_last_update = Time.now.to_i
|
567
|
+
{ keys: [jwk.export] }
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
begin
|
572
|
+
JWT.decode(token, nil, true, { algorithms: ['RS512'], jwks: jwk_loader })
|
573
|
+
rescue JWT::JWKError
|
574
|
+
# Handle problems with the provided JWKs
|
575
|
+
rescue JWT::DecodeError
|
576
|
+
# Handle other decode related issues e.g. no kid in header, no matching public key found etc.
|
577
|
+
end
|
578
|
+
```
|
579
|
+
|
580
|
+
or by passing the JWKs as a simple Hash
|
581
|
+
|
582
|
+
```
|
583
|
+
jwks = { keys: [{ ... }] } # keys accepts both of string and symbol
|
584
|
+
JWT.decode(token, nil, true, { algorithms: ['RS512'], jwks: jwks})
|
585
|
+
```
|
586
|
+
|
587
|
+
### Importing and exporting JSON Web Keys
|
588
|
+
|
589
|
+
The ::JWT::JWK class can be used to import and export both the public key (default behaviour) and the private key. To include the private key in the export pass the `include_private` parameter to the export method.
|
590
|
+
|
591
|
+
```ruby
|
592
|
+
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048))
|
593
|
+
|
594
|
+
jwk_hash = jwk.export
|
595
|
+
jwk_hash_with_private_key = jwk.export(include_private: true)
|
596
|
+
```
|
597
|
+
|
598
|
+
### Key ID (kid) and JWKs
|
599
|
+
|
600
|
+
The key id (kid) generation in the gem is a custom algorithm and not based on any standards. To use a standardized JWK thumbprint (RFC 7638) as the kid for JWKs a generator type can be specified in the global configuration or can be given to the JWK instance on initialization.
|
601
|
+
|
602
|
+
```ruby
|
603
|
+
JWT.configuration.jwk.kid_generator_type = :rfc7638_thumbprint
|
604
|
+
# OR
|
605
|
+
JWT.configuration.jwk.kid_generator = ::JWT::JWK::Thumbprint
|
606
|
+
# OR
|
607
|
+
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), kid_generator: ::JWT::JWK::Thumbprint)
|
608
|
+
|
609
|
+
jwk_hash = jwk.export
|
610
|
+
|
611
|
+
thumbprint_as_the_kid = jwk_hash[:kid]
|
612
|
+
|
613
|
+
```
|
614
|
+
|
615
|
+
# Development and Tests
|
616
|
+
|
617
|
+
We depend on [Bundler](http://rubygems.org/gems/bundler) for defining gemspec and performing releases to rubygems.org, which can be done with
|
618
|
+
|
619
|
+
```bash
|
620
|
+
rake release
|
621
|
+
```
|
622
|
+
|
623
|
+
The tests are written with rspec. [Appraisal](https://github.com/thoughtbot/appraisal) is used to ensure compatibility with 3rd party dependencies providing cryptographic features.
|
624
|
+
|
625
|
+
```bash
|
626
|
+
bundle install
|
627
|
+
bundle exec appraisal rake test
|
628
|
+
```
|
629
|
+
|
630
|
+
## How to contribute
|
631
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
632
|
+
|
633
|
+
## Contributors
|
634
|
+
|
635
|
+
See [AUTHORS](AUTHORS).
|
636
|
+
|
637
|
+
## License
|
638
|
+
|
639
|
+
See [LICENSE](LICENSE).
|
data/Rakefile
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
|
2
|
-
require 'rake'
|
3
|
-
require 'echoe'
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
require 'rubocop/rake_task'
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:test)
|
11
|
+
RuboCop::RakeTask.new(:rubocop)
|
14
12
|
|
15
|
-
task :test
|
16
|
-
|
13
|
+
task default: %i[rubocop test]
|
14
|
+
rescue LoadError
|
15
|
+
puts 'RSpec rake tasks not available. Please run "bundle install" to install missing dependencies.'
|
17
16
|
end
|