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 committed Oct 9, 2024
1 parent d9a87bc commit 7b0dbfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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 7b0dbfa

Please sign in to comment.