Skip to content

Commit

Permalink
Pass kid param through JWT::JWK.create_from
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun Guth authored and anakinj committed Sep 30, 2021
1 parent ffad2de commit a4270e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
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

0 comments on commit a4270e8

Please sign in to comment.