@oneie/cli 5.5.2 → 5.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 +29 -0
- package/dist/package.json +2 -2
- package/dist/src/index.js +15 -15
- package/dist/src/init.d.ts +10 -1
- package/dist/src/init.js +26 -11
- package/dist/src/join.d.ts +1 -1
- package/dist/src/join.js +2 -1
- package/dist/src/key-publish.d.ts +40 -0
- package/dist/src/key-publish.js +232 -0
- package/dist/src/key-seal.d.ts +21 -0
- package/dist/src/key-seal.js +142 -0
- package/dist/src/key.js +22 -5
- package/dist/src/lib/age-plugin.d.ts +51 -0
- package/dist/src/lib/age-plugin.js +130 -0
- package/dist/src/lib/env-sealed.d.ts +53 -0
- package/dist/src/lib/env-sealed.js +184 -0
- package/dist/src/lib/journey.d.ts +137 -0
- package/dist/src/lib/journey.js +232 -0
- package/dist/src/lib/key-receipt.d.ts +48 -0
- package/dist/src/lib/key-receipt.js +116 -0
- package/dist/src/lib/lock-lookup.d.ts +40 -0
- package/dist/src/lib/lock-lookup.js +92 -0
- package/dist/src/lib/message-seal.d.ts +63 -0
- package/dist/src/lib/message-seal.js +150 -0
- package/dist/src/lib/shop-init.js +2 -2
- package/dist/src/lib/vault-read.d.ts +24 -0
- package/dist/src/lib/vault-read.js +77 -0
- package/dist/src/media.d.ts +27 -0
- package/dist/src/media.js +249 -18
- package/dist/src/secrets-device.d.ts +32 -0
- package/dist/src/secrets-device.js +127 -0
- package/dist/src/secrets-give.d.ts +125 -0
- package/dist/src/secrets-give.js +236 -0
- package/dist/src/secrets-run.d.ts +31 -0
- package/dist/src/secrets-run.js +182 -0
- package/dist/src/secrets.d.ts +32 -0
- package/dist/src/secrets.js +197 -0
- package/dist/src/social.js +27 -8
- package/dist/src/start.d.ts +15 -0
- package/dist/src/start.js +200 -0
- package/dist/src/write.d.ts +43 -0
- package/dist/src/write.js +174 -0
- package/package.json +2 -2
package/dist/src/media.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Command } from 'commander';
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
2
3
|
import { readFileSync, statSync } from 'node:fs';
|
|
3
4
|
import { basename, extname } from 'node:path';
|
|
4
5
|
import { mediaListAllWith } from '@oneie/sdk';
|
|
5
6
|
import { authHeaders, jsonMode, resolveBase } from './lib/client.js';
|
|
6
7
|
import { askPath } from './lib/api-path.js';
|
|
8
|
+
import { webBase } from './lib/roster.js';
|
|
7
9
|
// `one media` — the two media doors a JSON body cannot serve comfortably.
|
|
8
10
|
//
|
|
9
11
|
// WHAT IS DELIBERATELY NOT HERE, and why. The CLI already reaches every receiver:
|
|
@@ -36,6 +38,100 @@ const UPLOAD_MAX_BYTES = 5 * 1024 * 1024;
|
|
|
36
38
|
* spend a request on a value the zod schema will reject. */
|
|
37
39
|
const PREFIXES = ['media', 'chat', 'ads', 'products', 'pages', 'brand', 'courses'];
|
|
38
40
|
const VIDEO_EXT = new Set(['.mp4', '.webm', '.mov', '.m4v', '.avi', '.mkv']);
|
|
41
|
+
/** The three containers PUT /api/media/video-upload sniffs. `.avi`/`.mkv` are
|
|
42
|
+
* refused locally — the door would store them, sniff them, and 415 after the
|
|
43
|
+
* whole upload; nothing is gained by sending 100 MB to learn that. */
|
|
44
|
+
export const VIDEO_CONTENT_TYPES = {
|
|
45
|
+
'.mp4': 'video/mp4',
|
|
46
|
+
'.m4v': 'video/mp4',
|
|
47
|
+
'.webm': 'video/webm',
|
|
48
|
+
'.mov': 'video/quicktime',
|
|
49
|
+
};
|
|
50
|
+
/** The door's own ceiling (video-upload.ts MAX_BYTES) — refused before a byte is sent. */
|
|
51
|
+
export const VIDEO_MAX_BYTES = 100 * 1024 * 1024;
|
|
52
|
+
/** The library hands back ROOT-RELATIVE urls. Meta, TikTok and every social
|
|
53
|
+
* platform fetch media by URL from their own servers, so the printed url is
|
|
54
|
+
* made absolute against the web host — the one form that works off this machine. */
|
|
55
|
+
export function absoluteUrl(base, url) {
|
|
56
|
+
if (!url)
|
|
57
|
+
return '';
|
|
58
|
+
return /^https?:\/\//i.test(url) ? url : `${webBase(base).replace(/\/$/, '')}${url.startsWith('/') ? '' : '/'}${url}`;
|
|
59
|
+
}
|
|
60
|
+
/** `{workspace}/media/{name}` — the receipt names where the bytes landed. */
|
|
61
|
+
export function workspaceOfKey(key) {
|
|
62
|
+
return key ? key.split('/')[0] ?? '' : '';
|
|
63
|
+
}
|
|
64
|
+
/** ffprobe's `-print_format json -show_streams -show_format` → the dims the
|
|
65
|
+
* content:everywhere door plans on. The first video stream's width/height; the
|
|
66
|
+
* container's duration when it has one (an image has none). Anything unreadable
|
|
67
|
+
* is `{}` — a dimension is measured or absent, never guessed. */
|
|
68
|
+
export function parseProbe(json) {
|
|
69
|
+
let parsed;
|
|
70
|
+
try {
|
|
71
|
+
parsed = JSON.parse(json);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return {};
|
|
75
|
+
}
|
|
76
|
+
const out = {};
|
|
77
|
+
const stream = (parsed.streams ?? []).find((s) => s.codec_type === 'video' && typeof s.width === 'number' && typeof s.height === 'number');
|
|
78
|
+
if (stream) {
|
|
79
|
+
out.width = stream.width;
|
|
80
|
+
out.height = stream.height;
|
|
81
|
+
}
|
|
82
|
+
const d = Number(parsed.format?.duration);
|
|
83
|
+
if (Number.isFinite(d) && d > 0)
|
|
84
|
+
out.durationS = d;
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
/** Neither upload door reports dimensions (video-upload.ts and media:upload answer
|
|
88
|
+
* key/url/contentType/size only), so the CLI measures the LOCAL file with ffprobe
|
|
89
|
+
* when there is one on this machine (`ONE_FFPROBE` names the binary; default
|
|
90
|
+
* `ffprobe` on PATH). `null` means it could not measure — the caller says so. */
|
|
91
|
+
export function probeMedia(path) {
|
|
92
|
+
const bin = process.env.ONE_FFPROBE || 'ffprobe';
|
|
93
|
+
try {
|
|
94
|
+
const r = spawnSync(bin, ['-v', 'error', '-print_format', 'json', '-show_streams', '-show_format', path], {
|
|
95
|
+
encoding: 'utf8',
|
|
96
|
+
timeout: 15_000,
|
|
97
|
+
});
|
|
98
|
+
if (r.error || r.status !== 0 || !r.stdout)
|
|
99
|
+
return null;
|
|
100
|
+
const dims = parseProbe(r.stdout);
|
|
101
|
+
return dims.width !== undefined ? dims : null;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** One `skip`/`impossible` row from the door, however it spells its fields —
|
|
108
|
+
* `platform`/`network`, `format`/`key`/`label`, and the `reason`. */
|
|
109
|
+
function misfitLine(row) {
|
|
110
|
+
if (!row || typeof row !== 'object')
|
|
111
|
+
return String(row);
|
|
112
|
+
const r = row;
|
|
113
|
+
const where = [r.platform ?? r.network, r.format ?? r.key ?? r.label].filter((x) => typeof x === 'string' && x).join('/');
|
|
114
|
+
const why = typeof r.reason === 'string' ? r.reason : typeof r.why === 'string' ? r.why : '';
|
|
115
|
+
return `${where || '?'}${why ? ` — ${why}` : ''}`;
|
|
116
|
+
}
|
|
117
|
+
/** The door's counts and misfits, as lines. Counts are every top-level number the
|
|
118
|
+
* door answered (posts, ads, derived, …) — printed by name, never summed into a
|
|
119
|
+
* claim. The one sentence this never prints is "published": the door drafts. */
|
|
120
|
+
function renderEverywhere(res, review) {
|
|
121
|
+
const w = process.stdout;
|
|
122
|
+
const counts = Object.entries(res)
|
|
123
|
+
.filter(([, v]) => typeof v === 'number')
|
|
124
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
125
|
+
.join(' ');
|
|
126
|
+
w.write(`everywhere ${counts || 'no counts answered'} — drafts only, nothing was published\n`);
|
|
127
|
+
for (const row of res.skipped ?? [])
|
|
128
|
+
w.write(`skip ${misfitLine(row)}\n`);
|
|
129
|
+
for (const row of res.impossible ?? [])
|
|
130
|
+
w.write(`impossible ${misfitLine(row)}\n`);
|
|
131
|
+
if (res.truncated !== undefined)
|
|
132
|
+
w.write(`TRUNCATED the door cut its answer short: ${JSON.stringify(res.truncated)}\n`);
|
|
133
|
+
w.write(`review ${review}\n`);
|
|
134
|
+
}
|
|
39
135
|
const CONTENT_TYPES = {
|
|
40
136
|
'.png': 'image/png',
|
|
41
137
|
'.jpg': 'image/jpeg',
|
|
@@ -134,6 +230,131 @@ function renderList(res) {
|
|
|
134
230
|
w.write(`${it.key} ${it.kind} ${human(it.size)} ${it.contentType} ${it.uploaded ?? '-'}\n`);
|
|
135
231
|
}
|
|
136
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* PUT the file to the streamed video door. It lives on the WEB host, never the
|
|
235
|
+
* gateway: api.one.ie forwards receivers, not this route, so webBase() picks the
|
|
236
|
+
* host whatever --api says. 100 MB fits in memory here; reading it whole gives fetch
|
|
237
|
+
* a Content-Length, which lets the door refuse an oversized body before writing.
|
|
238
|
+
*/
|
|
239
|
+
async function uploadVideo(api, path, contentType, workspace) {
|
|
240
|
+
const base = webBase(resolveBase({ api }, DEFAULT_API)).replace(/\/$/, '');
|
|
241
|
+
const qs = workspace ? `?workspace=${encodeURIComponent(workspace)}` : '';
|
|
242
|
+
const headers = { ...authHeaders(), 'Content-Type': contentType };
|
|
243
|
+
const res = await fetch(`${base}/api/media/video-upload${qs}`, {
|
|
244
|
+
method: 'PUT',
|
|
245
|
+
headers,
|
|
246
|
+
body: readFileSync(path),
|
|
247
|
+
});
|
|
248
|
+
const text = await res.text();
|
|
249
|
+
let body;
|
|
250
|
+
try {
|
|
251
|
+
body = text ? JSON.parse(text) : {};
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
throw new Error(`video-upload → ${res.status} ${text.slice(0, 200)}`);
|
|
255
|
+
}
|
|
256
|
+
if (!res.ok) {
|
|
257
|
+
const why = body.reason ?? body.error ?? text.slice(0, 200);
|
|
258
|
+
const hint = res.status === 401
|
|
259
|
+
? ' — no valid key: run `one login`'
|
|
260
|
+
: res.status === 403
|
|
261
|
+
? ` — your key may not write ${workspace ?? 'this workspace'}`
|
|
262
|
+
: '';
|
|
263
|
+
return { ok: false, error: `video-upload → ${res.status} ${why}${hint}` };
|
|
264
|
+
}
|
|
265
|
+
return { ...body, kind: 'video', workspace: body.workspace ?? workspaceOfKey(body.key) };
|
|
266
|
+
}
|
|
267
|
+
/** Draft a post carrying the uploaded media. Drafts only: the posts door refuses
|
|
268
|
+
* scheduledAt/status from a key, because a scheduled post publishes itself. */
|
|
269
|
+
async function draftPost(api, group, caption, platforms, mediaUrl, mediaType) {
|
|
270
|
+
const base = webBase(resolveBase({ api }, DEFAULT_API)).replace(/\/$/, '');
|
|
271
|
+
const res = await fetch(`${base}/api/social/posts`, {
|
|
272
|
+
method: 'POST',
|
|
273
|
+
headers: authHeaders(),
|
|
274
|
+
body: JSON.stringify({ groupId: group, content: caption, platforms, mediaUrl, mediaType }),
|
|
275
|
+
});
|
|
276
|
+
const text = await res.text();
|
|
277
|
+
if (!res.ok)
|
|
278
|
+
return { ok: false, error: `social/posts → ${res.status} ${text.slice(0, 200)}` };
|
|
279
|
+
try {
|
|
280
|
+
return { ok: true, id: JSON.parse(text).post?.id };
|
|
281
|
+
}
|
|
282
|
+
catch {
|
|
283
|
+
return { ok: false, error: `social/posts → ${res.status} unreadable body` };
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The same door the Everywhere panel presses: ONE ask, the pinned fields and no
|
|
288
|
+
* others — `{ workspace, campaignId, mediaUrl, mediaType, width, height,
|
|
289
|
+
* durationS, text, stageKey }`. Dims come from a local ffprobe; when there is
|
|
290
|
+
* none they are OMITTED (the door measures what it can), and the omission is
|
|
291
|
+
* returned so it is printed. Nothing here publishes — the door drafts.
|
|
292
|
+
*/
|
|
293
|
+
async function runEverywhere(opts, workspace, mediaUrl, mediaType) {
|
|
294
|
+
const dims = probeMedia(opts.path);
|
|
295
|
+
const text = opts.text ?? basename(opts.path, extname(opts.path));
|
|
296
|
+
const data = {
|
|
297
|
+
workspace,
|
|
298
|
+
campaignId: opts.campaign,
|
|
299
|
+
mediaUrl,
|
|
300
|
+
mediaType,
|
|
301
|
+
...(dims?.width !== undefined ? { width: dims.width } : {}),
|
|
302
|
+
...(dims?.height !== undefined ? { height: dims.height } : {}),
|
|
303
|
+
...(dims?.durationS !== undefined ? { durationS: dims.durationS } : {}),
|
|
304
|
+
text,
|
|
305
|
+
...(opts.stage ? { stageKey: opts.stage } : {}),
|
|
306
|
+
};
|
|
307
|
+
try {
|
|
308
|
+
// 30s is the ask door's own ceiling; the door derives and drafts inside it.
|
|
309
|
+
const res = (await asker(opts.api)('content:everywhere', data, 30_000));
|
|
310
|
+
return { res, probed: dims !== null };
|
|
311
|
+
}
|
|
312
|
+
catch (e) {
|
|
313
|
+
return { res: { ok: false, error: e instanceof Error ? e.message : String(e) }, probed: dims !== null };
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
async function finish(sub, res, sent, opts) {
|
|
317
|
+
if (res.ok === false) {
|
|
318
|
+
if (jsonMode(sub))
|
|
319
|
+
process.stdout.write(JSON.stringify(res, null, 2) + '\n');
|
|
320
|
+
return fail(res.error ?? 'upload failed');
|
|
321
|
+
}
|
|
322
|
+
const url = absoluteUrl(resolveBase({ api: opts.api }, DEFAULT_API), res.url);
|
|
323
|
+
const workspace = res.workspace ?? workspaceOfKey(res.key);
|
|
324
|
+
const mediaType = res.kind === 'video' || (res.contentType ?? '').startsWith('video/') ? 'video' : 'image';
|
|
325
|
+
let post;
|
|
326
|
+
if (opts.post) {
|
|
327
|
+
const platforms = opts.platforms.split(',').map((p) => p.trim()).filter(Boolean);
|
|
328
|
+
post = await draftPost(opts.api, workspace, opts.post, platforms, url, mediaType);
|
|
329
|
+
}
|
|
330
|
+
let everywhere;
|
|
331
|
+
if (opts.everywhere)
|
|
332
|
+
everywhere = await runEverywhere(opts, workspace, url, mediaType);
|
|
333
|
+
const review = `${webBase(resolveBase({ api: opts.api }, DEFAULT_API)).replace(/\/$/, '')}/u/${workspace}/marketing/social`;
|
|
334
|
+
if (jsonMode(sub)) {
|
|
335
|
+
process.stdout.write(JSON.stringify({ ...res, url, ...(post ? { post, review } : {}), ...(everywhere ? { everywhere: everywhere.res, review, probed: everywhere.probed } : {}) }, null, 2) + '\n');
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
// The `key` is the receipt: it names the workspace prefix the bytes ACTUALLY
|
|
339
|
+
// reached. Printed always, so a --workspace upload can never merely look right.
|
|
340
|
+
process.stdout.write(`ok key=${res.key ?? '?'} kind=${res.kind ?? '?'} type=${res.contentType ?? '?'} stored=${human(res.size)} sent=${human(sent)}\n` +
|
|
341
|
+
` ${url}\n`);
|
|
342
|
+
if (post?.ok)
|
|
343
|
+
process.stdout.write(`draft post=${post.id ?? '?'} — publish it here: ${review}\n`);
|
|
344
|
+
if (everywhere) {
|
|
345
|
+
if (!everywhere.probed) {
|
|
346
|
+
process.stdout.write('note no ffprobe on this machine — width/height/duration were not sent, the door measures what it can\n');
|
|
347
|
+
}
|
|
348
|
+
if (everywhere.res.ok !== false)
|
|
349
|
+
renderEverywhere(everywhere.res, review);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
if (post && !post.ok)
|
|
353
|
+
fail(`uploaded, but the post draft failed: ${post.error}`);
|
|
354
|
+
if (everywhere && everywhere.res.ok === false) {
|
|
355
|
+
fail(`uploaded, but content:everywhere refused: ${everywhere.res.error ?? 'unknown'}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
137
358
|
export function mediaCmd() {
|
|
138
359
|
const cmd = new Command('media').description('A workspace media library — list the WHOLE of it, upload a local file. ' +
|
|
139
360
|
'(generate has no subcommand on purpose: one ask media:generate --data \'{"prompt":"..."}\')');
|
|
@@ -183,14 +404,24 @@ export function mediaCmd() {
|
|
|
183
404
|
});
|
|
184
405
|
cmd
|
|
185
406
|
.command('upload <path>')
|
|
186
|
-
.description('Upload a LOCAL image
|
|
187
|
-
'
|
|
407
|
+
.description('Upload a LOCAL image (media:upload, 5 MiB) or video (.mp4/.mov/.webm, streamed, 100 MB) into the library. ' +
|
|
408
|
+
'Add --post "<caption>" [--platforms facebook] to draft a social post with it — a person publishes it. ' +
|
|
409
|
+
'For a remote image URL: one ask media:upload --data \'{"url":"https://…"}\'')
|
|
188
410
|
.option('--workspace <slug>', 'Group slug to write (default: your own). A workspace you may not write is REFUSED, never downgraded.')
|
|
189
411
|
.option('--content-type <ct>', 'A HINT only — the stored type is decided by the magic bytes')
|
|
190
412
|
.option('--filename <name>', 'A LABEL for the key, not a path (default: the basename). The extension always comes from the sniffed bytes.')
|
|
413
|
+
.option('--post <caption>', 'Also draft a social post carrying this media (drafts only — publish on the social page)')
|
|
414
|
+
.option('--platforms <list>', 'With --post: comma-separated platforms. Default facebook — the one platform that posts the video itself; linkedin/twitter post the caption only', 'facebook')
|
|
415
|
+
.option('--campaign <id>', 'The campaign (world_groups gid) this media is for — required by --everywhere')
|
|
416
|
+
.option('--everywhere', 'After the upload, run content:everywhere: draft it for every social format and ad format it fits, in the campaign. Drafts only; prints the skips. Dims come from a local ffprobe when there is one.')
|
|
417
|
+
.option('--text <text>', 'With --everywhere: the message the formats carry (default: the file name)')
|
|
418
|
+
.option('--stage <key>', 'With --everywhere: the campaign stage the drafts belong to')
|
|
191
419
|
.option('--api <url>', 'API base URL', DEFAULT_API)
|
|
192
420
|
.option('--json', 'Print the raw response')
|
|
193
421
|
.action(async (path, opts, sub) => {
|
|
422
|
+
if (opts.everywhere && !opts.campaign) {
|
|
423
|
+
return fail('--everywhere needs --campaign <id>: the drafts belong to a campaign. Nothing was sent.');
|
|
424
|
+
}
|
|
194
425
|
let size;
|
|
195
426
|
try {
|
|
196
427
|
const st = statSync(path);
|
|
@@ -205,11 +436,21 @@ export function mediaCmd() {
|
|
|
205
436
|
return fail(`${path} is empty`);
|
|
206
437
|
const ext = extname(path).toLowerCase();
|
|
207
438
|
if (VIDEO_EXT.has(ext)) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
439
|
+
const videoType = VIDEO_CONTENT_TYPES[ext];
|
|
440
|
+
if (!videoType) {
|
|
441
|
+
return fail(`${path} is ${ext} — the library takes .mp4, .mov and .webm. Nothing was sent. Convert it first (ffmpeg -i ${basename(path)} out.mp4).`);
|
|
442
|
+
}
|
|
443
|
+
if (size > VIDEO_MAX_BYTES) {
|
|
444
|
+
return fail(`${path} is ${human(size)} — the video door caps at ${human(VIDEO_MAX_BYTES)}. Nothing was sent.`);
|
|
445
|
+
}
|
|
446
|
+
let res;
|
|
447
|
+
try {
|
|
448
|
+
res = await uploadVideo(opts.api, path, videoType, opts.workspace);
|
|
449
|
+
}
|
|
450
|
+
catch (e) {
|
|
451
|
+
return fail(e instanceof Error ? e.message : String(e));
|
|
452
|
+
}
|
|
453
|
+
return finish(sub, res, size, { ...opts, path });
|
|
213
454
|
}
|
|
214
455
|
if (ext === '.svg') {
|
|
215
456
|
return fail(`${path} is SVG, which media:upload refuses deliberately (the public reader serves the stored type verbatim). Nothing was sent.`);
|
|
@@ -240,17 +481,7 @@ export function mediaCmd() {
|
|
|
240
481
|
catch (e) {
|
|
241
482
|
return fail(e instanceof Error ? e.message : String(e));
|
|
242
483
|
}
|
|
243
|
-
|
|
244
|
-
process.stdout.write(JSON.stringify(res, null, 2) + '\n');
|
|
245
|
-
}
|
|
246
|
-
else if (res.ok !== false) {
|
|
247
|
-
// The `key` is the receipt: it names the workspace prefix the bytes ACTUALLY
|
|
248
|
-
// reached. Printed always, so a --workspace upload can never merely look right.
|
|
249
|
-
process.stdout.write(`ok key=${res.key ?? '?'} kind=${res.kind ?? '?'} type=${res.contentType ?? '?'} stored=${human(res.size)} sent=${human(size)}\n` +
|
|
250
|
-
` ${res.url ?? ''}\n`);
|
|
251
|
-
}
|
|
252
|
-
if (res.ok === false)
|
|
253
|
-
fail(res.error ?? 'media:upload failed');
|
|
484
|
+
return finish(sub, res, size, { ...opts, path });
|
|
254
485
|
});
|
|
255
486
|
return cmd;
|
|
256
487
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* secrets-device.ts — `one secrets add-device`: make this Mac a reader of your sealed files
|
|
3
|
+
* (text/secrets.md deliverable 3; text/secrets-todo.md § C3).
|
|
4
|
+
*
|
|
5
|
+
* Runs `age-plugin-se keygen --access-control current-biometry -o <identity>`: a key born
|
|
6
|
+
* in the Mac's Secure Enclave that never leaves the chip. The identity file is only a
|
|
7
|
+
* handle to that key (the chip holds the secret), written mode 600 and never overwritten
|
|
8
|
+
* without --replace. The `age1se1…` lock is then appended to ~/.one/secrets-readers.json,
|
|
9
|
+
* where C1's `seal` finds it.
|
|
10
|
+
*
|
|
11
|
+
* The access control is NOT a flag. Real use is always `current-biometry`; the test seam
|
|
12
|
+
* below is a module value the command line cannot reach.
|
|
13
|
+
*
|
|
14
|
+
* Nothing the identity holds is read or printed here: the lock comes from
|
|
15
|
+
* `age-plugin-se recipients -i`, and age-plugin-se's own output is discarded.
|
|
16
|
+
*/
|
|
17
|
+
import type { Command } from 'commander';
|
|
18
|
+
/** The Secure Enclave identity file. ONE_SECRETS_DEVICE moves it (tests). */
|
|
19
|
+
export declare function deviceIdentityFile(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Test seam. Only a test module can set it; there is no flag and no env var for it, so a
|
|
22
|
+
* person at a terminal always gets the finger rule. A key made with `none` opens without Touch ID.
|
|
23
|
+
*/
|
|
24
|
+
export declare const deviceSeam: {
|
|
25
|
+
accessControl: 'current-biometry' | 'none';
|
|
26
|
+
};
|
|
27
|
+
interface AddDeviceOpts {
|
|
28
|
+
name?: string;
|
|
29
|
+
replace?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export declare function addDeviceAction(o: AddDeviceOpts, c: Command): Promise<void>;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* secrets-device.ts — `one secrets add-device`: make this Mac a reader of your sealed files
|
|
3
|
+
* (text/secrets.md deliverable 3; text/secrets-todo.md § C3).
|
|
4
|
+
*
|
|
5
|
+
* Runs `age-plugin-se keygen --access-control current-biometry -o <identity>`: a key born
|
|
6
|
+
* in the Mac's Secure Enclave that never leaves the chip. The identity file is only a
|
|
7
|
+
* handle to that key (the chip holds the secret), written mode 600 and never overwritten
|
|
8
|
+
* without --replace. The `age1se1…` lock is then appended to ~/.one/secrets-readers.json,
|
|
9
|
+
* where C1's `seal` finds it.
|
|
10
|
+
*
|
|
11
|
+
* The access control is NOT a flag. Real use is always `current-biometry`; the test seam
|
|
12
|
+
* below is a module value the command line cannot reach.
|
|
13
|
+
*
|
|
14
|
+
* Nothing the identity holds is read or printed here: the lock comes from
|
|
15
|
+
* `age-plugin-se recipients -i`, and age-plugin-se's own output is discarded.
|
|
16
|
+
*/
|
|
17
|
+
import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
18
|
+
import { homedir, hostname } from 'node:os';
|
|
19
|
+
import { dirname, join } from 'node:path';
|
|
20
|
+
import { jsonMode } from './lib/client.js';
|
|
21
|
+
import { keygen, PLUGIN_LOCK_RE, recipientOf, requireAge } from './lib/age-plugin.js';
|
|
22
|
+
import { fail, readersFile } from './secrets.js';
|
|
23
|
+
/** The Secure Enclave identity file. ONE_SECRETS_DEVICE moves it (tests). */
|
|
24
|
+
export function deviceIdentityFile() {
|
|
25
|
+
return process.env.ONE_SECRETS_DEVICE?.trim() || join(homedir(), '.one', 'secrets-device.txt');
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Test seam. Only a test module can set it; there is no flag and no env var for it, so a
|
|
29
|
+
* person at a terminal always gets the finger rule. A key made with `none` opens without Touch ID.
|
|
30
|
+
*/
|
|
31
|
+
export const deviceSeam = { accessControl: 'current-biometry' };
|
|
32
|
+
const badReaders = (path) => ({ ok: false, error: `${path} is not a list of age1… locks`, code: 'bad_readers_file' });
|
|
33
|
+
function lockOf(e) {
|
|
34
|
+
const l = typeof e === 'string' ? e : e?.lock;
|
|
35
|
+
return typeof l === 'string' ? l.trim() : undefined;
|
|
36
|
+
}
|
|
37
|
+
/** Both file shapes C1 reads: a JSON array, or `{ "readers": [...] }`. No file is a new list. */
|
|
38
|
+
function readReaders(path) {
|
|
39
|
+
if (!existsSync(path))
|
|
40
|
+
return { shape: 'new', items: [] };
|
|
41
|
+
let parsed;
|
|
42
|
+
try {
|
|
43
|
+
parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return badReaders(path);
|
|
47
|
+
}
|
|
48
|
+
if (Array.isArray(parsed))
|
|
49
|
+
return { shape: 'list', items: parsed };
|
|
50
|
+
const list = parsed?.readers;
|
|
51
|
+
if (Array.isArray(list))
|
|
52
|
+
return { shape: 'object', items: list };
|
|
53
|
+
return badReaders(path);
|
|
54
|
+
}
|
|
55
|
+
function writeReaders(path, doc, original) {
|
|
56
|
+
const body = doc.shape === 'object' ? { ...original, readers: doc.items } : doc.items;
|
|
57
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
58
|
+
const tmp = `${path}.tmp-${process.pid}`;
|
|
59
|
+
writeFileSync(tmp, JSON.stringify(body, null, 2) + '\n', { mode: 0o600 });
|
|
60
|
+
renameSync(tmp, path);
|
|
61
|
+
}
|
|
62
|
+
export async function addDeviceAction(o, c) {
|
|
63
|
+
const identity = deviceIdentityFile();
|
|
64
|
+
const readersPath = readersFile();
|
|
65
|
+
const bins = requireAge();
|
|
66
|
+
if (typeof bins === 'string') {
|
|
67
|
+
return fail(c, { ok: false, error: bins === 'no_plugin' ? 'the `age-plugin-se` program is not installed — brew install age-plugin-se' : 'the `age` program is not installed — brew install age', code: bins });
|
|
68
|
+
}
|
|
69
|
+
const doc = readReaders(readersPath);
|
|
70
|
+
if ('ok' in doc)
|
|
71
|
+
return fail(c, doc);
|
|
72
|
+
let oldLock = null;
|
|
73
|
+
try {
|
|
74
|
+
lstatSync(identity);
|
|
75
|
+
if (!o.replace)
|
|
76
|
+
return fail(c, { ok: false, error: `${identity} already exists — this Mac is already a reader (pass --replace to make a new key)`, code: 'device_exists' });
|
|
77
|
+
oldLock = recipientOf(identity);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
if (e.code !== 'ENOENT')
|
|
81
|
+
return fail(c, { ok: false, error: `cannot check ${identity}`, code: 'failed' });
|
|
82
|
+
}
|
|
83
|
+
const tmp = `${identity}.tmp-${process.pid}`;
|
|
84
|
+
try {
|
|
85
|
+
mkdirSync(dirname(identity), { recursive: true, mode: 0o700 });
|
|
86
|
+
const made = keygen(tmp, deviceSeam.accessControl);
|
|
87
|
+
if (made !== true) {
|
|
88
|
+
// No Secure Enclave (or no biometry) on this machine: a fixed message, exit 3.
|
|
89
|
+
fail(c, { ok: false, error: 'this Mac could not make a Secure Enclave key — it has no Secure Enclave, or no Touch ID set up', code: 'no_secure_enclave' });
|
|
90
|
+
process.exitCode = 3;
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
chmodSync(tmp, 0o600);
|
|
94
|
+
if ((statSync(tmp).mode & 0o777) !== 0o600)
|
|
95
|
+
return fail(c, { ok: false, error: `${identity} could not be made private (mode 600)`, code: 'bad_mode' });
|
|
96
|
+
const lock = recipientOf(tmp);
|
|
97
|
+
if (!lock || !PLUGIN_LOCK_RE.test(lock))
|
|
98
|
+
return fail(c, { ok: false, error: 'could not read the lock of the new key', code: 'failed' });
|
|
99
|
+
renameSync(tmp, identity);
|
|
100
|
+
// Re-reading after the keygen: the file may have changed under a long Touch ID wait.
|
|
101
|
+
const fresh = readReaders(readersPath);
|
|
102
|
+
if ('ok' in fresh)
|
|
103
|
+
return fail(c, fresh);
|
|
104
|
+
const original = existsSync(readersPath) ? JSON.parse(readFileSync(readersPath, 'utf8')) : undefined;
|
|
105
|
+
const name = o.name?.trim() || hostname();
|
|
106
|
+
const items = fresh.items.filter((e) => {
|
|
107
|
+
const l = lockOf(e);
|
|
108
|
+
return l !== lock && !(oldLock && l === oldLock);
|
|
109
|
+
});
|
|
110
|
+
items.push({ lock, name, added: new Date().toISOString() });
|
|
111
|
+
writeReaders(readersPath, { shape: fresh.shape, items }, original);
|
|
112
|
+
if (jsonMode(c))
|
|
113
|
+
process.stdout.write(JSON.stringify({ ok: true, lock, name, identity, replaced: Boolean(oldLock) }) + '\n');
|
|
114
|
+
else {
|
|
115
|
+
process.stdout.write(`device ${lock}\n`);
|
|
116
|
+
process.stdout.write(`name ${name}\nidentity ${identity} (a handle — the key stays in the chip)\n`);
|
|
117
|
+
process.stdout.write('re-seal your .env to add this Mac as a reader\n');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
fail(c, { ok: false, error: `could not add this Mac: ${e.code ?? 'failed'}`, code: 'failed' });
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
if (existsSync(tmp))
|
|
125
|
+
unlinkSync(tmp);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* secrets-give.ts — `one secrets give NAME`: give a key to ONE from the terminal
|
|
3
|
+
* (text/secrets.md deliverable 6; text/secrets-todo.md § C8).
|
|
4
|
+
*
|
|
5
|
+
* In order, all on this machine until the last step:
|
|
6
|
+
*
|
|
7
|
+
* 1 ONE's lock, read from ONE's vault (the single entry of its `readers`, plan D5) through
|
|
8
|
+
* `@oneie/sdk/secrets-give` `readOneLock`. No answer is `no_one_lock`; a `--lock` that
|
|
9
|
+
* differs is `lock_mismatch`. There is no constant to fall back to, ever.
|
|
10
|
+
* 2 the value — from `.env.sealed` (opened in memory with your paper key, the C2 way) or
|
|
11
|
+
* from piped stdin with `--stdin`. NEVER from argv: a word after NAME refuses exit 2,
|
|
12
|
+
* unread, so a key never sits in shell history or `ps`
|
|
13
|
+
* 3 sealed to ONE's lock alone (`sealForOne`, which reads ONE's vault itself and seals only to
|
|
14
|
+
* what it names — a `--lock` that differs is `lock_mismatch` there too), over the canonical
|
|
15
|
+
* plaintext naming this vault, NAME and --provider, so the blob is no other vault's, name's or
|
|
16
|
+
* provider's key; the value dropped
|
|
17
|
+
* 4 signed by an AGENT's own ed25519 key (`--signer-key <file>`, `giveData`) over
|
|
18
|
+
* `canonicalSigned(payload)`. Your master key never signs a give: `--key` opens the
|
|
19
|
+
* sealed file and does nothing else, and `--signer person` exits 2 — a person's touch is
|
|
20
|
+
* on the seal page (C9)
|
|
21
|
+
* 5 `secrets:give` through ask(), under your API key: the web side adds who you are and
|
|
22
|
+
* forwards to the opener. The answer names the last four characters and nothing more.
|
|
23
|
+
*
|
|
24
|
+
* The body names who gives: `--as <actor>` — the actor your API key is, which the web side attests;
|
|
25
|
+
* the opener refuses `subject_mismatch` for any other, so a captured give resent under another
|
|
26
|
+
* session is never theirs.
|
|
27
|
+
*
|
|
28
|
+
* ONE's lock has one source: the reader of ONE's vault. Until C14 publishes it, the shipped reader
|
|
29
|
+
* answers no lock and this command refuses `no_one_lock` — there is no environment or file
|
|
30
|
+
* override (a test injects a reader through `giveLoaders.oneVault`, in code).
|
|
31
|
+
*
|
|
32
|
+
* A rotate names what it replaces: `--replaces <sigHash>` (the `sigHash` of the last give, or the
|
|
33
|
+
* hash a `stale` refusal prints), signed into the body; a first give sends null. A captured give
|
|
34
|
+
* re-sent after a rotate is therefore stale at the opener, never a rollback.
|
|
35
|
+
*
|
|
36
|
+
* Exits 0 given (or checked) · 1 refused · 2 usage. Prints no value, no line of the file, no key.
|
|
37
|
+
*/
|
|
38
|
+
import { Command } from 'commander';
|
|
39
|
+
/** The part of `@oneie/sdk/secrets-give` this command uses, typed here so a typecheck does not need that SDK built. */
|
|
40
|
+
export interface OneVaultReader {
|
|
41
|
+
(): Promise<{
|
|
42
|
+
readers: string[];
|
|
43
|
+
sealedHash: string | null;
|
|
44
|
+
}>;
|
|
45
|
+
}
|
|
46
|
+
export interface GiveSdk {
|
|
47
|
+
readOneLock(reader: OneVaultReader): Promise<{
|
|
48
|
+
ok: true;
|
|
49
|
+
lock: string;
|
|
50
|
+
} | {
|
|
51
|
+
ok: false;
|
|
52
|
+
error: 'no_one_lock';
|
|
53
|
+
detail: string;
|
|
54
|
+
}>;
|
|
55
|
+
sealForOne(value: string, bind: {
|
|
56
|
+
vault: string;
|
|
57
|
+
name: string;
|
|
58
|
+
provider: string;
|
|
59
|
+
}, reader: OneVaultReader, lock?: string): Promise<{
|
|
60
|
+
ok: true;
|
|
61
|
+
sealed: string;
|
|
62
|
+
} | {
|
|
63
|
+
ok: false;
|
|
64
|
+
error: 'no_one_lock' | 'lock_mismatch';
|
|
65
|
+
detail: string;
|
|
66
|
+
}>;
|
|
67
|
+
giveData(input: {
|
|
68
|
+
vault: string;
|
|
69
|
+
name: string;
|
|
70
|
+
provider: string;
|
|
71
|
+
sealed: string;
|
|
72
|
+
mode?: 'give' | 'check';
|
|
73
|
+
allowFlagged?: boolean;
|
|
74
|
+
replaces?: string | null;
|
|
75
|
+
subject: string;
|
|
76
|
+
}, secret: Uint8Array): {
|
|
77
|
+
signer: {
|
|
78
|
+
kind: 'agent';
|
|
79
|
+
sig: string;
|
|
80
|
+
signer: string;
|
|
81
|
+
};
|
|
82
|
+
payload: {
|
|
83
|
+
type: 'give';
|
|
84
|
+
vault: string;
|
|
85
|
+
body: Record<string, unknown>;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
decodeSignerKey(text: string): Uint8Array | null;
|
|
89
|
+
fixtureReader(f?: {
|
|
90
|
+
oneLock: string | null;
|
|
91
|
+
sealPageHash: string | null;
|
|
92
|
+
}): OneVaultReader;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Swappable in tests (the message-seal.ts `loaders` shape): the cli resolves `@oneie/sdk` through
|
|
96
|
+
* its own node_modules, which can be a published or main-tree SDK older than `./secrets-give`;
|
|
97
|
+
* a test serves the source. `oneVault` is the reader of ONE's vault and the ONLY source of ONE's
|
|
98
|
+
* lock — the IC#7 fixture until C14 reaches testnet, which answers no lock, so a give refuses
|
|
99
|
+
* `no_one_lock`. Nothing in the environment or on disk can name another lock (C8 repair 3); a
|
|
100
|
+
* test injects a reader by assigning this field. `fetch` is the one network call.
|
|
101
|
+
*/
|
|
102
|
+
export declare const giveLoaders: {
|
|
103
|
+
sdk: () => Promise<GiveSdk>;
|
|
104
|
+
oneVault: (sdk: GiveSdk) => Promise<OneVaultReader>;
|
|
105
|
+
fetch: (input: string, init: RequestInit) => Promise<Response>;
|
|
106
|
+
};
|
|
107
|
+
interface GiveOpts {
|
|
108
|
+
provider?: string;
|
|
109
|
+
vault?: string;
|
|
110
|
+
file: string;
|
|
111
|
+
stdin?: boolean;
|
|
112
|
+
key?: string;
|
|
113
|
+
signer: string;
|
|
114
|
+
signerKey?: string;
|
|
115
|
+
lock?: string;
|
|
116
|
+
check?: boolean;
|
|
117
|
+
allowFlagged?: boolean;
|
|
118
|
+
replaces?: string;
|
|
119
|
+
as?: string;
|
|
120
|
+
api?: string;
|
|
121
|
+
}
|
|
122
|
+
export declare function giveAction(name: string, extra: string[], o: GiveOpts, c: Command): Promise<void>;
|
|
123
|
+
/** The `give` subcommand, fresh per call (a Command has one parent). */
|
|
124
|
+
export declare function buildGive(): Command;
|
|
125
|
+
export {};
|