npm.io
5.1.0 • Published 1 year ago

@travetto/jwt

Licence
MIT
Version
5.1.0
Deps
3
Size
14 kB
Vulns
0
Weekly
23
Stars
23

JWT

JSON Web Token implementation

Install: @travetto/jwt

npm install @travetto/jwt

# or

yarn add @travetto/jwt

This module is a simple component to support JWT signing and verification. The framework provides a port of node-jsonwebtoken. The API has been streamlined, and is intended as a lower level component as a basis for other modules.

The API exposes:

Code: Signing Options

export interface SignOptions {
  /**
   * Key to use
   */
  key?: OrProm<KeyItem>;
  /**
   * Ignore issued
   */
  iatExclude?: boolean;
  /**
   * Algorithm
   */
  alg?: AlgType;
  /**
   * Header type
   */
  header?: {
    typ?: 'JWT';
  } & {
    [key: string]: string;
  };
  /**
   * Encoding for key
   */
  encoding?: string;
}

Code: Verify Options

export type VerifyOptions = {
  /**
   * Clock starting point
   */
  clock?: {
    /**
     * Time to check against
     */
    timestamp?: number | Date;
    /**
     * Time tolerance
     */
    tolerance?: number;
  };
  /**
   * Ignore various checks
   */
  ignore?: {
    /**
     * Ignore expiration time
     */
    exp?: boolean;
    /**
     * Ignore not before timestamp
     */
    nbf?: boolean;
  };
  /**
   * Max age in seconds
   */
  maxAgeSec?: number;
  /**
   * Header
   */
  header?: Record<string, string>;
  /**
   * Encryption key
   */
  key?: OrProm<KeyItem | KeyItem[]>;
  /**
   * Encoding
   */
  encoding?: string;
  /**
   * Algorithms to use
   */
  alg?: AlgType | AlgType[];

  /**
   * Payload audience to check
   */
  payload?: {
    aud?: string | RegExp | (string | RegExp)[];
  } & PayloadCore;
};

Code: API

export class JWTUtil {
  /**
   * Sign the payload and return a token
   */
  static async create<T extends Payload>(payload: T, options: SignOptions = {}): Promise<string>;
  /**
   * Read and return full object with signatures
   */
  static read<T extends Payload = Payload>(jwt: string): TypedSig<T>;
  /**
   * Rewrite a token with a simple transformation
   */
  static async rewrite<T extends Payload>(jwt: string, transformer: (o: T) => T, options: SignOptions = {}): Promise<string>;
  /**
   * Verify the token
   */
  static async verify<T>(jwt: string, options: VerifyOptions = {}): Promise<Payload & T>;
}

Keywords