jwt 1.5.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +20 -0
  3. data/.ebert.yml +18 -0
  4. data/.gitignore +11 -0
  5. data/.reek.yml +40 -0
  6. data/.rspec +1 -0
  7. data/.rubocop.yml +98 -0
  8. data/.travis.yml +14 -0
  9. data/CHANGELOG.md +476 -0
  10. data/Gemfile +3 -0
  11. data/LICENSE +7 -0
  12. data/Manifest +3 -1
  13. data/README.md +478 -0
  14. data/Rakefile +8 -15
  15. data/lib/jwt/algos/ecdsa.rb +35 -0
  16. data/lib/jwt/algos/eddsa.rb +23 -0
  17. data/lib/jwt/algos/hmac.rb +33 -0
  18. data/lib/jwt/algos/rsa.rb +19 -0
  19. data/lib/jwt/algos/unsupported.rb +16 -0
  20. data/lib/jwt/decode.rb +49 -0
  21. data/lib/jwt/default_options.rb +15 -0
  22. data/lib/jwt/encode.rb +51 -0
  23. data/lib/jwt/error.rb +16 -0
  24. data/lib/jwt/security_utils.rb +51 -0
  25. data/lib/jwt/signature.rb +50 -0
  26. data/lib/jwt/verify.rb +102 -0
  27. data/lib/jwt/version.rb +24 -0
  28. data/lib/jwt.rb +33 -203
  29. data/ruby-jwt.gemspec +31 -0
  30. data/spec/fixtures/certs/ec256-private.pem +8 -0
  31. data/spec/fixtures/certs/ec256-public.pem +4 -0
  32. data/spec/fixtures/certs/ec256-wrong-private.pem +8 -0
  33. data/spec/fixtures/certs/ec256-wrong-public.pem +4 -0
  34. data/spec/fixtures/certs/ec384-private.pem +9 -0
  35. data/spec/fixtures/certs/ec384-public.pem +5 -0
  36. data/spec/fixtures/certs/ec384-wrong-private.pem +9 -0
  37. data/spec/fixtures/certs/ec384-wrong-public.pem +5 -0
  38. data/spec/fixtures/certs/ec512-private.pem +10 -0
  39. data/spec/fixtures/certs/ec512-public.pem +6 -0
  40. data/spec/fixtures/certs/ec512-wrong-private.pem +10 -0
  41. data/spec/fixtures/certs/ec512-wrong-public.pem +6 -0
  42. data/spec/fixtures/certs/rsa-1024-private.pem +15 -0
  43. data/spec/fixtures/certs/rsa-1024-public.pem +6 -0
  44. data/spec/fixtures/certs/rsa-2048-private.pem +27 -0
  45. data/spec/fixtures/certs/rsa-2048-public.pem +9 -0
  46. data/spec/fixtures/certs/rsa-2048-wrong-private.pem +27 -0
  47. data/spec/fixtures/certs/rsa-2048-wrong-public.pem +9 -0
  48. data/spec/fixtures/certs/rsa-4096-private.pem +51 -0
  49. data/spec/fixtures/certs/rsa-4096-public.pem +14 -0
  50. data/spec/integration/readme_examples_spec.rb +202 -0
  51. data/spec/jwt/verify_spec.rb +232 -0
  52. data/spec/jwt_spec.rb +236 -384
  53. data/spec/spec_helper.rb +28 -0
  54. metadata +187 -26
  55. data/jwt.gemspec +0 -34
  56. data/lib/jwt/json.rb +0 -32
  57. data/spec/helper.rb +0 -19
