@coffre/client 0.14.5 → 0.14.7
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.
- package/dist/credential-DyAuH0om.d.ts +57 -0
- package/dist/credential.d.ts +2 -54
- package/dist/credential.js +68 -2
- package/dist/{dpop-CYJtAnvD.d.ts → dpop-C9EOdNCi.d.ts} +3 -1
- package/dist/dpop.d.ts +2 -2
- package/dist/dpop.js +3 -1
- package/dist/{workload-BuKb9O4W.js → workload-ClMNMVqA.js} +42 -37
- package/dist/workload-postgres.d.ts +1 -1
- package/dist/workload-postgres.js +1 -1
- package/dist/{workload-DwWrQQg1.d.ts → workload-w5EbGZgy.d.ts} +10 -2
- package/dist/workload.d.ts +3 -2
- package/dist/workload.js +1 -1
- package/package.json +1 -1
- package/src/credential.ts +43 -3
- package/src/dpop.ts +3 -0
- package/src/workload.ts +34 -5
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
//#region src/credential.d.ts
|
|
2
|
+
/** What a cipher's encrypt gives: the IV coffre drew, 12 bytes, the ciphertext, and the 16-byte tag, which the app stores in its own format. */
|
|
3
|
+
type Sealed = {
|
|
4
|
+
iv: Uint8Array;
|
|
5
|
+
ciphertext: Uint8Array;
|
|
6
|
+
tag: Uint8Array;
|
|
7
|
+
};
|
|
8
|
+
type ProxiedCredential = {
|
|
9
|
+
/** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
|
|
10
|
+
url: string;
|
|
11
|
+
/** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
|
|
12
|
+
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
13
|
+
/**
|
|
14
|
+
* A presigned GET (or HEAD) of `path` under an AWS-signed credential's
|
|
15
|
+
* upstream, `/bucket/key` with each segment encoded as in a URL: coffre
|
|
16
|
+
* signs it with the real key pair, for `expiresIn` seconds (15 minutes
|
|
17
|
+
* unless set, an hour at most), and the URL is the upstream's own, for a
|
|
18
|
+
* browser or anyone to use until then.
|
|
19
|
+
*/
|
|
20
|
+
presign(path: string, options?: {
|
|
21
|
+
method?: 'GET' | 'HEAD';
|
|
22
|
+
expiresIn?: number;
|
|
23
|
+
query?: Record<string, string>;
|
|
24
|
+
}): Promise<{
|
|
25
|
+
url: string;
|
|
26
|
+
expiresAt: string;
|
|
27
|
+
}>;
|
|
28
|
+
/**
|
|
29
|
+
* A signature of `message` (a string signs as its UTF-8) by a signer's
|
|
30
|
+
* key, which stays in coffre: a private key's, what Node's
|
|
31
|
+
* `crypto.sign(undefined, message, key)` gives, base64url, each one use
|
|
32
|
+
* in coffre's log; an HMAC secret's, as its `hmac()` writes it. A message
|
|
33
|
+
* past 1 MiB goes raw, its bytes as the request's body, which coffre
|
|
34
|
+
* signs as they come, never held: an `hmac()` signer's whose `maxBytes`
|
|
35
|
+
* takes it, SumSub's document upload say.
|
|
36
|
+
*/
|
|
37
|
+
sign(message: Uint8Array | string): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Whether `signature`, as its header carried it (`sha256=…`), is a
|
|
40
|
+
* verifier's HMAC of `message`: an inbound webhook's body, the bytes as
|
|
41
|
+
* they came, never the JSON parsed and written again. The secret stays
|
|
42
|
+
* in coffre, which compares in constant time and says only yes or no.
|
|
43
|
+
*/
|
|
44
|
+
verify(message: Uint8Array | string, signature: string): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* `plaintext` (a string as its UTF-8) encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad`
|
|
47
|
+
* if given: under an IV coffre draws for each, never the app's.
|
|
48
|
+
*/
|
|
49
|
+
encrypt(plaintext: Uint8Array | string, aad?: Uint8Array | string): Promise<Sealed>;
|
|
50
|
+
/** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt: whatever part is wrong, coffre says no more. */
|
|
51
|
+
decrypt(sealed: Sealed, aad?: Uint8Array | string): Promise<Uint8Array | null>;
|
|
52
|
+
};
|
|
53
|
+
type Env = Record<string, string | undefined>;
|
|
54
|
+
/** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
|
|
55
|
+
declare function credential(name: string, env?: Env, fetcher?: (input: string, init?: RequestInit) => Promise<Response>): ProxiedCredential;
|
|
56
|
+
//#endregion
|
|
57
|
+
export { Sealed as n, credential as r, ProxiedCredential as t };
|
package/dist/credential.d.ts
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* A credential through coffre's credential proxy, for an app `coffre run`
|
|
4
|
-
* started with placeholders (docs/design/credential-proxy.md): where its
|
|
5
|
-
* client takes no base URL to point at the proxy, or the code is new.
|
|
6
|
-
*
|
|
7
|
-
* import { credential } from '@coffre/client/credential';
|
|
8
|
-
* const schumann = credential('schumann');
|
|
9
|
-
* const companies = await (await schumann.fetch('/companies?q=acme')).json();
|
|
10
|
-
*
|
|
11
|
-
* `coffre run` sets `COFFRE_CREDENTIALS`, each credential's URL on the
|
|
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.
|
|
15
|
-
*/
|
|
16
|
-
export type ProxiedCredential = {
|
|
17
|
-
/** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
|
|
18
|
-
url: string;
|
|
19
|
-
/** `fetch`, at a path under the credential's upstream, with the handle the proxy takes. */
|
|
20
|
-
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
21
|
-
/**
|
|
22
|
-
* A presigned GET (or HEAD) of `path` under an AWS-signed credential's
|
|
23
|
-
* upstream, `/bucket/key` with each segment encoded as in a URL: coffre
|
|
24
|
-
* signs it with the real key pair, for `expiresIn` seconds (15 minutes
|
|
25
|
-
* unless set, an hour at most), and the URL is the upstream's own, for a
|
|
26
|
-
* browser or anyone to use until then.
|
|
27
|
-
*/
|
|
28
|
-
presign(path: string, options?: {
|
|
29
|
-
method?: 'GET' | 'HEAD';
|
|
30
|
-
expiresIn?: number;
|
|
31
|
-
query?: Record<string, string>;
|
|
32
|
-
}): Promise<{
|
|
33
|
-
url: string;
|
|
34
|
-
expiresAt: string;
|
|
35
|
-
}>;
|
|
36
|
-
/**
|
|
37
|
-
* A signature of `message` (a string signs as its UTF-8) by a signer's
|
|
38
|
-
* key, which stays in coffre: a private key's, what Node's
|
|
39
|
-
* `crypto.sign(undefined, message, key)` gives, base64url, each one use
|
|
40
|
-
* in coffre's log; an HMAC secret's, as its `hmac()` writes it.
|
|
41
|
-
*/
|
|
42
|
-
sign(message: Uint8Array | string): Promise<string>;
|
|
43
|
-
/**
|
|
44
|
-
* Whether `signature`, as its header carried it (`sha256=…`), is a
|
|
45
|
-
* verifier's HMAC of `message`: an inbound webhook's body, the bytes as
|
|
46
|
-
* they came, never the JSON parsed and written again. The secret stays
|
|
47
|
-
* in coffre, which compares in constant time and says only yes or no.
|
|
48
|
-
*/
|
|
49
|
-
verify(message: Uint8Array | string, signature: string): Promise<boolean>;
|
|
50
|
-
};
|
|
51
|
-
type Env = Record<string, string | undefined>;
|
|
52
|
-
/** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
|
|
53
|
-
export declare function credential(name: string, env?: Env, fetcher?: (input: string, init?: RequestInit) => Promise<Response>): ProxiedCredential;
|
|
54
|
-
//#endregion
|
|
1
|
+
import { n as Sealed, r as credential, t as ProxiedCredential } from "./credential-DyAuH0om.js";
|
|
2
|
+
export { ProxiedCredential, Sealed, credential };
|
package/dist/credential.js
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
|
+
import { fromBase64url } from "./dpop.js";
|
|
1
2
|
//#region src/credential.ts
|
|
3
|
+
/**
|
|
4
|
+
* A credential through coffre's credential proxy, for an app `coffre run`
|
|
5
|
+
* started with placeholders (docs/design/credential-proxy.md): where its
|
|
6
|
+
* client takes no base URL to point at the proxy, or the code is new.
|
|
7
|
+
*
|
|
8
|
+
* import { credential } from '@coffre/client/credential';
|
|
9
|
+
* const schumann = credential('schumann');
|
|
10
|
+
* const companies = await (await schumann.fetch('/companies?q=acme')).json();
|
|
11
|
+
*
|
|
12
|
+
* `coffre run` sets `COFFRE_CREDENTIALS`, each credential's URL on the
|
|
13
|
+
* proxy by name, its environment's (`…/use/market/dev/schumann`), and
|
|
14
|
+
* `COFFRE_HANDLE`, the handle that stands in for the environment's values;
|
|
15
|
+
* the proxy injects the key, and the app never holds it.
|
|
16
|
+
*/
|
|
2
17
|
/** The credential `name`, as `coffre run` gave the app it; `env` and `fetcher` for tests and odd runtimes. */
|
|
3
18
|
function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input, init) => fetch(input, init)) {
|
|
4
19
|
let urls;
|
|
@@ -25,6 +40,24 @@ function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input,
|
|
|
25
40
|
body: await answer.json()
|
|
26
41
|
};
|
|
27
42
|
};
|
|
43
|
+
/** A message past what JSON takes, signed raw: its bytes as the body, with the SHA-256 that `coffre serve` streams it by, and coffre checks. */
|
|
44
|
+
const raw = async (bytes) => {
|
|
45
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
46
|
+
const answer = await fetcher(`${url.slice(0, url.lastIndexOf("/"))}?sign=${encodeURIComponent(name)}`, {
|
|
47
|
+
method: "POST",
|
|
48
|
+
headers: {
|
|
49
|
+
authorization: `Bearer ${handle}`,
|
|
50
|
+
"content-type": "application/octet-stream",
|
|
51
|
+
"content-length": String(bytes.length),
|
|
52
|
+
"content-digest": `sha-256=:${btoa(String.fromCharCode(...digest))}:`
|
|
53
|
+
},
|
|
54
|
+
body: bytes
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
answer,
|
|
58
|
+
body: await answer.json()
|
|
59
|
+
};
|
|
60
|
+
};
|
|
28
61
|
const bytesOf = (message) => {
|
|
29
62
|
const bytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
30
63
|
let binary = "";
|
|
@@ -54,9 +87,10 @@ function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input,
|
|
|
54
87
|
};
|
|
55
88
|
},
|
|
56
89
|
sign: async (message) => {
|
|
57
|
-
const
|
|
90
|
+
const bytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
91
|
+
const { answer, body } = bytes.length > 1048576 ? await raw(bytes) : await compute({ sign: {
|
|
58
92
|
credential: name,
|
|
59
|
-
message: bytesOf(
|
|
93
|
+
message: bytesOf(bytes)
|
|
60
94
|
} });
|
|
61
95
|
if (!answer.ok || body.signature === void 0) throw new Error(`coffre signed nothing: ${body.message ?? answer.status}`);
|
|
62
96
|
return body.signature;
|
|
@@ -69,6 +103,38 @@ function credential(name, env = globalThis.process?.env ?? {}, fetcher = (input,
|
|
|
69
103
|
} });
|
|
70
104
|
if (!answer.ok || typeof body.valid !== "boolean") throw new Error(`coffre verified nothing: ${body.message ?? answer.status}`);
|
|
71
105
|
return body.valid;
|
|
106
|
+
},
|
|
107
|
+
encrypt: async (plaintext, aad) => {
|
|
108
|
+
const { answer, body } = await compute({ encrypt: {
|
|
109
|
+
credential: name,
|
|
110
|
+
plaintext: bytesOf(plaintext),
|
|
111
|
+
...aad === void 0 ? {} : { aad: bytesOf(aad) }
|
|
112
|
+
} });
|
|
113
|
+
const [iv, ciphertext, tag] = [
|
|
114
|
+
body.iv,
|
|
115
|
+
body.ciphertext,
|
|
116
|
+
body.tag
|
|
117
|
+
].map((part) => typeof part === "string" ? fromBase64url(part) : null);
|
|
118
|
+
if (!answer.ok || !iv || !ciphertext || !tag) throw new Error(`coffre encrypted nothing: ${body.message ?? answer.status}`);
|
|
119
|
+
return {
|
|
120
|
+
iv,
|
|
121
|
+
ciphertext,
|
|
122
|
+
tag
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
decrypt: async ({ iv, ciphertext, tag }, aad) => {
|
|
126
|
+
const { answer, body } = await compute({ decrypt: {
|
|
127
|
+
credential: name,
|
|
128
|
+
iv: bytesOf(iv),
|
|
129
|
+
ciphertext: bytesOf(ciphertext),
|
|
130
|
+
tag: bytesOf(tag),
|
|
131
|
+
...aad === void 0 ? {} : { aad: bytesOf(aad) }
|
|
132
|
+
} });
|
|
133
|
+
if (!answer.ok || body.plaintext !== null && typeof body.plaintext !== "string") throw new Error(`coffre decrypted nothing: ${body.message ?? answer.status}`);
|
|
134
|
+
if (body.plaintext === null) return null;
|
|
135
|
+
const plaintext = fromBase64url(body.plaintext);
|
|
136
|
+
if (plaintext === null) throw new Error("coffre decrypted nothing: its answer is no base64url");
|
|
137
|
+
return plaintext;
|
|
72
138
|
}
|
|
73
139
|
};
|
|
74
140
|
}
|
|
@@ -97,6 +97,8 @@ declare function bodyDigest(body: Uint8Array | string): Promise<string>;
|
|
|
97
97
|
* not the zone's.
|
|
98
98
|
*/
|
|
99
99
|
declare const MAX_DIGESTED_BODY: number;
|
|
100
|
+
/** The longest message `sign` sends as JSON, base64url: what coffre reads whole. A longer one goes raw, the body itself. */
|
|
101
|
+
declare const JSON_MESSAGE_BYTES: number;
|
|
100
102
|
/** A body's `Content-Digest` (RFC 9530) from its SHA-256 in hex, as the SDK and `coffre serve` send it: `sha-256=:<base64>:`. */
|
|
101
103
|
declare function contentDigest(sha256: string): string;
|
|
102
104
|
/**
|
|
@@ -207,4 +209,4 @@ declare function createProof(key: ProofKey, request: {
|
|
|
207
209
|
jti?: string;
|
|
208
210
|
}): Promise<string>;
|
|
209
211
|
//#endregion
|
|
210
|
-
export {
|
|
212
|
+
export { sentTarget as C, tokenDigest as E, requestDigest as S, thumbprint as T, createProof as _, METHOD_TOKEN as a, fromBase64url as b, PublicJwk as c, base64url as d, bodyDigest as f, contentDigest as g, canonicalRequest as h, MAX_SIGNED_HEADERS as i, UNSIGNED_BODY as l, canonicalHtu as m, MAX_DIGESTED_BODY as n, ProofClaims as o, bodyLine as p, MAX_PROOF_LENGTH as r, ProofKey as s, JSON_MESSAGE_BYTES as t, UNSIGNED_HEADERS as u, declaredDigest as v, signedHeaders as w, generateProofKey as x, fetchMethod as y };
|
package/dist/dpop.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as
|
|
2
|
-
export { MAX_DIGESTED_BODY, MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, ProofClaims, ProofKey, PublicJwk, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, contentDigest, createProof, declaredDigest, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
|
|
1
|
+
import { C as sentTarget, E as tokenDigest, S as requestDigest, T as thumbprint, _ as createProof, a as METHOD_TOKEN, b as fromBase64url, c as PublicJwk, d as base64url, f as bodyDigest, g as contentDigest, h as canonicalRequest, i as MAX_SIGNED_HEADERS, l as UNSIGNED_BODY, m as canonicalHtu, n as MAX_DIGESTED_BODY, o as ProofClaims, p as bodyLine, r as MAX_PROOF_LENGTH, s as ProofKey, t as JSON_MESSAGE_BYTES, u as UNSIGNED_HEADERS, v as declaredDigest, w as signedHeaders, x as generateProofKey, y as fetchMethod } from "./dpop-C9EOdNCi.js";
|
|
2
|
+
export { JSON_MESSAGE_BYTES, MAX_DIGESTED_BODY, MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, ProofClaims, ProofKey, PublicJwk, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, contentDigest, createProof, declaredDigest, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
|
package/dist/dpop.js
CHANGED
|
@@ -167,6 +167,8 @@ async function bodyDigest(body) {
|
|
|
167
167
|
* not the zone's.
|
|
168
168
|
*/
|
|
169
169
|
const MAX_DIGESTED_BODY = 99614720;
|
|
170
|
+
/** The longest message `sign` sends as JSON, base64url: what coffre reads whole. A longer one goes raw, the body itself. */
|
|
171
|
+
const JSON_MESSAGE_BYTES = 1048576;
|
|
170
172
|
/** A body's `Content-Digest` (RFC 9530) from its SHA-256 in hex, as the SDK and `coffre serve` send it: `sha-256=:<base64>:`. */
|
|
171
173
|
function contentDigest(sha256) {
|
|
172
174
|
if (!SHA256_HEX.test(sha256)) throw new TypeError("a Content-Digest is made from a SHA-256 in hex");
|
|
@@ -324,4 +326,4 @@ async function createProof(key, request) {
|
|
|
324
326
|
return `${input}.${base64url(new Uint8Array(signature))}`;
|
|
325
327
|
}
|
|
326
328
|
//#endregion
|
|
327
|
-
export { MAX_DIGESTED_BODY, MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, contentDigest, createProof, declaredDigest, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
|
|
329
|
+
export { JSON_MESSAGE_BYTES, MAX_DIGESTED_BODY, MAX_PROOF_LENGTH, MAX_SIGNED_HEADERS, METHOD_TOKEN, UNSIGNED_BODY, UNSIGNED_HEADERS, base64url, bodyDigest, bodyLine, canonicalHtu, canonicalRequest, contentDigest, createProof, declaredDigest, fetchMethod, fromBase64url, generateProofKey, requestDigest, sentTarget, signedHeaders, thumbprint, tokenDigest };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as CoffreError } from "./src-BCKSeCER.js";
|
|
2
|
-
import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, generateProofKey, thumbprint } from "./dpop.js";
|
|
2
|
+
import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, fromBase64url, generateProofKey, thumbprint } from "./dpop.js";
|
|
3
3
|
//#region src/workload-proofs.ts
|
|
4
4
|
/** `<audience>#jkt=<thumbprint>`: an audience only this key can use (section 4.2). */
|
|
5
5
|
function keyAudience(context) {
|
|
@@ -350,35 +350,6 @@ function enrollment(code) {
|
|
|
350
350
|
const sleep$1 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
351
351
|
//#endregion
|
|
352
352
|
//#region src/workload.ts
|
|
353
|
-
/**
|
|
354
|
-
* A workload's session with coffre (docs/design/workload-access.md, section
|
|
355
|
-
* 9.1): it proves who it is once, gets a session bound to a key that never
|
|
356
|
-
* leaves this process, and from then on uses its credentials through coffre,
|
|
357
|
-
* every request signed. The app holds no secret that works anywhere else.
|
|
358
|
-
*
|
|
359
|
-
* import { workload, enrollment } from '@coffre/client/workload';
|
|
360
|
-
*
|
|
361
|
-
* const coffre = await workload({ url: 'https://secrets.acme.example', service: 'api-prod', proof: enrollment(process.env.COFFRE_ENROLL) });
|
|
362
|
-
* const openai = new OpenAI({ apiKey: 'coffre', baseURL: coffre.url('openai'), fetch: coffre.fetch });
|
|
363
|
-
* const charge = await coffre.credential('stripe').fetch('/v1/charges', { method: 'POST', body: new URLSearchParams({ amount: '2000' }) });
|
|
364
|
-
*
|
|
365
|
-
* What it does for the app, so the app does nothing:
|
|
366
|
-
*
|
|
367
|
-
* - a P-256 key made here, non-extractable; the key, the session's token
|
|
368
|
-
* and the gate's nonce live in this closure only, never on disk or in
|
|
369
|
-
* the environment;
|
|
370
|
-
* - the exchange, retried while an enrollment code is `full` (5 minutes)
|
|
371
|
-
* or its session awaits approval (10), and after a spent projected token,
|
|
372
|
-
* once the kubelet writes the next;
|
|
373
|
-
* - renewal at half-life, every 30 seconds for an enrollment session, whose
|
|
374
|
-
* expiry is its liveness; a fresh proof, with the same key, before the
|
|
375
|
-
* session's hard end, `notAfter`; and a new session whenever the old one
|
|
376
|
-
* is lost (revoked, frozen, expired while the process slept);
|
|
377
|
-
* - each request under `<url>/use/` signed whole (method, URL, query, the
|
|
378
|
-
* headers it was given, the body), with the newest nonce, and sent again
|
|
379
|
-
* once if the gate asks for a fresh one;
|
|
380
|
-
* - logout on `close()`, and on SIGTERM.
|
|
381
|
-
*/
|
|
382
353
|
const dispatch = (nonces) => ({
|
|
383
354
|
seq: ++nonces.sent,
|
|
384
355
|
at: Date.now()
|
|
@@ -754,14 +725,15 @@ function surface(core) {
|
|
|
754
725
|
const placeUrl = () => `${origin}/use/${placeOf().split("/").map(encodeURIComponent).join("/")}`;
|
|
755
726
|
const url = (name) => core.places().length === 1 ? `${origin}/use/${encodeURIComponent(name)}` : `${placeUrl()}/${encodeURIComponent(name)}`;
|
|
756
727
|
/** What coffre makes with the environment's values, at the environment's URL: a signature, a verdict, a presigned URL, a minted child. */
|
|
757
|
-
const compute =
|
|
758
|
-
|
|
728
|
+
const compute = (asked, what) => computed(placeUrl(), { "content-type": "application/json" }, JSON.stringify(asked), what);
|
|
729
|
+
const computed = async (at, headers, sent, what) => {
|
|
730
|
+
const response = await signed(at, {
|
|
759
731
|
method: "POST",
|
|
760
732
|
headers: {
|
|
761
|
-
|
|
733
|
+
...headers,
|
|
762
734
|
accept: "application/json"
|
|
763
735
|
},
|
|
764
|
-
body:
|
|
736
|
+
body: sent
|
|
765
737
|
});
|
|
766
738
|
const body = await response.json().catch(() => null);
|
|
767
739
|
if (!response.ok || body === null) throw refusal(response.status, body, `coffre ${what} nothing`);
|
|
@@ -783,10 +755,11 @@ function surface(core) {
|
|
|
783
755
|
return signed(target.href, init);
|
|
784
756
|
},
|
|
785
757
|
sign: async (message) => {
|
|
786
|
-
const
|
|
758
|
+
const bytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
759
|
+
const body = await (bytes.length > 1048576 ? computed(`${placeUrl()}?sign=${encodeURIComponent(name)}`, { "content-type": "application/octet-stream" }, bytes, "signed") : compute({ sign: {
|
|
787
760
|
credential: name,
|
|
788
|
-
message: bytesOf(
|
|
789
|
-
} }, "signed");
|
|
761
|
+
message: bytesOf(bytes)
|
|
762
|
+
} }, "signed"));
|
|
790
763
|
if (typeof body.signature !== "string") throw new Error("coffre signed nothing");
|
|
791
764
|
return body.signature;
|
|
792
765
|
},
|
|
@@ -795,6 +768,38 @@ function surface(core) {
|
|
|
795
768
|
message: bytesOf(message),
|
|
796
769
|
signature
|
|
797
770
|
} }, "verified")).valid === true,
|
|
771
|
+
encrypt: async (plaintext, aad) => {
|
|
772
|
+
const body = await compute({ encrypt: {
|
|
773
|
+
credential: name,
|
|
774
|
+
plaintext: bytesOf(plaintext),
|
|
775
|
+
...aad === void 0 ? {} : { aad: bytesOf(aad) }
|
|
776
|
+
} }, "encrypted");
|
|
777
|
+
const [iv, ciphertext, tag] = [
|
|
778
|
+
body.iv,
|
|
779
|
+
body.ciphertext,
|
|
780
|
+
body.tag
|
|
781
|
+
].map((part) => typeof part === "string" ? fromBase64url(part) : null);
|
|
782
|
+
if (!iv || !ciphertext || !tag) throw new Error("coffre encrypted nothing");
|
|
783
|
+
return {
|
|
784
|
+
iv,
|
|
785
|
+
ciphertext,
|
|
786
|
+
tag
|
|
787
|
+
};
|
|
788
|
+
},
|
|
789
|
+
decrypt: async ({ iv, ciphertext, tag }, aad) => {
|
|
790
|
+
const body = await compute({ decrypt: {
|
|
791
|
+
credential: name,
|
|
792
|
+
iv: bytesOf(iv),
|
|
793
|
+
ciphertext: bytesOf(ciphertext),
|
|
794
|
+
tag: bytesOf(tag),
|
|
795
|
+
...aad === void 0 ? {} : { aad: bytesOf(aad) }
|
|
796
|
+
} }, "decrypted");
|
|
797
|
+
if (body.plaintext !== null && typeof body.plaintext !== "string") throw new Error("coffre decrypted nothing");
|
|
798
|
+
if (body.plaintext === null) return null;
|
|
799
|
+
const plaintext = fromBase64url(body.plaintext);
|
|
800
|
+
if (plaintext === null) throw new Error("coffre decrypted nothing: its answer is no base64url");
|
|
801
|
+
return plaintext;
|
|
802
|
+
},
|
|
798
803
|
presign: async (path, options = {}) => {
|
|
799
804
|
const body = await compute({ presign: {
|
|
800
805
|
credential: name,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as upgradeRefused, r as upgradeHeaders } from "./workload-
|
|
1
|
+
import { i as upgradeRefused, r as upgradeHeaders } from "./workload-ClMNMVqA.js";
|
|
2
2
|
import { Buffer } from "node:buffer";
|
|
3
3
|
import { Duplex } from "node:stream";
|
|
4
4
|
//#region src/workload-postgres.ts
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as Sealed } from "./credential-DyAuH0om.js";
|
|
2
|
+
import { c as PublicJwk } from "./dpop-C9EOdNCi.js";
|
|
2
3
|
//#region src/workload-proofs.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* How a workload proves who it is, once per session (docs/design/workload-access.md,
|
|
@@ -201,10 +202,17 @@ interface Workload {
|
|
|
201
202
|
interface UsedCredential {
|
|
202
203
|
/** `<credential's URL>/<path>`, signed: the proxy forwards it to the upstream with the real key. */
|
|
203
204
|
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
204
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* A signature by a signer's key, which stays in coffre: a private key's base64url, an HMAC secret's as its `hmac()` writes it.
|
|
207
|
+
* A message past 1 MiB goes raw, its bytes as the body, signed as they come and never held: an `hmac()` signer's whose `maxBytes` takes it.
|
|
208
|
+
*/
|
|
205
209
|
sign(message: string | Uint8Array): Promise<string>;
|
|
206
210
|
/** Whether `signature`, as its header carried it, is a verifier's HMAC of `message`, an inbound webhook's body as it came: checked without reading its secret. */
|
|
207
211
|
verify(message: string | Uint8Array, signature: string): Promise<boolean>;
|
|
212
|
+
/** `plaintext` encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad` if given and an IV coffre draws. */
|
|
213
|
+
encrypt(plaintext: string | Uint8Array, aad?: string | Uint8Array): Promise<Sealed>;
|
|
214
|
+
/** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt. */
|
|
215
|
+
decrypt(sealed: Sealed, aad?: string | Uint8Array): Promise<Uint8Array | null>;
|
|
208
216
|
/** A presigned GET or HEAD of `path` under an AWS-signed credential's upstream: the upstream's own URL, for anyone until `expiresAt`. */
|
|
209
217
|
presign(path: string, options?: {
|
|
210
218
|
expiresIn?: number;
|
package/dist/workload.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { n as Sealed } from "./credential-DyAuH0om.js";
|
|
1
2
|
import { r as CoffreError } from "./index-CURF99SB.js";
|
|
2
|
-
import { C as oidc, S as kubernetes, _ as enrollment, a as askedNonce, b as github, c as upgradeRefused, d as KUBERNETES_TOKEN, f as PlatformProof, g as azure, h as aws, i as WorkloadOptions, l as workload, m as ProofContext, n as UsedCredential, o as fromCoffre, p as Proof, r as Workload, s as upgradeHeaders, t as NarrowOptions, u as AwsCredentials, v as fly, w as vercel, x as keyAudience, y as gcp } from "./workload-
|
|
3
|
-
export { type AwsCredentials, CoffreError, KUBERNETES_TOKEN, NarrowOptions, type PlatformProof, type Proof, type ProofContext, UsedCredential, Workload, WorkloadOptions, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
|
|
3
|
+
import { C as oidc, S as kubernetes, _ as enrollment, a as askedNonce, b as github, c as upgradeRefused, d as KUBERNETES_TOKEN, f as PlatformProof, g as azure, h as aws, i as WorkloadOptions, l as workload, m as ProofContext, n as UsedCredential, o as fromCoffre, p as Proof, r as Workload, s as upgradeHeaders, t as NarrowOptions, u as AwsCredentials, v as fly, w as vercel, x as keyAudience, y as gcp } from "./workload-w5EbGZgy.js";
|
|
4
|
+
export { type AwsCredentials, CoffreError, KUBERNETES_TOKEN, NarrowOptions, type PlatformProof, type Proof, type ProofContext, type Sealed, UsedCredential, Workload, WorkloadOptions, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
|
package/dist/workload.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { t as CoffreError } from "./src-BCKSeCER.js";
|
|
2
|
-
import { a as workload, c as azure, d as gcp, f as github, g as vercel, h as oidc, i as upgradeRefused, l as enrollment, m as kubernetes, n as fromCoffre, o as KUBERNETES_TOKEN, p as keyAudience, r as upgradeHeaders, s as aws, t as askedNonce, u as fly } from "./workload-
|
|
2
|
+
import { a as workload, c as azure, d as gcp, f as github, g as vercel, h as oidc, i as upgradeRefused, l as enrollment, m as kubernetes, n as fromCoffre, o as KUBERNETES_TOKEN, p as keyAudience, r as upgradeHeaders, s as aws, t as askedNonce, u as fly } from "./workload-ClMNMVqA.js";
|
|
3
3
|
export { CoffreError, KUBERNETES_TOKEN, askedNonce, aws, azure, enrollment, fly, fromCoffre, gcp, github, keyAudience, kubernetes, oidc, upgradeHeaders, upgradeRefused, vercel, workload };
|
package/package.json
CHANGED
package/src/credential.ts
CHANGED
|
@@ -12,6 +12,11 @@
|
|
|
12
12
|
* `COFFRE_HANDLE`, the handle that stands in for the environment's values;
|
|
13
13
|
* the proxy injects the key, and the app never holds it.
|
|
14
14
|
*/
|
|
15
|
+
import { fromBase64url, JSON_MESSAGE_BYTES } from './dpop.ts';
|
|
16
|
+
|
|
17
|
+
/** What a cipher's encrypt gives: the IV coffre drew, 12 bytes, the ciphertext, and the 16-byte tag, which the app stores in its own format. */
|
|
18
|
+
export type Sealed = { iv: Uint8Array; ciphertext: Uint8Array; tag: Uint8Array };
|
|
19
|
+
|
|
15
20
|
export type ProxiedCredential = {
|
|
16
21
|
/** The credential's URL on the proxy: `<url>/<path>` is `<upstream>/<path>`. */
|
|
17
22
|
url: string;
|
|
@@ -29,7 +34,10 @@ export type ProxiedCredential = {
|
|
|
29
34
|
* A signature of `message` (a string signs as its UTF-8) by a signer's
|
|
30
35
|
* key, which stays in coffre: a private key's, what Node's
|
|
31
36
|
* `crypto.sign(undefined, message, key)` gives, base64url, each one use
|
|
32
|
-
* in coffre's log; an HMAC secret's, as its `hmac()` writes it.
|
|
37
|
+
* in coffre's log; an HMAC secret's, as its `hmac()` writes it. A message
|
|
38
|
+
* past 1 MiB goes raw, its bytes as the request's body, which coffre
|
|
39
|
+
* signs as they come, never held: an `hmac()` signer's whose `maxBytes`
|
|
40
|
+
* takes it, SumSub's document upload say.
|
|
33
41
|
*/
|
|
34
42
|
sign(message: Uint8Array | string): Promise<string>;
|
|
35
43
|
/**
|
|
@@ -39,6 +47,13 @@ export type ProxiedCredential = {
|
|
|
39
47
|
* in coffre, which compares in constant time and says only yes or no.
|
|
40
48
|
*/
|
|
41
49
|
verify(message: Uint8Array | string, signature: string): Promise<boolean>;
|
|
50
|
+
/**
|
|
51
|
+
* `plaintext` (a string as its UTF-8) encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad`
|
|
52
|
+
* if given: under an IV coffre draws for each, never the app's.
|
|
53
|
+
*/
|
|
54
|
+
encrypt(plaintext: Uint8Array | string, aad?: Uint8Array | string): Promise<Sealed>;
|
|
55
|
+
/** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt: whatever part is wrong, coffre says no more. */
|
|
56
|
+
decrypt(sealed: Sealed, aad?: Uint8Array | string): Promise<Uint8Array | null>;
|
|
42
57
|
};
|
|
43
58
|
|
|
44
59
|
type Env = Record<string, string | undefined>;
|
|
@@ -67,7 +82,17 @@ export function credential(
|
|
|
67
82
|
headers: { authorization: `Bearer ${handle}`, 'content-type': 'application/json' },
|
|
68
83
|
body: JSON.stringify(asked),
|
|
69
84
|
});
|
|
70
|
-
return { answer, body: (await answer.json()) as { url?: string; expiresAt?: string; signature?: string; valid?: boolean; message?: string } };
|
|
85
|
+
return { answer, body: (await answer.json()) as { url?: string; expiresAt?: string; signature?: string; valid?: boolean; iv?: string; ciphertext?: string; tag?: string; plaintext?: string | null; message?: string } };
|
|
86
|
+
};
|
|
87
|
+
/** A message past what JSON takes, signed raw: its bytes as the body, with the SHA-256 that `coffre serve` streams it by, and coffre checks. */
|
|
88
|
+
const raw = async (bytes: Uint8Array) => {
|
|
89
|
+
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes as Uint8Array<ArrayBuffer>));
|
|
90
|
+
const answer = await fetcher(`${url.slice(0, url.lastIndexOf('/'))}?sign=${encodeURIComponent(name)}`, {
|
|
91
|
+
method: 'POST',
|
|
92
|
+
headers: { authorization: `Bearer ${handle}`, 'content-type': 'application/octet-stream', 'content-length': String(bytes.length), 'content-digest': `sha-256=:${btoa(String.fromCharCode(...digest))}:` },
|
|
93
|
+
body: bytes as Uint8Array<ArrayBuffer>,
|
|
94
|
+
});
|
|
95
|
+
return { answer, body: (await answer.json()) as { signature?: string; message?: string } };
|
|
71
96
|
};
|
|
72
97
|
const bytesOf = (message: Uint8Array | string) => {
|
|
73
98
|
const bytes = typeof message === 'string' ? new TextEncoder().encode(message) : message;
|
|
@@ -88,7 +113,8 @@ export function credential(
|
|
|
88
113
|
return { url: body.url, expiresAt: body.expiresAt! };
|
|
89
114
|
},
|
|
90
115
|
sign: async (message) => {
|
|
91
|
-
const
|
|
116
|
+
const bytes = typeof message === 'string' ? new TextEncoder().encode(message) : message;
|
|
117
|
+
const { answer, body } = bytes.length > JSON_MESSAGE_BYTES ? await raw(bytes) : await compute({ sign: { credential: name, message: bytesOf(bytes) } });
|
|
92
118
|
if (!answer.ok || body.signature === undefined) throw new Error(`coffre signed nothing: ${body.message ?? answer.status}`);
|
|
93
119
|
return body.signature;
|
|
94
120
|
},
|
|
@@ -97,5 +123,19 @@ export function credential(
|
|
|
97
123
|
if (!answer.ok || typeof body.valid !== 'boolean') throw new Error(`coffre verified nothing: ${body.message ?? answer.status}`);
|
|
98
124
|
return body.valid;
|
|
99
125
|
},
|
|
126
|
+
encrypt: async (plaintext, aad) => {
|
|
127
|
+
const { answer, body } = await compute({ encrypt: { credential: name, plaintext: bytesOf(plaintext), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } });
|
|
128
|
+
const [iv, ciphertext, tag] = [body.iv, body.ciphertext, body.tag].map((part) => (typeof part === 'string' ? fromBase64url(part) : null));
|
|
129
|
+
if (!answer.ok || !iv || !ciphertext || !tag) throw new Error(`coffre encrypted nothing: ${body.message ?? answer.status}`);
|
|
130
|
+
return { iv, ciphertext, tag };
|
|
131
|
+
},
|
|
132
|
+
decrypt: async ({ iv, ciphertext, tag }, aad) => {
|
|
133
|
+
const { answer, body } = await compute({ decrypt: { credential: name, iv: bytesOf(iv), ciphertext: bytesOf(ciphertext), tag: bytesOf(tag), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } });
|
|
134
|
+
if (!answer.ok || (body.plaintext !== null && typeof body.plaintext !== 'string')) throw new Error(`coffre decrypted nothing: ${body.message ?? answer.status}`);
|
|
135
|
+
if (body.plaintext === null) return null;
|
|
136
|
+
const plaintext = fromBase64url(body.plaintext);
|
|
137
|
+
if (plaintext === null) throw new Error('coffre decrypted nothing: its answer is no base64url');
|
|
138
|
+
return plaintext;
|
|
139
|
+
},
|
|
100
140
|
};
|
|
101
141
|
}
|
package/src/dpop.ts
CHANGED
|
@@ -183,6 +183,9 @@ export async function bodyDigest(body: Uint8Array | string): Promise<string> {
|
|
|
183
183
|
*/
|
|
184
184
|
export const MAX_DIGESTED_BODY = 95 * 1024 * 1024;
|
|
185
185
|
|
|
186
|
+
/** The longest message `sign` sends as JSON, base64url: what coffre reads whole. A longer one goes raw, the body itself. */
|
|
187
|
+
export const JSON_MESSAGE_BYTES = 1024 * 1024;
|
|
188
|
+
|
|
186
189
|
/** A body's `Content-Digest` (RFC 9530) from its SHA-256 in hex, as the SDK and `coffre serve` send it: `sha-256=:<base64>:`. */
|
|
187
190
|
export function contentDigest(sha256: string): string {
|
|
188
191
|
if (!SHA256_HEX.test(sha256)) throw new TypeError('a Content-Digest is made from a SHA-256 in hex');
|
package/src/workload.ts
CHANGED
|
@@ -27,7 +27,10 @@
|
|
|
27
27
|
* once if the gate asks for a fresh one;
|
|
28
28
|
* - logout on `close()`, and on SIGTERM.
|
|
29
29
|
*/
|
|
30
|
-
import
|
|
30
|
+
import type { Sealed } from './credential.ts';
|
|
31
|
+
|
|
32
|
+
export type { Sealed };
|
|
33
|
+
import { base64url, bodyDigest, bodyLine, contentDigest, createProof, declaredDigest, fromBase64url, generateProofKey, JSON_MESSAGE_BYTES, thumbprint, type ProofKey, type PublicJwk } from './dpop.ts';
|
|
31
34
|
import { CoffreError } from './index.ts';
|
|
32
35
|
import type { Proof, ProofContext } from './workload-proofs.ts';
|
|
33
36
|
|
|
@@ -94,10 +97,17 @@ export interface Workload {
|
|
|
94
97
|
export interface UsedCredential {
|
|
95
98
|
/** `<credential's URL>/<path>`, signed: the proxy forwards it to the upstream with the real key. */
|
|
96
99
|
fetch(path: string, init?: RequestInit): Promise<Response>;
|
|
97
|
-
/**
|
|
100
|
+
/**
|
|
101
|
+
* A signature by a signer's key, which stays in coffre: a private key's base64url, an HMAC secret's as its `hmac()` writes it.
|
|
102
|
+
* A message past 1 MiB goes raw, its bytes as the body, signed as they come and never held: an `hmac()` signer's whose `maxBytes` takes it.
|
|
103
|
+
*/
|
|
98
104
|
sign(message: string | Uint8Array): Promise<string>;
|
|
99
105
|
/** Whether `signature`, as its header carried it, is a verifier's HMAC of `message`, an inbound webhook's body as it came: checked without reading its secret. */
|
|
100
106
|
verify(message: string | Uint8Array, signature: string): Promise<boolean>;
|
|
107
|
+
/** `plaintext` encrypted with a cipher's AES-256-GCM key, which stays in coffre, under `aad` if given and an IV coffre draws. */
|
|
108
|
+
encrypt(plaintext: string | Uint8Array, aad?: string | Uint8Array): Promise<Sealed>;
|
|
109
|
+
/** The plaintext `sealed` holds under the cipher's key and `aad`, or null when it does not decrypt. */
|
|
110
|
+
decrypt(sealed: Sealed, aad?: string | Uint8Array): Promise<Uint8Array | null>;
|
|
101
111
|
/** A presigned GET or HEAD of `path` under an AWS-signed credential's upstream: the upstream's own URL, for anyone until `expiresAt`. */
|
|
102
112
|
presign(path: string, options?: { expiresIn?: number; method?: 'GET' | 'HEAD'; query?: Record<string, string> }): Promise<{ url: string; expiresAt: string }>;
|
|
103
113
|
/** A short-lived child of a minter (an AWS session, a GitHub installation token), where the binding lists it. */
|
|
@@ -491,8 +501,9 @@ function surface(core: Core): Workload {
|
|
|
491
501
|
const url = (name: string) => (core.places().length === 1 ? `${origin}/use/${encodeURIComponent(name)}` : `${placeUrl()}/${encodeURIComponent(name)}`);
|
|
492
502
|
|
|
493
503
|
/** What coffre makes with the environment's values, at the environment's URL: a signature, a verdict, a presigned URL, a minted child. */
|
|
494
|
-
const compute =
|
|
495
|
-
|
|
504
|
+
const compute = (asked: Record<string, unknown>, what: string) => computed(placeUrl(), { 'content-type': 'application/json' }, JSON.stringify(asked), what);
|
|
505
|
+
const computed = async (at: string, headers: Record<string, string>, sent: BodyInit, what: string): Promise<Record<string, unknown>> => {
|
|
506
|
+
const response = await signed(at, { method: 'POST', headers: { ...headers, accept: 'application/json' }, body: sent });
|
|
496
507
|
const body = (await response.json().catch(() => null)) as Record<string, unknown> | null;
|
|
497
508
|
if (!response.ok || body === null) throw refusal(response.status, body, `coffre ${what} nothing`);
|
|
498
509
|
return body;
|
|
@@ -515,11 +526,29 @@ function surface(core: Core): Workload {
|
|
|
515
526
|
return signed(target.href, init);
|
|
516
527
|
},
|
|
517
528
|
sign: async (message) => {
|
|
518
|
-
const
|
|
529
|
+
const bytes = typeof message === 'string' ? new TextEncoder().encode(message) : message;
|
|
530
|
+
// Past what JSON takes, raw: its bytes as the body, which the proof binds by the Content-Digest `signed` declares.
|
|
531
|
+
const body = await (bytes.length > JSON_MESSAGE_BYTES
|
|
532
|
+
? computed(`${placeUrl()}?sign=${encodeURIComponent(name)}`, { 'content-type': 'application/octet-stream' }, bytes as Uint8Array<ArrayBuffer>, 'signed')
|
|
533
|
+
: compute({ sign: { credential: name, message: bytesOf(bytes) } }, 'signed'));
|
|
519
534
|
if (typeof body.signature !== 'string') throw new Error('coffre signed nothing');
|
|
520
535
|
return body.signature;
|
|
521
536
|
},
|
|
522
537
|
verify: async (message, signature) => (await compute({ verify: { credential: name, message: bytesOf(message), signature } }, 'verified')).valid === true,
|
|
538
|
+
encrypt: async (plaintext, aad) => {
|
|
539
|
+
const body = await compute({ encrypt: { credential: name, plaintext: bytesOf(plaintext), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } }, 'encrypted');
|
|
540
|
+
const [iv, ciphertext, tag] = [body.iv, body.ciphertext, body.tag].map((part) => (typeof part === 'string' ? fromBase64url(part) : null));
|
|
541
|
+
if (!iv || !ciphertext || !tag) throw new Error('coffre encrypted nothing');
|
|
542
|
+
return { iv, ciphertext, tag };
|
|
543
|
+
},
|
|
544
|
+
decrypt: async ({ iv, ciphertext, tag }, aad) => {
|
|
545
|
+
const body = await compute({ decrypt: { credential: name, iv: bytesOf(iv), ciphertext: bytesOf(ciphertext), tag: bytesOf(tag), ...(aad === undefined ? {} : { aad: bytesOf(aad) }) } }, 'decrypted');
|
|
546
|
+
if (body.plaintext !== null && typeof body.plaintext !== 'string') throw new Error('coffre decrypted nothing');
|
|
547
|
+
if (body.plaintext === null) return null;
|
|
548
|
+
const plaintext = fromBase64url(body.plaintext);
|
|
549
|
+
if (plaintext === null) throw new Error('coffre decrypted nothing: its answer is no base64url');
|
|
550
|
+
return plaintext;
|
|
551
|
+
},
|
|
523
552
|
presign: async (path, options = {}) => {
|
|
524
553
|
const body = await compute({ presign: { credential: name, path, ...options } }, 'presigned');
|
|
525
554
|
if (typeof body.url !== 'string' || typeof body.expiresAt !== 'string') throw new Error('coffre presigned nothing');
|