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

JWKS Retrieval Fails in Latest Version Despite Unchanged Request Data #336

Open
saisankar428 opened this issue Feb 17, 2025 · 3 comments
Open

Comments

@saisankar428
Copy link

After upgrading to the latest version of get-jwks, we noticed that JWKS retrieval is failing, even though the request data (including kid, domain, and alg) remains unchanged from the previous working version. The older version successfully fetches the JWKS, but the latest version throws an error or returns an unexpected response. This suggests a possible regression in the library.

We have verified that the request payload is identical between versions and checked for any missing headers or required configurations. However, the root cause remains unclear. Could you please investigate this issue?

@simoneb
Copy link
Member

simoneb commented Feb 17, 2025

Can you provide a repro?

@saisankar428
Copy link
Author

saisankar428 commented Feb 21, 2025

import type { TokenOrHeader } from '@fastify/jwt';
import fastifyJWT from '@fastify/jwt';
import config from 'config';
import type { FastifyInstance, FastifyRequest } from 'fastify';
import fp from 'fastify-plugin';
import buildGetJwks from 'get-jwks';
import https from 'node:https';

import logger from '@util/logger';

interface Options {
  addRequestHook?: boolean;
}

function securityPlugin(fastify: FastifyInstance, { addRequestHook = true }: Options = {}) {
  fastify.log.info('Plugin: Security');

  const getJwks = buildGetJwks({
    providerDiscovery: true,
    // Force IPv4 dns resolution
    agent: new https.Agent({ family: 4 }),
  });

  fastify.register(fastifyJWT, {
    secret: (_: FastifyRequest, token: TokenOrHeader) => {
      if ('payload' in token) {
        const { header, payload } = token;

        return getJwks.getPublicKey({ kid: header.kid, domain: payload.iss, alg: header.alg });**// this is where we are getting error**
      }

      throw Error('Expected a decoded token with payload');
    },
    verify: {
      allowedAud: config.get<string>('auth.audience'),
      allowedIss: config.get<string[]>('auth.issuer'),
    },
    decode: { complete: true },
    formatUser(payload) {
      return {
       sampleId: payload.sampleid,
      };
    },
  });

  if (addRequestHook) {
    fastify.addHook('onRequest', async request => {
      fastify.log.info('Validate request with JWT verification');
      try {
        await request.jwtVerify();
      } catch (err) {
        logger().error(err);
        throw err;
      }
    });
  }

  return fastify;
}

export default fp(securityPlugin);

@simoneb
Copy link
Member

simoneb commented Feb 21, 2025

@saisankar428 this is not a reproducible repro I'm afraid, as we can't just run this code and see the issue. Please provide a minimal reproducible repro. See https://stackoverflow.com/help/minimal-reproducible-example

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

No branches or pull requests

2 participants