Skip to content

feat: Add Key ID (kid) support to JWT assertions #652

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

Merged
merged 3 commits into from
May 27, 2025

Conversation

mridang
Copy link
Contributor

@mridang mridang commented May 27, 2025

Feature: Support Key ID (kid) in JWT Bearer Assertions (RFC 7515)

This pull request implements support for the kid (Key ID) header parameter in JWT bearer assertions, as defined in RFC 7515 (JSON Web Signature - JWS).

The OAuth2::Strategy::Assertion class currently constructs JWTs for client authentication and authorization grants. While it supports specifying the alg (algorithm), it lacks the mechanism to pass a kid. The kid parameter is highly recommended in scenarios where a signer has multiple keys or undergoes regular key rotation, as it allows the JWT recipient to efficiently locate the appropriate public key for signature validation.

Motivation

To facilitate more robust and scalable OAuth2 client implementations, especially for integrations with providers that leverage kid for key discovery and management, this feature becomes essential. It aligns the library's JWT generation capabilities more closely with common production practices and the broader JWT ecosystem.

Implementation Details

  • The private method build_assertion within OAuth2::Strategy::Assertion has been modified.
  • It now conditionally checks for a :kid key within the encoding_opts hash passed to get_token.
  • If :kid is provided, it is correctly added as a header parameter to the JWT using JWT.encode(..., headers).
  • A dedicated RSpec test case has been added to confirm that the kid is correctly embedded in the JWT header when supplied.

mridang added 3 commits May 27, 2025 13:00
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.
Removes an unnecessary empty line within the `build_assertion` method for
improved code style and consistency.
Updates `.rubocop_gradual.lock` due to changes in `spec/oauth2/strategy/assertion_spec.rb`.
This reflects the introduction of the new `kid` test context.
@mridang
Copy link
Contributor Author

mridang commented May 27, 2025

@pboling would love to have your review on this. Thank you. 🙏🏽

@coveralls
Copy link

coveralls commented May 27, 2025

Pull Request Test Coverage Report for Build 15272497358

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 15221944780: 0.0%
Covered Lines: 520
Relevant Lines: 520

💛 - Coveralls

@pboling
Copy link
Member

pboling commented May 27, 2025

Kids are great! 😊

@pboling pboling merged commit 8f06a97 into oauth-xx:main May 27, 2025
37 checks passed
@pboling
Copy link
Member

pboling commented May 27, 2025

I will release this in 2.0.12 soon

Copy link

codecov bot commented May 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (a5de787) to head (5f4251e).
Report is 4 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #652   +/-   ##
===========================
===========================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pboling
Copy link
Member

pboling commented May 28, 2025

CodeCov has inexplicably stopped working. This repo still has 100% coverage, and uses coveralls as an alternative service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants