Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
5d4c51ad8a
|
|||
|
78a0eeee9c
|
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "2.2.8",
|
"version": "2.2.9",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "2.2.8",
|
"version": "2.2.9",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@cloudflare/workers-types": "^4.20231025.0",
|
"@cloudflare/workers-types": "^4.20231025.0",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@tsndr/cloudflare-worker-jwt",
|
"name": "@tsndr/cloudflare-worker-jwt",
|
||||||
"version": "2.2.8",
|
"version": "2.2.9",
|
||||||
"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",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
|
|||||||
33
src/index.ts
33
src/index.ts
@@ -139,28 +139,20 @@ function byteStringToBytes(byteStr: string): Uint8Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): string {
|
function arrayBufferToBase64String(arrayBuffer: ArrayBuffer): string {
|
||||||
const byteArray = new Uint8Array(arrayBuffer)
|
return btoa(bytesToByteString(new Uint8Array(arrayBuffer)))
|
||||||
const byteStr = bytesToByteString(byteArray)
|
|
||||||
return btoa(byteStr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function base64StringToArrayBuffer(b64str: string): ArrayBuffer {
|
function base64StringToArrayBuffer(b64str: string): ArrayBuffer {
|
||||||
const byteStr = atob(b64str)
|
return byteStringToBytes(atob(b64str)).buffer
|
||||||
const bytes = byteStringToBytes(byteStr)
|
|
||||||
return bytes.buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function textToArrayBuffer(str: string): ArrayBuffer {
|
function textToArrayBuffer(str: string): ArrayBuffer {
|
||||||
const buf = decodeURI(encodeURIComponent(str)) // 2 bytes for each char
|
return byteStringToBytes(decodeURI(encodeURIComponent(str)))
|
||||||
const bytes = byteStringToBytes(buf)
|
|
||||||
return bytes
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
function arrayBufferToText(arrayBuffer: ArrayBuffer): string {
|
function arrayBufferToText(arrayBuffer: ArrayBuffer): string {
|
||||||
const byteArray = new Uint8Array(arrayBuffer)
|
return bytesToByteString(new Uint8Array(arrayBuffer))
|
||||||
const byteStr = bytesToByteString(byteArray)
|
|
||||||
return byteStr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayBufferToBase64Url(arrayBuffer: ArrayBuffer): string {
|
function arrayBufferToBase64Url(arrayBuffer: ArrayBuffer): string {
|
||||||
@@ -212,23 +204,8 @@ async function importKey(key: string | JsonWebKey, algorithm: SubtleCryptoImport
|
|||||||
}
|
}
|
||||||
|
|
||||||
function decodePayload(raw: string): JwtHeader | JwtPayload | null {
|
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 {
|
try {
|
||||||
|
raw += '='.repeat(4-(raw.length % 4))
|
||||||
return JSON.parse(atob(raw))
|
return JSON.parse(atob(raw))
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
|
|||||||
Reference in New Issue
Block a user