Skip to content

Commit

Permalink
Add type check to key_base '==' operator
Browse files Browse the repository at this point in the history
  • Loading branch information
magneland committed Oct 13, 2023
1 parent 6951436 commit b273a16
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/jwt/jwk/key_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? ==
Expand Down
32 changes: 32 additions & 0 deletions spec/jwk/hmac_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit b273a16

Please sign in to comment.