Skip to content

feat: support client assertion for client credentials authentication #228

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ewanharris
Copy link
Member

Description

This PR introduces support for performing a client credentials authentication flow using client assertion (aka private key jwt, jwt ca) as an alternative to the existing client secret authentication method.

As this is an alternative ClientCredentials method I have split the existing configuration out into ClientSecretConfig and introduced a ClientAssertionConfig to go along side this and changed the ClientCredentialsConfig to be union of the two to allow type checking to work correctly.

The client assertion signing is performed using jose and I am using v5 of this library over v6 as the latter is an ESM only package which would have implications for our supported Node.js versions.

An extra, not insignificant change to this is the addition of a customClaims property on the client credentials configuration, this is to allow extra data to be passed to the token exchange for clients that need this.

References

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

@ewanharris ewanharris requested a review from a team as a code owner May 30, 2025 11:16
Copy link

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedjose@​5.10.010010010090100

View full report

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.16%. Comparing base (b605f86) to head (48b0e9f).

Files with missing lines Patch % Lines
credentials/credentials.ts 88.88% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #228      +/-   ##
==========================================
- Coverage   88.18%   88.16%   -0.02%     
==========================================
  Files          23       23              
  Lines        1202     1217      +15     
  Branches      211      197      -14     
==========================================
+ Hits         1060     1073      +13     
- Misses         84       86       +2     
  Partials       58       58              

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

.setExpirationTime("2m")
.sign(privateKey);
return {
...config.customClaims,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to spread config.customClaims first so that any of the required properties will be set, however I can also see a world where it might be desirable to allow this to be overridden.

.setSubject(config.clientId)
.setJti(randomUUID())
.setIssuer(config.clientId)
.setAudience(`https://${config.apiTokenIssuer}/`)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might seem unexpected, but per the spec the aud in this JWT assertion must identify the authorization server as it is the intended consumer of the JWT

/**
* Client Secret
*
* @type {string}
* @memberof Configuration
*/
clientSecret: string;

}
export type ClientAssertionConfig = BaseClientCredentialsConfig & {
Copy link
Member Author

@ewanharris ewanharris May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering whether to rename the config as that PrivateKeyJWT is the authentication type and ClientAssertion is just the property it is sent as (although I think it should remain clientAssertionSigningKey and clientAssertionSigningAlgorithm

Suggested change
export type ClientAssertionConfig = BaseClientCredentialsConfig & {
export type PrivateKeyJWTConfig = BaseClientCredentialsConfig & {

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.

2 participants