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

feat(core): support private_key_jwt token auth method #11132

Conversation

leemcmullen
Copy link
Contributor

@leemcmullen leemcmullen commented Jun 11, 2024

☕️ Reasoning

Currently there are 2 issues with NextAuth when attempting to use token_endpoint_auth_method: "private_key_jwt":

1 - There is nowhere to actually configure/use a private key which is required for this type of auth
2 - NextAuth currently does not pass the options.clientPrivateKey paramater into the underlying oauth4web library

This PR addresses both of those issues. So an example provider would end up looking like this:

    {
      id: "example-private_key_jwt-provider",
      name: "ExamplePrivateKeyJwtProvider",
      type: "oidc",
      clientId: "jwt-example-client",
      idToken: true,
      wellKnown:
        "https://auth.sandpit.example.jwt.madeupprovider.uk/.well-known/openid-configuration",
      issuer: "https://auth.sandpit.example.jwt.madeupprovider.uk",
      authorization: {
        params: {
          scope: "openid profile profile_extended",
        },
      },
      checks: [],
      client: {
        token_endpoint_auth_method: "private_key_jwt",
      },
      token: {
        clientPrivateKey: getKeyContentsFromEnv(), // fn which returns the private key
      },
    },

…tOptions.clientPrivateKey` property and pass it into the oauth4webapi.authorizationCodeGrantRequest method if `client.token_endpoint_auth_method === "private_key_jwt"`
Copy link

vercel bot commented Jun 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 14, 2024 11:28am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Jun 14, 2024 11:28am

Copy link

vercel bot commented Jun 11, 2024

@leemcmullen is attempting to deploy a commit to the authjs Team on Vercel.

A member of the Team first needs to authorize it.

@leemcmullen leemcmullen changed the title Pass private key to underlying oauth lib when using private_key_jwt auth fix(core): Pass private key to underlying oauth lib when using private_key_jwt auth Jun 13, 2024
@leemcmullen
Copy link
Contributor Author

@ThangHuuVu Hey! 👋

It seems like you've been allocated as the reviewer for my PR. Just wondered what the expectation in terms of timescales to get a review is likely to be?

Thanks, Lee 👍

Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

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

Thanks! Please check my comments.

packages/core/src/lib/actions/callback/oauth/callback.ts Outdated Show resolved Hide resolved
packages/core/src/providers/oauth.ts Outdated Show resolved Hide resolved
packages/core/src/lib/actions/callback/oauth/callback.ts Outdated Show resolved Hide resolved
packages/core/src/lib/actions/callback/oauth/callback.ts Outdated Show resolved Hide resolved
@balazsorban44 balazsorban44 changed the title fix(core): Pass private key to underlying oauth lib when using private_key_jwt auth feat(core): support private_key_jwt token auth method Jun 13, 2024
… Also update `normalizeEndpoint` to ensure the `clientPrivateKey` persists prior to the callback being invoked.
…hub.com:leemcmullen/next-auth into fix/pass-private-key-when-using-private_key_jwt
…perty if it exists on the incoming object.
@leemcmullen
Copy link
Contributor Author

leemcmullen commented Jun 14, 2024

@balazsorban44 Awesome, thanks for the comments!

I've resolved them all and had to make another tweak as a result of moving clientPrivateKey onto the provider.token prop. I had to update the normalizeEndpoint to ensure the clientPrivateKey is preserved because it was stripping it out. I noticed this method is used in some other cases too so let me know if this is ok or needs changing:

https://github.com/leemcmullen/next-auth/blob/d1dc98acfd971d1c8ed06a6fb935c0f6f632770e/packages/core/src/lib/utils/providers.ts#L158

@balazsorban44 balazsorban44 merged commit d8a9188 into nextauthjs:main Jun 18, 2024
6 of 7 checks passed
hillac pushed a commit to hillac/next-auth that referenced this pull request Jun 19, 2024
k3k8 pushed a commit to k3k8/next-auth that referenced this pull request Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Refers to `@auth/core` providers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants