@tsndr/cloudflare-worker-jwt
Advanced tools
Comparing version 2.4.2 to 2.4.3
@@ -40,3 +40,3 @@ import { textToArrayBuffer, arrayBufferToBase64Url, base64UrlToArrayBuffer, textToBase64Url, importKey, decodePayload } from "./utils"; | ||
const partialToken = `${textToBase64Url(JSON.stringify({ ...options.header, alg: options.algorithm }))}.${textToBase64Url(JSON.stringify(payload))}`; | ||
const key = secret instanceof CryptoKey ? secret : await importKey(secret, algorithm); | ||
const key = secret instanceof CryptoKey ? secret : await importKey(secret, algorithm, ['sign']); | ||
const signature = await crypto.subtle.sign(algorithm, key, textToArrayBuffer(partialToken)); | ||
@@ -78,3 +78,3 @@ return `${partialToken}.${arrayBufferToBase64Url(signature)}`; | ||
throw new Error('EXPIRED'); | ||
const key = secret instanceof CryptoKey ? secret : await importKey(secret, algorithm); | ||
const key = secret instanceof CryptoKey ? secret : await importKey(secret, algorithm, ['verify']); | ||
return await crypto.subtle.verify(algorithm, key, base64UrlToArrayBuffer(tokenParts[2]), textToArrayBuffer(`${tokenParts[0]}.${tokenParts[1]}`)); | ||
@@ -81,0 +81,0 @@ } |
{ | ||
"name": "@tsndr/cloudflare-worker-jwt", | ||
"version": "2.4.2", | ||
"version": "2.4.3", | ||
"description": "A lightweight JWT implementation with ZERO dependencies for Cloudflare Worker", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -12,7 +12,9 @@ /// <reference types="@cloudflare/workers-types" /> | ||
export declare function pemToBinary(pem: string): ArrayBuffer; | ||
export declare function importTextSecret(key: string, algorithm: SubtleCryptoImportKeyAlgorithm): Promise<CryptoKey>; | ||
export declare function importJwk(key: JsonWebKey, algorithm: SubtleCryptoImportKeyAlgorithm): Promise<CryptoKey>; | ||
export declare function importPublicKey(key: string, algorithm: SubtleCryptoImportKeyAlgorithm): Promise<CryptoKey>; | ||
export declare function importPrivateKey(key: string, algorithm: SubtleCryptoImportKeyAlgorithm): Promise<CryptoKey>; | ||
export declare function importKey(key: string | JsonWebKey, algorithm: SubtleCryptoImportKeyAlgorithm): Promise<CryptoKey>; | ||
type KeyUsages = 'sign' | 'verify'; | ||
export declare function importTextSecret(key: string, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey>; | ||
export declare function importJwk(key: JsonWebKey, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey>; | ||
export declare function importPublicKey(key: string, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey>; | ||
export declare function importPrivateKey(key: string, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey>; | ||
export declare function importKey(key: string | JsonWebKey, algorithm: SubtleCryptoImportKeyAlgorithm, keyUsages: KeyUsages[]): Promise<CryptoKey>; | ||
export declare function decodePayload<T = any>(raw: string): T | undefined; | ||
export {}; |
26
utils.js
@@ -42,24 +42,24 @@ export function bytesToByteString(bytes) { | ||
} | ||
export async function importTextSecret(key, algorithm) { | ||
return await crypto.subtle.importKey("raw", textToArrayBuffer(key), algorithm, true, ["verify", "sign"]); | ||
export async function importTextSecret(key, algorithm, keyUsages) { | ||
return await crypto.subtle.importKey("raw", textToArrayBuffer(key), algorithm, true, keyUsages); | ||
} | ||
export async function importJwk(key, algorithm) { | ||
return await crypto.subtle.importKey("jwk", key, algorithm, true, ["verify", "sign"]); | ||
export async function importJwk(key, algorithm, keyUsages) { | ||
return await crypto.subtle.importKey("jwk", key, algorithm, true, keyUsages); | ||
} | ||
export async function importPublicKey(key, algorithm) { | ||
return await crypto.subtle.importKey("spki", pemToBinary(key), algorithm, true, ["verify"]); | ||
export async function importPublicKey(key, algorithm, keyUsages) { | ||
return await crypto.subtle.importKey("spki", pemToBinary(key), algorithm, true, keyUsages); | ||
} | ||
export async function importPrivateKey(key, algorithm) { | ||
return await crypto.subtle.importKey("pkcs8", pemToBinary(key), algorithm, true, ["sign"]); | ||
export async function importPrivateKey(key, algorithm, keyUsages) { | ||
return await crypto.subtle.importKey("pkcs8", pemToBinary(key), algorithm, true, keyUsages); | ||
} | ||
export async function importKey(key, algorithm) { | ||
export async function importKey(key, algorithm, keyUsages) { | ||
if (typeof key === 'object') | ||
return importJwk(key, algorithm); | ||
return importJwk(key, algorithm, keyUsages); | ||
if (typeof key !== 'string') | ||
throw new Error('Unsupported key type!'); | ||
if (key.includes('PUBLIC')) | ||
return importPublicKey(key, algorithm); | ||
return importPublicKey(key, algorithm, keyUsages); | ||
if (key.includes('PRIVATE')) | ||
return importPrivateKey(key, algorithm); | ||
return importTextSecret(key, algorithm); | ||
return importPrivateKey(key, algorithm, keyUsages); | ||
return importTextSecret(key, algorithm, keyUsages); | ||
} | ||
@@ -66,0 +66,0 @@ export function decodePayload(raw) { |
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
19998
317