Skip to content

Commit a7dc59e

Browse files
committed
Resolve JWA based on curve
1 parent 3f7e0cb commit a7dc59e

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Raise an error if the ECDSA signing or verification key is not an instance of `OpenSSL::PKey::EC` [#688](https://github.com/jwt/ruby-jwt/pull/688) ([@anakinj](https://github.com/anakinj))
1111
- Allow `OpenSSL::PKey::EC::Point` to be used as the verification key in ECDSA [#689](https://github.com/jwt/ruby-jwt/pull/689) ([@anakinj](https://github.com/anakinj))
1212
- Require claims to have been verified before accessing the `JWT::EncodedToken#payload`
13-
- Resolve the verification algorithm based on the JWK "alg" parameter [#692](https://github.com/jwt/ruby-jwt/pull/692) ([@anakinj](https://github.com/anakinj))
13+
- Support passing the key as a JWK when verifying signatures [#692](https://github.com/jwt/ruby-jwt/pull/692) ([@anakinj](https://github.com/anakinj))
1414
- Your contribution here
1515

1616
**Fixes and enhancements:**

lib/jwt/jwa/ecdsa.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def verify(data:, signature:, verification_key:)
6464
register_algorithm(new(v[:algorithm], v[:digest]))
6565
end
6666

67+
# @api private
6768
def self.curve_by_name(name)
6869
NAMED_CURVES.fetch(name) do
6970
raise UnsupportedEcdsaCurve, "The ECDSA curve '#{name}' is not supported"

lib/jwt/jwk/ec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def []=(key, value)
7373

7474
private
7575

76+
def resolve_algorithm
77+
return super if self[:alg]
78+
79+
curve_name = self.class.to_openssl_curve(self[:crv])
80+
JWA.resolve(JWA::Ecdsa.curve_by_name(curve_name)[:algorithm])
81+
end
82+
7683
def ec_key
7784
@ec_key ||= create_ec_key(self[:crv], self[:x], self[:y], self[:d])
7885
end

spec/jwt/jwk/ec_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,37 @@
110110
end
111111
end
112112

113+
describe '#verify' do
114+
let(:data) { 'data_to_sign' }
115+
let(:signature) { jwk.sign(data: data) }
116+
117+
context 'when jwk is missing the alg parameter' do
118+
let(:jwk) { described_class.new(ec_key) }
119+
120+
context 'when the signature is valid' do
121+
it 'returns true' do
122+
expect(jwk.verify(data: data, signature: signature)).to be(true)
123+
end
124+
end
125+
end
126+
127+
context 'when jwk has alg parameter' do
128+
let(:jwk) { described_class.new(ec_key, alg: 'ES384') }
129+
130+
context 'when the signature is valid' do
131+
it 'returns true' do
132+
expect(jwk.verify(data: data, signature: signature)).to be(true)
133+
end
134+
end
135+
136+
context 'when the signature is invalid' do
137+
it 'returns true' do
138+
expect(jwk.verify(data: data, signature: 'invalid')).to be(false)
139+
end
140+
end
141+
end
142+
end
143+
113144
describe '.to_openssl_curve' do
114145
context 'when a valid curve name is given' do
115146
it 'returns the corresponding OpenSSL curve name' do

0 commit comments

Comments
 (0)