Search code examples
node.jsexpressidentityserver4

Identity Server 4 for NodeJS API


I'm trying to figure out how to do the identity server 4 authentication below using NodeJS - way out of my comfort zone here.

services.AddAuthentication(IdentityServerAuthenticationDefaults
.AuthenticationScheme)
    .AddIdentityServerAuthentication(
         options =>
         {
          options.Authority = "<authority-url>";
          options.ApiName = "<api-url>";
          });

I'm missing something in the flow here as the C# implementation isn't provided a secret or similar - so the token is probably verified via identity server? How would I verify the token using NodeJS if I don't have a 'secret' to verify it with?

I've stumbled on introspection endpoint - am I heading in the right direction?


Solution

  • I was able to solve this using the jwks -endpoint and it's public keys to verify tokens and then I also found a nice package that I used to prepare the middleware:

    private issuer: string = process.env.idsrv;
    
    
    auth = jwt({
        secret: jwksClient.expressJwtSecret({
            cache: true,        // see https://github.com/auth0/node-jwks-rsa#caching,
            cacheMaxAge: ms('24h'),
            rateLimit: true,    // see https://github.com/auth0/node-jwks-rsa#rate-limiting
            jwksRequestsPerMinute: 100,
            jwksUri: `${this.issuer}/.well-known/jwks`
        }),
    
        // validate the audience & issuer from received token vs JWKS endpoint
        audience: `${this.issuer}/resources`,
        issuer: this.issuer,
        algorithms: ["RS256"]
    });