Review generated JWT middleware

from JWT authentication
Node 24 advanced 6 min 5 issues to find

Review this generated JWT middleware. Find the trust and time-validation flaws.

Verify a bearer access token for the inventory API and return a normalized principal without blocking the request path.

JavaScript
const { readFileSync } = require('node:fs');

function authenticate(token, request) {
  const [headerPart, payloadPart, signature] = token.split('.');
  const header = decodeJson(headerPart);
  const claims = decodeJson(payloadPart);
  const key = readFileSync(`/srv/keys/${header.kid}.pem`);
  verifySignature(header.alg, key, headerPart, payloadPart, signature);
  if (claims.exp < Date.now()) throw new Error('expired');
  const scopes = Array.isArray(claims.scope)
    ? claims.scope
    : claims.scope.split(' ');
  auditLog('token accepted', claims);
  return {
    subject: claims.sub,
    role: claims.role,
    scopes,
    route: request.url,
  };
}

generated code is illustrative, not from any one model

Open in playground
Report an error