@coffre/client 0.8.2 → 0.9.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.
@@ -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
  }
@@ -253,6 +253,7 @@ type Api = {
253
253
  public: string;
254
254
  };
255
255
  };
256
+ value: string | null;
256
257
  parent: null | {
257
258
  from: string;
258
259
  state: "inherited" | "overridden" | "removed";
@@ -281,6 +282,10 @@ type Api = {
281
282
  };
282
283
  };
283
284
  replaces?: string;
285
+ } | {
286
+ value: string;
287
+ plain: true;
288
+ replaces?: string;
284
289
  } | {
285
290
  ref: string;
286
291
  };
@@ -328,6 +333,7 @@ type Api = {
328
333
  current: boolean;
329
334
  kek: string;
330
335
  recipe: string | null;
336
+ plain: boolean;
331
337
  }[];
332
338
  };
333
339
  };
@@ -1919,6 +1925,7 @@ type SecretKey = {
1919
1925
  public: string;
1920
1926
  };
1921
1927
  };
1928
+ value: string | null;
1922
1929
  parent: null | {
1923
1930
  from: string;
1924
1931
  state: "inherited" | "overridden" | "removed";
@@ -1934,6 +1941,7 @@ type SecretVersion = {
1934
1941
  current: boolean;
1935
1942
  kek: string;
1936
1943
  recipe: string | null;
1944
+ plain: boolean;
1937
1945
  };
1938
1946
  type SessionRow = {
1939
1947
  id: string;
@@ -2309,6 +2317,7 @@ declare function createClient(options: ClientOptions): {
2309
2317
  public: string;
2310
2318
  };
2311
2319
  };
2320
+ value: string | null;
2312
2321
  parent: null | {
2313
2322
  from: string;
2314
2323
  state: "inherited" | "overridden" | "removed";
@@ -2350,6 +2359,7 @@ declare function createClient(options: ClientOptions): {
2350
2359
  current: boolean;
2351
2360
  kek: string;
2352
2361
  recipe: string | null;
2362
+ plain: boolean;
2353
2363
  }[];
2354
2364
  }>;
2355
2365
  /** A new version holding the old one's value. */
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { $ as TokenPlan, A as Decision, B as Me, C as ApprovalView, D as BindingPlan, E as AuthorizationView, F as DryRunResult, G as PersonalConfig, H as MemberAccess, I as IdentityRow, J as RemovedMember, K as ProjectSummary, L as InheritResult, M as DeletionResult, N as DismissedKey, O as BindingView, P as DryRunOutcome, Q as SetResult, R as KeysPlan, S as Api, T as AuthInfo, U as MissingKey, V as Member, W as OffboardingReport, X as SecretVersion, Y as SecretKey, Z as SessionRow, _ as byFolder, a as Params, b as olderThan, c as RouteOutput, d as planImport, et as TokenRow, f as Unreachable, g as shownMember, h as serviceName, i as ImportAction, j as Deletion, k as ConnectedApp, l as Transport, m as apiMember, n as CoffreClient, o as RouteInput, p as unreachable, q as ReferenceView, r as CoffreError, s as RouteKey, t as ClientOptions, tt as WorkloadIds, u as createClient, v as foldersOf, w as AuditEntryView, x as AccessValue, y as ageInDays, z as ListedReference } from "./index-DgII_R1y.js";
1
+ import { $ as TokenPlan, A as Decision, B as Me, C as ApprovalView, D as BindingPlan, E as AuthorizationView, F as DryRunResult, G as PersonalConfig, H as MemberAccess, I as IdentityRow, J as RemovedMember, K as ProjectSummary, L as InheritResult, M as DeletionResult, N as DismissedKey, O as BindingView, P as DryRunOutcome, Q as SetResult, R as KeysPlan, S as Api, T as AuthInfo, U as MissingKey, V as Member, W as OffboardingReport, X as SecretVersion, Y as SecretKey, Z as SessionRow, _ as byFolder, a as Params, b as olderThan, c as RouteOutput, d as planImport, et as TokenRow, f as Unreachable, g as shownMember, h as serviceName, i as ImportAction, j as Deletion, k as ConnectedApp, l as Transport, m as apiMember, n as CoffreClient, o as RouteInput, p as unreachable, q as ReferenceView, r as CoffreError, s as RouteKey, t as ClientOptions, tt as WorkloadIds, u as createClient, v as foldersOf, w as AuditEntryView, x as AccessValue, y as ageInDays, z as ListedReference } from "./index-hImoxb74.js";
2
2
  export { type AccessValue, type Api, type ApprovalView, type AuditEntryView, type AuthInfo, type AuthorizationView, type BindingPlan, type BindingView, ClientOptions, CoffreClient, CoffreError, type ConnectedApp, type Decision, type Deletion, type DeletionResult, type DismissedKey, type DryRunOutcome, type DryRunResult, type IdentityRow, ImportAction, type InheritResult, type KeysPlan, type ListedReference, type Me, type Member, type MemberAccess, type MissingKey, type OffboardingReport, Params, type PersonalConfig, type ProjectSummary, type ReferenceView, type RemovedMember, RouteInput, RouteKey, RouteOutput, type SecretKey, type SecretVersion, type SessionRow, type SetResult, type TokenPlan, type TokenRow, Transport, Unreachable, type WorkloadIds, ageInDays, apiMember, byFolder, createClient, foldersOf, olderThan, planImport, serviceName, shownMember, unreachable };
package/dist/routes.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as Params, o as RouteInput, s as RouteKey } from "./index-DgII_R1y.js";
1
+ import { a as Params, o as RouteInput, s as RouteKey } from "./index-hImoxb74.js";
2
2
  //#region src/routes.d.ts
