JSPM

@travetto/jwt

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

JSON Web Token implementation

Package Exports

  • @travetto/jwt

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

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?: Key;
  /**
   * 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?: Key;
  /**
   * 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> ;
  /**
   * Verify the token
   */
  static async verify<T>(jwt: string, options: VerifyOptions = {}): Promise<Payload & T> ;
}

Extension - Auth Rest

The JWTPrincipalEncoder is exposed as a tool for allowing for converting an authenticated principal into a JWT, and back again. This token does not own a session, but allows for encoding the auth state into JWT constructs.