@blocklive/cli 0.0.0-stage → 0.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/LICENSE +3 -0
- package/README.md +202 -2
- package/dist/browser.js +194 -0
- package/dist/catalog.js +1016 -0
- package/dist/change.js +181 -0
- package/dist/cli.js +1176 -0
- package/dist/config.js +105 -0
- package/dist/csv.js +76 -0
- package/dist/errors.js +75 -0
- package/dist/http.js +189 -0
- package/dist/idempotency.js +39 -0
- package/dist/image-file.js +95 -0
- package/dist/lock.js +186 -0
- package/dist/login.js +203 -0
- package/dist/main.js +49 -0
- package/dist/read.js +155 -0
- package/dist/reads/developer-request.js +31 -0
- package/dist/reads/developers.js +53 -0
- package/dist/reads/domains.js +84 -0
- package/dist/reads/events.js +956 -0
- package/dist/reads/index.js +47 -0
- package/dist/reads/invoices.js +177 -0
- package/dist/reads/payment-account.js +177 -0
- package/dist/reads/places.js +55 -0
- package/dist/reads/promo-import.js +183 -0
- package/dist/reads/promos.js +62 -0
- package/dist/reads/requests.js +119 -0
- package/dist/reads/tickets.js +386 -0
- package/dist/safe.js +88 -0
- package/dist/secrets.js +267 -0
- package/dist/stdin.js +72 -0
- package/dist/style.js +44 -0
- package/dist/unattended.js +311 -0
- package/package.json +30 -5
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.legacyCredential = exports.LEGACY_SERVICE = exports.chooseStore = exports.probeStore = exports.splitTokens = exports.joinTokens = exports.TOKEN = exports.credentialScope = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const config_1 = require("./config");
|
|
9
|
+
const errors_1 = require("./errors");
|
|
10
|
+
const lock_1 = require("./lock");
|
|
11
|
+
// Where a sign-in's tokens live: the macOS Keychain, the Secret Service on
|
|
12
|
+
// Linux, or, only when the person asked for it with --insecure-storage, a file
|
|
13
|
+
// readable by them alone. They are kept as one value, the access token and
|
|
14
|
+
// the refresh token joined by a colon, and always passed to the keychain
|
|
15
|
+
// tools on stdin, never as an argument, so they don't show up in a process
|
|
16
|
+
// list. There is no environment variable for them: the pair changes every 15
|
|
17
|
+
// minutes. A script that runs without the person uses an unattended sign-in
|
|
18
|
+
// (login --unattended), kept the same way.
|
|
19
|
+
// One service per configuration folder and address, so a sign-in is only
|
|
20
|
+
// ever sent where it was made. Legacy entries (legacyCredential, below) have
|
|
21
|
+
// no trustworthy origin: never used for a request but ending them on
|
|
22
|
+
// Blocklive itself.
|
|
23
|
+
function credentialScope(dir, url) {
|
|
24
|
+
const namespace = JSON.stringify([(0, fs_1.realpathSync)(dir), (0, config_1.baseUrl)(url)]);
|
|
25
|
+
return ('blocklive-cli-' + (0, crypto_1.createHash)('sha256').update(namespace).digest('hex'));
|
|
26
|
+
}
|
|
27
|
+
exports.credentialScope = credentialScope;
|
|
28
|
+
const ID = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
|
|
29
|
+
exports.TOKEN = new RegExp(`^bl_cli_(live|test)_${ID}_[0-9a-f]{64}:bl_clr_(live|test)_${ID}_[0-9a-f]{64}$`);
|
|
30
|
+
const joinTokens = ({ access, refresh }) => `${access}:${refresh}`;
|
|
31
|
+
exports.joinTokens = joinTokens;
|
|
32
|
+
function splitTokens(value) {
|
|
33
|
+
if (!value || !exports.TOKEN.test(value))
|
|
34
|
+
return undefined;
|
|
35
|
+
const [access, refresh] = value.split(':');
|
|
36
|
+
return { access, refresh };
|
|
37
|
+
}
|
|
38
|
+
exports.splitTokens = splitTokens;
|
|
39
|
+
const run = (command, args, input) => {
|
|
40
|
+
const result = (0, child_process_1.spawnSync)(command, args, {
|
|
41
|
+
input,
|
|
42
|
+
encoding: 'utf8',
|
|
43
|
+
stdio: ['pipe', 'pipe', 'ignore'],
|
|
44
|
+
timeout: 15000,
|
|
45
|
+
});
|
|
46
|
+
if (result.error)
|
|
47
|
+
return { status: null, stdout: '' };
|
|
48
|
+
return { status: result.status, stdout: result.stdout ?? '' };
|
|
49
|
+
};
|
|
50
|
+
function keychain(exec, service) {
|
|
51
|
+
return {
|
|
52
|
+
kind: 'keychain',
|
|
53
|
+
read(profile) {
|
|
54
|
+
const found = exec('/usr/bin/security', [
|
|
55
|
+
'find-generic-password',
|
|
56
|
+
'-s',
|
|
57
|
+
service,
|
|
58
|
+
'-a',
|
|
59
|
+
profile,
|
|
60
|
+
'-w',
|
|
61
|
+
]);
|
|
62
|
+
const token = found.stdout.trim();
|
|
63
|
+
return found.status === 0 && exports.TOKEN.test(token) ? token : undefined;
|
|
64
|
+
},
|
|
65
|
+
write(profile, token) {
|
|
66
|
+
if (!exports.TOKEN.test(token))
|
|
67
|
+
throw new Error('invalid_token');
|
|
68
|
+
// security -i reads commands from stdin; the token and profile
|
|
69
|
+
// match patterns with nothing to quote.
|
|
70
|
+
const added = exec('/usr/bin/security', ['-i'], `add-generic-password -U -s ${service} -a ${profile} -w ${token}\n`);
|
|
71
|
+
if (added.status !== 0)
|
|
72
|
+
throw new errors_1.CliError("Couldn't save the sign-in in the Keychain.");
|
|
73
|
+
},
|
|
74
|
+
remove(profile) {
|
|
75
|
+
exec('/usr/bin/security', [
|
|
76
|
+
'delete-generic-password',
|
|
77
|
+
'-s',
|
|
78
|
+
service,
|
|
79
|
+
'-a',
|
|
80
|
+
profile,
|
|
81
|
+
]);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function secretService(exec, service) {
|
|
86
|
+
return {
|
|
87
|
+
kind: 'secret-service',
|
|
88
|
+
read(profile) {
|
|
89
|
+
const found = exec('secret-tool', [
|
|
90
|
+
'lookup',
|
|
91
|
+
'service',
|
|
92
|
+
service,
|
|
93
|
+
'account',
|
|
94
|
+
profile,
|
|
95
|
+
]);
|
|
96
|
+
const token = found.stdout.trim();
|
|
97
|
+
return found.status === 0 && exports.TOKEN.test(token) ? token : undefined;
|
|
98
|
+
},
|
|
99
|
+
write(profile, token) {
|
|
100
|
+
const stored = exec('secret-tool', [
|
|
101
|
+
'store',
|
|
102
|
+
`--label=Blocklive CLI (${profile})`,
|
|
103
|
+
'service',
|
|
104
|
+
service,
|
|
105
|
+
'account',
|
|
106
|
+
profile,
|
|
107
|
+
], token);
|
|
108
|
+
if (stored.status !== 0)
|
|
109
|
+
throw new errors_1.CliError("Couldn't save the sign-in in the Secret Service.");
|
|
110
|
+
},
|
|
111
|
+
remove(profile) {
|
|
112
|
+
exec('secret-tool', [
|
|
113
|
+
'clear',
|
|
114
|
+
'service',
|
|
115
|
+
service,
|
|
116
|
+
'account',
|
|
117
|
+
profile,
|
|
118
|
+
]);
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
// Every profile's pair in one file, so each change to it is read, changed
|
|
123
|
+
// and written back under one lock (lock.ts, withLockSync): two profiles
|
|
124
|
+
// renewing at once never put back a pair the other already spent. Read
|
|
125
|
+
// without the lock: it is replaced whole, by a rename.
|
|
126
|
+
function file(dir) {
|
|
127
|
+
const path = (0, path_1.join)(dir, 'credentials.json');
|
|
128
|
+
const load = () => {
|
|
129
|
+
try {
|
|
130
|
+
return JSON.parse((0, fs_1.readFileSync)(path, 'utf8'));
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return {};
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const save = (tokens) => {
|
|
137
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true, mode: 0o700 });
|
|
138
|
+
(0, fs_1.chmodSync)(dir, 0o700);
|
|
139
|
+
if (Object.keys(tokens).length === 0) {
|
|
140
|
+
(0, fs_1.rmSync)(path, { force: true });
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const temp = `${path}.${process.pid}.tmp`;
|
|
144
|
+
(0, fs_1.writeFileSync)(temp, `${JSON.stringify(tokens)}\n`, { mode: 0o600 });
|
|
145
|
+
(0, fs_1.renameSync)(temp, path);
|
|
146
|
+
};
|
|
147
|
+
const change = (edit) => {
|
|
148
|
+
(0, lock_1.withLockSync)(`${path}.lock`, () => {
|
|
149
|
+
const tokens = load();
|
|
150
|
+
edit(tokens);
|
|
151
|
+
save(tokens);
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
return {
|
|
155
|
+
kind: 'file',
|
|
156
|
+
read(profile) {
|
|
157
|
+
const token = load()[profile];
|
|
158
|
+
return token && exports.TOKEN.test(token) ? token : undefined;
|
|
159
|
+
},
|
|
160
|
+
write(profile, token) {
|
|
161
|
+
change((tokens) => {
|
|
162
|
+
tokens[profile] = token;
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
remove(profile) {
|
|
166
|
+
change((tokens) => {
|
|
167
|
+
delete tokens[profile];
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function available(exec, command, args) {
|
|
173
|
+
return exec(command, args).status !== null;
|
|
174
|
+
}
|
|
175
|
+
// What a check of the store writes and removes: no profile has this name
|
|
176
|
+
// (config.ts: a profile's starts with a letter or a digit).
|
|
177
|
+
const PROBE = '_probe';
|
|
178
|
+
const PROBE_ID = '00000000-0000-4000-8000-000000000000';
|
|
179
|
+
const PROBE_TOKEN = `bl_cli_test_${PROBE_ID}_${'0'.repeat(64)}:bl_clr_test_${PROBE_ID}_${'0'.repeat(64)}`;
|
|
180
|
+
/**
|
|
181
|
+
* Writes a throwaway value to the store, reads it back and removes it:
|
|
182
|
+
* before a setup code is asked for (login --unattended), so a store that
|
|
183
|
+
* can't keep the sign-in (a locked Keychain, a Secret Service with no
|
|
184
|
+
* keyring running) stops it before the code is spent.
|
|
185
|
+
*/
|
|
186
|
+
function probeStore(store) {
|
|
187
|
+
let kept = false;
|
|
188
|
+
try {
|
|
189
|
+
store.write(PROBE, PROBE_TOKEN);
|
|
190
|
+
kept = store.read(PROBE) === PROBE_TOKEN;
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
kept = false;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
store.remove(PROBE);
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
// Nothing was kept to remove.
|
|
200
|
+
}
|
|
201
|
+
if (kept)
|
|
202
|
+
return;
|
|
203
|
+
const spent = 'so the setup code wasn’t asked for: it would have been spent for nothing.';
|
|
204
|
+
throw new errors_1.CliError(store.kind === 'keychain'
|
|
205
|
+
? `Couldn’t keep a sign-in in the Keychain here, ${spent} Unlock the login Keychain and run it again, or add --insecure-storage to keep it in a file only you can read.`
|
|
206
|
+
: store.kind === 'secret-service'
|
|
207
|
+
? `Couldn’t keep a sign-in in the Secret Service here (is a keyring running?), ${spent} Run \`blocklive login --unattended --insecure-storage\` to keep it in a file only you can read.`
|
|
208
|
+
: `Couldn’t write the CLI’s sign-in file (credentials.json), ${spent} Check that you can write to the CLI’s settings folder.`, errors_1.EXIT.usage);
|
|
209
|
+
}
|
|
210
|
+
exports.probeStore = probeStore;
|
|
211
|
+
/**
|
|
212
|
+
* Where this profile's sign-in is kept: in the file when asked for
|
|
213
|
+
* (--insecure-storage, whatever keychain the computer has) or when the file
|
|
214
|
+
* already holds this profile's, else the macOS Keychain or the Secret
|
|
215
|
+
* Service; elsewhere the file once it exists (until `blocklive logout`
|
|
216
|
+
* empties it).
|
|
217
|
+
*/
|
|
218
|
+
function chooseStore({ platform = process.platform, dir, url, insecureStorage = false, exec = run, profile, }) {
|
|
219
|
+
(0, fs_1.mkdirSync)(dir, { recursive: true, mode: 0o700 });
|
|
220
|
+
const service = credentialScope(dir, url);
|
|
221
|
+
const directory = (0, path_1.join)(dir, service);
|
|
222
|
+
const kept = file(directory);
|
|
223
|
+
if (insecureStorage || (profile !== undefined && kept.read(profile)))
|
|
224
|
+
return kept;
|
|
225
|
+
if (platform === 'darwin')
|
|
226
|
+
return keychain(exec, service);
|
|
227
|
+
if (platform === 'linux' && available(exec, 'secret-tool', ['--version']))
|
|
228
|
+
return secretService(exec, service);
|
|
229
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(directory, 'credentials.json')))
|
|
230
|
+
return kept;
|
|
231
|
+
throw new errors_1.CliError('No keychain here to keep your sign-in. Run `blocklive login ' +
|
|
232
|
+
'--insecure-storage` to keep it in a file only you can read.', errors_1.EXIT.usage);
|
|
233
|
+
}
|
|
234
|
+
exports.chooseStore = chooseStore;
|
|
235
|
+
// What CLI 0.4.0 and earlier kept a sign-in under, whatever its address:
|
|
236
|
+
// the Keychain or Secret Service service `blocklive-cli`, or the file
|
|
237
|
+
// <configDir>/credentials.json (--insecure-storage). Its profile's saved
|
|
238
|
+
// address can't be trusted, so cli.ts sends a pair found here only to
|
|
239
|
+
// Blocklive itself (PRODUCTION_URL), and only to end it.
|
|
240
|
+
exports.LEGACY_SERVICE = 'blocklive-cli';
|
|
241
|
+
function legacyCredential({ platform = process.platform, dir, exec = run, profile, }) {
|
|
242
|
+
const kept = file(dir);
|
|
243
|
+
const stores = [];
|
|
244
|
+
if ((0, fs_1.existsSync)((0, path_1.join)(dir, 'credentials.json')))
|
|
245
|
+
stores.push(kept);
|
|
246
|
+
if (platform === 'darwin')
|
|
247
|
+
stores.push(keychain(exec, exports.LEGACY_SERVICE));
|
|
248
|
+
else if (platform === 'linux' &&
|
|
249
|
+
available(exec, 'secret-tool', ['--version']))
|
|
250
|
+
stores.push(secretService(exec, exports.LEGACY_SERVICE));
|
|
251
|
+
return {
|
|
252
|
+
find() {
|
|
253
|
+
for (const store of stores) {
|
|
254
|
+
const pair = splitTokens(store.read(profile));
|
|
255
|
+
if (pair)
|
|
256
|
+
return { store, pair };
|
|
257
|
+
}
|
|
258
|
+
return undefined;
|
|
259
|
+
},
|
|
260
|
+
remove() {
|
|
261
|
+
for (const store of stores)
|
|
262
|
+
if (store !== kept || (0, fs_1.existsSync)((0, path_1.join)(dir, 'credentials.json')))
|
|
263
|
+
store.remove(profile);
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
exports.legacyCredential = legacyCredential;
|
package/dist/stdin.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// One line from standard input that is a secret (a setup code): at a
|
|
3
|
+
// terminal, asked on stderr with echo off, so it is never on screen nor in
|
|
4
|
+
// the shell's history; otherwise the first line of what is piped in. Never
|
|
5
|
+
// an argument, an option or an environment variable (cli.ts refuses one on
|
|
6
|
+
// the command line). At most 4 KiB is read.
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.readSecretLine = void 0;
|
|
9
|
+
const MOST = 4096;
|
|
10
|
+
function readSecretLine(input, output, prompt) {
|
|
11
|
+
return input.isTTY ? fromTerminal(input, output, prompt) : fromPipe(input);
|
|
12
|
+
}
|
|
13
|
+
exports.readSecretLine = readSecretLine;
|
|
14
|
+
function fromTerminal(input, output, prompt) {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
let typed = '';
|
|
17
|
+
output.write(prompt);
|
|
18
|
+
input.setRawMode(true);
|
|
19
|
+
input.setEncoding('utf8');
|
|
20
|
+
const done = (line) => {
|
|
21
|
+
input.off('data', onData);
|
|
22
|
+
input.setRawMode(false);
|
|
23
|
+
input.pause();
|
|
24
|
+
// The Enter that ended it wasn't echoed either.
|
|
25
|
+
output.write('\n');
|
|
26
|
+
resolve(line);
|
|
27
|
+
};
|
|
28
|
+
const onData = (chunk) => {
|
|
29
|
+
for (const ch of chunk) {
|
|
30
|
+
if (ch === '\r' || ch === '\n')
|
|
31
|
+
return done(typed);
|
|
32
|
+
if (ch === '\u0003') {
|
|
33
|
+
// Ctrl-C: as anywhere else, it stops the command.
|
|
34
|
+
done('');
|
|
35
|
+
process.kill(process.pid, 'SIGINT');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (ch === '\u0004' && !typed)
|
|
39
|
+
return done('');
|
|
40
|
+
if (ch === '\u007f' || ch === '\b')
|
|
41
|
+
typed = typed.slice(0, -1);
|
|
42
|
+
else if (ch >= ' ' && typed.length < MOST)
|
|
43
|
+
typed += ch;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
input.on('data', onData);
|
|
47
|
+
input.resume();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function fromPipe(input) {
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
let read = '';
|
|
53
|
+
const finish = () => {
|
|
54
|
+
input.off('data', onData);
|
|
55
|
+
input.off('end', finish);
|
|
56
|
+
input.off('error', finish);
|
|
57
|
+
input.pause();
|
|
58
|
+
resolve(read.split(/\r?\n/)[0] ?? '');
|
|
59
|
+
};
|
|
60
|
+
const onData = (chunk) => {
|
|
61
|
+
read += chunk.toString();
|
|
62
|
+
if (read.includes('\n') || read.length >= MOST) {
|
|
63
|
+
read = read.slice(0, MOST);
|
|
64
|
+
finish();
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
input.on('data', onData);
|
|
68
|
+
input.on('end', finish);
|
|
69
|
+
input.on('error', finish);
|
|
70
|
+
input.resume();
|
|
71
|
+
});
|
|
72
|
+
}
|
package/dist/style.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// How the CLI talks to the person on stderr: a purple bar beside anything
|
|
3
|
+
// they need to act on, keys and codes set apart. 256-colour codes, which
|
|
4
|
+
// macOS Terminal shows too. Plain text when colour is off: not a terminal,
|
|
5
|
+
// NO_COLOR set (no-color.org), or TERM=dumb; FORCE_COLOR turns it on.
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.styler = exports.STYLE_CODES = exports.colorFor = void 0;
|
|
8
|
+
function colorFor(stream, env) {
|
|
9
|
+
if (env.NO_COLOR)
|
|
10
|
+
return false;
|
|
11
|
+
if (env.FORCE_COLOR && env.FORCE_COLOR !== '0')
|
|
12
|
+
return true;
|
|
13
|
+
return !!stream.isTTY && env.TERM !== 'dumb';
|
|
14
|
+
}
|
|
15
|
+
exports.colorFor = colorFor;
|
|
16
|
+
const CODES = {
|
|
17
|
+
bold: '1',
|
|
18
|
+
dim: '2',
|
|
19
|
+
purple: '38;5;99',
|
|
20
|
+
ok: '38;5;113',
|
|
21
|
+
code: '1;38;5;231;48;5;99',
|
|
22
|
+
key: '1;38;5;234;48;5;254',
|
|
23
|
+
reset: '0',
|
|
24
|
+
};
|
|
25
|
+
/** Every escape sequence the styler writes, and the only ones the CLI lets
|
|
26
|
+
* reach a terminal (safe.ts). */
|
|
27
|
+
exports.STYLE_CODES = new Set(Object.values(CODES).map((code) => `\x1b[${code}m`));
|
|
28
|
+
function styler(color = false) {
|
|
29
|
+
const paint = (code) => (text) => color ? `\x1b[${code}m${text}\x1b[${CODES.reset}m` : text;
|
|
30
|
+
const purple = paint(CODES.purple);
|
|
31
|
+
return {
|
|
32
|
+
bold: paint(CODES.bold),
|
|
33
|
+
dim: paint(CODES.dim),
|
|
34
|
+
purple,
|
|
35
|
+
ok: paint(CODES.ok),
|
|
36
|
+
/** A code to type, white on purple. */
|
|
37
|
+
code: (text) => (color ? paint(CODES.code)(` ${text} `) : text),
|
|
38
|
+
/** A key to press. */
|
|
39
|
+
key: (text) => (color ? paint(CODES.key)(` ${text} `) : text),
|
|
40
|
+
/** Lines behind the purple bar. */
|
|
41
|
+
block: (lines) => lines.map((line) => ` ${purple('┃')} ${line}`.trimEnd()),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
exports.styler = styler;
|