0.1.3 • Published 7 days ago

@byu-oit-sdk/jwt v0.1.3

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
7 days ago

@byu-oit-sdk/jwt

Requirements:

  • Node.js 18+
  • npm v9+

Installing

In addition to installing this module, you can install either the Jwt plugin for Fastify or for Express depending on what you're using.

npm install @byu-oit-sdk/jwt

Introduction

Use this module for verification and decoding of JWTs. Can be used in conjunction with the express and fastify plugins.

This package exports the following:

  • Jwk - A class for verifying Jwks.
  • RsaJwk - A class for verifying RSA JWKs. Extension of the Jwk class.
  • JwkSet - A class which handles construction, validation, and loading of Jwk Sets.
  • createJwt() - A function that returns a class with methods for verifying and decoding JWTs.

Options

The following options can be passed in to the CreateJwt() function.

OptionTypeDefaultDescription
SchemaObjectREQUIRED OPTIONThe schema of the payload of the JWT that you are creating.
transformerfunction-The function that you will use for manipulating the JWT you are authenticating.
additionalValidationsarray of functions-An optional array of functions that will also be used to validate the JWT.
keystringREQUIRED OPTIONThis is required but can be an empty string.
discoveryEndpointstring-The redirection endpoint that the authorization server should redirect to after authenticating the resource owner.
issuerstring-Used to configure where the user will be sent to sign in.

Usage

import { createJwt, Jwt, type JwtData } from '../src/Jwt.js'
import { type Static, Type } from '@sinclair/typebox'

const sampleJwtData = {
  header: {
    alg: 'HS256',
    typ: 'JWT'
  },
  payload: {
    id: 'some id',
    name: 'some Name'
  },
  signature: 'some signature'
}

const UserSchema = Type.Object({
  id: Type.String(),
  name: Type.String()
})

const TransformedJwtPayload = Type.Object({
  userId: Type.String(),
  userName: Type.String()
})

const jwtData: JwtData<Static<typeof UserSchema>> = {
  header: { alg: 'HS256', typ: 'JWT', kid: 'testKid' },
  payload: { id: '1', name: 'Alice' },
  signature: 'sample_signature'
}

const payloadTransformer = (payload: typeof UserSchema): Static<typeof TransformedJwtPayload> => {
  const userId = payload.id
  const userName = payload.name
  return {
    userId,
    userName
  }
}

const instance = new Jwt(jwtData, { transformer: payloadTransformer })

const CustomJwt = createJwt({
  schema: UserSchema,
  key: ''
})
0.1.3

7 days ago

0.1.2-beta.0

2 months ago

0.1.2

2 months ago

0.1.1-beta.5

2 months ago

0.1.1-beta.4

3 months ago

0.1.1

3 months ago

0.1.1-beta.3

4 months ago

0.1.1-beta.2

4 months ago

0.1.1-beta.1

4 months ago

0.1.1-beta.0

8 months ago

0.1.0

8 months ago

0.1.0-beta.7

8 months ago

0.1.0-beta.6

8 months ago

0.1.0-beta.5

9 months ago

0.1.0-beta.4

9 months ago

0.1.0-beta.3

9 months ago

0.1.0-beta.2

9 months ago

0.1.0-beta.1

9 months ago

0.1.0-beta.0

9 months ago