@flytedesk/app-kit 12.5.0 → 12.7.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.
- package/README.md +136 -0
- package/dist/auth/idpRequest.d.ts +16 -0
- package/dist/auth/idpRequest.js +37 -0
- package/dist/auth/idpRequest.js.map +1 -0
- package/dist/auth/jwks.d.ts +16 -0
- package/dist/auth/jwks.js +38 -0
- package/dist/auth/jwks.js.map +1 -0
- package/dist/auth/oidc-client.js +7 -25
- package/dist/auth/oidc-client.js.map +1 -1
- package/dist/auth/people/lookup.js +9 -16
- package/dist/auth/people/lookup.js.map +1 -1
- package/dist/auth/service/index.js +12 -19
- package/dist/auth/service/index.js.map +1 -1
- package/dist/connections/cipher/index.d.ts +20 -0
- package/dist/connections/cipher/index.js +26 -0
- package/dist/connections/cipher/index.js.map +1 -0
- package/dist/connections/cipher/kmsDecrypt.d.ts +3 -0
- package/dist/connections/cipher/kmsDecrypt.js +25 -0
- package/dist/connections/cipher/kmsDecrypt.js.map +1 -0
- package/dist/connections/cipher/kmsEncrypt.d.ts +3 -0
- package/dist/connections/cipher/kmsEncrypt.js +24 -0
- package/dist/connections/cipher/kmsEncrypt.js.map +1 -0
- package/dist/connections/cipher/localDecrypt.d.ts +3 -0
- package/dist/connections/cipher/localDecrypt.js +25 -0
- package/dist/connections/cipher/localDecrypt.js.map +1 -0
- package/dist/connections/cipher/localEncrypt.d.ts +3 -0
- package/dist/connections/cipher/localEncrypt.js +22 -0
- package/dist/connections/cipher/localEncrypt.js.map +1 -0
- package/dist/connections/cipher/localFormat.d.ts +5 -0
- package/dist/connections/cipher/localFormat.js +18 -0
- package/dist/connections/cipher/localFormat.js.map +1 -0
- package/dist/connections/cipher/types.d.ts +39 -0
- package/dist/connections/cipher/types.js +2 -0
- package/dist/connections/cipher/types.js.map +1 -0
- package/dist/connections/client.d.ts +52 -0
- package/dist/connections/client.js +227 -0
- package/dist/connections/client.js.map +1 -0
- package/dist/connections/contract.d.ts +63 -0
- package/dist/connections/contract.js +38 -0
- package/dist/connections/contract.js.map +1 -0
- package/dist/connections/errors.d.ts +36 -0
- package/dist/connections/errors.js +54 -0
- package/dist/connections/errors.js.map +1 -0
- package/dist/connections/index.d.ts +27 -0
- package/dist/connections/index.js +27 -0
- package/dist/connections/index.js.map +1 -0
- package/dist/connections/kinds.d.ts +180 -0
- package/dist/connections/kinds.js +75 -0
- package/dist/connections/kinds.js.map +1 -0
- package/dist/connections/testHandler.d.ts +44 -0
- package/dist/connections/testHandler.js +92 -0
- package/dist/connections/testHandler.js.map +1 -0
- package/dist/connections/testers/mysql.d.ts +14 -0
- package/dist/connections/testers/mysql.js +74 -0
- package/dist/connections/testers/mysql.js.map +1 -0
- package/dist/connections/testers/postgres.d.ts +41 -0
- package/dist/connections/testers/postgres.js +249 -0
- package/dist/connections/testers/postgres.js.map +1 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -218,6 +218,142 @@ const sub = await lookupSubByEmail(dir, "ada@example.com"); // sub | null
|
|
|
218
218
|
from the token exchange); an unreachable flytedesk ID, 5xx, 429 or bad body throws
|
|
219
219
|
`IdpUnavailableError` (`IdpTimeoutError` on timeout). A failure never returns `[]` or `null`.
|
|
220
220
|
|
|
221
|
+
### Reading connections
|
|
222
|
+
|
|
223
|
+
An app's secrets (database logins, Slack and Trello tokens, API keys) live in flytedesk
|
|
224
|
+
ID's secrets manager, encrypted with the app's own Cloud KMS key. flytedesk ID cannot
|
|
225
|
+
decrypt them: the app's server fetches its ciphertext and decrypts it itself.
|
|
226
|
+
`@flytedesk/app-kit/connections` is that code. It takes the Cloud KMS client from the app
|
|
227
|
+
and never builds one.
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import { KeyManagementServiceClient } from "@google-cloud/kms";
|
|
231
|
+
import { createServiceTokenSource } from "@flytedesk/app-kit/auth/service";
|
|
232
|
+
import { createConnectionCipher, createConnectionsClient } from "@flytedesk/app-kit/connections";
|
|
233
|
+
|
|
234
|
+
const connections = createConnectionsClient({
|
|
235
|
+
issuer: "https://research-id.flytedesk.com",
|
|
236
|
+
appId: "flytebot",
|
|
237
|
+
tokenSource: createServiceTokenSource({ issuer, appId, idToken }),
|
|
238
|
+
// Chosen once, at start-up: "kms" in production, "local" (AES-256-GCM under `localKey`)
|
|
239
|
+
// for development and tests. The local cipher with `production: true` throws here.
|
|
240
|
+
cipher: createConnectionCipher({ cipher: "kms", production: true, kms: new KeyManagementServiceClient() }),
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
const db = await connections.get("main-db", "postgres");
|
|
244
|
+
// db.settings: { host, port, database, user } db.secrets: { password }
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
- **Kinds:** `postgres`, `mysql`, `postgres_iam` (no secret), `bigquery` (no secret),
|
|
248
|
+
`slack`, `slack_bot` (token only), `trello`, `anthropic`, `api_key`, `airtable`, and the email
|
|
249
|
+
providers `resend`, `sendgrid`, `mandrill`. Each is one entry in `CONNECTION_KINDS`;
|
|
250
|
+
`get(slug, kind)` refuses a connection of another kind and returns typed values.
|
|
251
|
+
- **`get` and `read`:** `get(slug, kind, options?)` is what an app calls: it refuses a
|
|
252
|
+
connection of another kind (`ConnectionKindMismatchError`) and returns typed
|
|
253
|
+
`{ settings, secrets, previous }`. `read(slug, options?)` reads a connection whatever
|
|
254
|
+
its kind (the test handler's case, where the kind is not known up front) and returns
|
|
255
|
+
`{ kind, settings, secrets, previous, versions }`, with `versions` the current version of
|
|
256
|
+
each secret field. Both take `{ fresh?: boolean }` and share one cache.
|
|
257
|
+
- **Caching:** a read is reused for 60 seconds, so a replaced value is used within a
|
|
258
|
+
minute with no redeploy. If flytedesk ID cannot be reached when a refresh is due, the
|
|
259
|
+
last value is served for up to 15 minutes after it was fetched (each failed refresh is
|
|
260
|
+
logged as an error); after that `get` throws `ConnectionOutageError`. A refusal
|
|
261
|
+
(`ConnectionRefusedError`: disabled, value not set, no permission, unknown) is never
|
|
262
|
+
served from cache. A `fresh: true` read (on `get` or `read`) always asks flytedesk ID, never
|
|
263
|
+
serves a stale value, and replaces the cached one; it also supersedes any refresh already
|
|
264
|
+
in flight, so an older read can never re-cache a value after it.
|
|
265
|
+
- **Rotation:** `previous` holds, per secret that has an earlier version, that value and
|
|
266
|
+
when it was replaced, so a webhook can still accept a signature made with the old
|
|
267
|
+
secret for a grace period.
|
|
268
|
+
- **Nothing is logged or thrown with a value:** log lines and errors name the connection
|
|
269
|
+
and the cause only.
|
|
270
|
+
- **Testing a connection:** flytedesk ID asks the app to test one (the Test button).
|
|
271
|
+
Mount `createConnectionTestHandler({ issuer, appId, client: connections, testers })` on
|
|
272
|
+
your server at the URL registered with flytedesk ID: it takes `{ authorization, body }`
|
|
273
|
+
and answers `{ status, body }`, where a test's body is `{ ok, detail, tested }`: `tested` is the
|
|
274
|
+
version of each secret field the handler read fresh, so flytedesk ID records what was
|
|
275
|
+
actually tested. `createPostgresTester(connect)` and `createMysqlTester(connect)` prove a
|
|
276
|
+
login connects and holds nothing beyond read rights; you supply `connect` from the driver
|
|
277
|
+
you already use, and the tester for any other kind. Both are allow-lists: anything not
|
|
278
|
+
listed fails, including a grant option.
|
|
279
|
+
- **mysql** passes only SELECT, SHOW VIEW, USAGE and SHOW DATABASES in `SHOW GRANTS`
|
|
280
|
+
(DROP, ALTER, TRIGGER, EVENT, INDEX, FILE and the rest fail), and no `WITH GRANT OPTION`.
|
|
281
|
+
- **postgres** applies one rule to every catalog that carries privileges (relations
|
|
282
|
+
including sequences, columns, schemas, every database, functions, types, languages, large
|
|
283
|
+
objects, foreign data wrappers and servers, tablespaces, and server parameters; Postgres 15 or newer),
|
|
284
|
+
reading each object's effective ACL with its defaults for the
|
|
285
|
+
login, PUBLIC and every role the login belongs to. It allows only: SELECT on tables,
|
|
286
|
+
views, materialized views and foreign tables; USAGE on schemas, types and trusted
|
|
287
|
+
languages; CONNECT and TEMPORARY on any database; EXECUTE on a function that runs with
|
|
288
|
+
the caller's rights (see below). Everything else fails: any other
|
|
289
|
+
privilege, any grant option or role admin option, a role attribute beyond LOGIN
|
|
290
|
+
(superuser, createrole, createdb, bypassrls, replication), membership in any role
|
|
291
|
+
except pg_read_all_data, pg_read_all_settings, pg_read_all_stats, pg_stat_scan_tables
|
|
292
|
+
and pg_monitor. It also fails if the login, or any role it belongs to, owns anything:
|
|
293
|
+
ownership is read from `pg_shdepend`, so every kind of owned object counts (an
|
|
294
|
+
extension, a collation, a text search configuration, another database, ...), in this
|
|
295
|
+
database and cluster-wide; temporary schemas are the only exception (a temporary table
|
|
296
|
+
another session holds does not count; an object's schema decides that, never its
|
|
297
|
+
name). CREATE on any database in the cluster fails; CONNECT and TEMPORARY are fine.
|
|
298
|
+
**Scope:** the database the connection names, plus cluster-wide objects and role state
|
|
299
|
+
(a passing result says "checked: database <name> and cluster-wide"). Grants and owned
|
|
300
|
+
objects inside the server's other databases are out of scope: point the test at each
|
|
301
|
+
database the app's login can reach if you need that.
|
|
302
|
+
- **Make the login fit the rule.** A function the login can run must use the caller's
|
|
303
|
+
rights: a `SECURITY DEFINER` function fails the test, because it can write with its
|
|
304
|
+
owner's rights. So does a function in an untrusted language (C, for one) unless an
|
|
305
|
+
extension installed it (pgcrypto and pg_trgm in a user schema are fine) and the login
|
|
306
|
+
runs it only through the function's untouched default PUBLIC grant or a grant, to PUBLIC
|
|
307
|
+
or one of the five read roles above, that the extension's own script made
|
|
308
|
+
(pg_buffercache's functions for pg_monitor, for example; Postgres records these in
|
|
309
|
+
`pg_init_privs`). A PUBLIC grant an administrator adds later to a function the
|
|
310
|
+
extension had revoked from PUBLIC (adminpack's `pg_file_write`) fails. An explicit `GRANT EXECUTE`
|
|
311
|
+
on such a function to the login or any other role it belongs to, or to a read role by
|
|
312
|
+
anyone but the extension, fails. Only the functions initdb shipped (oid below 16384)
|
|
313
|
+
pass exactly as initdb granted them: an extra grant on one, on `lo_export` for
|
|
314
|
+
instance, fails. A function created later is a user function wherever it lives, even in
|
|
315
|
+
`pg_catalog`, `information_schema` or `pg_toast`. The functions Postgres makes for a
|
|
316
|
+
type (range constructors) are fine. An aggregate, extension member or not, passes only
|
|
317
|
+
when every support function it runs passes: a caller-rights function in a trusted
|
|
318
|
+
language, one an extension installed that is reachable only as the extension left it
|
|
319
|
+
(the function's untouched default, or the extension's own recorded grant to PUBLIC or a
|
|
320
|
+
read role; adminpack's `pg_file_write` as a final function fails), or an initdb
|
|
321
|
+
function that a role the login holds may execute in initdb's grants (`lo_export` as a final function fails, because
|
|
322
|
+
initdb revoked it from PUBLIC; Postgres checks an aggregate's support functions against
|
|
323
|
+
the aggregate's owner, so the login's own grants would not stop it). The functions an
|
|
324
|
+
extension made at initdb (plpgsql's handlers and validator) are initdb's own and are
|
|
325
|
+
judged against initdb's grants. An extension's member objects are trusted as the
|
|
326
|
+
extension installed them; a superuser adding objects to an extension (`ALTER EXTENSION
|
|
327
|
+
... ADD`) is superuser tampering, outside the threat model. Give the app's read-only
|
|
328
|
+
login only SELECT on its tables.
|
|
329
|
+
- **Postgres version.** The postgres test needs Postgres 15 or newer; an older server
|
|
330
|
+
fails with "unsupported server version".
|
|
331
|
+
- **Settings and default privileges.** The test also fails if `lo_compat_privileges` is on
|
|
332
|
+
for the login (it lets any large object be read and written), and if `ALTER DEFAULT
|
|
333
|
+
PRIVILEGES` will give the login or a role it belongs to anything beyond SELECT on future
|
|
334
|
+
tables, USAGE on schemas and types, or EXECUTE on functions.
|
|
335
|
+
- **Threat model.** The postgres test catches a login that an administrator set up with a
|
|
336
|
+
direct way to write: a grant, role membership, ownership, role attribute or server
|
|
337
|
+
setting held by the login, a role it belongs to, or PUBLIC. It also applies the function
|
|
338
|
+
and aggregate rules above. It does not follow write paths that run through another
|
|
339
|
+
role's own code on that role's rights: views, triggers and rules, and in MySQL
|
|
340
|
+
definer-rights views and routines. That is code an administrator deliberately wrote to
|
|
341
|
+
act for others, so review it separately. It does not defend against a superuser
|
|
342
|
+
rewriting the definition of a system object (replacing a built-in function's body, say);
|
|
343
|
+
that is a compromised server, not a misconfigured login, and no check of the catalogs
|
|
344
|
+
can bound it. No schema is excluded: grants on `pg_catalog`, `information_schema` or a
|
|
345
|
+
temporary schema count, and objects initdb shipped are judged against the grants initdb
|
|
346
|
+
recorded (`pg_init_privs`).
|
|
347
|
+
- **mysql and mandatory roles.** `SHOW GRANTS` does not list the privileges of a role the
|
|
348
|
+
server gives every account, so the mysql test fails while `mandatory_roles` is not empty.
|
|
349
|
+
- **What it does not claim.** Any login can create its own temporary tables and its own
|
|
350
|
+
large objects through built-in functions. Neither touches data the login does not
|
|
351
|
+
own, so the test does not look for them and does not claim they are impossible. But a
|
|
352
|
+
login that *owns* a large object fails like one that owns any other object.
|
|
353
|
+
- **flytedesk ID's side** imports the same module: the kinds and the wire contract
|
|
354
|
+
(`connectionAad`, the error codes, the test token's type and claims) and
|
|
355
|
+
the encrypt side (`createKmsEncryptor`, `createLocalEncryptor`).
|
|
356
|
+
|
|
221
357
|
### What an app must not build
|
|
222
358
|
|
|
223
359
|
Everything below is app-kit's:
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Throws for an issuer that is empty or ends in `/` (flytedesk ID compares it exactly). */
|
|
2
|
+
export declare function assertBareIssuer(issuer: string, who: string): void;
|
|
3
|
+
export interface IdpAnswerRules {
|
|
4
|
+
/** The path for messages, as `idpFetch` was given it. */
|
|
5
|
+
path: string;
|
|
6
|
+
/** Which non-2xx statuses are a verdict about the caller. */
|
|
7
|
+
refuses(status: number): boolean;
|
|
8
|
+
/** The error for a refused answer. */
|
|
9
|
+
refusal(status: number, body: string): Error;
|
|
10
|
+
}
|
|
11
|
+
/** A 4xx other than 429: the usual verdict about the caller. */
|
|
12
|
+
export declare const isClientRefusal: (status: number) => boolean;
|
|
13
|
+
/** The body text of a 2xx answer; a refusal throws `rules.refusal`, anything else `IdpUnavailableError`. */
|
|
14
|
+
export declare function readIdpAnswer(res: Response, rules: IdpAnswerRules): Promise<string>;
|
|
15
|
+
/** `JSON.parse(text)`, a body that is not JSON being an outage. */
|
|
16
|
+
export declare function parseIdpJson(text: string, path: string): unknown;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two steps every server-side call to flytedesk ID shares, after the bounded request
|
|
3
|
+
* (`./idpFetch.ts`): the issuer check before it, and the refusal-vs-outage split of the
|
|
4
|
+
* answer after it. Used by `./service/index.ts`, `./people/lookup.ts` and
|
|
5
|
+
* `../connections/client.ts`. No imports beyond `./errors.js`: never reaches Fastify.
|
|
6
|
+
*
|
|
7
|
+
* A refusal is a verdict about the caller (retrying will not help); everything else that is
|
|
8
|
+
* not a 2xx, and any body that is not usable, is an outage (`IdpUnavailableError`).
|
|
9
|
+
*/
|
|
10
|
+
import { IdpUnavailableError } from "./errors.js";
|
|
11
|
+
/** Throws for an issuer that is empty or ends in `/` (flytedesk ID compares it exactly). */
|
|
12
|
+
export function assertBareIssuer(issuer, who) {
|
|
13
|
+
if (issuer.length === 0 || issuer.endsWith("/")) {
|
|
14
|
+
throw new Error(`${who}: issuer must be flytedesk ID's issuer URL with no trailing slash, got ${JSON.stringify(issuer)}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** A 4xx other than 429: the usual verdict about the caller. */
|
|
18
|
+
export const isClientRefusal = (status) => status >= 400 && status < 500 && status !== 429;
|
|
19
|
+
/** The body text of a 2xx answer; a refusal throws `rules.refusal`, anything else `IdpUnavailableError`. */
|
|
20
|
+
export async function readIdpAnswer(res, rules) {
|
|
21
|
+
const text = await res.text().catch(() => "");
|
|
22
|
+
if (rules.refuses(res.status))
|
|
23
|
+
throw rules.refusal(res.status, text);
|
|
24
|
+
if (!res.ok)
|
|
25
|
+
throw new IdpUnavailableError(`flytedesk-id ${rules.path} returned ${res.status}`);
|
|
26
|
+
return text;
|
|
27
|
+
}
|
|
28
|
+
/** `JSON.parse(text)`, a body that is not JSON being an outage. */
|
|
29
|
+
export function parseIdpJson(text, path) {
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(text);
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
throw new IdpUnavailableError(`flytedesk-id ${path} answered a body that is not JSON`, err);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=idpRequest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idpRequest.js","sourceRoot":"","sources":["../../src/auth/idpRequest.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,4FAA4F;AAC5F,MAAM,UAAU,gBAAgB,CAAC,MAAc,EAAE,GAAW;IAC1D,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CACb,GAAG,GAAG,0EAA0E,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CACzG,CAAC;IACJ,CAAC;AACH,CAAC;AAWD,gEAAgE;AAChE,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAc,EAAW,EAAE,CAAC,MAAM,IAAI,GAAG,IAAI,MAAM,GAAG,GAAG,IAAI,MAAM,KAAK,GAAG,CAAC;AAE5G,4GAA4G;AAC5G,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,GAAa,EAAE,KAAqB;IACtE,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;IAC9C,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC;QAAE,MAAM,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACrE,IAAI,CAAC,GAAG,CAAC,EAAE;QAAE,MAAM,IAAI,mBAAmB,CAAC,gBAAgB,KAAK,CAAC,IAAI,aAAa,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;IAChG,OAAO,IAAI,CAAC;AACd,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,IAAY;IACrD,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,IAAI,mBAAmB,CAAC,gBAAgB,IAAI,mCAAmC,EAAE,GAAG,CAAC,CAAC;IAC9F,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one JWKS key lookup for tokens flytedesk ID signs: its published keys at
|
|
3
|
+
* `{issuer}/jwks`, with a failure to FETCH them classified as an outage. Used by
|
|
4
|
+
* `./oidc-client.ts` (service access tokens) and `../connections/testHandler.ts`
|
|
5
|
+
* (connection-test tokens). Never imports Fastify.
|
|
6
|
+
*/
|
|
7
|
+
import { type JWTVerifyGetKey } from "jose";
|
|
8
|
+
/** `createRemoteJWKSet(url)`, created on first use so a bad URL fails there, as before. */
|
|
9
|
+
export declare function createLazyRemoteJwks(jwksUrl: () => string): JWTVerifyGetKey;
|
|
10
|
+
/**
|
|
11
|
+
* `keys`, with a failure to FETCH the key set (flytedesk ID down, a timeout, a non-200 or
|
|
12
|
+
* unparseable answer) raised as `IdpUnavailableError`, so an outage is never mistaken for a
|
|
13
|
+
* refused token. A key set that was fetched but has no (or more than one) key for the
|
|
14
|
+
* token's header is a verdict about the token and passes through as jose raised it.
|
|
15
|
+
*/
|
|
16
|
+
export declare function outageAwareKeys(keys: JWTVerifyGetKey): JWTVerifyGetKey;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one JWKS key lookup for tokens flytedesk ID signs: its published keys at
|
|
3
|
+
* `{issuer}/jwks`, with a failure to FETCH them classified as an outage. Used by
|
|
4
|
+
* `./oidc-client.ts` (service access tokens) and `../connections/testHandler.ts`
|
|
5
|
+
* (connection-test tokens). Never imports Fastify.
|
|
6
|
+
*/
|
|
7
|
+
import { createRemoteJWKSet, errors as joseErrors } from "jose";
|
|
8
|
+
import { IdpUnavailableError } from "./errors.js";
|
|
9
|
+
/** `createRemoteJWKSet(url)`, created on first use so a bad URL fails there, as before. */
|
|
10
|
+
export function createLazyRemoteJwks(jwksUrl) {
|
|
11
|
+
let keys;
|
|
12
|
+
return (header, token) => {
|
|
13
|
+
keys ??= createRemoteJWKSet(new URL(jwksUrl()));
|
|
14
|
+
return keys(header, token);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* `keys`, with a failure to FETCH the key set (flytedesk ID down, a timeout, a non-200 or
|
|
19
|
+
* unparseable answer) raised as `IdpUnavailableError`, so an outage is never mistaken for a
|
|
20
|
+
* refused token. A key set that was fetched but has no (or more than one) key for the
|
|
21
|
+
* token's header is a verdict about the token and passes through as jose raised it.
|
|
22
|
+
*/
|
|
23
|
+
export function outageAwareKeys(keys) {
|
|
24
|
+
return async (header, token) => {
|
|
25
|
+
try {
|
|
26
|
+
return await keys(header, token);
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
if (err instanceof IdpUnavailableError ||
|
|
30
|
+
err instanceof joseErrors.JWKSNoMatchingKey ||
|
|
31
|
+
err instanceof joseErrors.JWKSMultipleMatchingKeys) {
|
|
32
|
+
throw err;
|
|
33
|
+
}
|
|
34
|
+
throw new IdpUnavailableError(`flytedesk-id JWKS could not be fetched: ${err instanceof Error ? err.message : String(err)}`, err);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=jwks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jwks.js","sourceRoot":"","sources":["../../src/auth/jwks.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,kBAAkB,EAAE,MAAM,IAAI,UAAU,EAAwB,MAAM,MAAM,CAAC;AACtF,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,2FAA2F;AAC3F,MAAM,UAAU,oBAAoB,CAAC,OAAqB;IACxD,IAAI,IAAiC,CAAC;IACtC,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QACvB,IAAI,KAAK,kBAAkB,CAAC,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QAChD,OAAO,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC7B,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,IAAqB;IACnD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE;QAC7B,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QACnC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IACE,GAAG,YAAY,mBAAmB;gBAClC,GAAG,YAAY,UAAU,CAAC,iBAAiB;gBAC3C,GAAG,YAAY,UAAU,CAAC,wBAAwB,EAClD,CAAC;gBACD,MAAM,GAAG,CAAC;YACZ,CAAC;YACD,MAAM,IAAI,mBAAmB,CAC3B,2CAA2C,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,EAC7F,GAAG,CACJ,CAAC;QACJ,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/auth/oidc-client.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jwtVerify } from "jose";
|
|
2
2
|
import { IdpAccessRevokedError, IdpUnavailableError, OidcHttpError, OidcTokenError } from "./errors.js";
|
|
3
3
|
import { DEFAULT_IDP_REQUEST_TIMEOUT_MS, idpFetch as boundedIdpFetch } from "./idpFetch.js";
|
|
4
|
+
import { createLazyRemoteJwks, outageAwareKeys } from "./jwks.js";
|
|
4
5
|
import { SERVICE_ACCESS_TOKEN_ALG, SERVICE_ACCESS_TOKEN_TYP, SERVICE_TOKEN_KIND, SERVICE_TOKEN_KIND_CLAIM, } from "./serviceTokenContract.js";
|
|
5
6
|
/**
|
|
6
7
|
* Requested by default (AK-14, gap 1): without `offline_access`, flytedesk-id's
|
|
@@ -69,29 +70,10 @@ export function createOidcClient(opts) {
|
|
|
69
70
|
}
|
|
70
71
|
return (await res.json());
|
|
71
72
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* The JWKS key lookup for service access tokens: `getJwks()`, with a failure to
|
|
79
|
-
* FETCH the key set (flytedesk ID down, a timeout, a non-200 or unparseable answer)
|
|
80
|
-
* raised as `IdpUnavailableError`, so an outage is never mistaken for a refused token.
|
|
81
|
-
* A key set that was fetched but has no (or more than one) key for the token's header
|
|
82
|
-
* is a verdict about the token and passes through as jose raised it.
|
|
83
|
-
*/
|
|
84
|
-
const serviceTokenKey = async (header, token) => {
|
|
85
|
-
try {
|
|
86
|
-
return await getJwks()(header, token);
|
|
87
|
-
}
|
|
88
|
-
catch (err) {
|
|
89
|
-
if (err instanceof joseErrors.JWKSNoMatchingKey || err instanceof joseErrors.JWKSMultipleMatchingKeys) {
|
|
90
|
-
throw err;
|
|
91
|
-
}
|
|
92
|
-
throw new IdpUnavailableError(`flytedesk-id JWKS could not be fetched: ${err instanceof Error ? err.message : String(err)}`, err);
|
|
93
|
-
}
|
|
94
|
-
};
|
|
73
|
+
const getJwks = createLazyRemoteJwks(() => issuerUrl("/jwks"));
|
|
74
|
+
/** The JWKS key lookup for service access tokens: `getJwks`, with a failure to FETCH the
|
|
75
|
+
* key set raised as `IdpUnavailableError` (see `./jwks.ts`). */
|
|
76
|
+
const serviceTokenKey = outageAwareKeys(getJwks);
|
|
95
77
|
/** `GET {issuer}/apps/{appId}/authorization`'s JSON body, for either kind of bearer. */
|
|
96
78
|
async function authorizationRequest(bearer, appId) {
|
|
97
79
|
const res = await idpFetch(issuerUrl(`/apps/${appId}/authorization`), { headers: { Authorization: `Bearer ${bearer}` } }, "authorization endpoint");
|
|
@@ -170,7 +152,7 @@ export function createOidcClient(opts) {
|
|
|
170
152
|
* those actually come from.
|
|
171
153
|
*/
|
|
172
154
|
async verifyIdToken(idToken) {
|
|
173
|
-
const { payload } = await jwtVerify(idToken, getJwks
|
|
155
|
+
const { payload } = await jwtVerify(idToken, getJwks, {
|
|
174
156
|
issuer: opts.issuer,
|
|
175
157
|
audience: opts.clientId,
|
|
176
158
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oidc-client.js","sourceRoot":"","sources":["../../src/auth/oidc-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"oidc-client.js","sourceRoot":"","sources":["../../src/auth/oidc-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAwB,MAAM,MAAM,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AACxG,OAAO,EAAE,8BAA8B,EAAE,QAAQ,IAAI,eAAe,EAAE,MAAM,eAAe,CAAC;AAC5F,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAClE,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACxB,kBAAkB,EAClB,wBAAwB,GACzB,MAAM,2BAA2B,CAAC;AAInC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,gBAAgB,CAAU,CAAC;AAE7F,OAAO,EAAE,8BAA8B,EAAE,CAAC;AA0H1C;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAuB;IACtD,SAAS,SAAS,CAAC,IAAY;QAC7B,OAAO,I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d,OAAO,EAAE;wBACP,cAAc,EAAE,mCAAmC;wBACnD,aAAa,EAAE,eAAe,EAAE;qBACjC;oBACD,IAAI,EAAE,IAAI,eAAe,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,QAAQ,EAAE;iBAChD,EACD,YAAY,CACb,CAAC;gBACF,OAAO,GAAG,CAAC,EAAE,CAAC;YAChB,CAAC;YAAC,MAAM,CAAC;gBACP,+EAA+E;gBAC/E,+EAA+E;gBAC/E,iFAAiF;gBACjF,iDAAiD;gBACjD,OAAO,KAAK,CAAC;YACf,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -5,15 +5,14 @@
|
|
|
5
5
|
* The caller supplies the `ServiceTokenSource` from `@flytedesk/app-kit/auth/service`
|
|
6
6
|
* (its principal needs the app permission `people.lookup`). Never imports Fastify.
|
|
7
7
|
*/
|
|
8
|
-
import { IdpUnavailableError, PeopleDirectoryRefusedError
|
|
8
|
+
import { IdpUnavailableError, PeopleDirectoryRefusedError } from "../errors.js";
|
|
9
9
|
import { DEFAULT_IDP_REQUEST_TIMEOUT_MS, idpFetch } from "../idpFetch.js";
|
|
10
|
+
import { assertBareIssuer, isClientRefusal, parseIdpJson, readIdpAnswer } from "../idpRequest.js";
|
|
10
11
|
export { PeopleDirectoryRefusedError } from "../errors.js";
|
|
11
12
|
/** The directory accepts 1 to 100 subs per call. */
|
|
12
13
|
export const PEOPLE_LOOKUP_BATCH_SIZE = 100;
|
|
13
14
|
async function post(options, route, body) {
|
|
14
|
-
|
|
15
|
-
throw new Error(`people directory: issuer must have no trailing slash, got ${JSON.stringify(options.issuer)}`);
|
|
16
|
-
}
|
|
15
|
+
assertBareIssuer(options.issuer, "people directory");
|
|
17
16
|
const path = `/apps/${encodeURIComponent(options.appId)}/people/${route}`;
|
|
18
17
|
const res = await idpFetch(`${options.issuer}${path}`, {
|
|
19
18
|
method: "POST",
|
|
@@ -24,18 +23,12 @@ async function post(options, route, body) {
|
|
|
24
23
|
},
|
|
25
24
|
body: JSON.stringify(body),
|
|
26
25
|
}, path, options.timeoutMs ?? DEFAULT_IDP_REQUEST_TIMEOUT_MS, options.fetch);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
return await res.json();
|
|
35
|
-
}
|
|
36
|
-
catch (err) {
|
|
37
|
-
throw new IdpUnavailableError(`flytedesk-id ${path} answered a body that is not JSON`, err);
|
|
38
|
-
}
|
|
26
|
+
const text = await readIdpAnswer(res, {
|
|
27
|
+
path,
|
|
28
|
+
refuses: isClientRefusal,
|
|
29
|
+
refusal: (status) => new PeopleDirectoryRefusedError(`flytedesk-id refused ${path} (${status})`, status),
|
|
30
|
+
});
|
|
31
|
+
return parseIdpJson(text, path);
|
|
39
32
|
}
|
|
40
33
|
function malformed(path) {
|
|
41
34
|
return new IdpUnavailableError(`flytedesk-id people/${path} answered an unexpected body`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lookup.js","sourceRoot":"","sources":["../../../src/auth/people/lookup.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"lookup.js","sourceRoot":"","sources":["../../../src/auth/people/lookup.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAiB,mBAAmB,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAC/F,OAAO,EAAE,8BAA8B,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGlG,OAAO,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAE3D,oDAAoD;AACpD,MAAM,CAAC,MAAM,wBAAwB,GAAG,GAAG,CAAC;AAsB5C,KAAK,UAAU,IAAI,CAAC,OAA+B,EAAE,KAAa,EAAE,IA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AAU,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IACxD,MAAM,GAAG,GAAI,IAAiC,EAAE,GAAG,CAAC;IACpD,IAAI,GAAG,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAC9B,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,MAAM,SAAS,CAAC,UAAU,CAAC,CAAC;IACzD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
import { decodeJwt } from "jose";
|
|
28
28
|
import { IdpUnavailableError, ServiceTokenRefusedError } from "../errors.js";
|
|
29
29
|
import { DEFAULT_IDP_REQUEST_TIMEOUT_MS, idpFetch } from "../idpFetch.js";
|
|
30
|
+
import { assertBareIssuer, parseIdpJson, readIdpAnswer } from "../idpRequest.js";
|
|
30
31
|
import { ID_TOKEN_TYPE, SERVICE_TOKEN_PATH, TOKEN_EXCHANGE_GRANT_TYPE } from "../serviceTokenContract.js";
|
|
31
32
|
export { IdpError, IdpTimeoutError, IdpUnavailableError, ServiceTokenRefusedError } from "../errors.js";
|
|
32
33
|
/** How long before its `exp` a cached service access token is replaced. */
|
|
@@ -37,9 +38,7 @@ export const DEFAULT_SERVICE_TOKEN_REFRESH_SKEW_MS = 60_000;
|
|
|
37
38
|
* this string exactly, and the plugin compares the issued token's `iss` exactly.
|
|
38
39
|
*/
|
|
39
40
|
export function serviceTokenAudience(issuer) {
|
|
40
|
-
|
|
41
|
-
throw new Error(`@flytedesk/app-kit/auth/service: issuer must be flytedesk ID's issuer URL with no trailing slash, got ${JSON.stringify(issuer)}`);
|
|
42
|
-
}
|
|
41
|
+
assertBareIssuer(issuer, "@flytedesk/app-kit/auth/service");
|
|
43
42
|
return `${issuer}${SERVICE_TOKEN_PATH}`;
|
|
44
43
|
}
|
|
45
44
|
/** An OAuth error body's `error` and `error_description`, when it has them. */
|
|
@@ -75,22 +74,16 @@ export async function exchangeServiceToken(options) {
|
|
|
75
74
|
audience: options.appId,
|
|
76
75
|
}).toString(),
|
|
77
76
|
}, SERVICE_TOKEN_PATH, options.timeoutMs ?? DEFAULT_IDP_REQUEST_TIMEOUT_MS, options.fetch);
|
|
78
|
-
const text = await res
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
try {
|
|
89
|
-
accessToken = JSON.parse(text).access_token;
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
throw new IdpUnavailableError(`flytedesk-id ${SERVICE_TOKEN_PATH} answered a body that is not JSON`, err);
|
|
93
|
-
}
|
|
77
|
+
const text = await readIdpAnswer(res, {
|
|
78
|
+
path: SERVICE_TOKEN_PATH,
|
|
79
|
+
refuses: (status) => status >= 400 && status < 500,
|
|
80
|
+
refusal: (status, body) => {
|
|
81
|
+
const { error, description } = oauthErrorOf(body);
|
|
82
|
+
return new ServiceTokenRefusedError(`flytedesk-id refused the service-token exchange for "${options.appId}" (${status}${error ? ` ${error}` : ""})` +
|
|
83
|
+
(description ? `: ${description}` : ""), status, error, description);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
const accessToken = parseIdpJson(text, SERVICE_TOKEN_PATH)?.access_token;
|
|
94
87
|
if (typeof accessToken !== "string" || accessToken.length === 0) {
|
|
95
88
|
throw new IdpUnavailableError(`flytedesk-id ${SERVICE_TOKEN_PATH} answered without an access_token`);
|
|
96
89
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/auth/service/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AACjC,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,8BAA8B,EAAE,QAAQ,EAAE,MAAM,gBAAg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AAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ConnectionCipher, KmsClient } from "./types.js";
|
|
2
|
+
export type ConnectionCipherOptions = {
|
|
3
|
+
/** Cloud KMS: values are decrypted by the key flytedesk ID names. */
|
|
4
|
+
cipher: "kms";
|
|
5
|
+
/** Whether the app runs in production; the app decides, app-kit reads no environment variable. */
|
|
6
|
+
production: boolean;
|
|
7
|
+
/** A Cloud KMS client built with the runtime service account's own credentials. */
|
|
8
|
+
kms: KmsClient;
|
|
9
|
+
} | {
|
|
10
|
+
/** AES-256-GCM under a local key, for local development and tests only. */
|
|
11
|
+
cipher: "local";
|
|
12
|
+
production: boolean;
|
|
13
|
+
/** 32 bytes. */
|
|
14
|
+
localKey: Uint8Array;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* The one place a cipher is chosen. The app builds it at start-up, so a missing choice, or
|
|
18
|
+
* the local cipher in production, stops the process there and never at the first read.
|
|
19
|
+
*/
|
|
20
|
+
export declare function createConnectionCipher(options: ConnectionCipherOptions): ConnectionCipher;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createKmsDecryptor } from "./kmsDecrypt.js";
|
|
2
|
+
import { createKmsEncryptor } from "./kmsEncrypt.js";
|
|
3
|
+
import { createLocalDecryptor } from "./localDecrypt.js";
|
|
4
|
+
import { createLocalEncryptor } from "./localEncrypt.js";
|
|
5
|
+
/**
|
|
6
|
+
* The one place a cipher is chosen. The app builds it at start-up, so a missing choice, or
|
|
7
|
+
* the local cipher in production, stops the process there and never at the first read.
|
|
8
|
+
*/
|
|
9
|
+
export function createConnectionCipher(options) {
|
|
10
|
+
const choice = options?.cipher;
|
|
11
|
+
if (choice === "local") {
|
|
12
|
+
if (options.production) {
|
|
13
|
+
throw new Error("the local cipher must not be used in production: choose the KMS cipher");
|
|
14
|
+
}
|
|
15
|
+
const { localKey } = options;
|
|
16
|
+
return { ...createLocalDecryptor(localKey), ...createLocalEncryptor(localKey) };
|
|
17
|
+
}
|
|
18
|
+
if (choice === "kms") {
|
|
19
|
+
const { kms } = options;
|
|
20
|
+
if (!kms)
|
|
21
|
+
throw new Error("the KMS cipher needs a Cloud KMS client");
|
|
22
|
+
return { ...createKmsDecryptor(kms), ...createKmsEncryptor(kms) };
|
|
23
|
+
}
|
|
24
|
+
throw new Error(`a cipher choice is required: "kms" or "local", got ${JSON.stringify(choice)}`);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/connections/cipher/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAoBzD;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAgC;IACrE,MAAM,MAAM,GAAa,OAA4C,EAAE,MAAM,CAAC;IAC9E,IAAI,MAAM,KAAK,OAAO,EAAE,CAAC;QACvB,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,wEAAwE,CAAC,CAAC;QAC5F,CAAC;QACD,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAgE,CAAC;QACtF,OAAO,EAAE,GAAG,oBAAoB,CAAC,QAAQ,CAAC,EAAE,GAAG,oBAAoB,CAAC,QAAQ,CAAC,EAAE,CAAC;IAClF,CAAC;IACD,IAAI,MAAM,KAAK,KAAK,EAAE,CAAC;QACrB,MAAM,EAAE,GAAG,EAAE,GAAG,OAA8D,CAAC;QAC/E,IAAI,CAAC,GAAG;YAAE,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QACrE,OAAO,EAAE,GAAG,kBAAkB,CAAC,GAAG,CAAC,EAAE,GAAG,kBAAkB,CAAC,GAAG,CAAC,EAAE,CAAC;IACpE,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,sDAAsD,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AAClG,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ConnectionCipherError } from "../errors.js";
|
|
2
|
+
/** The Cloud KMS cipher's decrypt side, over a client the app built with its own credentials. */
|
|
3
|
+
export function createKmsDecryptor(client) {
|
|
4
|
+
return {
|
|
5
|
+
async decrypt({ keyName, ciphertext, aad }) {
|
|
6
|
+
try {
|
|
7
|
+
const [response] = await client.decrypt({
|
|
8
|
+
name: keyName,
|
|
9
|
+
ciphertext: Buffer.from(ciphertext, "base64"),
|
|
10
|
+
additionalAuthenticatedData: Buffer.from(aad),
|
|
11
|
+
});
|
|
12
|
+
const plaintext = response.plaintext;
|
|
13
|
+
if (plaintext === undefined || plaintext === null)
|
|
14
|
+
throw new Error("empty response");
|
|
15
|
+
// The client hands bytes back as a Buffer, or as base64 text when it is told to.
|
|
16
|
+
return (typeof plaintext === "string" ? Buffer.from(plaintext, "base64") : Buffer.from(plaintext)).toString("utf8");
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
const code = err?.code;
|
|
20
|
+
throw new ConnectionCipherError(`a value could not be decrypted under key ${keyName}${typeof code === "number" ? ` (KMS code ${code})` : ""}`);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=kmsDecrypt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kmsDecrypt.js","sourceRoot":"","sources":["../../../src/connections/cipher/kmsDecrypt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAGrD,iGAAiG;AACjG,MAAM,UAAU,kBAAkB,CAAC,MAAkC;IACnE,OAAO;QACL,KAAK,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE;YACxC,IAAI,CAAC;gBACH,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC;oBACtC,IAAI,EAAE,OAAO;oBACb,UAAU,EAAE,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,QAAQ,CAAC;oBAC7C,2BAA2B,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;iBAC9C,CAAC,CAAC;gBACH,MAAM,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAC;gBACrC,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,IAAI;oBAAE,MAAM,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC;gBACrF,iFAAiF;gBACjF,OAAO,CAAC,OAAO,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YACtH,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,MAAM,IAAI,GAAI,GAAiC,EAAE,IAAI,CAAC;gBACtD,MAAM,IAAI,qBAAqB,CAC7B,4CAA4C,OAAO,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,cAAc,IAAI,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAC9G,CAAC;YACJ,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ConnectionEncryptor, KmsClient } from "./types.js";
|
|
2
|
+
/** The Cloud KMS cipher's encrypt side (flytedesk ID's), over a client built with its own credentials. */
|
|
3
|
+
export declare function createKmsEncryptor(client: Pick<KmsClient, "encrypt">): ConnectionEncryptor;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ConnectionCipherError } from "../errors.js";
|
|
2
|
+
/** The Cloud KMS cipher's encrypt side (flytedesk ID's), over a client built with its own credentials. */
|
|
3
|
+
export function createKmsEncryptor(client) {
|
|
4
|
+
return {
|
|
5
|
+
async encrypt({ keyName, plaintext, aad }) {
|
|
6
|
+
try {
|
|
7
|
+
const [response] = await client.encrypt({
|
|
8
|
+
name: keyName,
|
|
9
|
+
plaintext: Buffer.from(plaintext, "utf8"),
|
|
10
|
+
additionalAuthenticatedData: Buffer.from(aad),
|
|
11
|
+
});
|
|
12
|
+
const ciphertext = response.ciphertext;
|
|
13
|
+
if (ciphertext === undefined || ciphertext === null)
|
|
14
|
+
throw new Error("empty response");
|
|
15
|
+
return (typeof ciphertext === "string" ? Buffer.from(ciphertext, "base64") : Buffer.from(ciphertext)).toString("base64");
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
const code = err?.code;
|
|
19
|
+
throw new ConnectionCipherError(`a value could not be encrypted under key ${keyName}${typeof code === "number" ? ` (KMS code ${code})` : ""}`);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=kmsEncrypt.js.map
|