Skip to content

Commit 74c11d0

Browse files
committed
React on copilot comments
1 parent 277bd80 commit 74c11d0

6 files changed

Lines changed: 15 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
**Features:**
88

9-
- Revamp error hierarchy: introduce `JWT::Error`, `JWT::TokenError`, `JWT::MalformedTokenError`, `JWT::SignatureError`, and `JWT::ClaimValidationError` grouping classes [#722](https://github.com/jwt/ruby-jwt/pull/722) ([@anakinj](https://github.com/anakinj))
9+
- Revamp error hierarchy: introduce `JWT::Error`, `JWT::TokenError`, `JWT::MalformedTokenError`, `JWT::SignatureError`, and `JWT::ClaimValidationError` grouping classes. `JWT::DecodeError` is now a deprecated alias for `JWT::Error` [#722](https://github.com/jwt/ruby-jwt/pull/722) ([@anakinj](https://github.com/anakinj))
1010
- Your contribution here
1111

1212
**Fixes and enhancements:**

lib/jwt/jwa.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def resolve_and_sort(algorithms:, preferred_algorithm:)
3737
# @api private
3838
def create_signer(algorithm:, key:)
3939
if key.is_a?(JWK::KeyBase)
40-
validate_jwk_algorithms!(key, algorithm, DecodeError)
40+
validate_jwk_algorithms!(key, algorithm, EncodeError)
4141

4242
return key
4343
end

spec/integration/readme_examples_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@
304304
JWT.decode(token, nil, true, { algorithms: ['RS512'], jwks: jwk_loader })
305305
rescue JWT::JWKError
306306
# Handle problems with the provided JWKs
307-
rescue JWT::DecodeError
308-
# Handle other decode related issues e.g. no kid in header, no matching public key found etc.
307+
rescue JWT::TokenError
308+
# Handle other token related issues e.g. no kid in header, no matching public key found etc.
309309
end
310310

311311
## This is not in the example but verifies that the cache is invalidated after 5 minutes
@@ -355,8 +355,8 @@
355355
JWT.decode(token, nil, true, { algorithms: ['RS512'], jwks: jwks_loader })
356356
rescue JWT::JWKError
357357
# Handle problems with the provided JWKs
358-
rescue JWT::DecodeError
359-
# Handle other decode related issues e.g. no kid in header, no matching public key found etc.
358+
rescue JWT::TokenError
359+
# Handle other token related issues e.g. no kid in header, no matching public key found etc.
360360
end
361361
end
362362
end

spec/jwt/jwk/decode_with_jwk_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@
131131
context 'when RSA key is pointed to as HMAC secret' do
132132
let(:signed_token) { described_class.encode({ 'foo' => 'bar' }, 'is not really relevant in the scenario', 'HS256', { kid: rsa_jwk.kid }) }
133133

134-
it 'raises JWT::SignatureError' do
134+
it 'raises JWT::VerificationError' do
135135
expect { described_class.decode(signed_token, nil, true, algorithms: ['HS256'], jwks: jwks) }.to raise_error(JWT::VerificationError, 'HMAC key expected to be a String')
136136
end
137137
end
138138

139139
context 'when EC key is pointed to as HMAC secret' do
140140
let(:signed_token) { described_class.encode({ 'foo' => 'bar' }, 'is not really relevant in the scenario', 'HS256', { kid: ec_jwk_secp384r1.kid }) }
141141

142-
it 'raises JWT::SignatureError' do
142+
it 'raises JWT::VerificationError' do
143143
expect { described_class.decode(signed_token, nil, true, algorithms: ['HS256'], jwks: jwks) }.to raise_error(JWT::VerificationError, 'HMAC key expected to be a String')
144144
end
145145
end

spec/jwt/jwt_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,12 @@
538538
end
539539
end
540540

541+
context 'when nil is passed as the token' do
542+
it 'raises JWT::MalformedTokenError' do
543+
expect { JWT.decode(nil, nil, true) }.to raise_error(JWT::MalformedTokenError, 'Nil JSON web token')
544+
end
545+
end
546+
541547
context 'a token with no segments' do
542548
it 'raises JWT::MalformedTokenError' do
543549
expect { JWT.decode('ThisIsNotAValidJWTToken', nil, true) }.to raise_error(JWT::MalformedTokenError, 'Not enough or too many segments')

spec/jwt/token_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
context 'with mismatching algorithm provided in sign call' do
4444
it 'signs the token' do
45-
expect { token.sign!(algorithm: %w[RS384 RS512], key: jwk) }.to raise_error(JWT::Error, 'Provided JWKs do not support one of the specified algorithms: RS384, RS512')
45+
expect { token.sign!(algorithm: %w[RS384 RS512], key: jwk) }.to raise_error(JWT::EncodeError, 'Provided JWKs do not support one of the specified algorithms: RS384, RS512')
4646
end
4747
end
4848
end

0 commit comments

Comments
 (0)