Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eebc0e988a | |||
| bf90a7c855 | |||
| 6e0ce1cf82 | |||
| 4ed24b18c5 |
7
index.d.ts
vendored
Normal file
7
index.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
declare class JWT {
|
||||||
|
sign(payload: object, secret: string, algorithm?: "HS256" | "HS512"): Promise<string>
|
||||||
|
verify(token: string, secret: string, algorithm?: "HS256" | "HS512"): Promise<boolean>
|
||||||
|
decode(token: string): object | null
|
||||||
|
}
|
||||||
|
declare const _exports: JWT
|
||||||
|
export = _exports
|
||||||
22
index.js
22
index.js
@@ -27,22 +27,20 @@ class JWT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
utf8ToUint8Array(str) {
|
utf8ToUint8Array(str) {
|
||||||
const chars = []
|
return Base64URL.parse(btoa(unescape(encodeURIComponent(str))))
|
||||||
str = btoa(unescape(encodeURIComponent(str)))
|
|
||||||
return Base64URL.parse(str)
|
|
||||||
}
|
}
|
||||||
async sign(payload, secret, alg = 'HS256') {
|
async sign(payload, secret, algorithm = 'HS256') {
|
||||||
if (payload === null || typeof payload !== 'object')
|
if (payload === null || typeof payload !== 'object')
|
||||||
throw new Error('payload must be an object')
|
throw new Error('payload must be an object')
|
||||||
if (typeof secret !== 'string')
|
if (typeof secret !== 'string')
|
||||||
throw new Error('secret must be a string')
|
throw new Error('secret must be a string')
|
||||||
if (typeof alg !== 'string')
|
if (typeof algorithm !== 'string')
|
||||||
throw new Error('alg must be a string')
|
throw new Error('algorithm must be a string')
|
||||||
const importAlgorithm = this.algorithms[alg]
|
const importAlgorithm = this.algorithms[algorithm]
|
||||||
if (!importAlgorithm)
|
if (!importAlgorithm)
|
||||||
throw new Error('algorithm not found')
|
throw new Error('algorithm not found')
|
||||||
const payloadAsJSON = JSON.stringify(payload)
|
const payloadAsJSON = JSON.stringify(payload)
|
||||||
const partialToken = `${Base64URL.stringify(this.utf8ToUint8Array(JSON.stringify({ alg, typ: 'JWT' })))}.${Base64URL.stringify(this.utf8ToUint8Array(payloadAsJSON))}`
|
const partialToken = `${Base64URL.stringify(this.utf8ToUint8Array(JSON.stringify({ alg: algorithm, typ: 'JWT' })))}.${Base64URL.stringify(this.utf8ToUint8Array(payloadAsJSON))}`
|
||||||
const key = await crypto.subtle.importKey('raw', this.utf8ToUint8Array(secret), importAlgorithm, false, ['sign'])
|
const key = await crypto.subtle.importKey('raw', this.utf8ToUint8Array(secret), importAlgorithm, false, ['sign'])
|
||||||
const characters = payloadAsJSON.split('')
|
const characters = payloadAsJSON.split('')
|
||||||
const it = this.utf8ToUint8Array(payloadAsJSON).entries()
|
const it = this.utf8ToUint8Array(payloadAsJSON).entries()
|
||||||
@@ -56,17 +54,17 @@ class JWT {
|
|||||||
const signature = await crypto.subtle.sign(importAlgorithm.name, key, this.utf8ToUint8Array(partialToken))
|
const signature = await crypto.subtle.sign(importAlgorithm.name, key, this.utf8ToUint8Array(partialToken))
|
||||||
return `${partialToken}.${Base64URL.stringify(new Uint8Array(signature))}`
|
return `${partialToken}.${Base64URL.stringify(new Uint8Array(signature))}`
|
||||||
}
|
}
|
||||||
async verify(token, secret, alg = 'HS256') {
|
async verify(token, secret, algorithm = 'HS256') {
|
||||||
if (typeof token !== 'string')
|
if (typeof token !== 'string')
|
||||||
throw new Error('token must be a string')
|
throw new Error('token must be a string')
|
||||||
if (typeof secret !== 'string')
|
if (typeof secret !== 'string')
|
||||||
throw new Error('secret must be a string')
|
throw new Error('secret must be a string')
|
||||||
if (typeof alg !== 'string')
|
if (typeof algorithm !== 'string')
|
||||||
throw new Error('alg must be a string')
|
throw new Error('algorithm must be a string')
|
||||||
const tokenParts = token.split('.')
|
const tokenParts = token.split('.')
|
||||||
if (tokenParts.length !== 3)
|
if (tokenParts.length !== 3)
|
||||||
throw new Error('token must have 3 parts')
|
throw new Error('token must have 3 parts')
|
||||||
const importAlgorithm = this.algorithms[alg]
|
const importAlgorithm = this.algorithms[algorithm]
|
||||||
if (!importAlgorithm)
|
if (!importAlgorithm)
|
||||||
throw new Error('algorithm not found')
|
throw new Error('algorithm not found')
|
||||||
const keyData = this.utf8ToUint8Array(secret)
|
const keyData = this.utf8ToUint8Array(secret)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "1.0.6",
|
"version": "1.0.7",
|
||||||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user