From 09e3de72fc9ac8eb1c3083bf5d6df7145a230845 Mon Sep 17 00:00:00 2001 From: timonson Date: Thu, 28 Mar 2024 12:31:55 +0100 Subject: [PATCH] Bump versions --- deps.ts | 5 ++++- dist/mod.js | 25 ++++++++++--------------- examples/deps.ts | 5 ++++- examples/pkcs8_storing.ts | 4 ++-- mod.ts | 12 ++++++------ signature.ts | 4 ++-- tests/test.ts | 12 ++++-------- tests/test_deps.ts | 6 ++---- 8 files changed, 34 insertions(+), 39 deletions(-) diff --git a/deps.ts b/deps.ts index e574928..df9408c 100644 --- a/deps.ts +++ b/deps.ts @@ -1 +1,4 @@ -export * as base64url from "https://deno.land/std@0.205.0/encoding/base64url.ts"; +export { + decodeBase64Url, + encodeBase64Url, +} from "https://deno.land/std@0.221.0/encoding/base64url.ts"; diff --git a/dist/mod.js b/dist/mod.js index e98efd0..24dc2c3 100644 --- a/dist/mod.js +++ b/dist/mod.js @@ -91,7 +91,8 @@ const base64abc = [ ]; function encodeBase64(data) { const uint8 = validateBinaryLike(data); - let result = "", i; + let result = ""; + let i; const l = uint8.length; for(i = 2; i < l; i += 3){ result += base64abc[uint8[i - 2] >> 2]; @@ -138,20 +139,12 @@ function convertBase64urlToBase64(b64url) { function convertBase64ToBase64url(b64) { return b64.endsWith("=") ? b64.endsWith("==") ? b64.replace(/\+/g, "-").replace(/\//g, "_").slice(0, -2) : b64.replace(/\+/g, "-").replace(/\//g, "_").slice(0, -1) : b64.replace(/\+/g, "-").replace(/\//g, "_"); } -const encode = encodeBase64Url; -const decode = decodeBase64Url; function encodeBase64Url(data) { return convertBase64ToBase64url(encodeBase64(data)); } function decodeBase64Url(b64url) { return decodeBase64(convertBase64urlToBase64(b64url)); } -const mod = { - encode: encode, - decode: decode, - encodeBase64Url: encodeBase64Url, - decodeBase64Url: decodeBase64Url -}; const encoder1 = new TextEncoder(); const decoder = new TextDecoder(); function isArray(input) { @@ -304,7 +297,7 @@ async function verify1(signature, key, alg, signingInput) { return isNull(key) ? signature.length === 0 : await crypto.subtle.verify(getAlgorithm(alg), key, signature, encoder1.encode(signingInput)); } async function create(alg, key, signingInput) { - return isNull(key) ? "" : mod.encode(new Uint8Array(await crypto.subtle.sign(getAlgorithm(alg), key, encoder1.encode(signingInput)))); + return isNull(key) ? "" : encodeBase64Url(new Uint8Array(await crypto.subtle.sign(getAlgorithm(alg), key, encoder1.encode(signingInput)))); } function isExpired(exp, leeway) { return exp + leeway < Date.now() / 1000; @@ -351,9 +344,9 @@ function validateAudClaim(aud, audience) { throw new Error(`The jwt has an invalid 'aud' claim.`); } } -function decode1(jwt) { +function decode(jwt) { try { - const arr = jwt.split(".").map(mod.decode).map((uint8Array, index)=>index === 0 || index === 1 ? JSON.parse(decoder.decode(uint8Array)) : uint8Array); + const arr = jwt.split(".").map(decodeBase64Url).map((uint8Array, index)=>index === 0 || index === 1 ? JSON.parse(decoder.decode(uint8Array)) : uint8Array); if (is3Tuple(arr)) return arr; else throw new Error(); } catch { @@ -379,7 +372,7 @@ function validate([header, payload, signature], options) { } } async function verify2(jwt, key, options) { - const { header, payload, signature } = validate(decode1(jwt), options); + const { header, payload, signature } = validate(decode(jwt), options); if (verify(header.alg, key)) { if (!await verify1(signature, key, header.alg, jwt.slice(0, jwt.lastIndexOf(".")))) { throw new Error("The jwt's signature does not match the verification signature."); @@ -393,7 +386,7 @@ async function verify2(jwt, key, options) { } } function createSigningInput(header, payload) { - return `${mod.encode(encoder1.encode(JSON.stringify(header)))}.${mod.encode(encoder1.encode(JSON.stringify(payload)))}`; + return `${encodeBase64Url(encoder1.encode(JSON.stringify(header)))}.${encodeBase64Url(encoder1.encode(JSON.stringify(payload)))}`; } async function create1(header, payload, key) { if (isObject(payload)) { @@ -411,7 +404,9 @@ async function create1(header, payload, key) { function getNumericDate(exp) { return Math.round((exp instanceof Date ? exp.getTime() : Date.now() + exp * 1000) / 1000); } -export { decode1 as decode }; +export { validateTimingClaims as validateTimingClaims }; +export { validateAudClaim as validateAudClaim }; +export { decode as decode }; export { validate as validate }; export { verify2 as verify }; export { create1 as create }; diff --git a/examples/deps.ts b/examples/deps.ts index 75456e7..df9408c 100644 --- a/examples/deps.ts +++ b/examples/deps.ts @@ -1 +1,4 @@ -export * as base64 from "https://deno.land/std@0.205.0/encoding/base64.ts"; +export { + decodeBase64Url, + encodeBase64Url, +} from "https://deno.land/std@0.221.0/encoding/base64url.ts"; diff --git a/examples/pkcs8_storing.ts b/examples/pkcs8_storing.ts index 97aa110..ff1ef4f 100644 --- a/examples/pkcs8_storing.ts +++ b/examples/pkcs8_storing.ts @@ -1,4 +1,4 @@ -import { base64 } from "./deps.ts"; +import { encodeBase64Url } from "./deps.ts"; /* Import a PEM encoded RSA private key, to use for RSA-PSS signing. @@ -28,7 +28,7 @@ function importPrivateKey(pem: string) { async function generatePemFromPrivateCryptoKey(privateKey: CryptoKey) { const exportedKey = await crypto.subtle.exportKey("pkcs8", privateKey); - const exportedAsBase64 = base64.encode(exportedKey); + const exportedAsBase64 = encodeBase64Url(exportedKey); return `-----BEGIN PRIVATE KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`; } diff --git a/mod.ts b/mod.ts index e626f36..3feb1fb 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,4 @@ -import { base64url } from "./deps.ts"; +import { decodeBase64Url, encodeBase64Url } from "./deps.ts"; import { create as createSignature, verify as verifySignature, @@ -83,7 +83,7 @@ function hasInvalidTimingClaims(...claimValues: unknown[]): boolean { ); } -function validateTimingClaims( +export function validateTimingClaims( payload: Payload, { expLeeway = 1, nbfLeeway = 1, ignoreExp, ignoreNbf }: VerifyOptions = {}, ): void { @@ -111,7 +111,7 @@ function hasValidAudClaim(claimValue: unknown): claimValue is Payload["aud"] { else return isArray(claimValue) && claimValue.every(isString); } -function validateAudClaim( +export function validateAudClaim( aud: unknown, audience: Required["audience"], ): void { @@ -148,7 +148,7 @@ export function decode( try { const arr = jwt .split(".") - .map(base64url.decode) + .map(decodeBase64Url) .map((uint8Array, index) => index === 0 || index === 1 ? JSON.parse(decoder.decode(uint8Array)) @@ -241,8 +241,8 @@ export async function verify( * BASE64URL(JWS Signature) */ function createSigningInput(header: Header, payload: Payload): string { - return `${base64url.encode(encoder.encode(JSON.stringify(header)))}.${ - base64url.encode(encoder.encode(JSON.stringify(payload))) + return `${encodeBase64Url(encoder.encode(JSON.stringify(header)))}.${ + encodeBase64Url(encoder.encode(JSON.stringify(payload))) }`; } diff --git a/signature.ts b/signature.ts index 5b67025..f91865e 100644 --- a/signature.ts +++ b/signature.ts @@ -1,5 +1,5 @@ import { getAlgorithm } from "./algorithm.ts"; -import { base64url } from "./deps.ts"; +import { encodeBase64Url } from "./deps.ts"; import { encoder, isNull } from "./util.ts"; import type { Algorithm } from "./algorithm.ts"; @@ -23,7 +23,7 @@ export async function create( key: CryptoKey | null, signingInput: string, ): Promise { - return isNull(key) ? "" : base64url.encode( + return isNull(key) ? "" : encodeBase64Url( new Uint8Array( await crypto.subtle.sign( getAlgorithm(alg), diff --git a/tests/test.ts b/tests/test.ts index e2a66f8..5254887 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -385,9 +385,9 @@ Deno.test({ { alg: "HS256", typ: "JWT" }, {}, - decodeHex(new TextEncoder().encode( + decodeHex( "4d509e165d679d959431090040ab92a3f23ded87886404bc4f580e904ad3ec5f", - )), + ), ], ); assertThrows( @@ -430,9 +430,7 @@ Deno.test({ header, payload, decodeHex( - new TextEncoder().encode( - "49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397", - ), + "49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397", ), ]); assertEquals( @@ -536,9 +534,7 @@ Deno.test({ header, payload, decodeHex( - new TextEncoder().encode( - "49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397", - ), + "49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397", ), ]); assertEquals( diff --git a/tests/test_deps.ts b/tests/test_deps.ts index d6083c1..fefe42d 100644 --- a/tests/test_deps.ts +++ b/tests/test_deps.ts @@ -2,8 +2,6 @@ export { assertEquals, assertRejects, assertThrows, -} from "https://deno.land/std@0.205.0/testing/asserts.ts"; +} from "https://deno.land/std@0.221.0/testing/asserts.ts"; -export { - decode as decodeHex, -} from "https://deno.land/std@0.205.0/encoding/hex.ts"; +export { decodeHex } from "https://deno.land/std@0.221.0/encoding/hex.ts";