JwtParser parse jwt token, handle with jwk

interface JwtParser {
    parse(token, pk): [Record<string, unknown>, Record<string, unknown>];
    parsePublicKeyFromJwks(jwks, kid): Buffer;
    parseWithJwks(token, jwks): [Record<string, unknown>, Record<string, unknown>];
    parseWithoutVerify(token): [Record<string, unknown>, Record<string, unknown>];
    verifyWithJwks(token, jwks): boolean;
}

Methods

  • parse, validate and verify signature by public key

    Parameters

    • token: string

      JWT

    • pk: Buffer

      Public Key

    Returns [Record<string, unknown>, Record<string, unknown>]

    Example

    import * as crypto from "crypto";

    let parser = crypto.newJwtParser();
    let [header, payload] = parser.parse(token, pk);
  • get PEM public key from jwks

    Parameters

    Returns Buffer

    Example

    import * as crypto from "crypto";

    let parser = crypto.newJwtParser();
    let [header, payload] = parser.ParseWithoutVerify(token)
    let url = payload['iss'] + '.well-known/jwks.json'
    let resp = http.newClient().get(url)
    let pub = parser.parsePublicKeyFromJwks(resp.body, kid)
  • parse, validate and verify by JWKS

    Parameters

    Returns [Record<string, unknown>, Record<string, unknown>]

    Example

    import * as crypto from "crypto";

    let parser = crypto.newJwtParser();
    let [header, payload] = parser.parseWithoutVerify(token)
    let url = payload['iss'] + '.well-known/jwks.json'
    let resp = http.newClient().get(url)
    let [header, payload] = parser.parseWithJwks(token);
  • parse without valid and verify

    Parameters

    • token: string

      JWT

    Returns [Record<string, unknown>, Record<string, unknown>]

    Example

    import * as crypto from "crypto";

    let parser = crypto.newJwtParser();
    let [header, payload] = parser.parseWithoutVerify(token);
  • verify signature by JWKS

    Parameters

    Returns boolean

    Example

    import * as crypto from "crypto";

    let parser = crypto.newJwtParser();
    let [header, payload] = parser.ParseWithoutVerify(token)
    let url = payload['iss'] + '.well-known/jwks.json'
    let resp = http.newClient().get(url)
    if (!parser.verifyWithJwks(token, resp.body)) {
    throw new Error("invalid signature")
    }