JSPM

@travetto/jwt

5.0.6
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q37067F
  • License MIT

JSON Web Token implementation

Package Exports

  • @travetto/jwt
  • @travetto/jwt/__index__.ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@travetto/jwt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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>;
}