Skip to content

Commit

Permalink
Bump versions
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Mar 28, 2024
1 parent 1860a27 commit 09e3de7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
5 changes: 4 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -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";
25 changes: 10 additions & 15 deletions dist/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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.");
Expand All @@ -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)) {
Expand All @@ -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 };
Expand Down
5 changes: 4 additions & 1 deletion examples/deps.ts
Original file line number Diff line number Diff line change
@@ -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";
4 changes: 2 additions & 2 deletions examples/pkcs8_storing.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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-----`;
}

Expand Down
12 changes: 6 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base64url } from "./deps.ts";
import { decodeBase64Url, encodeBase64Url } from "./deps.ts";
import {
create as createSignature,
verify as verifySignature,
Expand Down Expand Up @@ -83,7 +83,7 @@ function hasInvalidTimingClaims(...claimValues: unknown[]): boolean {
);
}

function validateTimingClaims(
export function validateTimingClaims(
payload: Payload,
{ expLeeway = 1, nbfLeeway = 1, ignoreExp, ignoreNbf }: VerifyOptions = {},
): void {
Expand Down Expand Up @@ -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<VerifyOptions>["audience"],
): void {
Expand Down Expand Up @@ -148,7 +148,7 @@ export function decode<PayloadType extends Payload | unknown = unknown>(
try {
const arr = jwt
.split(".")
.map(base64url.decode)
.map(decodeBase64Url)
.map((uint8Array, index) =>
index === 0 || index === 1
? JSON.parse(decoder.decode(uint8Array))
Expand Down Expand Up @@ -241,8 +241,8 @@ export async function verify<PayloadType extends Payload>(
* 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)))
}`;
}

Expand Down
4 changes: 2 additions & 2 deletions signature.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -23,7 +23,7 @@ export async function create(
key: CryptoKey | null,
signingInput: string,
): Promise<string> {
return isNull(key) ? "" : base64url.encode(
return isNull(key) ? "" : encodeBase64Url(
new Uint8Array(
await crypto.subtle.sign(
getAlgorithm(alg),
Expand Down
12 changes: 4 additions & 8 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ Deno.test({
{ alg: "HS256", typ: "JWT" },
{},

decodeHex(new TextEncoder().encode(
decodeHex(
"4d509e165d679d959431090040ab92a3f23ded87886404bc4f580e904ad3ec5f",
)),
),
],
);
assertThrows(
Expand Down Expand Up @@ -430,9 +430,7 @@ Deno.test({
header,
payload,
decodeHex(
new TextEncoder().encode(
"49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397",
),
"49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397",
),
]);
assertEquals(
Expand Down Expand Up @@ -536,9 +534,7 @@ Deno.test({
header,
payload,
decodeHex(
new TextEncoder().encode(
"49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397",
),
"49f94ac7044948c78a285d904f87f0a4c7897f7e8f3a4eb2255fda750b2cc397",
),
]);
assertEquals(
Expand Down
6 changes: 2 additions & 4 deletions tests/test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 09e3de7

Please sign in to comment.