decryption 0.5.1 → 0.6.1

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.
Files changed (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -325
  3. package/index.js +8 -64
  4. package/package.json +8 -24
  5. package/commands/account.d.ts +0 -4
  6. package/commands/account.js +0 -470
  7. package/commands/cosmology.d.ts +0 -4
  8. package/commands/cosmology.js +0 -97
  9. package/commands/doctor.d.ts +0 -3
  10. package/commands/doctor.js +0 -42
  11. package/commands/encrypt.d.ts +0 -12
  12. package/commands/encrypt.js +0 -251
  13. package/commands/guard.d.ts +0 -3
  14. package/commands/guard.js +0 -92
  15. package/commands/keychain.d.ts +0 -4
  16. package/commands/keychain.js +0 -92
  17. package/commands/keys.d.ts +0 -4
  18. package/commands/keys.js +0 -74
  19. package/commands/passkey.d.ts +0 -4
  20. package/commands/passkey.js +0 -135
  21. package/commands/salt.d.ts +0 -4
  22. package/commands/salt.js +0 -113
  23. package/commands/secrets.d.ts +0 -4
  24. package/commands/secrets.js +0 -214
  25. package/commands/shamir.d.ts +0 -4
  26. package/commands/shamir.js +0 -62
  27. package/commands/vault.d.ts +0 -7
  28. package/commands/vault.js +0 -382
  29. package/commands/wallet.d.ts +0 -5
  30. package/commands/wallet.js +0 -129
  31. package/commands/workspace.d.ts +0 -8
  32. package/commands/workspace.js +0 -289
  33. package/commands.d.ts +0 -8
  34. package/commands.js +0 -126
  35. package/esm/commands/account.js +0 -466
  36. package/esm/commands/cosmology.js +0 -93
  37. package/esm/commands/doctor.js +0 -38
  38. package/esm/commands/encrypt.js +0 -245
  39. package/esm/commands/guard.js +0 -88
  40. package/esm/commands/keychain.js +0 -88
  41. package/esm/commands/keys.js +0 -70
  42. package/esm/commands/passkey.js +0 -131
  43. package/esm/commands/salt.js +0 -109
  44. package/esm/commands/secrets.js +0 -210
  45. package/esm/commands/shamir.js +0 -58
  46. package/esm/commands/vault.js +0 -376
  47. package/esm/commands/wallet.js +0 -125
  48. package/esm/commands/workspace.js +0 -283
  49. package/esm/commands.js +0 -120
  50. package/esm/index.js +0 -50
  51. package/esm/utils/bytes.js +0 -286
  52. package/esm/utils/dispatch.js +0 -52
  53. package/esm/utils/env.js +0 -44
  54. package/esm/utils/errors.js +0 -44
  55. package/esm/utils/header.js +0 -14
  56. package/esm/utils/io.js +0 -49
  57. package/esm/utils/passphrase.js +0 -49
  58. package/esm/utils/recipients.js +0 -74
  59. package/esm/utils/stash.js +0 -61
  60. package/esm/utils/temp.js +0 -51
  61. package/esm/utils/text.js +0 -2
  62. package/esm/workspace/bundle.js +0 -239
  63. package/esm/workspace/doctor.js +0 -104
  64. package/esm/workspace/git.js +0 -72
  65. package/esm/workspace/guard.js +0 -111
  66. package/esm/workspace/layout.js +0 -106
  67. package/esm/workspace/manifest.js +0 -147
  68. package/esm/workspace/recipients.js +0 -37
  69. package/esm/workspace/snippets.js +0 -54
  70. package/esm/workspace/state.js +0 -46
  71. package/esm/workspace/status.js +0 -93
  72. package/esm/workspace/tar.js +0 -235
  73. package/index.d.ts +0 -12
  74. package/utils/bytes.d.ts +0 -86
  75. package/utils/bytes.js +0 -301
  76. package/utils/dispatch.d.ts +0 -24
  77. package/utils/dispatch.js +0 -58
  78. package/utils/env.d.ts +0 -26
  79. package/utils/env.js +0 -49
  80. package/utils/errors.d.ts +0 -25
  81. package/utils/errors.js +0 -50
  82. package/utils/header.d.ts +0 -4
  83. package/utils/header.js +0 -18
  84. package/utils/io.d.ts +0 -10
  85. package/utils/io.js +0 -57
  86. package/utils/passphrase.d.ts +0 -15
  87. package/utils/passphrase.js +0 -53
  88. package/utils/recipients.d.ts +0 -28
  89. package/utils/recipients.js +0 -80
  90. package/utils/stash.d.ts +0 -40
  91. package/utils/stash.js +0 -76
  92. package/utils/temp.d.ts +0 -10
  93. package/utils/temp.js +0 -59
  94. package/utils/text.d.ts +0 -2
  95. package/utils/text.js +0 -6
  96. package/workspace/bundle.d.ts +0 -50
  97. package/workspace/bundle.js +0 -248
  98. package/workspace/doctor.d.ts +0 -8
  99. package/workspace/doctor.js +0 -108
  100. package/workspace/git.d.ts +0 -21
  101. package/workspace/git.js +0 -83
  102. package/workspace/guard.d.ts +0 -23
  103. package/workspace/guard.js +0 -118
  104. package/workspace/layout.d.ts +0 -47
  105. package/workspace/layout.js +0 -113
  106. package/workspace/manifest.d.ts +0 -52
  107. package/workspace/manifest.js +0 -157
  108. package/workspace/recipients.d.ts +0 -16
  109. package/workspace/recipients.js +0 -45
  110. package/workspace/snippets.d.ts +0 -29
  111. package/workspace/snippets.js +0 -58
  112. package/workspace/state.d.ts +0 -13
  113. package/workspace/state.js +0 -52
  114. package/workspace/status.d.ts +0 -54
  115. package/workspace/status.js +0 -99
  116. package/workspace/tar.d.ts +0 -29
  117. package/workspace/tar.js +0 -240
@@ -1,466 +0,0 @@
1
- import { AccountManager, StepUpRequiredError, VaultCredentials, } from '@decryption/accounts';
2
- import { readFileSync } from 'fs';
3
- import { runSubcommand, takeFirst } from '../utils/dispatch';
4
- import { fromEnv } from '../utils/env';
5
- import { CliError, EXIT } from '../utils/errors';
6
- import { emit, readStdin } from '../utils/io';
7
- import { openVault } from './vault';
8
- export const accountUsage = `
9
- Account Command:
10
-
11
- dcrypt account <subcommand> [OPTIONS]
12
-
13
- Constructive accounts and their API keys, held in the local vault. This is the
14
- one part of dcrypt that talks to a server: the endpoint you name, nothing else.
15
-
16
- Subcommands:
17
- list List the accounts in the vault
18
- signup <email> Create an account on --endpoint, then store the session
19
- signin <email> Sign in and store the session
20
- signout <email> Sign out and drop the stored token
21
- forget <email> Remove the account and its keys from the vault
22
- link-code <email> <code> Answer this account's MFA step-ups with a vault code
23
- unlink-code <email> Stop answering its step-ups from the vault
24
- key list [email] List stored API keys
25
- key create <name> Mint an API key for --account
26
- key reveal <name> Print an API key secret
27
- key revoke <name> Revoke server-side, then delete the local copy
28
- key assign <name> <db> Serve this key as that database's data-plane token
29
- token [email] Print the bearer a harness would be given
30
- principal list [email] Scoped sub-identities, with their scopes and masks
31
- principal create <name> Create one — personal, or scoped to --org
32
- principal delete <id> Remove one server-side
33
-
34
- Options:
35
- --endpoint <url> Auth endpoint (or DCRYPT_AUTH_ENDPOINT)
36
- --account <email> Which account a key command applies to
37
- --expires-days <n> Lifetime for a new key (default: no expiry)
38
- --access-level <level> Access level for a new key
39
- --database <id> Tag a new key as that database's data-plane token
40
- --principal <id> Mint the key as this principal, not as you
41
- --org <id> Scope a principal to this org; omit for a personal one
42
- --read-only The principal may only read
43
- --bypass-step-up The principal may skip MFA step-up (for CI)
44
- --password-file <path> Read the account password from a file
45
- --password-stdin Read the account password from stdin
46
- --json Machine-readable output
47
- --passphrase-file <p> Read the master password from a file
48
- --help, -h Show this help message
49
-
50
- Examples:
51
- dcrypt account signin dev@example.com --endpoint http://auth.localhost:3000/graphql
52
- dcrypt account key create ci --account dev@example.com --expires-days 30
53
- dcrypt account link-code dev@example.com "Constructive dev"
54
- dcrypt account key reveal ci
55
- dcrypt account token dev@example.com
56
- dcrypt account principal create ci-deploy --org <org-id> --read-only
57
- dcrypt account key create ci --principal <principal-id> --org <org-id>
58
- `;
59
- /**
60
- * The account password, never from argv — it would be visible in `ps` to every
61
- * process on the machine, exactly like the master passphrase.
62
- */
63
- const resolveAccountPassword = async (argv, prompter, confirm) => {
64
- if (argv.password !== undefined) {
65
- throw new CliError('refusing to read a password from argv (it is visible in `ps`); use --password-file <path> or --password-stdin');
66
- }
67
- const file = argv['password-file'] ?? argv.passwordFile;
68
- if (typeof file === 'string' && file.length) {
69
- try {
70
- return readFileSync(file, 'utf8').replace(/\r?\n$/, '');
71
- }
72
- catch {
73
- throw new CliError(`cannot read password file ${file}`, EXIT.notFound);
74
- }
75
- }
76
- const fromEnvironment = fromEnv('accountPassword');
77
- if (fromEnvironment !== undefined)
78
- return fromEnvironment.replace(/\r?\n$/, '');
79
- if (argv['password-stdin'] || argv.passwordStdin) {
80
- return readStdin().replace(/\r?\n$/, '');
81
- }
82
- const { accountPassword } = await prompter.prompt({}, [
83
- {
84
- type: 'password',
85
- name: 'accountPassword',
86
- message: 'Account password',
87
- required: true,
88
- },
89
- ]);
90
- if (!accountPassword)
91
- throw new CliError('an account password is required');
92
- if (confirm) {
93
- const { confirmation } = await prompter.prompt({}, [
94
- {
95
- type: 'password',
96
- name: 'confirmation',
97
- message: 'Confirm account password',
98
- required: true,
99
- },
100
- ]);
101
- if (confirmation !== accountPassword) {
102
- throw new CliError('the passwords do not match');
103
- }
104
- }
105
- return accountPassword;
106
- };
107
- const resolveEndpoint = (argv) => {
108
- const endpoint = typeof argv.endpoint === 'string' ? argv.endpoint : fromEnv('authEndpoint');
109
- if (!endpoint) {
110
- throw new CliError('an --endpoint is required (or set DCRYPT_AUTH_ENDPOINT)');
111
- }
112
- return endpoint;
113
- };
114
- const withVault = async (argv, prompter, run) => {
115
- const vault = await openVault(argv, prompter);
116
- try {
117
- await run(new AccountManager(vault), vault);
118
- }
119
- finally {
120
- await vault.lock();
121
- }
122
- };
123
- const findAccount = async (accounts, ref) => {
124
- const all = await accounts.listAccounts();
125
- const match = all.find((account) => account.itemId === ref) ??
126
- all.find((account) => account.email === ref) ??
127
- all.find((account) => account.email.toLowerCase() === ref.toLowerCase());
128
- if (!match)
129
- throw new CliError(`no account "${ref}" in the vault`, EXIT.notFound);
130
- return match;
131
- };
132
- /**
133
- * The account a command applies to: the one named, or the only one there is.
134
- * Never a guess when the vault holds several.
135
- */
136
- const resolveAccount = async (accounts, ref) => {
137
- if (ref)
138
- return findAccount(accounts, ref);
139
- const all = await accounts.listAccounts();
140
- if (all.length === 0) {
141
- throw new CliError('no account in the vault — sign in first', EXIT.notFound);
142
- }
143
- if (all.length > 1) {
144
- throw new CliError('name an account: the vault holds more than one');
145
- }
146
- return all[0];
147
- };
148
- const findKey = async (accounts, ref) => {
149
- const all = await accounts.listApiKeys();
150
- const match = all.find((key) => key.itemId === ref) ??
151
- all.find((key) => key.keyId === ref) ??
152
- all.find((key) => key.name === ref);
153
- if (!match)
154
- throw new CliError(`no API key "${ref}" in the vault`, EXIT.notFound);
155
- return match;
156
- };
157
- const describe = (account) => `${account.signedIn ? 'signed in ' : 'signed out'} ${account.email.padEnd(28)} ${account.endpoint}`;
158
- const list = async (argv, prompter) => withVault(argv, prompter, async (accounts) => {
159
- const all = await accounts.listAccounts();
160
- emit(argv, all, () => all.map(describe).join('\n') || '(no accounts)');
161
- });
162
- const authenticate = async (argv, prompter, mode) => {
163
- const { first, newArgv } = takeFirst(argv);
164
- if (!first)
165
- throw new CliError('an email address is required');
166
- const endpoint = resolveEndpoint(newArgv);
167
- const password = await resolveAccountPassword(newArgv, prompter, mode === 'signUp');
168
- await withVault(newArgv, prompter, async (accounts) => {
169
- const account = await accounts[mode]({ endpoint, email: first, password });
170
- emit(newArgv, account, () => `${mode === 'signUp' ? 'created' : 'signed in as'} ${account.email} (${account.userId})`);
171
- });
172
- };
173
- const signup = async (argv, prompter) => authenticate(argv, prompter, 'signUp');
174
- const signin = async (argv, prompter) => authenticate(argv, prompter, 'signIn');
175
- const signout = async (argv, prompter) => {
176
- const { first, newArgv } = takeFirst(argv);
177
- if (!first)
178
- throw new CliError('an account is required');
179
- await withVault(newArgv, prompter, async (accounts) => {
180
- const account = await findAccount(accounts, first);
181
- await accounts.signOut(account.itemId);
182
- emit(newArgv, { itemId: account.itemId }, () => `signed out ${account.email}`);
183
- });
184
- };
185
- const forget = async (argv, prompter) => {
186
- const { first, newArgv } = takeFirst(argv);
187
- if (!first)
188
- throw new CliError('an account is required');
189
- await withVault(newArgv, prompter, async (accounts) => {
190
- const account = await findAccount(accounts, first);
191
- await accounts.forget(account.itemId);
192
- emit(newArgv, { itemId: account.itemId }, () => `removed ${account.email} and its keys from the vault`);
193
- });
194
- };
195
- /**
196
- * Point an account at a one-time code already in the vault, so the server's
197
- * MFA step-up is answered without a human reaching for a phone.
198
- */
199
- const linkCode = async (argv, prompter) => {
200
- const { first, newArgv } = takeFirst(argv);
201
- const { first: codeRef, newArgv: rest } = takeFirst(newArgv);
202
- if (!first)
203
- throw new CliError('an account is required');
204
- if (!codeRef)
205
- throw new CliError('a one-time code item is required');
206
- await withVault(rest, prompter, async (accounts, vault) => {
207
- const account = await findAccount(accounts, first);
208
- const codes = await vault.listItems({ kind: 'totp' });
209
- const code = codes.find((item) => item.id === codeRef) ??
210
- codes.find((item) => item.title === codeRef) ??
211
- codes.find((item) => item.title.toLowerCase() === codeRef.toLowerCase());
212
- if (!code) {
213
- throw new CliError(`no one-time code "${codeRef}" in the vault`, EXIT.notFound);
214
- }
215
- await accounts.linkTotp(account.itemId, code.id);
216
- emit(rest, { itemId: account.itemId, totpItemId: code.id }, () => `"${code.title}" will answer MFA for ${account.email}`);
217
- });
218
- };
219
- const unlinkCode = async (argv, prompter) => {
220
- const { first, newArgv } = takeFirst(argv);
221
- if (!first)
222
- throw new CliError('an account is required');
223
- await withVault(newArgv, prompter, async (accounts) => {
224
- const account = await findAccount(accounts, first);
225
- await accounts.unlinkTotp(account.itemId);
226
- emit(newArgv, { itemId: account.itemId }, () => `${account.email} will ask for a code again`);
227
- });
228
- };
229
- const keyList = async (argv, prompter) => {
230
- const { first, newArgv } = takeFirst(argv);
231
- await withVault(newArgv, prompter, async (accounts) => {
232
- const accountItemId = first
233
- ? (await findAccount(accounts, first)).itemId
234
- : undefined;
235
- const keys = await accounts.listApiKeys(accountItemId);
236
- emit(newArgv, keys, () => keys
237
- .map((key) => `${key.name.padEnd(24)} ${key.keyId} ${key.expiresAt ?? 'no expiry'}`)
238
- .join('\n') || '(no API keys)');
239
- });
240
- };
241
- /**
242
- * Run something the server may refuse until a factor is re-proved: ask for the
243
- * factor it named, then run the identical call once more. Not a blanket retry —
244
- * only a `StepUpRequiredError` reaches here, and only one extra attempt is made.
245
- */
246
- const withStepUp = async (prompter, run) => {
247
- try {
248
- return await run();
249
- }
250
- catch (error) {
251
- if (!(error instanceof StepUpRequiredError))
252
- throw error;
253
- const mfa = error.kind === 'mfa';
254
- const { proof } = await prompter.prompt({}, [
255
- {
256
- type: mfa ? 'text' : 'password',
257
- name: 'proof',
258
- message: mfa
259
- ? 'The server wants a fresh one-time code'
260
- : 'The server wants your account password again',
261
- required: true,
262
- },
263
- ]);
264
- if (!proof)
265
- throw error;
266
- return run(mfa ? { totpCode: proof } : { password: proof });
267
- }
268
- };
269
- const text = (value) => typeof value === 'string' && value.length ? value : undefined;
270
- const lifetime = (argv) => {
271
- const raw = argv['expires-days'] ?? argv.expiresDays;
272
- if (raw === undefined)
273
- return undefined;
274
- const days = Number(raw);
275
- if (!Number.isInteger(days) || days <= 0) {
276
- throw new CliError('--expires-days must be a positive whole number of days');
277
- }
278
- return { days };
279
- };
280
- const keyCreate = async (argv, prompter) => {
281
- const { first, newArgv } = takeFirst(argv);
282
- if (!first)
283
- throw new CliError('a key name is required');
284
- const accessLevel = newArgv['access-level'] ?? newArgv.accessLevel;
285
- await withVault(newArgv, prompter, async (accounts) => {
286
- const all = await accounts.listAccounts();
287
- const ref = typeof newArgv.account === 'string' ? newArgv.account : undefined;
288
- if (!ref && all.length === 0) {
289
- throw new CliError('no account in the vault — sign in first');
290
- }
291
- if (!ref && all.length > 1) {
292
- throw new CliError('--account is required when the vault holds more than one account');
293
- }
294
- const account = ref ? await findAccount(accounts, ref) : all[0];
295
- const request = {
296
- name: first,
297
- expiresIn: lifetime(newArgv),
298
- accessLevel: typeof accessLevel === 'string' ? accessLevel : undefined,
299
- databaseId: text(newArgv.database),
300
- principalId: text(newArgv.principal),
301
- orgId: text(newArgv.org),
302
- };
303
- const key = await withStepUp(prompter, (proof) => accounts.createApiKey(account.itemId, request, proof));
304
- emit(newArgv, key, () => `created "${key.name}" (${key.keyId}) — stored in the vault; read it with "dcrypt account key reveal ${key.name}"`);
305
- });
306
- };
307
- const keyReveal = async (argv, prompter) => {
308
- const { first, newArgv } = takeFirst(argv);
309
- if (!first)
310
- throw new CliError('a key name is required');
311
- await withVault(newArgv, prompter, async (accounts) => {
312
- const key = await findKey(accounts, first);
313
- const secret = await accounts.revealApiKey(key.itemId);
314
- emit(newArgv, { apiKey: secret }, () => secret);
315
- });
316
- };
317
- const keyRevoke = async (argv, prompter) => {
318
- const { first, newArgv } = takeFirst(argv);
319
- if (!first)
320
- throw new CliError('a key name is required');
321
- await withVault(newArgv, prompter, async (accounts) => {
322
- const key = await findKey(accounts, first);
323
- await withStepUp(prompter, (proof) => accounts.revokeApiKey(key.itemId, proof));
324
- emit(newArgv, { keyId: key.keyId }, () => `revoked "${key.name}" (${key.keyId})`);
325
- });
326
- };
327
- /**
328
- * Say that an existing key is a database's data-plane token, which is what a
329
- * harness host asks for by database id.
330
- */
331
- const keyAssign = async (argv, prompter) => {
332
- const { first, newArgv } = takeFirst(argv);
333
- const { first: databaseId, newArgv: rest } = takeFirst(newArgv);
334
- if (!first)
335
- throw new CliError('a key name is required');
336
- if (!databaseId)
337
- throw new CliError('a database id is required');
338
- await withVault(rest, prompter, async (accounts) => {
339
- const key = await findKey(accounts, first);
340
- await accounts.assignKeyToDatabase(key.itemId, databaseId);
341
- emit(rest, { itemId: key.itemId, databaseId }, () => `"${key.name}" is now the data-plane token for ${databaseId}`);
342
- });
343
- };
344
- /**
345
- * The bearer a harness host would be handed. Printing a live token is the
346
- * point of the command, so it goes to stdout alone and nowhere else — but the
347
- * vault stays the only place it is stored.
348
- */
349
- const token = async (argv, prompter) => {
350
- const { first, newArgv } = takeFirst(argv);
351
- const databaseRef = newArgv.database;
352
- await withVault(newArgv, prompter, async (accounts) => {
353
- const accountItemId = first
354
- ? (await findAccount(accounts, first)).itemId
355
- : undefined;
356
- const credentials = new VaultCredentials(accounts, { accountItemId });
357
- if (typeof databaseRef === 'string' && databaseRef.length) {
358
- const result = await credentials.dataToken(databaseRef);
359
- if (!result.token) {
360
- throw new CliError(`no key in the vault is the data-plane token for ${databaseRef} — tag one with "dcrypt account key assign <name> ${databaseRef}"`, EXIT.notFound);
361
- }
362
- emit(newArgv, result, () => result.token);
363
- return;
364
- }
365
- const bearer = await credentials.accountBearer();
366
- if (!bearer) {
367
- throw new CliError(first
368
- ? `${first} is signed out — sign in again first`
369
- : 'name an account: a bearer is only served when exactly one account is signed in', EXIT.notFound);
370
- }
371
- emit(newArgv, { accountBearer: bearer }, () => bearer);
372
- });
373
- };
374
- /**
375
- * A principal's reach: the entities it touches, and the per-scope overrides
376
- * that narrow it. No override row means it simply inherits its owner there,
377
- * which is worth saying out loud rather than rendering an empty list.
378
- */
379
- const describePrincipal = (principal) => {
380
- const flags = [
381
- principal.isReadOnly ? 'read-only' : null,
382
- principal.bypassStepUp ? 'skips step-up' : null,
383
- principal.useAdminOwner ? "inherits owner's admin" : null,
384
- ].filter(Boolean);
385
- const scopes = principal.scopes.length
386
- ? principal.scopes
387
- .map((scope) => ` scope ${scope.membershipType}: ${scope.isActive ? 'active' : 'disabled'}${scope.isReadOnly ? ', read-only' : ''}, mask ${scope.allowedMask ?? 'inherited'}`)
388
- .join('\n')
389
- : ' (no overrides — inherits the owner everywhere it is scoped)';
390
- return [
391
- `${principal.name.padEnd(24)} ${principal.principalId}`,
392
- ` ${flags.join(', ') || 'no flags'}`,
393
- ` entities: ${principal.entityIds.join(', ') || '(none)'}`,
394
- scopes,
395
- ].join('\n');
396
- };
397
- const principalList = async (argv, prompter) => {
398
- const { first, newArgv } = takeFirst(argv);
399
- await withVault(newArgv, prompter, async (accounts) => {
400
- const account = await resolveAccount(accounts, first);
401
- const principals = await accounts.listPrincipals(account.itemId);
402
- emit(newArgv, principals, () => principals.map(describePrincipal).join('\n\n') || '(no principals)');
403
- });
404
- };
405
- const principalCreate = async (argv, prompter) => {
406
- const { first, newArgv } = takeFirst(argv);
407
- if (!first)
408
- throw new CliError('a principal name is required');
409
- const orgId = text(newArgv.org);
410
- await withVault(newArgv, prompter, async (accounts) => {
411
- const account = await resolveAccount(accounts, text(newArgv.account));
412
- const principalId = await withStepUp(prompter, (proof) => accounts.createPrincipal(account.itemId, {
413
- name: first,
414
- orgId,
415
- isReadOnly: Boolean(newArgv['read-only'] ?? newArgv.readOnly),
416
- bypassStepUp: Boolean(newArgv['bypass-step-up'] ?? newArgv.bypassStepUp),
417
- }, proof));
418
- emit(newArgv, { principalId }, () => `created "${first}" (${principalId}) — mint keys as it with --principal ${principalId}`);
419
- });
420
- };
421
- const principalDelete = async (argv, prompter) => {
422
- const { first, newArgv } = takeFirst(argv);
423
- if (!first)
424
- throw new CliError('a principal id is required');
425
- await withVault(newArgv, prompter, async (accounts) => {
426
- const account = await resolveAccount(accounts, text(newArgv.account));
427
- await withStepUp(prompter, (proof) => accounts.deletePrincipal(account.itemId, first, proof));
428
- emit(newArgv, { principalId: first }, () => `removed ${first}`);
429
- });
430
- };
431
- const principalCommand = async (argv, prompter) => runSubcommand(argv, prompter, {
432
- name: 'account principal',
433
- usage: accountUsage,
434
- handlers: {
435
- list: principalList,
436
- create: principalCreate,
437
- delete: principalDelete,
438
- },
439
- });
440
- const keyCommand = async (argv, prompter) => runSubcommand(argv, prompter, {
441
- name: 'account key',
442
- usage: accountUsage,
443
- handlers: {
444
- list: keyList,
445
- create: keyCreate,
446
- reveal: keyReveal,
447
- revoke: keyRevoke,
448
- assign: keyAssign,
449
- },
450
- });
451
- export const accountCommand = async (argv, prompter) => runSubcommand(argv, prompter, {
452
- name: 'account',
453
- usage: accountUsage,
454
- handlers: {
455
- list,
456
- signup,
457
- signin,
458
- signout,
459
- forget,
460
- 'link-code': linkCode,
461
- 'unlink-code': unlinkCode,
462
- key: keyCommand,
463
- principal: principalCommand,
464
- token,
465
- },
466
- });
@@ -1,93 +0,0 @@
1
- import { armor, encrypt } from '@decryption/core';
2
- import { crypt as compatEncrypt, decrypt as compatDecrypt, decryptWithEncryptedSalt, encryptWithEncryptedSalt, } from '@decryption/cosmology-compat';
3
- import { runSubcommand, takeFirst } from '../utils/dispatch';
4
- import { fromEnv } from '../utils/env';
5
- import { CliError } from '../utils/errors';
6
- import { readInput, writeOutput } from '../utils/io';
7
- import { resolvePassphrase } from '../utils/passphrase';
8
- import { resolveKdf } from './encrypt';
9
- export const cosmologyUsage = `
10
- Cosmology Command:
11
-
12
- dcrypt cosmology <subcommand> [OPTIONS]
13
-
14
- Read and write data in the cosmology CLI's format (CryptoJS AES). That format
15
- is unauthenticated and derives its key with a single round of MD5 — prefer
16
- "dcrypt encrypt" for anything new, and use "upgrade" to migrate old data.
17
-
18
- Subcommands:
19
- decrypt Decrypt an old blob and print the plaintext
20
- encrypt Encrypt in the old format, for tools that still read it
21
- upgrade Decrypt an old blob and re-encrypt it with a new passphrase
22
-
23
- Options:
24
- --in <file> Read the ciphertext from a file ("-" for stdin)
25
- --out <file> Write output to a file
26
- --salt-file <file> Read the old salt from a file (avoids putting it in argv)
27
- --encrypted-salt <s> The two-layer scheme's encrypted salt
28
- --passphrase-file <p> New passphrase, for "upgrade"
29
- --passphrase-stdin Read the new passphrase from stdin
30
- --help, -h Show this help message
31
-
32
- Environment:
33
- SALT, ENCRYPTED_SALT The cosmology CLI's own variables, still honoured
34
-
35
- Examples:
36
- dcrypt cosmology decrypt --in old.txt --salt-file salt.txt
37
- SALT=... dcrypt cosmology decrypt --in old.txt
38
- dcrypt cosmology encrypt --in plain.txt --salt-file salt.txt --out old.txt
39
- dcrypt cosmology upgrade --in old.txt --salt-file salt.txt --out new.dcrypt
40
- `;
41
- const readSalt = async (argv, prompter) => {
42
- const file = argv['salt-file'] ?? argv.saltFile;
43
- if (typeof file === 'string' && file.length) {
44
- return readInput({ ...argv, in: file }).trim();
45
- }
46
- if (argv.salt !== undefined) {
47
- throw new CliError('refusing to read the old salt from argv (it is visible in `ps`); use --salt-file <path> or SALT');
48
- }
49
- const envSalt = fromEnv('salt');
50
- if (envSalt !== undefined)
51
- return envSalt.trim();
52
- const { salt } = await prompter.prompt({}, [
53
- { type: 'password', name: 'salt', message: 'Legacy salt', required: true },
54
- ]);
55
- return String(salt);
56
- };
57
- const readLegacyPlaintext = async (argv, prompter) => {
58
- const { first, newArgv } = takeFirst(argv);
59
- const ciphertext = readInput(newArgv, first).trim();
60
- const salt = await readSalt(newArgv, prompter);
61
- const encryptedSalt = newArgv['encrypted-salt'] ?? newArgv.encryptedSalt ?? fromEnv('encryptedSalt');
62
- return typeof encryptedSalt === 'string' && encryptedSalt.length
63
- ? decryptWithEncryptedSalt(salt, encryptedSalt, ciphertext)
64
- : compatDecrypt(salt, ciphertext);
65
- };
66
- const decryptCmd = async (argv, prompter) => {
67
- writeOutput(argv, await readLegacyPlaintext(argv, prompter));
68
- };
69
- const encryptCmd = async (argv, prompter) => {
70
- const { first, newArgv } = takeFirst(argv);
71
- const plaintext = readInput(newArgv, first);
72
- const salt = await readSalt(newArgv, prompter);
73
- const encryptedSalt = newArgv['encrypted-salt'] ?? newArgv.encryptedSalt ?? fromEnv('encryptedSalt');
74
- const ciphertext = typeof encryptedSalt === 'string' && encryptedSalt.length
75
- ? encryptWithEncryptedSalt(salt, encryptedSalt, plaintext)
76
- : compatEncrypt(salt, plaintext);
77
- writeOutput(newArgv, ciphertext);
78
- };
79
- const upgrade = async (argv, prompter) => {
80
- const plaintext = await readLegacyPlaintext(argv, prompter);
81
- const passphrase = await resolvePassphrase(argv, prompter, {
82
- confirm: true,
83
- message: 'New passphrase',
84
- });
85
- writeOutput(argv, armor(encrypt(plaintext, passphrase, { kdf: resolveKdf(argv.kdf) })));
86
- };
87
- export const cosmologyCommand = async (argv, prompter) => {
88
- await runSubcommand(argv, prompter, {
89
- name: 'cosmology',
90
- usage: cosmologyUsage,
91
- handlers: { decrypt: decryptCmd, encrypt: encryptCmd, upgrade },
92
- });
93
- };
@@ -1,38 +0,0 @@
1
- import { wantsHelp } from '../utils/dispatch';
2
- import { CliError } from '../utils/errors';
3
- import { emit } from '../utils/io';
4
- import { count } from '../utils/text';
5
- import { runDoctor } from '../workspace/doctor';
6
- import { findWorkspace } from '../workspace/layout';
7
- export const doctorUsage = `
8
- Doctor Command:
9
-
10
- dcrypt doctor [OPTIONS]
11
-
12
- Check this machine and this workspace: Node.js, your identity, recipients.txt,
13
- each section, and what git would let through. Warns when there is no recovery
14
- recipient. Reads only; asks for no passphrase.
15
-
16
- Exits 0 when nothing failed (warnings included), 1 otherwise.
17
-
18
- Options:
19
- --workspace <dir> The workspace root (default: the nearest folder upwards with a
20
- recipients.txt, otherwise the current folder)
21
- --json Machine-readable output
22
- --help, -h Show this help message
23
- `;
24
- export const doctorCommand = async (argv) => {
25
- if (wantsHelp(argv, doctorUsage))
26
- return;
27
- const workspace = findWorkspace(argv);
28
- const checks = await runDoctor(workspace);
29
- const failed = checks.filter((check) => check.level === 'fail').length;
30
- const warned = checks.filter((check) => check.level === 'warn').length;
31
- emit(argv, { workspace: workspace.root, ok: failed === 0, checks }, () => [
32
- `workspace: ${workspace.root}`,
33
- ...checks.map(({ level, name, message }) => `${level.padEnd(4)} ${name}: ${message}`),
34
- `${failed} failed, ${count(warned, 'warning')}`,
35
- ].join('\n'));
36
- if (failed)
37
- throw new CliError(`doctor found ${count(failed, 'problem')}`);
38
- };