Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
0c0919f78d
|
|||
|
7299ea0614
|
|||
|
7004cd16ed
|
|||
|
5d4c51ad8a
|
|||
|
78a0eeee9c
|
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-jwt",
|
||||
"version": "2.2.8",
|
||||
"version": "2.2.10",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@tsndr/cloudflare-worker-jwt",
|
||||
"version": "2.2.8",
|
||||
"version": "2.2.10",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20231025.0",
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
{
|
||||
"name": "@tsndr/cloudflare-worker-jwt",
|
||||
"version": "2.2.8",
|
||||
"version": "2.2.10",
|
||||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"exports": "./index.js",
|
||||
"types": "index.d.ts",
|
||||
"engine": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "jest"
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import crypto from 'node:crypto'
|
||||
Object.defineProperty(global, 'crypto', { value: { subtle: crypto.webcrypto.subtle }})
|
||||
|
||||
|
||||
33
src/index.ts
33
src/index.ts
@@ -139,28 +139,20 @@ function byteStringToBytes(byteStr: string): Uint8Array {
|
||||
}
|
||||
|
||||
function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): string {
|
||||
const byteArray = new Uint8Array(arrayBuffer)
|
||||
const byteStr = bytesToByteString(byteArray)
|
||||
return btoa(byteStr)
|
||||
return btoa(bytesToByteString(new Uint8Array(arrayBuffer)))
|
||||
}
|
||||
|
||||
function base64StringToArrayBuffer(b64str: string): ArrayBuffer {
|
||||
const byteStr = atob(b64str)
|
||||
const bytes = byteStringToBytes(byteStr)
|
||||
return bytes.buffer
|
||||
return byteStringToBytes(atob(b64str)).buffer
|
||||
}
|
||||
|
||||
function textToArrayBuffer(str: string): ArrayBuffer {
|
||||
const buf = decodeURI(encodeURIComponent(str)) // 2 bytes for each char
|
||||
const bytes = byteStringToBytes(buf)
|
||||
return bytes
|
||||
return byteStringToBytes(decodeURI(encodeURIComponent(str)))
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
function arrayBufferToText(arrayBuffer: ArrayBuffer): string {
|
||||
const byteArray = new Uint8Array(arrayBuffer)
|
||||
const byteStr = bytesToByteString(byteArray)
|
||||
return byteStr
|
||||
return bytesToByteString(new Uint8Array(arrayBuffer))
|
||||
}
|
||||
|
||||
function arrayBufferToBase64Url(arrayBuffer: ArrayBuffer): string {
|
||||
@@ -212,23 +204,8 @@ async function importKey(key: string | JsonWebKey, algorithm: SubtleCryptoImport
|
||||
}
|
||||
|
||||
function decodePayload(raw: string): JwtHeader | JwtPayload | null {
|
||||
switch (raw.length % 4) {
|
||||
case 0:
|
||||
break
|
||||
case 1:
|
||||
raw += '==='
|
||||
break
|
||||
case 2:
|
||||
raw += '=='
|
||||
break
|
||||
case 3:
|
||||
raw += '='
|
||||
break
|
||||
default:
|
||||
throw new Error('Invalid base64url string!')
|
||||
}
|
||||
|
||||
try {
|
||||
raw += '='.repeat(4-(raw.length % 4))
|
||||
return JSON.parse(atob(raw))
|
||||
} catch {
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user