1.0.7 โข Published 2 years ago
@feelinglovelynow/jwt v1.0.7
๐ @feelinglovelynow/jwt
๐ Install
pnpm add @feelinglovelynow/jwt
pnpm add buffer # only necessary if @ browser or edge (cloudflare workers)๐ค Unit Tests
๐ Description
- Node and/or Edge helper functions to create JWK's, create JWT's, decode JWT's and verify JWT's with the subtle crypto api's ECDSA: SHA-512 algorithm
- First we create JWK's which give us a privateandpublicJWK (I love to put them in my.envfile)
- Then with the privateJWK we maycreateJWT's
- With the publicJWK we mayverifyJWT's
- And with no JWK required we may decodeJWT's
๐ Create public and private JWK's
- This function will log the public & private JWK's in the terminal
- Calling this function is only necessary when we first create the JWK's. Once we store the pulic and private JWK's this code may be removed. So place this code locally somewhere server side, call it for the number of JWK's you'd love and then remove it when they're in your .envfile. The same private JWK can create many JWT's.
import { createJWKs } from '@feelinglovelynow/jwt'
createJWKs()๐ Create JWT
- createJWT(jwtPayload: Object, expiresInAsSeconds: number, privateJWK: string, Buffer: any): Promise<string>
import { Buffer } from 'buffer/' // edge
import { Buffer } from 'node:buffer' // node
import { createJWT } from '@feelinglovelynow/jwt'
import { JWK_PRIVATE } from '$env/static/private'
const jwtPayload = { userId: 1 }
const expiresInAsSeconds = 32400 // 9 hours
const jwt = await createJWT(jwtPayload, expiresInAsSeconds, JWK_PRIVATE, Buffer)๐งก Decode JWT
- decodeJWT(jwt: string, Buffer: any): any
import { Buffer } from 'buffer/' // edge
import { Buffer } from 'node:buffer' // node
import { decodeJWT } from '@feelinglovelynow/jwt'
const decoded = decodeJWT(jwt, Buffer)- ๐ฅ Errors we may throw
if (!jwt || typeof jwt !== 'string' || jwt.split('.').length !== 3) {
  throw { id: 'fln__decode__invalid-jwt', message: 'Please provide a string token, with 3 parts, seperated by a dot', _errorData: { jwt } }
}โค๏ธ Verify JWT
- verifyJWT(jwt: string, publicJWK: string, Buffer: any): Promise<any>
import { Buffer } from 'buffer/' // edge
import { Buffer } from 'node:buffer' // node
import { verifyJWT } from '@feelinglovelynow/jwt'
import { JWK_PUBLIC } from '$env/static/private'
const payload = await verifyJWT(jwt, JWK_PUBLIC, Buffer)- ๐ฅ Errors we may throw
if (!jwt || typeof jwt !== 'string' || jwt.split('.').length !== 3) {
  throw { id: 'fln__verify__bad-format', message: 'Please provide a string token, with 3 parts, seperated by a dot', _errorData: { jwt } }
}
if (expiresInAsSeconds <= now()) {
  throw { id: 'fln__verify__expired', message: 'Token has expired', _errorData: { jwt } }
}
if (!isValid) throw { id: 'fln__verify__invalid', message: 'Token is invalid', _errorData: { jwt } }๐ All Our Packages
- @feelinglovelynow/datetime-local: NPM โ Github
- @feelinglovelynow/dgraph: NPM โ Github
- @feelinglovelynow/env-write: NPM โ Github
- @feelinglovelynow/get-form-entries: NPM โ Github
- @feelinglovelynow/get-relative-time: NPM โ Github
- @feelinglovelynow/global-style: NPM โ Github
- @feelinglovelynow/jwt: NPM โ Github
- @feelinglovelynow/loop-backwards: NPM โ Github
- @feelinglovelynow/slug: NPM โ Github
- @feelinglovelynow/svelte-catch: NPM โ Github
- @feelinglovelynow/svelte-kv: NPM โ Github
- @feelinglovelynow/svelte-loading-anchor: NPM โ Github
- @feelinglovelynow/svelte-modal: NPM โ Github
- @feelinglovelynow/svelte-turnstile: NPM โ Github
- @feelinglovelynow/toast: NPM โ Github