1.0.1 • Published 3 years ago

@itwalton/jwt-node v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

JWT Node

Introduction

Issues and verifies JWT access tokens from a supplied keypair

See RFC7519

Usage

  1. Read PEM-formatted keys into memory (see fs)
  2. Instantiate token service with:
  • issuer: the CN of the service issuing the token
  • subject: the CN of any service that may accept the token
  • algorithm: only supports 'SHA256' for now
  • publicKeyPEMBuffer: A Buffer representation of a PEM encoded public key
  • privateKeyPEMBuffer: A Buffer representation of a PEM encoded private key

Example

  // instantiate the token service
  const tokenService = new TokenService(
    process.env.TOKEN_ISSUER as string,
    [process.env.TOKEN_AUDIENCE as string],
    'SHA256',
    publicKeyPEMBuffer,
    privateKeyPEMBuffer,
  )
  // after a successful login
  const {expiresOn, accessToken} = await tokenService.issueAccessToken(user.id)
  // on a request to a protected route
  const isValid = await tokenService.validateAccessToken(accessToken)

Getting Started

  1. Clone the repo
  2. Install dependencies npm i
  3. Run tests npm test
  4. Watch/compile npm run start:dev