Skip to content

Commit

Permalink
Simpler option! crypto: false
Browse files Browse the repository at this point in the history
  • Loading branch information
r-n-o committed Aug 19, 2023
1 parent 1868493 commit 0d6633e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 94 deletions.
4 changes: 3 additions & 1 deletion packages/api-key-stamper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@
"engines": {
"node": ">=16.0.0"
},
"browser": "dist/index-browser.js"
"browser": {
"crypto": false
}
}
5 changes: 4 additions & 1 deletion packages/api-key-stamper/src/__tests__/signature-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as crypto from "crypto";
import { test, expect, beforeAll } from "@jest/globals";
import { signWithApiKey as signUniversal } from "../index";
import { assertValidSignature } from "./shared";
import { signWithApiKey as signNode } from "../nodecrypto";
import { signWithApiKey as signWeb } from "../webcrypto";
Expand All @@ -8,13 +9,14 @@ import { readFixture } from "../__fixtures__/shared";
import { generateKeyPairWithOpenSsl } from "./shared";

beforeAll(() => {
// @ts-expect-error -- polyfilling the runtime for "webcrypto"
// @ts-expect-error -- polyfilling the runtime for "webcrypto" and "universal"
globalThis.crypto = crypto.webcrypto;
});

test.each([
{ impl: signNode, name: "sign (node crypto)" },
{ impl: signWeb, name: "sign (WebCrypto)" },
{ impl: signUniversal, name: "sign (universal)" },
])("sign with Turnkey fixture: $name", async ({ impl: sign }) => {
const { privateKey, publicKey, pemPublicKey } = await readFixture();

Expand Down Expand Up @@ -48,6 +50,7 @@ test.each([
test.each([
{ impl: signNode, name: "stamp (node)" },
{ impl: signWeb, name: "stamp (WebCrypto)" },
{ impl: signUniversal, name: "stamp (universal)" },
])("sign with openssl generated key pairs: $name", async ({ impl: stamp }) => {
// Run 20 times, where each run spawns 10 keys in parallel -> 200 tests in total
for (let i = 0; i < 20; i++) {
Expand Down
45 changes: 0 additions & 45 deletions packages/api-key-stamper/src/__tests__/stamp-browser-test.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/api-key-stamper/src/common.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/api-key-stamper/src/index-browser.ts

This file was deleted.

26 changes: 24 additions & 2 deletions packages/api-key-stamper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import { TApiKeyStamperConfig, stampHeaderName } from "./common";
/// <reference lib="dom" />

import { stringToBase64urlString } from "./encoding";

const signWithApiKey = require("./nodecrypto").signWithApiKey;
// Header name for an API key stamp
const stampHeaderName = "X-Stamp";

export type TApiKeyStamperConfig = {
apiPublicKey: string;
apiPrivateKey: string;
};

/**
* Signature function abstracting the differences between NodeJS and web environments for signing with API keys.
*/
let signWithApiKey: (input: {
content: string;
publicKey: string;
privateKey: string;
}) => string;

if (typeof globalThis?.crypto?.subtle !== "undefined") {
signWithApiKey = require("./webcrypto").signWithApiKey;
} else {
signWithApiKey = require("./nodecrypto").signWithApiKey;
}
export { signWithApiKey };

/**
Expand Down

0 comments on commit 0d6633e

Please sign in to comment.