A simple library to generate JWT in cloudflare.
There is not much to show off. This package might provide a hint how to do JWT tasks in Cloudflare. You may skip install and copy the codes.
Those functions are intended to make & verify JWT with given cryptography algorithm.
export async function getToken<H = Header, P = Payload>( // (1)
header: H, // (2)
payload: P,
sign: (data: string) => Promise<string>, // (3)
): Promise<string> { // (4)
const h = base64UrlEncode(JSON.stringify(header))
const p = base64UrlEncode(JSON.stringify(payload))
const part = h + "." + p
const s = await sign(part)
return part + "." + s
}
- You can provide custom Header and Payload type.
- You must set
alg
and other fields manually. There is no autofill. - Provide your desired cryptography algorithm.
- This function returns a complete JWT.
export async function verifyToken<H = Header, P = Payload>( // (1)
token: string, // (2)
verifyPayload: (p: P) => Promise<boolean>, // (3)
signByHeader: (h: H, data: string) => Promise<string>, // (4)
): Promise<[boolean, P]> { // (5)
const parts = token.split(".")
const h: H = JSON.parse(base64UrlDecode(parts[0]))
const p: P = JSON.parse(base64UrlDecode(parts[1]))
if (! await verifyPayload(p)) return [false, p] // (6)
const s = await signByHeader(h, parts[0] + "." + parts[1])
return [parts[2] == s, p]
}
- You can provide custom Header and Payload type.
- Provide JWT directly.
- Provide a complete payload verification function.
- Provide a function that reads the Header and generates appropriate signature.
- This function returns a verification result and the parsed Payload.
- The payload verfication function will be applied before comparing the signatures.