Crypto Middleware
    Preparing search index...

    Function verifyJwt

    • Verify a JWT (HS256 only) using HMAC-SHA256.

      The algorithm is pinned to HS256: any other alg, including none, is rejected. The token must carry a numeric exp claim; nbf is checked when present, and iss / aud when options require them.

      This is a minimal JWT verifier intended for middleware use cases. For production systems requiring RS256/ES256 or full JOSE support, consider using a dedicated JWT library.

      Parameters

      • jwtSecret: string

        The HMAC secret (UTF-8 string or hex-encoded key).

      • token: string

        The raw JWT string (header.payload.signature).

      • options: JwtVerifyOptions = {}

        Required issuer and audience, if any.

      Returns JwtPayload

      The decoded JWT payload.

      On invalid/expired tokens.

      const token = req.headers.authorization?.replace("Bearer ", "") ?? "";
      const payload = verifyJwt("my-hs256-secret", token, {
      issuer: "https://auth.example.com",
      audience: "orders-api",
      });
      console.log(payload.sub); // "user-123"