jwt 3.3.2 → 3.4.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 +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +106 -4
- data/UPGRADING.md +15 -0
- data/lib/jwt/base64.rb +4 -1
- data/lib/jwt/claims/known_crit.rb +41 -0
- data/lib/jwt/claims/verifier.rb +1 -0
- data/lib/jwt/claims.rb +1 -0
- data/lib/jwt/decode.rb +7 -1
- data/lib/jwt/deprecations.rb +36 -0
- data/lib/jwt/encoded_nested_token.rb +136 -0
- data/lib/jwt/encoded_token/segment_parser.rb +31 -0
- data/lib/jwt/encoded_token/signature_verifier.rb +53 -0
- data/lib/jwt/encoded_token.rb +19 -37
- data/lib/jwt/jwa/ecdsa.rb +7 -1
- data/lib/jwt/jwa/hmac.rb +19 -4
- data/lib/jwt/jwa/ps.rb +17 -1
- data/lib/jwt/jwa/rsa.rb +8 -1
- data/lib/jwt/jwa.rb +19 -12
- data/lib/jwt/nested_token.rb +44 -0
- data/lib/jwt/version.rb +2 -2
- data/lib/jwt.rb +3 -0
- metadata +8 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d0bdd88f7e9d3efc449c4291d4ad0014fc80aafe480d6e2a58954d2fedf5817f
|
|
4
|
+
data.tar.gz: ddac2777513185973f9ada3a3c7d8a4bbc3e2623431f5bbee061419924092ddb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 61c6dae94b940d0915a0d7468d41d3179e4c3f6c88838e04149e63a8c131e83a78d5d919ebf75202088ab5bf065cf569e2a8c23d7728cdaebe9a3c8551dafe24
|
|
7
|
+
data.tar.gz: 1d8008c8299d1ecbd2ae18c5324b1915f23835697863d5c940358a3c9aabb865e912ce6150ff5eb4e459b082611d2248d699aec9fec93d3d0d91b8bcb5864c4e
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [3.4.0](https://github.com/jwt/ruby-jwt/compare/v3.3.2...v3.4.0) (2026-12-05)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* Add an opt-in known_crit check for critical headers ([#824](https://github.com/jwt/ruby-jwt/issues/824)) ([2ac099c](https://github.com/jwt/ruby-jwt/commit/2ac099ce7edabdac08a408fe60febfcb34f138bc))
|
|
9
|
+
* Add an opt-in strict_base64url option to JWT::EncodedToken ([#825](https://github.com/jwt/ruby-jwt/issues/825)) ([ad9897c](https://github.com/jwt/ruby-jwt/commit/ad9897cb767237a8c412dd145313c0321f6b8e2e))
|
|
10
|
+
* Add support for signing and verifying nested JWTs ([#712](https://github.com/jwt/ruby-jwt/issues/712)) ([b9ce04f](https://github.com/jwt/ruby-jwt/commit/b9ce04f26913a5fac5fe4cf7ab266fa5c643a19a))
|
|
11
|
+
* Warn about alg headers that only match when the case is ignored ([12ca1b5](https://github.com/jwt/ruby-jwt/commit/12ca1b50d26c5288d52a9a0ffd2059b07936ed96))
|
|
12
|
+
* Warn about HMAC secrets shorter than RFC 7518 requires and RSA keys under 2048 bits ([12ca1b5](https://github.com/jwt/ruby-jwt/commit/12ca1b50d26c5288d52a9a0ffd2059b07936ed96))
|
|
13
|
+
* Warn about PSS signatures whose salt is not as long as the hash ([12ca1b5](https://github.com/jwt/ruby-jwt/commit/12ca1b50d26c5288d52a9a0ffd2059b07936ed96))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Fixes and enhancements
|
|
17
|
+
|
|
18
|
+
* Accept a JWK as the verification key in JWT.decode ([#818](https://github.com/jwt/ruby-jwt/issues/818)) ([2651e5f](https://github.com/jwt/ruby-jwt/commit/2651e5f9ba39c6d84c8363fb281e4f803d465f66))
|
|
19
|
+
* Reject ECDSA and RSA signatures of the wrong length ([#831](https://github.com/jwt/ruby-jwt/issues/831)) ([b64ae98](https://github.com/jwt/ruby-jwt/commit/b64ae98ac7f583d24fb679407552524c13cbdfb1))
|
|
20
|
+
* Treat a cty header that isn't valid UTF-8 as not nested ([#829](https://github.com/jwt/ruby-jwt/issues/829)) ([9f2ec8f](https://github.com/jwt/ruby-jwt/commit/9f2ec8f499a0a0b92ada66baf36cc88e447877b5))
|
|
21
|
+
* Use the hash length as the minimum key length for custom HMAC algorithms ([12ca1b5](https://github.com/jwt/ruby-jwt/commit/12ca1b50d26c5288d52a9a0ffd2059b07936ed96))
|
|
22
|
+
|
|
3
23
|
## [3.3.2](https://github.com/jwt/ruby-jwt/compare/v3.3.1...v3.3.2) (2026-11-28)
|
|
4
24
|
|
|
5
25
|
|
data/README.md
CHANGED
|
@@ -71,12 +71,14 @@ decoded_token = JWT.decode(token, nil, true, { algorithm: 'none' })
|
|
|
71
71
|
- HS384 - HMAC using SHA-384 hash algorithm
|
|
72
72
|
- HS512 - HMAC using SHA-512 hash algorithm
|
|
73
73
|
|
|
74
|
+
The secret has to be at least as long as the hash output: 32 bytes for HS256, 48 for HS384 and 64 for HS512 (RFC 7518). Shorter secrets still work but print a [deprecation warning](#deprecation-warnings).
|
|
75
|
+
|
|
74
76
|
```ruby
|
|
75
77
|
payload = { data: 'test' }
|
|
76
|
-
hmac_secret = 'my$ecretK3y'
|
|
78
|
+
hmac_secret = 'my$ecretK3y-at-least-32-bytes-long'
|
|
77
79
|
|
|
78
80
|
token = JWT.encode(payload, hmac_secret, 'HS256')
|
|
79
|
-
# => "eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.
|
|
81
|
+
# => "eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.t_QSIvho3MV7jEdxxBKo--dTT3-7Uu1tMnc_W1ukwB0"
|
|
80
82
|
|
|
81
83
|
decoded_token = JWT.decode(token, hmac_secret, true, { algorithm: 'HS256' })
|
|
82
84
|
# => [
|
|
@@ -192,6 +194,8 @@ decoded_token = JWT.decode(token, hmac_secret, true, { algorithm: CustomHS512Alg
|
|
|
192
194
|
# ]
|
|
193
195
|
```
|
|
194
196
|
|
|
197
|
+
A JWK signs and verifies with the algorithm registered under its `alg`, or for EC and OKP keys without `alg`, the one for its curve. A custom algorithm given with a JWK only selects the JWKs whose `alg` has the same name. Its `sign` and `verify` methods are only called if it is registered under that name with `JWT::JWA.register_algorithm`. To use an unregistered implementation, pass the key itself, for example `jwk.signing_key` or `jwk.verify_key`.
|
|
198
|
+
|
|
195
199
|
### Add custom header fields
|
|
196
200
|
|
|
197
201
|
The ruby-jwt gem supports custom [header fields](https://tools.ietf.org/html/rfc7519#section-5).
|
|
@@ -219,13 +223,13 @@ The `JWT::Token` and `JWT::EncodedToken` classes can be used to manage your JWTs
|
|
|
219
223
|
```ruby
|
|
220
224
|
payload = { exp: Time.now.to_i + 60, jti: '1234', sub: 'my-subject' }
|
|
221
225
|
header = { kid: 'hmac' }
|
|
222
|
-
hmac_secret = 'my$ecretK3y'
|
|
226
|
+
hmac_secret = 'my$ecretK3y-at-least-32-bytes-long'
|
|
223
227
|
|
|
224
228
|
token = JWT::Token.new(payload: payload, header: header)
|
|
225
229
|
token.sign!(algorithm: 'HS256', key: hmac_secret)
|
|
226
230
|
|
|
227
231
|
token.jwt
|
|
228
|
-
# => "eyJraWQiOiJobWFjIiwiYWxnIjoiSFMyNTYifQ.eyJleHAiOjE3OTU4MDQzMDAsImp0aSI6IjEyMzQiLCJzdWIiOiJteS1zdWJqZWN0In0.
|
|
232
|
+
# => "eyJraWQiOiJobWFjIiwiYWxnIjoiSFMyNTYifQ.eyJleHAiOjE3OTU4MDQzMDAsImp0aSI6IjEyMzQiLCJzdWIiOiJteS1zdWJqZWN0In0.qn_cOo18Vt6n5hrlrfhj4zAoiZqUw4LaCk1xxMunRY0"
|
|
229
233
|
```
|
|
230
234
|
|
|
231
235
|
### Verifying and decoding a token
|
|
@@ -328,6 +332,68 @@ encoded_token.verify!(signature: { algorithm: 'HS256', key: hmac_secret })
|
|
|
328
332
|
encoded_token.payload # => {"data"=>"test"}
|
|
329
333
|
```
|
|
330
334
|
|
|
335
|
+
### Strict base64url
|
|
336
|
+
|
|
337
|
+
[RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-2) writes the parts of a token in base64url without padding. By default ruby-jwt also accepts `=` padding and the standard Base64 characters `+` and `/`, because some issuers, such as AWS Application Load Balancer, pad their tokens. To accept only unpadded base64url, create the `JWT::EncodedToken` with `strict_base64url: true`:
|
|
338
|
+
|
|
339
|
+
```ruby
|
|
340
|
+
token = JWT::Token.new(payload: { data: 'test' })
|
|
341
|
+
token.sign!(algorithm: 'HS256', key: hmac_secret)
|
|
342
|
+
padded_jwt = "#{token.jwt}="
|
|
343
|
+
|
|
344
|
+
JWT::EncodedToken.new(padded_jwt).verify_signature!(algorithm: 'HS256', key: hmac_secret) # accepted
|
|
345
|
+
JWT::EncodedToken.new(padded_jwt, strict_base64url: true).verify_signature!(algorithm: 'HS256', key: hmac_secret) # raises JWT::Base64DecodeError
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
This applies to the header and the payload, and to the signature when it is verified. JWK values and `x5c` certificates are decoded as before.
|
|
349
|
+
|
|
350
|
+
It doesn't make the token string unique, because an ECDSA signature can be written in a second valid form. To block or revoke tokens, use the `jti` claim instead of the token string.
|
|
351
|
+
|
|
352
|
+
## Nested JWT
|
|
353
|
+
|
|
354
|
+
A Nested JWT is a JWT that is used as the payload of another JWT, as defined in RFC 7519 Section 5.2. This API supports signed JWS layers.
|
|
355
|
+
|
|
356
|
+
### Creating a Nested JWT
|
|
357
|
+
|
|
358
|
+
```ruby
|
|
359
|
+
# Create the inner JWT
|
|
360
|
+
inner_payload = { user_id: 123, role: 'admin' }
|
|
361
|
+
inner_key = 'inner_secret'
|
|
362
|
+
inner_jwt = JWT.encode(inner_payload, inner_key, 'HS256')
|
|
363
|
+
|
|
364
|
+
# Wrap it in an outer JWT with a different key/algorithm
|
|
365
|
+
outer_key = OpenSSL::PKey::RSA.generate(2048)
|
|
366
|
+
nested = JWT::NestedToken.new(inner_jwt)
|
|
367
|
+
nested.sign!(algorithm: 'RS256', key: outer_key)
|
|
368
|
+
nested_jwt = nested.jwt
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
### Decoding a Nested JWT
|
|
372
|
+
|
|
373
|
+
```ruby
|
|
374
|
+
# Decode and verify all nesting levels
|
|
375
|
+
tokens = JWT::EncodedNestedToken.new(nested_jwt).verify!(
|
|
376
|
+
keys: [
|
|
377
|
+
{ algorithm: 'RS256', key: outer_key.public_key },
|
|
378
|
+
{ algorithm: 'HS256', key: inner_key }
|
|
379
|
+
]
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
inner_payload = tokens.last.payload
|
|
383
|
+
# => { 'user_id' => 123, 'role' => 'admin' }
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
### Checking for Nested JWTs
|
|
387
|
+
|
|
388
|
+
`inner_token` and `unwrap_all` return tokens for inspection. Use `verify!` to authenticate each layer and check the innermost claims. The default maximum depth is 10 layers, including the innermost JWT. Pass `max_depth:` to change this limit.
|
|
389
|
+
|
|
390
|
+
```ruby
|
|
391
|
+
token = JWT::EncodedNestedToken.new(nested_jwt)
|
|
392
|
+
token.nested? # => true
|
|
393
|
+
token.inner_token # => JWT::EncodedToken of the inner JWT
|
|
394
|
+
token.unwrap_all # => [outer_token, inner_token]
|
|
395
|
+
```
|
|
396
|
+
|
|
331
397
|
## Claims
|
|
332
398
|
|
|
333
399
|
JSON Web Token defines some reserved claim names and defines how they should be
|
|
@@ -615,6 +681,33 @@ You can specify claims that must be present for decoding to be successful. `JWT:
|
|
|
615
681
|
JWT.decode(token, hmac_secret, true, { required_claims: ['exp'], algorithm: 'HS256' })
|
|
616
682
|
```
|
|
617
683
|
|
|
684
|
+
### Critical header parameters
|
|
685
|
+
|
|
686
|
+
The `crit` header lists extensions that the recipient must understand. [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.11) says to reject a token whose `crit` lists an extension the recipient doesn't understand. Which extensions your application understands is up to you, so ruby-jwt only rejects unknown extensions when you give the `known_crit` option. `JWT::InvalidCritError` is then raised if `crit` lists anything other than those extensions, or isn't an array of strings. `b64` is always known, because ruby-jwt supports it.
|
|
687
|
+
|
|
688
|
+
```ruby
|
|
689
|
+
encoded_token = JWT::EncodedToken.new(token)
|
|
690
|
+
encoded_token.verify_signature!(algorithm: 'HS256', key: hmac_secret)
|
|
691
|
+
|
|
692
|
+
# Rejects every extension except b64
|
|
693
|
+
encoded_token.verify_claims!({ known_crit: [] }, :exp)
|
|
694
|
+
|
|
695
|
+
# Also accepts the extensions your application understands
|
|
696
|
+
encoded_token.verify_claims!({ known_crit: ['urn:example:my-extension'] }, :exp)
|
|
697
|
+
```
|
|
698
|
+
|
|
699
|
+
Claims are checked in the order you list them, so put `known_crit` first to check the `crit` header before the payload claims.
|
|
700
|
+
|
|
701
|
+
`JWT.decode` takes the same `known_crit` option and checks it before the payload claims:
|
|
702
|
+
|
|
703
|
+
```ruby
|
|
704
|
+
JWT.decode(token, hmac_secret, true, { algorithm: 'HS256', known_crit: [] })
|
|
705
|
+
```
|
|
706
|
+
|
|
707
|
+
In `JWT.decode`, leaving out `known_crit` or setting it to `nil` turns the check off. In `verify_claims!`, `known_crit: nil` is the same as `known_crit: []`.
|
|
708
|
+
|
|
709
|
+
`JWT::EncodedNestedToken#verify!` checks `known_crit` on every layer, not only on the innermost token.
|
|
710
|
+
|
|
618
711
|
### X.509 certificates in x5c header
|
|
619
712
|
|
|
620
713
|
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).
|
|
@@ -749,6 +842,15 @@ jwk_hash = jwk.export
|
|
|
749
842
|
thumbprint_as_the_kid = jwk_hash[:kid]
|
|
750
843
|
```
|
|
751
844
|
|
|
845
|
+
## Deprecation warnings
|
|
846
|
+
|
|
847
|
+
Keys and tokens that the next major version will reject still work, but print a deprecation warning. Examples are an HMAC secret shorter than the hash output, and an `alg` header in another case. [UPGRADING.md](UPGRADING.md) lists them all. By default each warning is printed once:
|
|
848
|
+
|
|
849
|
+
```ruby
|
|
850
|
+
JWT.configuration.deprecation_warnings = :warn # print every time
|
|
851
|
+
JWT.configuration.deprecation_warnings = :silent # never print
|
|
852
|
+
```
|
|
853
|
+
|
|
752
854
|
## Development and testing
|
|
753
855
|
|
|
754
856
|
The tests are written with rspec. [Appraisal](https://github.com/thoughtbot/appraisal) is used to ensure compatibility with 3rd party dependencies providing cryptographic features.
|
data/UPGRADING.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
# Upgrading ruby-jwt to >= 3.4.0
|
|
2
|
+
|
|
3
|
+
## Deprecation warnings for keys and tokens the next major version will reject
|
|
4
|
+
|
|
5
|
+
These still work, but now print a deprecation warning:
|
|
6
|
+
|
|
7
|
+
- **HMAC secrets shorter than the hash output,** when signing or verifying: 32 bytes for HS256, 48 for HS384 and 64 for HS512, as RFC 7518 requires. Replace the secret with a longer random one. To reject short secrets now, set `JWT.configuration.decode.enforce_hmac_key_length = true`. For a custom HMAC algorithm, such as `JWT::JWA::Hmac.new('HS1', OpenSSL::Digest::SHA1)`, the minimum is the length of its hash output. `enforce_hmac_key_length` now applies to it too, where it used to raise an `ArgumentError`.
|
|
8
|
+
- **RSA keys shorter than 2048 bits, when verifying RS* and PS* tokens.** Signing with them already raises. If the tokens come from another service, it has to move to a longer key.
|
|
9
|
+
- **PS256, PS384 and PS512 signatures whose salt is not as long as the hash.** RFC 7518 requires the hash length. ruby-jwt has always signed that way, so only tokens from other libraries can be affected.
|
|
10
|
+
- **An `alg` header that only matches when the case is ignored,** such as `hs256` for HS256. Algorithm names are case-sensitive (RFC 7515). The `algorithm` option of `JWT.decode` and `JWT.encode` can stay in any case, because only the token's header is checked.
|
|
11
|
+
|
|
12
|
+
When verifying, the warnings are printed only after the signature has verified, so a forged token doesn't print them. The exception is a token whose `alg` header is `none` in another case, such as `NONE`, when you allow the `none` algorithm.
|
|
13
|
+
|
|
14
|
+
By default each warning is printed once. `JWT.configuration.deprecation_warnings = :warn` prints them every time, and `:silent` turns them off.
|
|
15
|
+
|
|
1
16
|
# Upgrading ruby-jwt to >= 3.3.2
|
|
2
17
|
|
|
3
18
|
## Claim checks fail when the claim is missing
|
data/lib/jwt/base64.rb
CHANGED
|
@@ -14,8 +14,11 @@ module JWT
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
# Decode a string with URL-safe Base64 complying with RFC 4648.
|
|
17
|
+
# @param strict [Boolean] whether to reject padding and the `+` and `/` characters, as RFC 7515 requires.
|
|
17
18
|
# @api private
|
|
18
|
-
def url_decode(str)
|
|
19
|
+
def url_decode(str, strict: false)
|
|
20
|
+
raise Base64DecodeError, 'Invalid base64 encoding' if strict && !str.count('^A-Za-z0-9_-').zero?
|
|
21
|
+
|
|
19
22
|
::Base64.urlsafe_decode64(str)
|
|
20
23
|
rescue ArgumentError => e
|
|
21
24
|
raise unless e.message == 'invalid base64'
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
module Claims
|
|
5
|
+
# Rejects tokens whose crit header lists an extension the application does not understand.
|
|
6
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.11 RFC 7515 section 4.1.11
|
|
7
|
+
class KnownCrit
|
|
8
|
+
SUPPORTED_CRITS = ['b64'].freeze
|
|
9
|
+
private_constant(:SUPPORTED_CRITS)
|
|
10
|
+
|
|
11
|
+
# Initializes a new KnownCrit instance.
|
|
12
|
+
#
|
|
13
|
+
# @param known_crits [Array<String>] the extensions the application understands, in addition to `b64`.
|
|
14
|
+
def initialize(known_crits:)
|
|
15
|
+
@known_crits = Array(known_crits).map(&:to_s) | SUPPORTED_CRITS
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Verifies that every extension in the crit header is known.
|
|
19
|
+
#
|
|
20
|
+
# @param context [Object] the context containing the JWT header.
|
|
21
|
+
# @param _args [Hash] additional arguments (not used).
|
|
22
|
+
# @raise [JWT::InvalidCritError] if the crit header is not a non-empty array of strings or lists an unknown extension.
|
|
23
|
+
# @return [nil]
|
|
24
|
+
def verify!(context:, **_args)
|
|
25
|
+
return unless context.header.key?('crit')
|
|
26
|
+
|
|
27
|
+
crit = context.header['crit']
|
|
28
|
+
raise(JWT::InvalidCritError, 'Crit header should be a non-empty array of strings') unless crit.is_a?(Array) && !crit.empty? && crit.all?(String)
|
|
29
|
+
|
|
30
|
+
unknown = crit.uniq - known_crits
|
|
31
|
+
raise(JWT::InvalidCritError, "Crit header contains unknown extensions: #{unknown.join(', ')}") unless unknown.empty?
|
|
32
|
+
|
|
33
|
+
nil
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
attr_reader :known_crits
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/jwt/claims/verifier.rb
CHANGED
|
@@ -13,6 +13,7 @@ module JWT
|
|
|
13
13
|
aud: ->(options) { Claims::Audience.new(expected_audience: options[:aud]) },
|
|
14
14
|
sub: ->(options) { Claims::Subject.new(expected_subject: options[:sub]) },
|
|
15
15
|
crit: ->(options) { Claims::Crit.new(expected_crits: options[:crit]) },
|
|
16
|
+
known_crit: ->(options) { Claims::KnownCrit.new(known_crits: options[:known_crit]) },
|
|
16
17
|
required: ->(options) { Claims::Required.new(required_claims: options[:required]) },
|
|
17
18
|
numeric: ->(*) { Claims::Numeric.new }
|
|
18
19
|
}.freeze
|
data/lib/jwt/claims.rb
CHANGED
|
@@ -7,6 +7,7 @@ require_relative 'claims/expiration'
|
|
|
7
7
|
require_relative 'claims/issued_at'
|
|
8
8
|
require_relative 'claims/issuer'
|
|
9
9
|
require_relative 'claims/jwt_id'
|
|
10
|
+
require_relative 'claims/known_crit'
|
|
10
11
|
require_relative 'claims/not_before'
|
|
11
12
|
require_relative 'claims/numeric'
|
|
12
13
|
require_relative 'claims/required'
|
data/lib/jwt/decode.rb
CHANGED
|
@@ -22,7 +22,7 @@ module JWT
|
|
|
22
22
|
def initialize(jwt, key, verify, options, &keyfinder)
|
|
23
23
|
raise JWT::MalformedTokenError, 'Nil JSON web token' unless jwt
|
|
24
24
|
|
|
25
|
-
@token = EncodedToken.new(jwt)
|
|
25
|
+
@token = EncodedToken.new(jwt, strict_base64url: options[:strict_base64url] || options['strict_base64url'] || false)
|
|
26
26
|
@key = key
|
|
27
27
|
@options = options
|
|
28
28
|
@verify = verify
|
|
@@ -38,6 +38,7 @@ module JWT
|
|
|
38
38
|
verify_algo
|
|
39
39
|
set_key
|
|
40
40
|
verify_signature
|
|
41
|
+
verify_known_crit
|
|
41
42
|
Claims::DecodeVerifier.verify!(token.unverified_payload, @options)
|
|
42
43
|
end
|
|
43
44
|
|
|
@@ -56,6 +57,11 @@ module JWT
|
|
|
56
57
|
token.verify_signature!(algorithm: allowed_and_valid_algorithms, key: @key)
|
|
57
58
|
end
|
|
58
59
|
|
|
60
|
+
def verify_known_crit
|
|
61
|
+
known_crit = @options[:known_crit] || @options['known_crit']
|
|
62
|
+
token.verify_claims!(known_crit: known_crit) if known_crit
|
|
63
|
+
end
|
|
64
|
+
|
|
59
65
|
def verify_algo
|
|
60
66
|
raise JWT::IncorrectAlgorithm, 'An algorithm must be specified' if allowed_algorithms.empty?
|
|
61
67
|
raise JWT::IncorrectAlgorithm, 'Token is missing alg header' unless alg_in_header
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
# Prints deprecation warnings as set by `JWT.configuration.deprecation_warnings`.
|
|
5
|
+
# @api private
|
|
6
|
+
module Deprecations
|
|
7
|
+
@warned = {}
|
|
8
|
+
@mutex = Mutex.new
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
# @param message [String] the warning, printed with a `[DEPRECATION WARNING]` prefix.
|
|
12
|
+
# @return [nil]
|
|
13
|
+
def warning(message)
|
|
14
|
+
case JWT.configuration.deprecation_warnings
|
|
15
|
+
when :warn then emit(message)
|
|
16
|
+
when :once then emit(message) if first_time?(message)
|
|
17
|
+
end
|
|
18
|
+
nil
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def emit(message)
|
|
24
|
+
Kernel.warn("[DEPRECATION WARNING] #{message}")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def first_time?(message)
|
|
28
|
+
@mutex.synchronize do
|
|
29
|
+
next false if @warned.key?(message)
|
|
30
|
+
|
|
31
|
+
@warned[message] = true
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
# Inspects and verifies an encoded JWT one layer at a time, outermost first.
|
|
5
|
+
class EncodedNestedToken
|
|
6
|
+
MAX_DEPTH = 10
|
|
7
|
+
LAYER_COUNT_MISMATCH = 'JWT layer count does not match the number of key configurations'
|
|
8
|
+
private_constant :MAX_DEPTH, :LAYER_COUNT_MISMATCH
|
|
9
|
+
|
|
10
|
+
# @return [Array<JWT::EncodedToken>, nil] layers after successful signature verification.
|
|
11
|
+
attr_reader :tokens
|
|
12
|
+
|
|
13
|
+
def initialize(jwt)
|
|
14
|
+
@outer = EncodedToken.new(jwt)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def nested?
|
|
18
|
+
nested_layer?(@outer)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def inner_token
|
|
22
|
+
decode_inner(@outer) if nested?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Returns the layers for inspection without verifying their signatures or claims.
|
|
26
|
+
def unwrap_all(max_depth: MAX_DEPTH)
|
|
27
|
+
validate_depth!(max_depth)
|
|
28
|
+
layers = [@outer]
|
|
29
|
+
layers << next_layer(layers, max_depth) while nested_layer?(layers.last)
|
|
30
|
+
layers
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Verifies signatures of all nested levels and the claims of the innermost token.
|
|
34
|
+
#
|
|
35
|
+
# @param max_depth [Integer] maximum number of layers, including the innermost JWT.
|
|
36
|
+
# @param keys [Array<Hash>] key configuration per nesting level (outermost to innermost).
|
|
37
|
+
# @param claims [Array<Symbol>, Hash, nil] claim verification options for the innermost token.
|
|
38
|
+
# The `known_crit` option is checked on every layer, because each layer has its own crit header.
|
|
39
|
+
# @return [Array<JWT::EncodedToken>] verified tokens from outermost to innermost.
|
|
40
|
+
def verify!(keys:, claims: nil, max_depth: MAX_DEPTH)
|
|
41
|
+
verify_signatures!(keys: keys, max_depth: max_depth)
|
|
42
|
+
verify_claims!(claims: claims)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Verifies signatures of all nested levels.
|
|
46
|
+
#
|
|
47
|
+
# @param max_depth [Integer] maximum number of layers, including the innermost JWT.
|
|
48
|
+
# @param keys [Array<Hash>] key configuration per nesting level (outermost to innermost).
|
|
49
|
+
# @return [Array<JWT::EncodedToken>] verified tokens from outermost to innermost.
|
|
50
|
+
def verify_signatures!(keys:, max_depth: MAX_DEPTH)
|
|
51
|
+
@tokens = nil
|
|
52
|
+
raise ArgumentError, 'keys must be an Array of per-layer configurations' unless keys.is_a?(Array)
|
|
53
|
+
raise JWT::MalformedTokenError, LAYER_COUNT_MISMATCH if keys.empty?
|
|
54
|
+
|
|
55
|
+
validate_depth!(max_depth)
|
|
56
|
+
layers = [@outer]
|
|
57
|
+
keys.each do |config|
|
|
58
|
+
token = layers.last
|
|
59
|
+
raise ArgumentError, 'Each key configuration must be a Hash' unless config.is_a?(Hash)
|
|
60
|
+
|
|
61
|
+
verify_layer!(token, **config)
|
|
62
|
+
nested = nested_layer?(token)
|
|
63
|
+
raise JWT::MalformedTokenError, LAYER_COUNT_MISMATCH unless nested == (layers.size < keys.size)
|
|
64
|
+
|
|
65
|
+
layers << next_layer(layers, max_depth) if nested
|
|
66
|
+
end
|
|
67
|
+
@tokens = layers
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Verifies claims of the innermost token after signatures have been verified.
|
|
71
|
+
#
|
|
72
|
+
# @param claims [Array<Symbol>, Hash, nil] claim verification options for the innermost token.
|
|
73
|
+
# The `known_crit` option is checked on every layer, because each layer has its own crit header.
|
|
74
|
+
# @return [Array<JWT::EncodedToken>] verified tokens from outermost to innermost.
|
|
75
|
+
def verify_claims!(claims: nil)
|
|
76
|
+
raise JWT::TokenError, 'Verify nested token signatures before verifying claims' unless tokens
|
|
77
|
+
|
|
78
|
+
verify_outer_crit!(claims)
|
|
79
|
+
innermost_token = tokens.last
|
|
80
|
+
claims.is_a?(Array) ? innermost_token.verify_claims!(*claims) : innermost_token.verify_claims!(claims)
|
|
81
|
+
tokens
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def verify_layer!(token, algorithm:, **options)
|
|
87
|
+
unsigned = Array(algorithm).any? { |alg| JWA.resolve(alg).is_a?(JWA::None) }
|
|
88
|
+
raise JWT::IncorrectAlgorithm, 'Nested JWT verification requires a signing algorithm' if unsigned
|
|
89
|
+
|
|
90
|
+
token.verify_signature!(algorithm: algorithm, **options)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Like a failed claim check on a single token, a failed check on an outer layer
|
|
94
|
+
# leaves the innermost token's claims unverified.
|
|
95
|
+
def verify_outer_crit!(claims)
|
|
96
|
+
known_crits = known_crit_options(claims).map do |option|
|
|
97
|
+
Claims::KnownCrit.new(known_crits: option.is_a?(Hash) ? option[:known_crit] : nil)
|
|
98
|
+
end
|
|
99
|
+
tokens[0...-1].each do |token|
|
|
100
|
+
known_crits.each { |known_crit| known_crit.verify!(context: token) }
|
|
101
|
+
end
|
|
102
|
+
rescue StandardError
|
|
103
|
+
tokens.last.reset_claims_verification!
|
|
104
|
+
raise
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def known_crit_options(claims)
|
|
108
|
+
(claims.is_a?(Array) ? claims : [claims]).select do |option|
|
|
109
|
+
option == :known_crit || (option.is_a?(Hash) && option.key?(:known_crit))
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def validate_depth!(max_depth)
|
|
114
|
+
raise ArgumentError, 'max_depth must be a positive Integer' unless max_depth.is_a?(Integer) && max_depth.positive?
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def next_layer(layers, max_depth)
|
|
118
|
+
raise JWT::MalformedTokenError, "Nested JWT exceeds maximum depth of #{max_depth}" if layers.size >= max_depth
|
|
119
|
+
|
|
120
|
+
decode_inner(layers.last)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def nested_layer?(token)
|
|
124
|
+
cty = token.header['cty']
|
|
125
|
+
cty.is_a?(String) && (cty.casecmp('JWT')&.zero? || cty.casecmp('application/jwt')&.zero?)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def decode_inner(token)
|
|
129
|
+
payload = token.encoded_payload
|
|
130
|
+
raise JWT::MalformedTokenError, 'Encoded payload is empty' if payload.nil? || payload.empty?
|
|
131
|
+
raise JWT::MalformedTokenError, 'Nested JWT payload must be base64url-encoded' if token.header['b64'] == false
|
|
132
|
+
|
|
133
|
+
EncodedToken.new(::JWT::Base64.url_decode(payload))
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
class EncodedToken
|
|
5
|
+
# @private
|
|
6
|
+
# Handles segment decoding and parsing.
|
|
7
|
+
class SegmentParser
|
|
8
|
+
def initialize(strict_base64url: false)
|
|
9
|
+
@strict_base64url = strict_base64url
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def decode(segment)
|
|
13
|
+
::JWT::Base64.url_decode(segment || '', strict: @strict_base64url)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def parse_and_decode(segment)
|
|
17
|
+
parse(decode(segment))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def parse_unencoded(segment)
|
|
21
|
+
parse(segment)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def parse(segment)
|
|
25
|
+
JWT::JSON.parse(segment)
|
|
26
|
+
rescue ::JSON::ParserError
|
|
27
|
+
raise JWT::MalformedTokenError, 'Invalid segment encoding'
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
class EncodedToken
|
|
5
|
+
# @private
|
|
6
|
+
# Handles signature verification logic.
|
|
7
|
+
class SignatureVerifier
|
|
8
|
+
def initialize(token)
|
|
9
|
+
@token = token
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def verify(algorithm:, key: nil, key_finder: nil)
|
|
13
|
+
validate_segment_count!
|
|
14
|
+
return false if key_finder && !algorithm_allowed?(algorithm)
|
|
15
|
+
|
|
16
|
+
keys = Array(key || key_finder.call(@token))
|
|
17
|
+
verifiers = JWA.create_verifiers(algorithms: algorithm, keys: keys, preferred_algorithm: @token.header['alg'])
|
|
18
|
+
|
|
19
|
+
raise JWT::VerificationError, 'No algorithm provided' if verifiers.empty?
|
|
20
|
+
|
|
21
|
+
verified_by_any?(verifiers)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def validate_segment_count!
|
|
27
|
+
raise JWT::MalformedTokenError, 'Too many segments' if @token.jwt.count('.') > 2
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# An empty list leaves the choice of algorithm to the JWKs given by the key finder.
|
|
31
|
+
# An unknown algorithm is a configuration error that the verifier reports.
|
|
32
|
+
def algorithm_allowed?(algorithms)
|
|
33
|
+
algorithms = Array(algorithms).map { |algorithm| JWA.resolve(algorithm) }
|
|
34
|
+
|
|
35
|
+
algorithms.empty? || algorithms.any? { |algorithm| algorithm == JWA::Unsupported || algorithm.valid_alg?(@token.header['alg']) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def verified_by_any?(verifiers)
|
|
39
|
+
verifier = verifiers.find { |jwa| jwa.verify(data: @token.signing_input, signature: @token.signature) }
|
|
40
|
+
warn_about_alg_case(verifier.jwa) if verifier
|
|
41
|
+
!verifier.nil?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def warn_about_alg_case(jwa)
|
|
45
|
+
alg = @token.header['alg']
|
|
46
|
+
return unless alg.is_a?(String) && jwa.alg.is_a?(String) && jwa.alg != alg && jwa.alg.casecmp(alg)&.zero?
|
|
47
|
+
|
|
48
|
+
Deprecations.warning("The alg header #{alg} only matches #{jwa.alg} when the case is ignored. Algorithm names are case-sensitive, " \
|
|
49
|
+
'and such tokens will be rejected in the next major version of ruby-jwt.')
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/jwt/encoded_token.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'encoded_token/claims_context'
|
|
4
|
+
require_relative 'encoded_token/segment_parser'
|
|
5
|
+
require_relative 'encoded_token/signature_verifier'
|
|
4
6
|
|
|
5
7
|
module JWT
|
|
6
8
|
# Represents an encoded JWT token
|
|
@@ -25,11 +27,14 @@ module JWT
|
|
|
25
27
|
# Initializes a new EncodedToken instance.
|
|
26
28
|
#
|
|
27
29
|
# @param jwt [String] the encoded JWT token.
|
|
30
|
+
# @param strict_base64url [Boolean] whether to reject a header, payload or signature that isn't unpadded base64url, as RFC 7515 requires.
|
|
31
|
+
# By default `=` padding and the `+` and `/` characters are accepted.
|
|
28
32
|
# @raise [ArgumentError] if the provided JWT is not a String.
|
|
29
|
-
def initialize(jwt)
|
|
33
|
+
def initialize(jwt, strict_base64url: false)
|
|
30
34
|
raise ArgumentError, 'Provided JWT must be a String' unless jwt.is_a?(String)
|
|
31
35
|
|
|
32
36
|
@jwt = jwt
|
|
37
|
+
@strict_base64url = strict_base64url
|
|
33
38
|
@signature_verified = false
|
|
34
39
|
@claims_verified = false
|
|
35
40
|
|
|
@@ -40,7 +45,7 @@ module JWT
|
|
|
40
45
|
#
|
|
41
46
|
# @return [String] the decoded signature.
|
|
42
47
|
def signature
|
|
43
|
-
@signature ||=
|
|
48
|
+
@signature ||= parser.decode(encoded_signature)
|
|
44
49
|
end
|
|
45
50
|
|
|
46
51
|
# Returns the encoded signature of the JWT token.
|
|
@@ -53,7 +58,7 @@ module JWT
|
|
|
53
58
|
# @return [Hash] the header.
|
|
54
59
|
# @raise [JWT::MalformedTokenError] if the header is not a JSON object.
|
|
55
60
|
def header
|
|
56
|
-
@header ||= parse_and_decode(@encoded_header).tap do |decoded_header|
|
|
61
|
+
@header ||= parser.parse_and_decode(@encoded_header).tap do |decoded_header|
|
|
57
62
|
raise JWT::MalformedTokenError, 'Token header not a JSON object' unless decoded_header.is_a?(Hash)
|
|
58
63
|
end
|
|
59
64
|
end
|
|
@@ -159,18 +164,7 @@ module JWT
|
|
|
159
164
|
raise ArgumentError, 'Provide either key or key_finder, not both or neither' if key.nil? == key_finder.nil?
|
|
160
165
|
|
|
161
166
|
@signature_verified = false
|
|
162
|
-
|
|
163
|
-
return false if key_finder && !algorithm_allowed?(algorithm)
|
|
164
|
-
|
|
165
|
-
keys = Array(key || key_finder.call(self))
|
|
166
|
-
verifiers = JWA.create_verifiers(algorithms: algorithm, keys: keys, preferred_algorithm: header['alg'])
|
|
167
|
-
|
|
168
|
-
raise JWT::VerificationError, 'No algorithm provided' if verifiers.empty?
|
|
169
|
-
|
|
170
|
-
valid = verifiers.any? do |jwa|
|
|
171
|
-
jwa.verify(data: signing_input, signature: signature)
|
|
172
|
-
end
|
|
173
|
-
valid.tap { |verified| @signature_verified = verified }
|
|
167
|
+
@signature_verified = SignatureVerifier.new(self).verify(algorithm: algorithm, key: key, key_finder: key_finder)
|
|
174
168
|
end
|
|
175
169
|
|
|
176
170
|
# Verifies the claims of the token.
|
|
@@ -200,21 +194,15 @@ module JWT
|
|
|
200
194
|
claim_errors(*claims_options(options)).empty?.tap { |verified| @claims_verified = verified }
|
|
201
195
|
end
|
|
202
196
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
def validate_segment_count!
|
|
208
|
-
raise JWT::MalformedTokenError, 'Too many segments' if jwt.count('.') > 2
|
|
197
|
+
# Marks the claims as not verified, so they have to be verified again before the payload can be read.
|
|
198
|
+
# @api private
|
|
199
|
+
def reset_claims_verification!
|
|
200
|
+
@claims_verified = false
|
|
209
201
|
end
|
|
210
202
|
|
|
211
|
-
|
|
212
|
-
# An unknown algorithm is a configuration error that the verifier reports.
|
|
213
|
-
def algorithm_allowed?(algorithms)
|
|
214
|
-
algorithms = Array(algorithms).map { |algorithm| JWA.resolve(algorithm) }
|
|
203
|
+
alias to_s jwt
|
|
215
204
|
|
|
216
|
-
|
|
217
|
-
end
|
|
205
|
+
private
|
|
218
206
|
|
|
219
207
|
def claims_options(options)
|
|
220
208
|
return DEFAULT_CLAIMS if options.first.nil?
|
|
@@ -227,24 +215,18 @@ module JWT
|
|
|
227
215
|
|
|
228
216
|
if unencoded_payload?
|
|
229
217
|
Claims::Verifier.verify!(ClaimsContext.new(self), crit: ['b64'])
|
|
230
|
-
return
|
|
218
|
+
return parser.parse_unencoded(encoded_payload)
|
|
231
219
|
end
|
|
232
220
|
|
|
233
|
-
parse_and_decode(encoded_payload)
|
|
221
|
+
parser.parse_and_decode(encoded_payload)
|
|
234
222
|
end
|
|
235
223
|
|
|
236
224
|
def unencoded_payload?
|
|
237
225
|
header['b64'] == false
|
|
238
226
|
end
|
|
239
227
|
|
|
240
|
-
def
|
|
241
|
-
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
def parse(segment)
|
|
245
|
-
JWT::JSON.parse(segment)
|
|
246
|
-
rescue ::JSON::ParserError
|
|
247
|
-
raise JWT::MalformedTokenError, 'Invalid segment encoding'
|
|
228
|
+
def parser
|
|
229
|
+
@parser ||= SegmentParser.new(strict_base64url: @strict_base64url)
|
|
248
230
|
end
|
|
249
231
|
|
|
250
232
|
def decoded_payload
|
data/lib/jwt/jwa/ecdsa.rb
CHANGED
|
@@ -29,6 +29,7 @@ module JWT
|
|
|
29
29
|
curve_definition = curve_by_name(verification_key.group.curve_name)
|
|
30
30
|
key_algorithm = curve_definition[:algorithm]
|
|
31
31
|
raise IncorrectAlgorithm, "payload algorithm is #{alg} but #{key_algorithm} verification key was provided" if alg != key_algorithm
|
|
32
|
+
return false unless signature.bytesize == 2 * coordinate_size(verification_key)
|
|
32
33
|
|
|
33
34
|
verification_key.dsa_verify_asn1(OpenSSL::Digest.new(digest).digest(data), raw_to_asn1(signature, verification_key))
|
|
34
35
|
rescue OpenSSL::PKey::PKeyError
|
|
@@ -100,8 +101,13 @@ module JWT
|
|
|
100
101
|
NAMED_CURVES.fetch(curve_name) { raise_sign_error!("The ECDSA curve '#{curve_name}' is not supported") }[:algorithm]
|
|
101
102
|
end
|
|
102
103
|
|
|
104
|
+
# RFC 7518 section 3.4: the signature is R and S, each this many octets.
|
|
105
|
+
def coordinate_size(key)
|
|
106
|
+
(key.group.degree + 7) / 8
|
|
107
|
+
end
|
|
108
|
+
|
|
103
109
|
def raw_to_asn1(signature, private_key)
|
|
104
|
-
byte_size = (private_key
|
|
110
|
+
byte_size = coordinate_size(private_key)
|
|
105
111
|
sig_bytes = signature[0..(byte_size - 1)]
|
|
106
112
|
sig_char = signature[byte_size..-1] || ''
|
|
107
113
|
OpenSSL::ASN1::Sequence.new([sig_bytes, sig_char].map { |int| OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(int, 2)) }).to_der
|
data/lib/jwt/jwa/hmac.rb
CHANGED
|
@@ -26,6 +26,7 @@ module JWT
|
|
|
26
26
|
|
|
27
27
|
def sign(data:, signing_key:)
|
|
28
28
|
validate_key!(signing_key) { |message| raise_sign_error!(message) }
|
|
29
|
+
warn_about_short_key(signing_key)
|
|
29
30
|
|
|
30
31
|
OpenSSL::HMAC.digest(digest.new, signing_key, data)
|
|
31
32
|
end
|
|
@@ -33,7 +34,9 @@ module JWT
|
|
|
33
34
|
def verify(data:, signature:, verification_key:)
|
|
34
35
|
validate_key!(verification_key) { |message| raise_verify_error!(message) }
|
|
35
36
|
|
|
36
|
-
SecurityUtils.secure_compare(signature, OpenSSL::HMAC.digest(digest.new, verification_key, data))
|
|
37
|
+
SecurityUtils.secure_compare(signature, OpenSSL::HMAC.digest(digest.new, verification_key, data)).tap do |valid|
|
|
38
|
+
warn_about_short_key(verification_key) if valid
|
|
39
|
+
end
|
|
37
40
|
end
|
|
38
41
|
|
|
39
42
|
register_algorithm(new('HS256', OpenSSL::Digest::SHA256))
|
|
@@ -51,10 +54,22 @@ module JWT
|
|
|
51
54
|
yield 'HMAC key cannot be empty' if key.empty?
|
|
52
55
|
yield 'HMAC key cannot be a public key or certificate' if public_key_material?(key)
|
|
53
56
|
|
|
54
|
-
|
|
57
|
+
yield "HMAC key must be at least #{min_key_length} bytes for #{alg} algorithm" if JWT.configuration.decode.enforce_hmac_key_length && short_key?(key)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def warn_about_short_key(key)
|
|
61
|
+
return unless short_key?(key)
|
|
62
|
+
|
|
63
|
+
Deprecations.warning("HMAC keys shorter than #{min_key_length} bytes for #{alg} are deprecated and will be rejected in the next " \
|
|
64
|
+
'major version of ruby-jwt. Set JWT.configuration.decode.enforce_hmac_key_length = true to reject them now.')
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def short_key?(key)
|
|
68
|
+
key.bytesize < min_key_length
|
|
69
|
+
end
|
|
55
70
|
|
|
56
|
-
|
|
57
|
-
|
|
71
|
+
def min_key_length
|
|
72
|
+
MIN_KEY_LENGTHS.fetch(alg) { digest.new.digest_length }
|
|
58
73
|
end
|
|
59
74
|
|
|
60
75
|
def public_key_material?(key)
|
data/lib/jwt/jwa/ps.rb
CHANGED
|
@@ -6,6 +6,9 @@ module JWT
|
|
|
6
6
|
class Ps
|
|
7
7
|
include JWT::JWA::SigningAlgorithm
|
|
8
8
|
|
|
9
|
+
SHORT_KEY_WARNING = 'Verifying with RSA keys shorter than 2048 bits is deprecated and will be rejected in the next major version of ruby-jwt.'
|
|
10
|
+
private_constant :SHORT_KEY_WARNING
|
|
11
|
+
|
|
9
12
|
def initialize(alg)
|
|
10
13
|
@alg = alg
|
|
11
14
|
@digest_algorithm = alg.sub('PS', 'sha')
|
|
@@ -21,8 +24,12 @@ module JWT
|
|
|
21
24
|
|
|
22
25
|
def verify(data:, signature:, verification_key:)
|
|
23
26
|
raise_verify_error!("The given key is a #{verification_key.class}. It has to be an OpenSSL::PKey::RSA instance") unless verification_key.is_a?(::OpenSSL::PKey::RSA)
|
|
27
|
+
# RFC 8017 section 8.1.2: the signature is exactly as long as the modulus.
|
|
28
|
+
return false unless signature.bytesize == verification_key.n.num_bytes
|
|
24
29
|
|
|
25
|
-
|
|
30
|
+
verify_pss(verification_key, signature, data).tap do |valid|
|
|
31
|
+
Deprecations.warning(SHORT_KEY_WARNING) if valid && verification_key.n.num_bits < 2048
|
|
32
|
+
end
|
|
26
33
|
rescue OpenSSL::PKey::PKeyError
|
|
27
34
|
raise JWT::VerificationError, 'Signature verification raised'
|
|
28
35
|
end
|
|
@@ -34,6 +41,15 @@ module JWT
|
|
|
34
41
|
private
|
|
35
42
|
|
|
36
43
|
attr_reader :digest_algorithm
|
|
44
|
+
|
|
45
|
+
def verify_pss(verification_key, signature, data)
|
|
46
|
+
return true if verification_key.verify_pss(digest_algorithm, signature, data, salt_length: :digest, mgf1_hash: digest_algorithm)
|
|
47
|
+
return false unless verification_key.verify_pss(digest_algorithm, signature, data, salt_length: :auto, mgf1_hash: digest_algorithm)
|
|
48
|
+
|
|
49
|
+
Deprecations.warning("#{alg} signatures with a salt that is not as long as the hash are deprecated and will be rejected in the next " \
|
|
50
|
+
'major version of ruby-jwt.')
|
|
51
|
+
true
|
|
52
|
+
end
|
|
37
53
|
end
|
|
38
54
|
end
|
|
39
55
|
end
|
data/lib/jwt/jwa/rsa.rb
CHANGED
|
@@ -6,6 +6,9 @@ module JWT
|
|
|
6
6
|
class Rsa
|
|
7
7
|
include JWT::JWA::SigningAlgorithm
|
|
8
8
|
|
|
9
|
+
SHORT_KEY_WARNING = 'Verifying with RSA keys shorter than 2048 bits is deprecated and will be rejected in the next major version of ruby-jwt.'
|
|
10
|
+
private_constant :SHORT_KEY_WARNING
|
|
11
|
+
|
|
9
12
|
def initialize(alg)
|
|
10
13
|
@alg = alg
|
|
11
14
|
@digest = alg.sub('RS', 'SHA')
|
|
@@ -21,8 +24,12 @@ module JWT
|
|
|
21
24
|
|
|
22
25
|
def verify(data:, signature:, verification_key:)
|
|
23
26
|
raise_verify_error!("The given key is a #{verification_key.class}. It has to be an OpenSSL::PKey::RSA instance") unless verification_key.is_a?(::OpenSSL::PKey::RSA)
|
|
27
|
+
# RFC 8017 section 8.2.2: the signature is exactly as long as the modulus.
|
|
28
|
+
return false unless signature.bytesize == verification_key.n.num_bytes
|
|
24
29
|
|
|
25
|
-
verification_key.verify(OpenSSL::Digest.new(digest), signature, data)
|
|
30
|
+
verification_key.verify(OpenSSL::Digest.new(digest), signature, data).tap do |valid|
|
|
31
|
+
Deprecations.warning(SHORT_KEY_WARNING) if valid && verification_key.n.num_bits < 2048
|
|
32
|
+
end
|
|
26
33
|
rescue OpenSSL::PKey::PKeyError
|
|
27
34
|
raise JWT::VerificationError, 'Signature verification raised'
|
|
28
35
|
end
|
data/lib/jwt/jwa.rb
CHANGED
|
@@ -48,27 +48,34 @@ module JWT
|
|
|
48
48
|
# @api private
|
|
49
49
|
def create_verifiers(algorithms:, keys:, preferred_algorithm:)
|
|
50
50
|
jwks, other_keys = keys.partition { |key| key.is_a?(JWK::KeyBase) }
|
|
51
|
+
usable_jwks = jwks.select { |jwk| jwk_supports_algorithms?(jwk, algorithms) }
|
|
51
52
|
|
|
52
|
-
validate_jwk_algorithms!(jwks, algorithms, VerificationKeyError)
|
|
53
|
+
validate_jwk_algorithms!(jwks, algorithms, VerificationKeyError) if usable_jwks.empty? && other_keys.empty?
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
usable_jwks + resolve_and_sort(algorithms: algorithms,
|
|
56
|
+
preferred_algorithm: preferred_algorithm)
|
|
57
|
+
.map { |jwa| VerifierContext.new(jwa: jwa, keys: other_keys) }
|
|
57
58
|
end
|
|
58
59
|
|
|
59
60
|
# @api private
|
|
60
61
|
def validate_jwk_algorithms!(jwks, algorithms, error_class)
|
|
61
|
-
|
|
62
|
+
return if Array(jwks).all? { |jwk| jwk_supports_algorithms?(jwk, algorithms) }
|
|
62
63
|
|
|
63
|
-
|
|
64
|
+
labels = Array(algorithms).map { |alg| (alg.alg if alg.is_a?(SigningAlgorithm)) || alg.to_s }
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
raise error_class, "Provided JWKs do not support one of the specified algorithms: #{labels.join(', ')}"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @api private
|
|
70
|
+
def jwk_supports_algorithms?(jwk, algorithms)
|
|
71
|
+
names = algorithm_names(algorithms)
|
|
70
72
|
|
|
71
|
-
|
|
73
|
+
names.empty? || names.any? { |name| jwk.jwa.valid_alg?(name) }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# @api private
|
|
77
|
+
def algorithm_names(algorithms)
|
|
78
|
+
Array(algorithms).map { |alg| alg.is_a?(SigningAlgorithm) ? alg.alg : alg.to_s }
|
|
72
79
|
end
|
|
73
80
|
end
|
|
74
81
|
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JWT
|
|
4
|
+
# Represents a Nested JWT as defined in RFC 7519 Section 5.2, Section 7.1 Step 5, and Appendix A.2.
|
|
5
|
+
#
|
|
6
|
+
# A Nested JWT wraps an existing JWT string as the payload of another signed JWT.
|
|
7
|
+
#
|
|
8
|
+
# @example Creating a Nested JWT
|
|
9
|
+
# inner_jwt = JWT.encode({ user_id: 123 }, 'inner_secret', 'HS256')
|
|
10
|
+
# nested = JWT::NestedToken.new(inner_jwt)
|
|
11
|
+
# nested.sign!(algorithm: 'RS256', key: rsa_private_key)
|
|
12
|
+
# nested.jwt
|
|
13
|
+
#
|
|
14
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7519#section-5.2 RFC 7519 Section 5.2
|
|
15
|
+
class NestedToken < Token
|
|
16
|
+
# @param jwt [String] the compact JWT to wrap.
|
|
17
|
+
def initialize(jwt)
|
|
18
|
+
raise ArgumentError, 'Provided JWT must be a String' unless jwt.is_a?(String)
|
|
19
|
+
|
|
20
|
+
super(payload: jwt, header: { 'cty' => 'JWT' })
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The payload of an outer JWS is the compact inner JWT itself.
|
|
24
|
+
def encoded_payload
|
|
25
|
+
@encoded_payload ||= ::JWT::Base64.url_encode(payload)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def sign!(algorithm:, key:, header: {})
|
|
29
|
+
original_header = @header
|
|
30
|
+
unless @signature
|
|
31
|
+
@header = original_header.merge((header || {}).transform_keys(&:to_s)).merge('cty' => 'JWT')
|
|
32
|
+
raise JWT::EncodeError, 'Nested JWT payload must be base64url-encoded' if @header['b64'] == false
|
|
33
|
+
|
|
34
|
+
@encoded_header = @signing_input = nil
|
|
35
|
+
end
|
|
36
|
+
super(algorithm: algorithm, key: key)
|
|
37
|
+
ensure
|
|
38
|
+
unless @signature
|
|
39
|
+
@header = original_header
|
|
40
|
+
@encoded_header = @signing_input = nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/jwt/version.rb
CHANGED
|
@@ -15,8 +15,8 @@ module JWT
|
|
|
15
15
|
# Version constants
|
|
16
16
|
module VERSION
|
|
17
17
|
MAJOR = 3 # x-release-please-major
|
|
18
|
-
MINOR =
|
|
19
|
-
TINY =
|
|
18
|
+
MINOR = 4 # x-release-please-minor
|
|
19
|
+
TINY = 0 # x-release-please-patch
|
|
20
20
|
PRE = nil
|
|
21
21
|
|
|
22
22
|
STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
|
data/lib/jwt.rb
CHANGED
|
@@ -5,12 +5,15 @@ require 'jwt/base64'
|
|
|
5
5
|
require 'jwt/json'
|
|
6
6
|
require 'jwt/decode'
|
|
7
7
|
require 'jwt/configuration'
|
|
8
|
+
require 'jwt/deprecations'
|
|
8
9
|
require 'jwt/encode'
|
|
9
10
|
require 'jwt/error'
|
|
10
11
|
require 'jwt/jwk'
|
|
11
12
|
require 'jwt/claims'
|
|
12
13
|
require 'jwt/encoded_token'
|
|
13
14
|
require 'jwt/token'
|
|
15
|
+
require 'jwt/nested_token'
|
|
16
|
+
require 'jwt/encoded_nested_token'
|
|
14
17
|
|
|
15
18
|
# JSON Web Token implementation
|
|
16
19
|
#
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: jwt
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tim Rudat
|
|
@@ -159,6 +159,7 @@ files:
|
|
|
159
159
|
- lib/jwt/claims/issued_at.rb
|
|
160
160
|
- lib/jwt/claims/issuer.rb
|
|
161
161
|
- lib/jwt/claims/jwt_id.rb
|
|
162
|
+
- lib/jwt/claims/known_crit.rb
|
|
162
163
|
- lib/jwt/claims/not_before.rb
|
|
163
164
|
- lib/jwt/claims/numeric.rb
|
|
164
165
|
- lib/jwt/claims/required.rb
|
|
@@ -169,9 +170,13 @@ files:
|
|
|
169
170
|
- lib/jwt/configuration/decode_configuration.rb
|
|
170
171
|
- lib/jwt/configuration/jwk_configuration.rb
|
|
171
172
|
- lib/jwt/decode.rb
|
|
173
|
+
- lib/jwt/deprecations.rb
|
|
172
174
|
- lib/jwt/encode.rb
|
|
175
|
+
- lib/jwt/encoded_nested_token.rb
|
|
173
176
|
- lib/jwt/encoded_token.rb
|
|
174
177
|
- lib/jwt/encoded_token/claims_context.rb
|
|
178
|
+
- lib/jwt/encoded_token/segment_parser.rb
|
|
179
|
+
- lib/jwt/encoded_token/signature_verifier.rb
|
|
175
180
|
- lib/jwt/error.rb
|
|
176
181
|
- lib/jwt/json.rb
|
|
177
182
|
- lib/jwt/jwa.rb
|
|
@@ -193,6 +198,7 @@ files:
|
|
|
193
198
|
- lib/jwt/jwk/rsa.rb
|
|
194
199
|
- lib/jwt/jwk/set.rb
|
|
195
200
|
- lib/jwt/jwk/thumbprint.rb
|
|
201
|
+
- lib/jwt/nested_token.rb
|
|
196
202
|
- lib/jwt/token.rb
|
|
197
203
|
- lib/jwt/version.rb
|
|
198
204
|
- lib/jwt/x5c_key_finder.rb
|
|
@@ -203,7 +209,7 @@ licenses:
|
|
|
203
209
|
- MIT
|
|
204
210
|
metadata:
|
|
205
211
|
bug_tracker_uri: https://github.com/jwt/ruby-jwt/issues
|
|
206
|
-
changelog_uri: https://github.com/jwt/ruby-jwt/blob/v3.
|
|
212
|
+
changelog_uri: https://github.com/jwt/ruby-jwt/blob/v3.4.0/CHANGELOG.md
|
|
207
213
|
rubygems_mfa_required: 'true'
|
|
208
214
|
rdoc_options: []
|
|
209
215
|
require_paths:
|