data/README.md ADDED
@@ -0,0 +1,478 @@
1
+ # JWT
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/jwt.svg)](https://badge.fury.io/rb/jwt)
4
+ [![Build Status](https://travis-ci.org/jwt/ruby-jwt.svg)](https://travis-ci.org/jwt/ruby-jwt)
5
+ [![Code Climate](https://codeclimate.com/github/jwt/ruby-jwt/badges/gpa.svg)](https://codeclimate.com/github/jwt/ruby-jwt)
6
+ [![Test Coverage](https://codeclimate.com/github/jwt/ruby-jwt/badges/coverage.svg)](https://codeclimate.com/github/jwt/ruby-jwt/coverage)
7
+ [![Issue Count](https://codeclimate.com/github/jwt/ruby-jwt/badges/issue_count.svg)](https://codeclimate.com/github/jwt/ruby-jwt)
8
+
9
+ A pure ruby implementation of the [RFC 7519 OAuth JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) standard.
10
+
11
+ If you have further questions related to development or usage, join us: [ruby-jwt google group](https://groups.google.com/forum/#!forum/ruby-jwt).
12
+
13
+ ## Announcements
14
+
15
+ * Ruby 1.9.3 support was dropped at December 31st, 2016.
16
+ * 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)
17
+
18
+ ## Installing
19
+
20
+ ### Using Rubygems:
21
+ ```bash
22
+ sudo gem install jwt
23
+ ```
24
+
25
+ ### Using Bundler:
26
+ Add the following to your Gemfile
27
+ ```
28
+ gem 'jwt'
29
+ ```
30
+ And run `bundle install`
31
+
32
+ ## Algorithms and Usage
33
+
34
+ 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/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/).
35
+
36
+ See: [ JSON Web Algorithms (JWA) 3.1. "alg" (Algorithm) Header Parameter Values for JWS](https://tools.ietf.org/html/rfc7518#section-3.1)
37
+
38
+ **NONE**
39
+
40
+ * none - unsigned token
41
+
42
+ ```ruby
43
+ require 'jwt'
44
+
45
+ payload = {:data => 'test'}
46
+
47
+ # IMPORTANT: set nil as password parameter
48
+ token = JWT.encode payload, nil, 'none'
49
+
50
+ # eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.
51
+ puts token
52
+
53
+ # Set password to nil and validation to false otherwise this won't work
54
+ decoded_token = JWT.decode token, nil, false
55
+
56
+ # Array
57
+ # [
58
+ # {"data"=>"test"}, # payload
59
+ # {"alg"=>"none"} # header
60
+ # ]
61
+ puts decoded_token
62
+ ```
63
+
64
+ **HMAC**
65
+
66
+ * HS256 - HMAC using SHA-256 hash algorithm
67
+ * HS512256 - HMAC using SHA-512-256 hash algorithm (only available with RbNaCl; see note below)
68
+ * HS384 - HMAC using SHA-384 hash algorithm
69
+ * HS512 - HMAC using SHA-512 hash algorithm
70
+
71
+ ```ruby
72
+ hmac_secret = 'my$ecretK3y'
73
+
74
+ token = JWT.encode payload, hmac_secret, 'HS256'
75
+
76
+ # eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.ZxW8go9hz3ETCSfxFxpwSkYg_602gOPKearsf6DsxgY
77
+ puts token
78
+
79
+ decoded_token = JWT.decode token, hmac_secret, true, { :algorithm => 'HS256' }
80
+
81
+ # Array
82
+ # [
83
+ # {"data"=>"test"}, # payload
84
+ # {"alg"=>"HS256"} # header
85
+ # ]
86
+ puts decoded_token
87
+ ```
88
+
89
+ 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.
90
+
91
+ [RbNaCl](https://github.com/cryptosphere/rbnacl) requires
92
+ [libsodium](https://github.com/jedisct1/libsodium), it can be installed
93
+ on MacOS with `brew install libsodium`.
94
+
95
+ **RSA**
96
+
97
+ * RS256 - RSA using SHA-256 hash algorithm
98
+ * RS384 - RSA using SHA-384 hash algorithm
99
+ * RS512 - RSA using SHA-512 hash algorithm
100
+
101
+ ```ruby
102
+ rsa_private = OpenSSL::PKey::RSA.generate 2048
103
+ rsa_public = rsa_private.public_key
104
+
105
+ token = JWT.encode payload, rsa_private, 'RS256'
106
+
107
+ # eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ0ZXN0IjoiZGF0YSJ9.c2FynXNyi6_PeKxrDGxfS3OLwQ8lTDbWBWdq7oMviCy2ZfFpzvW2E_odCWJrbLof-eplHCsKzW7MGAntHMALXgclm_Cs9i2Exi6BZHzpr9suYkrhIjwqV1tCgMBCQpdeMwIq6SyKVjgH3L51ivIt0-GDDPDH1Rcut3jRQzp3Q35bg3tcI2iVg7t3Msvl9QrxXAdYNFiS5KXH22aJZ8X_O2HgqVYBXfSB1ygTYUmKTIIyLbntPQ7R22rFko1knGWOgQCoYXwbtpuKRZVFrxX958L2gUWgb4jEQNf3fhOtkBm1mJpj-7BGst00o8g_3P2zHy-3aKgpPo1XlKQGjRrrxA
108
+ puts token
109
+
110
+ decoded_token = JWT.decode token, rsa_public, true, { :algorithm => 'RS256' }
111
+
112
+ # Array
113
+ # [
114
+ # {"data"=>"test"}, # payload
115
+ # {"alg"=>"RS256"} # header
116
+ # ]
117
+ puts decoded_token
118
+ ```
119
+
120
+ **ECDSA**
121
+
122
+ * ES256 - ECDSA using P-256 and SHA-256
123
+ * ES384 - ECDSA using P-384 and SHA-384
124
+ * ES512 - ECDSA using P-521 and SHA-512
125
+
126
+ ```ruby
127
+ ecdsa_key = OpenSSL::PKey::EC.new 'prime256v1'
128
+ ecdsa_key.generate_key
129
+ ecdsa_public = OpenSSL::PKey::EC.new ecdsa_key
130
+ ecdsa_public.private_key = nil
131
+
132
+ token = JWT.encode payload, ecdsa_key, 'ES256'
133
+
134
+ # eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ0ZXN0IjoiZGF0YSJ9.MEQCIAtShrxRwP1L9SapqaT4f7hajDJH4t_rfm-YlZcNDsBNAiB64M4-JRfyS8nRMlywtQ9lHbvvec9U54KznzOe1YxTyA
135
+ puts token
136
+
137
+ decoded_token = JWT.decode token, ecdsa_public, true, { :algorithm => 'ES256' }
138
+
139
+ # Array
140
+ # [
141
+ # {"test"=>"data"}, # payload
142
+ # {"alg"=>"ES256"} # header
143
+ # ]
144
+ puts decoded_token
145
+ ```
146
+
147
+ **EDDSA**
148
+
149
+ In order to use this algorithm you need to add the `RbNaCl` gem to you `Gemfile`.
150
+
151
+ ```ruby
152
+ gem 'rbnacl'
153
+ ```
154
+
155
+ For more detailed installation instruction check the official [repository](https://github.com/cryptosphere/rbnacl) on GitHub.
156
+
157
+ * ED25519
158
+
159
+ ```ruby
160
+ private_key = RbNaCl::Signatures::Ed25519::SigningKey.new("abcdefghijklmnopqrstuvwxyzABCDEF")
161
+ public_key = private_key.verify_key
162
+ token = JWT.encode payload, private_key, 'ED25519'
163
+
164
+ # eyJhbGciOiJFRDI1NTE5In0.eyJ0ZXN0IjoiZGF0YSJ9.-Ki0vxVOlsPXovPsYRT_9OXrLSgQd4RDAgCLY_PLmcP4q32RYy-yUUmX82ycegdekR9wo26me1wOzjmSU5nTCQ
165
+ puts token
166
+
167
+ decoded_token = JWT.decode token, public_key, true, {:algorithm => 'ED25519' }
168
+ # Array
169
+ # [
170
+ # {"test"=>"data"}, # payload
171
+ # {"alg"=>"ED25519"} # header
172
+ # ]
173
+
174
+ ```
175
+
176
+ **RSASSA-PSS**
177
+
178
+ Not implemented.
179
+
180
+ ## Support for reserved claim names
181
+ JSON Web Token defines some reserved claim names and defines how they should be
182
+ used. JWT supports these reserved claim names:
183
+
184
+ - 'exp' (Expiration Time) Claim
185
+ - 'nbf' (Not Before Time) Claim
186
+ - 'iss' (Issuer) Claim
187
+ - 'aud' (Audience) Claim
188
+ - 'jti' (JWT ID) Claim
189
+ - 'iat' (Issued At) Claim
190
+ - 'sub' (Subject) Claim
191
+
192
+ ## Add custom header fields
193
+ Ruby-jwt gem supports custom [header fields] (https://tools.ietf.org/html/rfc7519#section-5)
194
+ To add custom header fields you need to pass `header_fields` parameter
195
+
196
+ ```ruby
197
+ token = JWT.encode payload, key, algorithm='HS256', header_fields={}
198
+ ```
199
+
200
+ **Example:**
201
+
202
+ ```ruby
203
+ require 'jwt'
204
+
205
+ payload = {:data => 'test'}
206
+
207
+ # IMPORTANT: set nil as password parameter
208
+ token = JWT.encode payload, nil, 'none', { :typ => "JWT" }
209
+
210
+ # eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJkYXRhIjoidGVzdCJ9.
211
+ puts token
212
+
213
+ # Set password to nil and validation to false otherwise this won't work
214
+ decoded_token = JWT.decode token, nil, false
215
+
216
+ # Array
217
+ # [
218
+ # {"data"=>"test"}, # payload
219
+ # {"typ"=>"JWT", "alg"=>"none"} # header
220
+ # ]
221
+ puts decoded_token
222
+ ```
223
+
224
+ ### Expiration Time Claim
225
+
226
+ From [Oauth JSON Web Token 4.1.4. "exp" (Expiration Time) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.4):
227
+
228
+ > 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.
229
+
230
+ **Handle Expiration Claim**
231
+
232
+ ```ruby
233
+ exp = Time.now.to_i + 4 * 3600
234
+ exp_payload = { :data => 'data', :exp => exp }
235
+
236
+ token = JWT.encode exp_payload, hmac_secret, 'HS256'
237
+
238
+ begin
239
+ decoded_token = JWT.decode token, hmac_secret, true, { :algorithm => 'HS256' }
240
+ rescue JWT::ExpiredSignature
241
+ # Handle expired token, e.g. logout user or deny access
242
+ end
243
+ ```
244
+
245
+ **Adding Leeway**
246
+
247
+ ```ruby
248
+ exp = Time.now.to_i - 10
249
+ leeway = 30 # seconds
250
+
251
+ exp_payload = { :data => 'data', :exp => exp }
252
+
253
+ # build expired token
254
+ token = JWT.encode exp_payload, hmac_secret, 'HS256'
255
+
256
+ begin
257
+ # add leeway to ensure the token is still accepted
258
+ decoded_token = JWT.decode token, hmac_secret, true, { :exp_leeway => leeway, :algorithm => 'HS256' }
259
+ rescue JWT::ExpiredSignature
260
+ # Handle expired token, e.g. logout user or deny access
261
+ end
262
+ ```
263
+
264
+ ### Not Before Time Claim
265
+
266
+ From [Oauth JSON Web Token 4.1.5. "nbf" (Not Before) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.5):
267
+
268
+ > 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.
269
+
270
+ **Handle Not Before Claim**
271
+
272
+ ```ruby
273
+ nbf = Time.now.to_i - 3600
274
+ nbf_payload = { :data => 'data', :nbf => nbf }
275
+
276
+ token = JWT.encode nbf_payload, hmac_secret, 'HS256'
277
+
278
+ begin
279
+ decoded_token = JWT.decode token, hmac_secret, true, { :algorithm => 'HS256' }
280
+ rescue JWT::ImmatureSignature
281
+ # Handle invalid token, e.g. logout user or deny access
282
+ end
283
+ ```
284
+
285
+ **Adding Leeway**
286
+
287
+ ```ruby
288
+ nbf = Time.now.to_i + 10
289
+ leeway = 30
290
+
291
+ nbf_payload = { :data => 'data', :nbf => nbf }
292
+
293
+ # build expired token
294
+ token = JWT.encode nbf_payload, hmac_secret, 'HS256'
295
+
296
+ begin
297
+ # add leeway to ensure the token is valid
298
+ decoded_token = JWT.decode token, hmac_secret, true, { :nbf_leeway => leeway, :algorithm => 'HS256' }
299
+ rescue JWT::ImmatureSignature
300
+ # Handle invalid token, e.g. logout user or deny access
301
+ end
302
+ ```
303
+
304
+ ### Issuer Claim
305
+
306
+ From [Oauth JSON Web Token 4.1.1. "iss" (Issuer) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.1):
307
+
308
+ > 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.
309
+
310
+ You can pass multiple allowed issuers as an Array, verification will pass if one of them matches the `iss` value in the payload.
311
+
312
+ ```ruby
313
+ iss = 'My Awesome Company Inc. or https://my.awesome.website/'
314
+ iss_payload = { :data => 'data', :iss => iss }
315
+
316
+ token = JWT.encode iss_payload, hmac_secret, 'HS256'
317
+
318
+ begin
319
+ # Add iss to the validation to check if the token has been manipulated
320
+ decoded_token = JWT.decode token, hmac_secret, true, { :iss => iss, :verify_iss => true, :algorithm => 'HS256' }
321
+ rescue JWT::InvalidIssuerError
322
+ # Handle invalid token, e.g. logout user or deny access
323
+ end
324
+ ```
325
+
326
+ ### Audience Claim
327
+
328
+ From [Oauth JSON Web Token 4.1.3. "aud" (Audience) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.3):
329
+
330
+ > 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.
331
+
332
+ ```ruby
333
+ aud = ['Young', 'Old']
334
+ aud_payload = { :data => 'data', :aud => aud }
335
+
336
+ token = JWT.encode aud_payload, hmac_secret, 'HS256'
337
+
338
+ begin
339
+ # Add aud to the validation to check if the token has been manipulated
340
+ decoded_token = JWT.decode token, hmac_secret, true, { :aud => aud, :verify_aud => true, :algorithm => 'HS256' }
341
+ rescue JWT::InvalidAudError
342
+ # Handle invalid token, e.g. logout user or deny access
343
+ puts 'Audience Error'
344
+ end
345
+ ```
346
+
347
+ ### JWT ID Claim
348
+
349
+ From [Oauth JSON Web Token 4.1.7. "jti" (JWT ID) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.7):
350
+
351
+ > 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.
352
+
353
+ ```ruby
354
+ # Use the secret and iat to create a unique key per request to prevent replay attacks
355
+ jti_raw = [hmac_secret, iat].join(':').to_s
356
+ jti = Digest::MD5.hexdigest(jti_raw)
357
+ jti_payload = { :data => 'data', :iat => iat, :jti => jti }
358
+
359
+ token = JWT.encode jti_payload, hmac_secret, 'HS256'
360
+
361
+ begin
362
+ # If :verify_jti is true, validation will pass if a JTI is present
363
+ #decoded_token = JWT.decode token, hmac_secret, true, { :verify_jti => true, :algorithm => 'HS256' }
364
+ # Alternatively, pass a proc with your own code to check if the JTI has already been used
365
+ decoded_token = JWT.decode token, hmac_secret, true, { :verify_jti => proc { |jti| my_validation_method(jti) }, :algorithm => 'HS256' }
366
+ rescue JWT::InvalidJtiError
367
+ # Handle invalid token, e.g. logout user or deny access
368
+ puts 'Error'
369
+ end
370
+
371
+ ```
372
+
373
+ ### Issued At Claim
374
+
375
+ From [Oauth JSON Web Token 4.1.6. "iat" (Issued At) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.6):
376
+
377
+ > 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. Its value MUST be a number containing a ***NumericDate*** value. Use of this claim is OPTIONAL.
378
+
379
+ **Handle Issued At Claim**
380
+
381
+ ```ruby
382
+ iat = Time.now.to_i
383
+ iat_payload = { :data => 'data', :iat => iat }
384
+
385
+ token = JWT.encode iat_payload, hmac_secret, 'HS256'
386
+
387
+ begin
388
+ # Add iat to the validation to check if the token has been manipulated
389
+ decoded_token = JWT.decode token, hmac_secret, true, { :verify_iat => true, :algorithm => 'HS256' }
390
+ rescue JWT::InvalidIatError
391
+ # Handle invalid token, e.g. logout user or deny access
392
+ end
393
+ ```
394
+
395
+ **Adding Leeway**
396
+
397
+ ```ruby
398
+ iat = Time.now.to_i + 10
399
+ leeway = 30 # seconds
400
+
401
+ iat_payload = { :data => 'data', :iat => iat }
402
+
403
+ # build token issued in the future
404
+ token = JWT.encode iat_payload, hmac_secret, 'HS256'
405
+
406
+ begin
407
+ # add leeway to ensure the token is accepted
408
+ decoded_token = JWT.decode token, hmac_secret, true, { :iat_leeway => leeway, :verify_iat => true, :algorithm => 'HS256' }
409
+ rescue JWT::InvalidIatError
410
+ # Handle invalid token, e.g. logout user or deny access
411
+ end
412
+ ```
413
+
414
+ ### Subject Claim
415
+
416
+ From [Oauth JSON Web Token 4.1.2. "sub" (Subject) Claim](https://tools.ietf.org/html/rfc7519#section-4.1.2):
417
+
418
+ > 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.
419
+
420
+ ```ruby
421
+ sub = 'Subject'
422
+ sub_payload = { :data => 'data', :sub => sub }
423
+
424
+ token = JWT.encode sub_payload, hmac_secret, 'HS256'
425
+
426
+ begin
427
+ # Add sub to the validation to check if the token has been manipulated
428
+ decoded_token = JWT.decode token, hmac_secret, true, { 'sub' => sub, :verify_sub => true, :algorithm => 'HS256' }
429
+ rescue JWT::InvalidSubError
430
+ # Handle invalid token, e.g. logout user or deny access
431
+ end
432
+ ```
433
+
434
+ # Development and Tests
435
+
436
+ We depend on [Bundler](http://rubygems.org/gems/bundler) for defining gemspec and performing releases to rubygems.org, which can be done with
437
+
438
+ ```bash
439
+ rake release
440
+ ```
441
+
442
+ The tests are written with rspec. Given you have installed the dependencies via bundler, you can run tests with
443
+
444
+ ```bash
445
+ bundle exec rspec
446
+ ```
447
+
448
+ **If you want a release cut with your PR, please include a version bump according to [Semantic Versioning](http://semver.org/)**
449
+
450
+ ## Contributors
451
+
452
+ * Jordan Brough <github.jordanb@xoxy.net>
453
+ * Ilya Zhitomirskiy <ilya@joindiaspora.com>
454
+ * Daniel Grippi <daniel@joindiaspora.com>
455
+ * Jeff Lindsay <progrium@gmail.com>
456
+ * Bob Aman <bob@sporkmonger.com>
457
+ * Micah Gates <github@mgates.com>
458
+ * Rob Wygand <rob@wygand.com>
459
+ * Ariel Salomon (Oscil8)
460
+ * Paul Battley <pbattley@gmail.com>
461
+ * Zane Shannon [@zshannon](https://github.com/zshannon)
462
+ * Brian Fletcher [@punkle](https://github.com/punkle)
463
+ * Alex [@ZhangHanDong](https://github.com/ZhangHanDong)
464
+ * John Downey [@jtdowney](https://github.com/jtdowney)
465
+ * Adam Greene [@skippy](https://github.com/skippy)
466
+ * Tim Rudat [@excpt](https://github.com/excpt) <timrudat@gmail.com> - Maintainer
467
+
468
+ ## License
469
+
470
+ MIT
471
+
472
+ Copyright (c) 2011 Jeff Lindsay
473
+
474
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
475
+
476
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
477
+
478
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1,18 +1,11 @@
1
- # encoding: utf-8
2
- require 'rubygems'
3
- require 'rake'
4
- require 'echoe'
1
+ require 'bundler/gem_tasks'
5
2
 
6
- Echoe.new('jwt', '1.5.1') do |p|
7
- p.description = 'JSON Web Token implementation in Ruby'
8
- p.url = 'http://github.com/progrium/ruby-jwt'
9
- p.author = 'Jeff Lindsay'
10
- p.email = 'progrium@gmail.com'
11
- p.ignore_pattern = ['tmp/*']
12
- p.development_dependencies = ['echoe >=4.6.3']
13
- p.licenses = 'MIT'
14
- end
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:test)
15
7
 
16
- task :test do
17
- sh 'rspec spec/jwt_spec.rb'
8
+ task default: :test
9
+ rescue LoadError
10
+ puts 'RSpec rake tasks not available. Please run "bundle install" to install missing dependencies.'
18
11
  end
@@ -0,0 +1,35 @@
1
+ module JWT
2
+ module Algos
3
+ module Ecdsa
4
+ module_function
5
+
6
+ SUPPORTED = %(ES256 ES384 ES512).freeze
7
+ NAMED_CURVES = {
8
+ 'prime256v1' => 'ES256',
9
+ 'secp384r1' => 'ES384',
10
+ 'secp521r1' => 'ES512'
11
+ }.freeze
12
+
13
+ def sign(to_sign)
14
+ algorithm, msg, key = to_sign.values
15
+ key_algorithm = NAMED_CURVES[key.group.curve_name]
16
+ if algorithm != key_algorithm
17
+ raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} signing key was provided"
18
+ end
19
+
20
+ digest = OpenSSL::Digest.new(algorithm.sub('ES', 'sha'))
21
+ SecurityUtils.asn1_to_raw(key.dsa_sign_asn1(digest.digest(msg)), key)
22
+ end
23
+
24
+ def verify(to_verify)
25
+ algorithm, public_key, signing_input, signature = to_verify.values
26
+ key_algorithm = NAMED_CURVES[public_key.group.curve_name]
27
+ if algorithm != key_algorithm
28
+ raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key_algorithm} verification key was provided"
29
+ end
30
+ digest = OpenSSL::Digest.new(algorithm.sub('ES', 'sha'))
31
+ public_key.dsa_verify_asn1(digest.digest(signing_input), SecurityUtils.raw_to_asn1(signature, public_key))
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ module JWT
2
+ module Algos
3
+ module Eddsa
4
+ module_function
5
+
6
+ SUPPORTED = %w[ED25519].freeze
7
+
8
+ def sign(to_sign)
9
+ algorithm, msg, key = to_sign.values
10
+ raise EncodeError, "Key given is a #{key.class} but has to be an RbNaCl::Signatures::Ed25519::SigningKey" if key.class != RbNaCl::Signatures::Ed25519::SigningKey
11
+ raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{key.primitive} signing key was provided" if algorithm.downcase.to_sym != key.primitive
12
+ key.sign(msg)
13
+ end
14
+
15
+ def verify(to_verify)
16
+ algorithm, public_key, signing_input, signature = to_verify.values
17
+ raise IncorrectAlgorithm, "payload algorithm is #{algorithm} but #{public_key.primitive} verification key was provided" if algorithm.downcase.to_sym != public_key.primitive
18
+ raise DecodeError, "key given is a #{public_key.class} but has to be a RbNaCl::Signatures::Ed25519::VerifyKey" if public_key.class != RbNaCl::Signatures::Ed25519::VerifyKey
19
+ public_key.verify(signature, signing_input)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module JWT
2
+ module Algos
3
+ module Hmac
4
+ module_function
5
+
6
+ SUPPORTED = %w[HS256 HS512256 HS384 HS512].freeze
7
+
8
+ def sign(to_sign)
9
+ algorithm, msg, key = to_sign.values
10
+ authenticator, padded_key = SecurityUtils.rbnacl_fixup(algorithm, key)
11
+ if authenticator && padded_key
12
+ authenticator.auth(padded_key, msg.encode('binary'))
13
+ else
14
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'sha')), key, msg)
15
+ end
16
+ end
17
+
18
+ def verify(to_verify)
19
+ algorithm, public_key, signing_input, signature = to_verify.values
20
+ authenticator, padded_key = SecurityUtils.rbnacl_fixup(algorithm, public_key)
21
+ if authenticator && padded_key
22
+ begin
23
+ authenticator.verify(padded_key, signature.encode('binary'), signing_input.encode('binary'))
24
+ rescue RbNaCl::BadAuthenticatorError
25
+ false
26
+ end
27
+ else
28
+ SecurityUtils.secure_compare(signature, sign(JWT::Signature::ToSign.new(algorithm, signing_input, public_key)))
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module JWT
2
+ module Algos
3
+ module Rsa
4
+ module_function
5
+
6
+ SUPPORTED = %w[RS256 RS384 RS512].freeze
7
+
8
+ def sign(to_sign)
9
+ algorithm, msg, key = to_sign.values
10
+ raise EncodeError, "The given key is a #{key.class}. It has to be an OpenSSL::PKey::RSA instance." if key.class == String
11
+ key.sign(OpenSSL::Digest.new(algorithm.sub('RS', 'sha')), msg)
12
+ end
13
+
14
+ def verify(to_verify)
15
+ SecurityUtils.verify_rsa(to_verify.algorithm, to_verify.public_key, to_verify.signing_input, to_verify.signature)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ module JWT
2
+ module Algos
3
+ module Unsupported
4
+ module_function
5
+
6
+ SUPPORTED = Object.new.tap { |object| object.define_singleton_method(:include?) { |*| true } }
7
+ def verify(*)
8
+ raise JWT::VerificationError, 'Algorithm not supported'
9
+ end
10
+
11
+ def sign(*)
12
+ raise NotImplementedError, 'Unsupported signing method'
13
+ end
14
+ end
15
+ end
16
+ end
data/lib/jwt/decode.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ # JWT::Decode module
6
+ module JWT
7
+ # Decoding logic for JWT
8
+ class Decode
9
+ attr_reader :header, :payload, :signature
10
+
11
+ def self.base64url_decode(str)
12
+ str += '=' * (4 - str.length.modulo(4))
13
+ Base64.decode64(str.tr('-_', '+/'))
14
+ end
15
+
16
+ def initialize(jwt, verify)
17
+ @jwt = jwt
18
+ @verify = verify
19
+ @header = ''
20
+ @payload = ''
21
+ @signature = ''
22
+ end
23
+
24
+ def decode_segments
25
+ header_segment, payload_segment, crypto_segment = raw_segments
26
+ @header, @payload = decode_header_and_payload(header_segment, payload_segment)
27
+ @signature = Decode.base64url_decode(crypto_segment.to_s) if @verify
28
+ signing_input = [header_segment, payload_segment].join('.')
29
+ [@header, @payload, @signature, signing_input]
30
+ end
31
+
32
+ private
33
+
34
+ def raw_segments
35
+ segments = @jwt.split('.')
36
+ required_num_segments = @verify ? [3] : [2, 3]
37
+ raise(JWT::DecodeError, 'Not enough or too many segments') unless required_num_segments.include? segments.length
38
+ segments
39
+ end
40
+
41
+ def decode_header_and_payload(header_segment, payload_segment)
42
+ header = JSON.parse(Decode.base64url_decode(header_segment))
43
+ payload = JSON.parse(Decode.base64url_decode(payload_segment))
44
+ [header, payload]
45
+ rescue JSON::ParserError
46
+ raise JWT::DecodeError, 'Invalid segment encoding'
47
+ end
48
+ end
49
+ end