Skip to content

Commit

Permalink
Use correct methods when raising error during signing/verification wi…
Browse files Browse the repository at this point in the history
…th EdDSA
  • Loading branch information
hieuk09 authored and anakinj committed Oct 9, 2024
1 parent d9a87bc commit 42f905c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

- Deprecation warnings for deprecated methods and classes [#629](https://github.com/jwt/ruby-jwt/pull/629) ([@anakinj](https://github.com/anakinj))
- Improved documentation for public apis [#629](https://github.com/jwt/ruby-jwt/pull/629) ([@anakinj](https://github.com/anakinj))
- Use correct methods when raising error during signing/verification with EdDSA [#633](https://github.com/jwt/ruby-jwt/pull/633)
- Your contribution here

## [v2.9.3](https://github.com/jwt/ruby-jwt/tree/v2.9.3) (2024-10-03)
Expand Down
4 changes: 2 additions & 2 deletions lib/jwt/jwa/eddsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def initialize(alg)

def sign(data:, signing_key:)
unless signing_key.is_a?(RbNaCl::Signatures::Ed25519::SigningKey)
raise_encode_error!("Key given is a #{signing_key.class} but has to be an RbNaCl::Signatures::Ed25519::SigningKey")
raise_sign_error!("Key given is a #{signing_key.class} but has to be an RbNaCl::Signatures::Ed25519::SigningKey")
end

signing_key.sign(data)
end

def verify(data:, signature:, verification_key:)
unless verification_key.is_a?(RbNaCl::Signatures::Ed25519::VerifyKey)
raise_decode_error!("key given is a #{verification_key.class} but has to be a RbNaCl::Signatures::Ed25519::VerifyKey")
raise_verify_error!("key given is a #{verification_key.class} but has to be a RbNaCl::Signatures::Ed25519::VerifyKey")
end

verification_key.verify(signature, data)
Expand Down
16 changes: 16 additions & 0 deletions spec/jwt/jwa/eddsa_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@
expect(JWT::JWA::Eddsa.verify('RS256', key.verify_key, 'data', signature)).to be(true)
end
end

context 'when when signing with invalid RbNaCl::Signatures::Ed25519::SigningKey' do
it 'raises an error' do
expect do
JWT::JWA::Eddsa.sign('RS256', 'data', 'key')
end.to raise_error(JWT::EncodeError, 'Key given is a String but has to be an RbNaCl::Signatures::Ed25519::SigningKey')
end
end

context 'when when verifying with invalid RbNaCl::Signatures::Ed25519::VerifyKey' do
it 'raises an error' do
expect do
JWT::JWA::Eddsa.verify('RS256', 'key', 'data', 'signature')
end.to raise_error(JWT::DecodeError, 'key given is a String but has to be a RbNaCl::Signatures::Ed25519::VerifyKey')
end
end
end

0 comments on commit 42f905c

Please sign in to comment.