Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix key base equality and spaceship operators #569

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
**Fixes and enhancements:**

- Fix signature has expired error if payload is a string [#555](https://github.com/jwt/ruby-jwt/pull/555) - [@GobinathAL](https://github.com/GobinathAL).
- Fix key base equality and spaceship operators [#569](https://github.com/jwt/ruby-jwt/pull/569) - [@magneland](https://github.com/magneland).
- Your contribution here

## [v2.7.1](https://github.com/jwt/ruby-jwt/tree/v2.8.0) (2023-06-09)
Expand Down
4 changes: 3 additions & 1 deletion lib/jwt/jwk/key_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ def []=(key, value)
end

def ==(other)
self[:kid] == other[:kid]
other.is_a?(::JWT::JWK::KeyBase) && self[:kid] == other[:kid]
end

alias eql? ==

def <=>(other)
return nil unless other.is_a?(::JWT::JWK::KeyBase)

self[:kid] <=> other[:kid]
end

Expand Down
64 changes: 64 additions & 0 deletions spec/jwk/hmac_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,68 @@
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

describe '#<=>' do
it 'is equal to itself' do
other = jwk
expect(jwk <=> other).to eq 0
end

it 'is equal to a clone of itself' do
other = jwk.clone
expect(jwk <=> other).to eq 0
end

it 'is not comparable to nil' do
other = nil
expect(jwk <=> other).to eq nil
end

it 'is not comparable to boolean true' do
other = true
expect(jwk <=> other).to eq nil
end

it 'is not comparable to a non-key' do
other = Object.new
expect(jwk <=> other).to eq nil
end

it 'is not equal to a different key' do
other = described_class.new('other-key')
expect(jwk <=> other).not_to eq 0
end
end
end