@coffre/client 0.8.2 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  //#region src/credential.d.ts
2
2
  /**
3
3
  * A credential through coffre's credential proxy, for an app `coffre run`
4
- * started with stand-ins (docs/design/credential-proxy.md): where its
4
+ * started with placeholders (docs/design/credential-proxy.md): where its
5
5
  * client takes no base URL to point at the proxy, or the code is new.
6
6
  *
7
7
  * import { credential } from '@coffre/client/credential';
@@ -9,8 +9,9 @@
9
9
  * const companies = await (await schumann.fetch('/companies?q=acme')).json();
10
10
  *
11
11
  * `coffre run` sets `COFFRE_CREDENTIALS`, each credential's URL on the
12
- * proxy by name, and `COFFRE_HANDLE`, the handle that stands in for the
13
- * environment's values; the proxy injects the key, and the app never holds it.
12
+ * proxy by name, its environment's (`…/use/market/dev/schumann`), and
13
+ * `COFFRE_HANDLE`, the handle that stands in for the environment's values;
14
+ * the proxy injects the key, and the app never holds it.
14
15
  */
15
16
  export type ProxiedCredential = {
16
17
  /** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
@@ -32,6 +33,12 @@ export type ProxiedCredential = {
32
33
  url: string;
33
34
  expiresAt: string;
34
35
  }>;
36
+ /**
37
+ * A signature of `message` (a string signs as its UTF-8) by a signer's
38
+ * private key, which stays in coffre: what Node's `crypto.sign(undefined,
39
+ * message, key)` gives, base64url. Each is one use in coffre's log.
40
+ */
41
+ sign(message: Uint8Array | string): Promise<string>;
35
42
  };
36
43
  type Env = Record<string, string | undefined>;
37
44
  /** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
@@ -10,6 +10,21 @@ function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input,
10
10
  const url = urls[name];
11
11
  const handle = env.COFFRE_HANDLE;
12
12
  if (typeof url !== "string" || handle === void 0) throw new Error(`no credential ${name} here: \`coffre run\` gives the app the credentials its environment holds, which the deployment's proxy names`);
13
+ /** What the handle asks coffre to make with the environment's values, at the environment's own URL on the proxy, which the credential's is under. */
14
+ const compute = async (asked) => {
15
+ const answer = await fetcher(url.slice(0, url.lastIndexOf("/")), {
16
+ method: "POST",
17
+ headers: {
18
+ authorization: `Bearer ${handle}`,
19
+ "content-type": "application/json"
20
+ },
21
+ body: JSON.stringify(asked)
22
+ });
23
+ return {
24
+ answer,
25
+ body: await answer.json()
26
+ };
27
+ };
13
28
  return {
14
29
  url,
15
30
  fetch: (path, init = {}) => {
@@ -21,24 +36,27 @@ function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input,
21
36
  });
22
37
  },
23
38
  presign: async (path, options = {}) => {
24
- const answer = await fetcher(url.slice(0, url.lastIndexOf("/")), {
25
- method: "POST",
26
- headers: {
27
- authorization: `Bearer ${handle}`,
28
- "content-type": "application/json"
29
- },
30
- body: JSON.stringify({ presign: {
31
- credential: name,
32
- path,
33
- ...options
34
- } })
35
- });
36
- const body = await answer.json();
39
+ const { answer, body } = await compute({ presign: {
40
+ credential: name,
41
+ path,
42
+ ...options
43
+ } });
37
44
  if (!answer.ok || body.url === void 0) throw new Error(`coffre presigned nothing: ${body.message ?? answer.status}`);
38
45
  return {
39
46
  url: body.url,
40
47
  expiresAt: body.expiresAt
41
48
  };
49
+ },
50
+ sign: async (message) => {
51
+ const bytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
52
+ let binary = "";
53
+ for (const byte of bytes) binary += String.fromCharCode(byte);
54
+ const { answer, body } = await compute({ sign: {
55
+ credential: name,
56
+ message: btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")
57
+ } });
58
+ if (!answer.ok || body.signature === void 0) throw new Error(`coffre signed nothing: ${body.message ?? answer.status}`);
59
+ return body.signature;
42
60
  }
43
61
  };
44
62
  }