Skip to content

Commit c4a6187

Browse files
magnelandanakinj
authored andcommitted
Add type check to key_base '==' operator
1 parent c3362af commit c4a6187

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/jwt/jwk/key_base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def []=(key, value)
3838
end
3939

4040
def ==(other)
41-
self[:kid] == other[:kid]
41+
other.is_a?(::JWT::JWK::KeyBase) && self[:kid] == other[:kid]
4242
end
4343

4444
alias eql? ==

spec/jwk/hmac_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,36 @@
8282
end
8383
end
8484
end
85+
86+
describe '#==' do
87+
it 'is equal to itself' do
88+
other = jwk
89+
expect(jwk == other).to eq true
90+
end
91+
92+
it 'is equal to a clone of itself' do
93+
other = jwk.clone
94+
expect(jwk == other).to eq true
95+
end
96+
97+
it 'is not equal to nil' do
98+
other = nil
99+
expect(jwk == other).to eq false
100+
end
101+
102+
it 'is not equal to boolean true' do
103+
other = true
104+
expect(jwk == other).to eq false
105+
end
106+
107+
it 'is not equal to a non-key' do
108+
other = Object.new
109+
expect(jwk == other).to eq false
110+
end
111+
112+
it 'is not equal to a different key' do
113+
other = described_class.new('other-key')
114+
expect(jwk == other).to eq false
115+
end
116+
end
85117
end

0 commit comments

Comments
 (0)