From b273a1662b9fb67f0e3004998017b3d8bd367a8f Mon Sep 17 00:00:00 2001 From: Magne Land Date: Thu, 12 Oct 2023 17:53:54 -0700 Subject: [PATCH] Add type check to key_base '==' operator --- lib/jwt/jwk/key_base.rb | 2 +- spec/jwk/hmac_spec.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/jwt/jwk/key_base.rb b/lib/jwt/jwk/key_base.rb index 6f0df55a..856a5d2d 100644 --- a/lib/jwt/jwk/key_base.rb +++ b/lib/jwt/jwk/key_base.rb @@ -38,7 +38,7 @@ def []=(key, value) end def ==(other) - self[:kid] == other[:kid] + other.is_a?(::JWT::JWK::KeyBase) && self[:kid] == other[:kid] end alias eql? == diff --git a/spec/jwk/hmac_spec.rb b/spec/jwk/hmac_spec.rb index dcc7d4ce..7702e779 100644 --- a/spec/jwk/hmac_spec.rb +++ b/spec/jwk/hmac_spec.rb @@ -82,4 +82,36 @@ end end end + + describe '#==' do + it 'is equal to itself' do + other = jwk + expect(jwk == other).to eq true + end + + it 'is equal to a clone of itself' do + other = jwk.clone + expect(jwk == other).to eq true + end + + it 'is not equal to nil' do + other = nil + expect(jwk == other).to eq false + end + + it 'is not equal to boolean true' do + other = true + expect(jwk == other).to eq false + end + + it 'is not equal to a non-key' do + other = Object.new + expect(jwk == other).to eq false + end + + it 'is not equal to a different key' do + other = described_class.new('other-key') + expect(jwk == other).to eq false + end + end end