@meistrari/data-token
v0.0.10
Published
Internal library to verify and decode the API Gateway data token
Downloads
4,246
Readme
@meistrari/data-token
Internal library to verify and decode the API Gateway data token.
Installation
pnpm install @meistrari/data-token
Usage
Basic Usage (No Adapter)
import { getValidator } from '@meistrari/data-token'
const validator = getValidator('https://some.api.com/well-known/jwks.json')
async function validateToken(token: string): Promise<DataToken> {
return await validator(token)
}
Usage with Hono
import { Hono } from 'hono'
import { DataTokenContext, createDataTokenMiddleware } from '@meistrari/data-token/hono'
// Combine DataTokenContext with your own context type
type Env = {
Variables: {
customProp: string
}
}
type AppContext = Env & DataTokenContext
const app = new Hono<AppContext>()
const dataToken = createDataTokenMiddleware('https://some.api.com/well-known/jwks.json')
app.use(dataToken)
app.get('/', (c) => {
const authData = c.get('authData')
return c.json({ data: authData })
})
Usage with Elysia
import { Elysia } from 'elysia'
import { createDataTokenPlugin } from '@meistrari/data-token/elysia'
const app = new Elysia()
const dataToken = createDataTokenPlugin('https://some.api.com/well-known/jwks.json')
app
.use(dataToken)
.get('/', ({ authData }) => ({ authData }))
API Reference
getValidator(jwksUrl: string, options?: { algorithms?: string[] })
Creates a validator function that can be used to validate and decode tokens.
createDataTokenMiddleware(url: string, getToken?: (c: Context) => string)
Creates a Hono middleware for token validation.
createDataTokenPlugin(url: string, getToken?: (c: Context) => string)
Creates an Elysia plugin for token validation.
Types
The DataToken type, as well as a Zod schema for the token payload is available from the /types entrypoint, both under the name of DataToken
:
import { DataToken } from '@meistrari/data-token/types'
This type represents the structure of the decoded token data.