Skip to content

Commit a713d4a

Browse files
committed
feat: Add Key ID (kid) support to JWT assertions
Adds support for the 'kid' (Key ID) header parameter in JWT assertions, allowing clients to specify the key identifier used for signing. This improves key management and verification in systems consuming JWTs. Updates `OAuth2::Strategy::Assertion#build_assertion` to accept `kid` in `encoding_opts` and include it in the JWT header. Also adds a test case to verify the functionality.
1 parent a5de787 commit a713d4a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/oauth2/strategy/assertion.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ def build_request(assertion, request_opts = {})
9595
def build_assertion(claims, encoding_opts)
9696
raise ArgumentError.new(message: "Please provide an encoding_opts hash with :algorithm and :key") if !encoding_opts.is_a?(Hash) || (%i[algorithm key] - encoding_opts.keys).any?
9797

98-
JWT.encode(claims, encoding_opts[:key], encoding_opts[:algorithm])
98+
headers = {}
99+
headers[:kid] = encoding_opts[:kid] if encoding_opts.key?(:kid)
100+
101+
JWT.encode(claims, encoding_opts[:key], encoding_opts[:algorithm], headers)
99102
end
103+
100104
end
101105
end
102106
end

spec/oauth2/strategy/assertion_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,24 @@
164164
expect { client_assertion.get_token(claims, encoding_opts) }.to raise_error(ArgumentError, /encoding_opts/)
165165
end
166166
end
167+
168+
context "when including a Key ID (kid)" do
169+
let(:algorithm) { "HS256" }
170+
let(:key) { "new_secret_key" }
171+
let(:kid) { "my_super_secure_key_id_123" }
172+
173+
before do
174+
client_assertion.get_token(claims, algorithm: algorithm, key: key, kid: kid)
175+
raise "No request made!" if @request_body.nil?
176+
end
177+
178+
it_behaves_like "encodes the JWT"
179+
180+
it "includes the kid in the JWT header" do
181+
expect(header).not_to be_nil
182+
expect(header["kid"]).to eq(kid)
183+
end
184+
end
167185
end
168186
end
169187

0 commit comments

Comments
 (0)