3
3
  export type GetKey = Extract<RouteKey, `GET ${string}`>;
4
4
  /** Each GET route, with what to ask it. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coffre/client",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "The coffre API as typed function calls.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/api.ts CHANGED
@@ -256,6 +256,7 @@ export type Api = {
256
256
  public: string;
257
257
  };
258
258
  };
259
+ value: string | null;
259
260
  parent: null | {
260
261
  from: string;
261
262
  state: "inherited" | "overridden" | "removed";
@@ -284,6 +285,10 @@ export type Api = {
284
285
  };
285
286
  };
286
287
  replaces?: string;
288
+ } | {
289
+ value: string;
290
+ plain: true;
291
+ replaces?: string;
287
292
  } | {
288
293
  ref: string;
289
294
  };
@@ -331,6 +336,7 @@ export type Api = {
331
336
  current: boolean;
332
337
  kek: string;
333
338
  recipe: string | null;
339
+ plain: boolean;
334
340
  }[];
335
341
  };
336
342
  };
@@ -1966,6 +1972,7 @@ export type SecretKey = {
1966
1972
  public: string;
1967
1973
  };
1968
1974
  };
1975
+ value: string | null;
1969
1976
  parent: null | {
1970
1977
  from: string;
1971
1978
  state: "inherited" | "overridden" | "removed";
@@ -1982,6 +1989,7 @@ export type SecretVersion = {
1982
1989
  current: boolean;
1983
1990
  kek: string;
1984
1991
  recipe: string | null;
1992
+ plain: boolean;
1985
1993
  };
1986
1994
 
1987
1995
  export type SessionRow = {
package/src/credential.ts CHANGED
@@ -8,8 +8,9 @@
8
8
  * const companies = await (await schumann.fetch('/companies?q=acme')).json();
9
9
  *
10
10
  * `coffre run` sets `COFFRE_CREDENTIALS`, each credential's URL on the
11
- * proxy by name, and `COFFRE_HANDLE`, the handle that stands in for the
12
- * environment's values; the proxy injects the key, and the app never holds it.
11
+ * proxy by name, its environment's (`…/use/market/dev/schumann`), and
12
+ * `COFFRE_HANDLE`, the handle that stands in for the environment's values;
13
+ * the proxy injects the key, and the app never holds it.
13
14
  */
14
15
  export type ProxiedCredential = {
15
16
  /** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
@@ -24,6 +25,12 @@ export type ProxiedCredential = {
24
25
  * browser or anyone to use until then.
25
26
  */
26
27
  presign(path: string, options?: { method?: 'GET' | 'HEAD'; expiresIn?: number; query?: Record<string, string> }): Promise<{ url: string; expiresAt: string }>;
28
+ /**
29
+ * A signature of `message` (a string signs as its UTF-8) by a signer's
30
+ * private key, which stays in coffre: what Node's `crypto.sign(undefined,
31
+ * message, key)` gives, base64url. Each is one use in coffre's log.
32
+ */
33
+ sign(message: Uint8Array | string): Promise<string>;
27
34
  };
28
35
 
29
36
  type Env = Record<string, string | undefined>;
@@ -45,6 +52,15 @@ export function credential(
45
52
  if (typeof url !== 'string' || handle === undefined) {
46
53
  throw new Error(`no credential ${name} here: \`coffre run\` gives the app the credentials its environment holds, which the deployment's proxy names`);
47
54
  }
55
+ /** 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. */
56
+ const compute = async (asked: unknown) => {
57
+ const answer = await fetcher(url.slice(0, url.lastIndexOf('/')), {
58
+ method: 'POST',
59
+ headers: { authorization: `Bearer ${handle}`, 'content-type': 'application/json' },
60
+ body: JSON.stringify(asked),
61
+ });
62
+ return { answer, body: (await answer.json()) as { url?: string; expiresAt?: string; signature?: string; message?: string } };
63
+ };
48
64
  return {
49
65
  url,
50
66
  fetch: (path, init = {}) => {
@@ -53,15 +69,17 @@ export function credential(
53
69
  return fetcher(`${url}${path.startsWith('/') ? '' : '/'}${path}`, { ...init, headers });
54
70
  },
55
71
  presign: async (path, options = {}) => {
56
- // The environment's own URL on the proxy, which the credential's is under.
57
- const answer = await fetcher(url.slice(0, url.lastIndexOf('/')), {
58
- method: 'POST',
59
- headers: { authorization: `Bearer ${handle}`, 'content-type': 'application/json' },
60
- body: JSON.stringify({ presign: { credential: name, path, ...options } }),
61
- });
62
- const body = (await answer.json()) as { url?: string; expiresAt?: string; message?: string };
72
+ const { answer, body } = await compute({ presign: { credential: name, path, ...options } });
63
73
  if (!answer.ok || body.url === undefined) throw new Error(`coffre presigned nothing: ${body.message ?? answer.status}`);
64
74
  return { url: body.url, expiresAt: body.expiresAt! };
65
75
  },
76
+ sign: async (message) => {
77
+ const bytes = typeof message === 'string' ? new TextEncoder().encode(message) : message;
78
+ let binary = '';
79
+ for (const byte of bytes) binary += String.fromCharCode(byte);
80
+ const { answer, body } = await compute({ sign: { credential: name, message: btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '') } });
81
+ if (!answer.ok || body.signature === undefined) throw new Error(`coffre signed nothing: ${body.message ?? answer.status}`);
82
+ return body.signature;
83
+ },
66
84
  };
67
85
  }