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

async secretOrKeyProvider #235

Closed
rafaelcorreiapoli opened this issue Apr 3, 2020 · 14 comments
Closed

async secretOrKeyProvider #235

rafaelcorreiapoli opened this issue Apr 3, 2020 · 14 comments

Comments

@rafaelcorreiapoli
Copy link

rafaelcorreiapoli commented Apr 3, 2020

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

secretOrKeyProvider must return the key synchronously

Expected behavior

If we are using jwks, we need to get this key from a http endpoint. Therefore, secretOrKeyProvider must support promises

What is the motivation / use case for changing the behavior?

Auth0 exposes the public keys via JWKS (https://github.com/auth0/node-jwks-rsa/tree/master/examples/express-demo)

Environment


Nest version: 7.0.0
 
For Tooling issues:
- Node version: 12
- Platform:  Mac
@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@rafaelcorreiapoli
Copy link
Author

Sure! If you think it makes sense I can work on a PR :)

@kamilmysliwiec
Copy link
Member

That would be great!

@iamsuneeth
Copy link

Hi @rafaelcorreiapoli , any update on the PR for this one?

@rafaelcorreiapoli
Copy link
Author

@iamsuneeth Sorry I could not advance with the PR.

@katesroad
Copy link

Hey, any updates with this issue?

@guru2228
Copy link

Hi, any updates on supporting JWKS URI as secretOrKeyProvider

@foolishell
Copy link

Hi, from Japan.
I'm also looking forward to this feature!

@vtrphan
Copy link

vtrphan commented Jul 23, 2021

i need this feature too. I think with the package passport-jwt we can use this feature already

@sergeyampo
Copy link

Russian needs too :)

@muuran
Copy link

muuran commented Oct 24, 2022

I found temporary workaround for AzureAD.

import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { JwksClient } from 'jwks-rsa';
import * as jwt from 'jsonwebtoken';

@Module({
  imports: [
    JwtModule.registerAsync({
      async useFactory() {
        const client = new JwksClient({
          cache: true,
          rateLimit: true,
          jwksRequestsPerMinute: 5,
          jwksUri: `https://login.microsoftonline.com/common/discovery/keys`,
        });
        const keys = await client.getSigningKeys();

        return {
          secretOrKeyProvider(requestType, token: string) {
            const decoded = jwt.decode(token, { complete: true });
            return keys
              .find((key) => key.kid === decoded.header.kid)
              .getPublicKey();
          },
          verifyOptions: {
            audience: '(client id)',
            issuer:
              'https://login.microsoftonline.com/(tenant id}/v2.0',
            algorithms: ['RS256'],
          },
        };
      },
    }),
  ],
})
export class AuthModule {}

@xzyfer
Copy link
Contributor

xzyfer commented Nov 2, 2023

Please 🙏

@xzyfer
Copy link
Contributor

xzyfer commented Nov 8, 2023

I've updated #469 in #1486 to resolve conflicts and typos as the proposed workaround only fetches keys on boot which isn't suitable to handle key rotations.

@kamilmysliwiec
Copy link
Member

Let's track this here #1486

@nestjs nestjs locked and limited conversation to collaborators Nov 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants