@tsndr/cloudflare-worker-jwt
Advanced tools
Comparing version 1.0.6 to 1.0.7
22
index.js
@@ -30,7 +30,5 @@ class Base64URL { | ||
utf8ToUint8Array(str) { | ||
const chars = [] | ||
str = btoa(unescape(encodeURIComponent(str))) | ||
return Base64URL.parse(str) | ||
return Base64URL.parse(btoa(unescape(encodeURIComponent(str)))) | ||
} | ||
async sign(payload, secret, alg = 'HS256') { | ||
async sign(payload, secret, algorithm = 'HS256') { | ||
if (payload === null || typeof payload !== 'object') | ||
@@ -40,9 +38,9 @@ throw new Error('payload must be an object') | ||
throw new Error('secret must be a string') | ||
if (typeof alg !== 'string') | ||
throw new Error('alg must be a string') | ||
const importAlgorithm = this.algorithms[alg] | ||
if (typeof algorithm !== 'string') | ||
throw new Error('algorithm must be a string') | ||
const importAlgorithm = this.algorithms[algorithm] | ||
if (!importAlgorithm) | ||
throw new Error('algorithm not found') | ||
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']) | ||
@@ -61,3 +59,3 @@ const characters = payloadAsJSON.split('') | ||
} | ||
async verify(token, secret, alg = 'HS256') { | ||
async verify(token, secret, algorithm = 'HS256') { | ||
if (typeof token !== 'string') | ||
@@ -67,8 +65,8 @@ throw new Error('token must be a string') | ||
throw new Error('secret must be a string') | ||
if (typeof alg !== 'string') | ||
throw new Error('alg must be a string') | ||
if (typeof algorithm !== 'string') | ||
throw new Error('algorithm must be a string') | ||
const tokenParts = token.split('.') | ||
if (tokenParts.length !== 3) | ||
throw new Error('token must have 3 parts') | ||
const importAlgorithm = this.algorithms[alg] | ||
const importAlgorithm = this.algorithms[algorithm] | ||
if (!importAlgorithm) | ||
@@ -75,0 +73,0 @@ throw new Error('algorithm not found') |
{ | ||
"name": "@tsndr/cloudflare-worker-jwt", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8080
6
104