.decode() syntax change to support headers
This commit is contained in:
5
index.js
5
index.js
@@ -116,7 +116,10 @@ class JWT {
|
||||
return await crypto.subtle.verify(importAlgorithm, key, Base64URL.parse(tokenParts[2]), this._utf8ToUint8Array(`${tokenParts[0]}.${tokenParts[1]}`))
|
||||
}
|
||||
decode(token) {
|
||||
return this._decodePayload(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
||||
return {
|
||||
header: this._decodePayload(token.split('.')[0].replace(/-/g, '+').replace(/_/g, '/')),
|
||||
payload: this._decodePayload(token.split('.')[1].replace(/-/g, '+').replace(/_/g, '/'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -115,12 +115,14 @@ test.each(Object.entries(secrets))(`Self test: %s`, async (algorithm, key) => {
|
||||
privateKey = key.private
|
||||
publicKey = key.public
|
||||
}
|
||||
|
||||
const token = await JWT.sign(testPayload, privateKey, { algorithm })
|
||||
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
|
||||
|
||||
const verified = await JWT.verify(token, publicKey, { algorithm })
|
||||
expect(verified).toBeTruthy()
|
||||
const payload = JWT.decode(token)
|
||||
expect(payload).toBeTruthy()
|
||||
|
||||
const { payload } = JWT.decode(token)
|
||||
expect({
|
||||
sub: payload.sub,
|
||||
name: payload.name
|
||||
@@ -151,9 +153,11 @@ test.each(Object.entries(externalTokens))('Verify external tokens: %s', async (a
|
||||
privateKey = key.private
|
||||
publicKey = key.public
|
||||
}
|
||||
|
||||
const verified = await JWT.verify(token, publicKey, { algorithm })
|
||||
expect(verified).toBeTruthy()
|
||||
const payload = JWT.decode(token)
|
||||
|
||||
const { payload } = JWT.decode(token)
|
||||
expect({
|
||||
sub: payload.sub,
|
||||
name: payload.name
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-jwt",
|
||||
"version": "1.3.1",
|
||||
"version": "1.4.0",
|
||||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user