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

Pass kid param through JWT::JWK.create_from #445

Merged
merged 1 commit into from Sep 30, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -495,7 +495,7 @@ JWT.decode token, hmac_secret, true, { required_claims: ['exp'], algorithm: 'HS2
JWK is a JSON structure representing a cryptographic key. Currently only supports RSA public keys.

```ruby
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048))
jwk = JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), "optional-kid")
payload, headers = { data: 'data' }, { kid: jwk.kid }

token = JWT.encode(payload, jwk.keypair, 'RS512', headers)
Expand Down
4 changes: 2 additions & 2 deletions lib/jwt/jwk.rb
Expand Up @@ -14,10 +14,10 @@ def import(jwk_data)
end.import(jwk_data)
end

def create_from(keypair)
def create_from(keypair, kid = nil)
mappings.fetch(keypair.class) do |klass|
raise JWT::JWKError, "Cannot create JWK from a #{klass.name}"
end.new(keypair)
end.new(keypair, kid)
end

def classes
Expand Down
11 changes: 10 additions & 1 deletion spec/jwk_spec.rb
Expand Up @@ -41,7 +41,8 @@
end

describe '.new' do
subject { described_class.new(keypair) }
let(:kid) { nil }
subject { described_class.new(keypair, kid) }

context 'when RSA key is given' do
let(:keypair) { rsa_key }
Expand All @@ -57,5 +58,13 @@
let(:keypair) { ec_key }
it { is_expected.to be_a ::JWT::JWK::EC }
end

context 'when kid is given' do
let(:keypair) { rsa_key }
let(:kid) { "CUSTOM_KID" }
it 'sets the kid' do
expect(subject.kid).to eq(kid)
end
end
end
end