@foliwake/cli 0.11.0 → 0.12.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/CHANGELOG.md +36 -0
- package/README.md +14 -5
- package/dist/commands/connect.d.ts +1 -0
- package/dist/commands/connect.js +3 -1
- package/dist/commands/create.d.ts +1 -0
- package/dist/commands/create.js +13 -5
- package/dist/commands/login.js +2 -3
- package/dist/commands/logout.js +3 -2
- package/dist/create-connect.d.ts +14 -0
- package/dist/create-connect.js +14 -0
- package/dist/remote/credentials.d.ts +19 -3
- package/dist/remote/credentials.js +75 -26
- package/dist/remote/device-login.d.ts +2 -0
- package/dist/remote/device-login.js +2 -0
- package/dist/remote/sign-in-here.d.ts +5 -1
- package/dist/remote/sign-in-here.js +5 -4
- package/dist/remote/sign-in.d.ts +7 -1
- package/dist/remote/sign-in.js +68 -16
- package/dist/sign-in-flags.d.ts +2 -0
- package/dist/sign-in-flags.js +7 -0
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
# @foliwake/cli
|
|
2
2
|
|
|
3
|
+
## 0.12.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5c55ff8: Signing in works without a system keychain, as `foliwake create` should on a server or in a container. `login`,
|
|
8
|
+
`create` and `connect` keep the token in the macOS Keychain, or in the Linux Secret Service only when a probe
|
|
9
|
+
item is stored, read back and cleared before the code is shown; otherwise (also on Windows) in the 0600 file
|
|
10
|
+
`credentials.json`, with one line that says where. There is no question and no failure any more. A keychain that
|
|
11
|
+
refuses the token after the approval gives way to the file; when even the file cannot keep it, the new token is
|
|
12
|
+
revoked on the server and the sign-in fails. `--insecure-storage` is on `create` and `connect` too and uses the
|
|
13
|
+
file even where a keychain works. After a sign-in that failed, `create` prints the exact command that links the
|
|
14
|
+
folder later (`cd <folder> && foliwake connect --create <id> --server <origin>`). The lifetime of a token that
|
|
15
|
+
could not be revoked now says 30 days for an account with installation authority.
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [fb45a71]
|
|
20
|
+
- @foliwake/site-check@0.12.0
|
|
21
|
+
- create-foliwake@0.12.0
|
|
22
|
+
- @foliwake/block-kit@0.12.0
|
|
23
|
+
- @foliwake/blocks-contract@0.12.0
|
|
24
|
+
- @foliwake/core-contract@0.12.0
|
|
25
|
+
- @foliwake/server@0.12.0
|
|
26
|
+
|
|
27
|
+
## 0.11.1
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- Updated dependencies [038e7ee]
|
|
32
|
+
- @foliwake/site-check@0.11.1
|
|
33
|
+
- create-foliwake@0.11.1
|
|
34
|
+
- @foliwake/block-kit@0.11.1
|
|
35
|
+
- @foliwake/blocks-contract@0.11.1
|
|
36
|
+
- @foliwake/core-contract@0.11.1
|
|
37
|
+
- @foliwake/server@0.11.1
|
|
38
|
+
|
|
3
39
|
## 0.11.0
|
|
4
40
|
|
|
5
41
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -20,10 +20,14 @@ Or install it: `npm i -g @foliwake/cli`, then `foliwake …`. Node.js 22.22 or l
|
|
|
20
20
|
|
|
21
21
|
`foliwake login <server>` signs this computer in through the browser with a one-time code (`--no-browser` only
|
|
22
22
|
prints the approval address; without a server it asks for one in a terminal). The token is kept in the macOS
|
|
23
|
-
Keychain or the Linux Secret Service (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
Keychain, or in the Linux Secret Service when one is running (the CLI checks that it stores before it shows the
|
|
24
|
+
code). Without a system keychain (a server, a container, Windows) the token is kept in
|
|
25
|
+
`~/.config/foliwake/credentials.json`, a 0600 file (readable only by you, except on Windows), and the CLI says so.
|
|
26
|
+
`--insecure-storage` (on `login`, `create` and `connect`) uses that file even where a keychain works. A keychain
|
|
27
|
+
that refuses the token after the approval gives way to the file; a token that cannot be kept at all is revoked.
|
|
28
|
+
The list of servers, without secrets, is `~/.config/foliwake/servers.json` (`FOLIWAKE_CONFIG_DIR` moves both).
|
|
29
|
+
These are the same places the engine's own `foliwake` uses, so a sign-in made with either works for both.
|
|
30
|
+
`foliwake logout` revokes the token on the server and forgets it here;
|
|
27
31
|
`foliwake whoami` shows the server, your account and what it may do.
|
|
28
32
|
|
|
29
33
|
You can be signed in to several servers. The first sign-in becomes the default server of commands that name
|
|
@@ -297,6 +301,8 @@ Link this project folder to a site on your Foliwake server: a new one or an exis
|
|
|
297
301
|
|
|
298
302
|
- `--create <value>`: create this new site and link the folder to it
|
|
299
303
|
- `--force`: replace the link of a folder that is linked already
|
|
304
|
+
- `--insecure-storage`: keep the token of a sign-in in a 0600 file even when a system keychain works (without one, the
|
|
305
|
+
file is used anyway)
|
|
300
306
|
- `--interactive` / --no-interactive: ask for what the flags leave open (default: only when stdin and stdout are a
|
|
301
307
|
terminal)
|
|
302
308
|
- `--link <value>`: link the folder to this existing site
|
|
@@ -314,6 +320,8 @@ Create a new site frontend: blocks, styles, local pages, checks and workflows
|
|
|
314
320
|
- `--create-site`: at the end, create the site (id: --site, else the folder name) on your server, as `site create`
|
|
315
321
|
- `--git` / --no-git: create a Git repository (branch main) and commit the project with your Git identity (default:
|
|
316
322
|
yes); skipped inside an existing repository
|
|
323
|
+
- `--insecure-storage`: keep the token of a sign-in in a 0600 file even when a system keychain works (without one, the
|
|
324
|
+
file is used anyway)
|
|
317
325
|
- `--install` / --no-install: install Storybook's npm packages; default: asked in a terminal after Storybook, else no
|
|
318
326
|
- `--interactive` / --no-interactive: ask for missing values (default: only when stdin and stdout are a terminal)
|
|
319
327
|
- `--keep-existing`: the folder is not empty: add the project next to its files (refused if one would be replaced)
|
|
@@ -526,7 +534,8 @@ Sign this computer in to a Foliwake server through the browser (device code)
|
|
|
526
534
|
|
|
527
535
|
- `--browser` / --no-browser: open the approval page in the browser (--no-browser only prints its address)
|
|
528
536
|
- `--default`: make this server the default of other commands (the first sign-in always is)
|
|
529
|
-
- `--insecure-storage`: keep the token in a 0600 file when
|
|
537
|
+
- `--insecure-storage`: keep the token of a sign-in in a 0600 file even when a system keychain works (without one, the
|
|
538
|
+
file is used anyway)
|
|
530
539
|
|
|
531
540
|
### `foliwake logout [server]`
|
|
532
541
|
|
|
@@ -11,6 +11,7 @@ export default class Connect extends Command {
|
|
|
11
11
|
org: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
12
12
|
repository: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
13
13
|
force: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
14
|
+
'insecure-storage': import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
14
15
|
interactive: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
15
16
|
};
|
|
16
17
|
run(): Promise<unknown>;
|
package/dist/commands/connect.js
CHANGED
|
@@ -5,6 +5,7 @@ import { execFileSync } from 'node:child_process';
|
|
|
5
5
|
import { Command, Flags } from '@oclif/core';
|
|
6
6
|
import { ServerAddressError, serverOrigin } from '../remote/servers.js';
|
|
7
7
|
import { COMMAND } from '../settings.js';
|
|
8
|
+
import { insecureStorageFlag } from '../sign-in-flags.js';
|
|
8
9
|
/** Whether the folder's Git repository has a commit (a new site gets its first code from it). */
|
|
9
10
|
function hasCommit(cwd) {
|
|
10
11
|
try {
|
|
@@ -45,6 +46,7 @@ export default class Connect extends Command {
|
|
|
45
46
|
'repository yet)',
|
|
46
47
|
}),
|
|
47
48
|
force: Flags.boolean({ description: 'replace the link of a folder that is linked already' }),
|
|
49
|
+
'insecure-storage': insecureStorageFlag(),
|
|
48
50
|
interactive: Flags.boolean({
|
|
49
51
|
allowNo: true,
|
|
50
52
|
description: 'ask for what the flags leave open (default: only when stdin and stdout are a terminal)',
|
|
@@ -95,7 +97,7 @@ export default class Connect extends Command {
|
|
|
95
97
|
org: flags.org,
|
|
96
98
|
repository: flags.repository,
|
|
97
99
|
prompts: { input: process.stdin, output: process.stdout },
|
|
98
|
-
signIn: signInHere,
|
|
100
|
+
signIn: (origin) => signInHere(origin, { insecureStorage: flags['insecure-storage'] }),
|
|
99
101
|
onLinked,
|
|
100
102
|
};
|
|
101
103
|
return connectStep(step, io);
|
|
@@ -22,6 +22,7 @@ export default class Create extends Command {
|
|
|
22
22
|
org: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
|
|
23
23
|
connect: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
24
24
|
interactive: import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
25
|
+
'insecure-storage': import("@oclif/core/interfaces").BooleanFlag<boolean>;
|
|
25
26
|
help: import("@oclif/core/interfaces").BooleanFlag<void>;
|
|
26
27
|
};
|
|
27
28
|
run(): Promise<void>;
|
package/dist/commands/create.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// (`site create` or `site link` in the new folder). Reads no project configuration; only its last step reads
|
|
3
3
|
// this computer's sign-ins and may sign in, as `login` does.
|
|
4
4
|
import { Args, Command, Flags } from '@oclif/core';
|
|
5
|
+
import { insecureStorageFlag } from '../sign-in-flags.js';
|
|
5
6
|
export default class Create extends Command {
|
|
6
7
|
static summary = 'Create a new site frontend: blocks, styles, local pages, checks and workflows';
|
|
7
8
|
static description = `Runs create-foliwake (\`npm create foliwake\`): asks in a terminal only for what the flags ` +
|
|
@@ -84,6 +85,7 @@ At the end it can create or link the site on your Foliwake server (--create-site
|
|
|
84
85
|
allowNo: true,
|
|
85
86
|
description: 'ask for missing values (default: only when stdin and stdout are a terminal)',
|
|
86
87
|
}),
|
|
88
|
+
'insecure-storage': insecureStorageFlag(),
|
|
87
89
|
help: Flags.help({ char: 'h' }),
|
|
88
90
|
};
|
|
89
91
|
async run() {
|
|
@@ -147,14 +149,16 @@ At the end it can create or link the site on your Foliwake server (--create-site
|
|
|
147
149
|
}
|
|
148
150
|
}
|
|
149
151
|
async connect(result, flags, server) {
|
|
150
|
-
const { COMMAND } = await import('../settings.js');
|
|
151
152
|
const answers = result.answers;
|
|
152
153
|
const interactive = flags.interactive ?? (process.stdin.isTTY === true && process.stdout.isTTY === true);
|
|
153
154
|
const out = (stream) => (text = '') => void stream.write(`${text}\n`);
|
|
154
155
|
const io = { out: out(process.stdout), err: out(process.stderr) };
|
|
156
|
+
const { connectStep, laterConnectCommand } = await import('../create-connect.js');
|
|
157
|
+
const { signInHere } = await import('../remote/sign-in-here.js');
|
|
158
|
+
let known = server ?? null;
|
|
155
159
|
const connected = flags.connect === false
|
|
156
160
|
? null
|
|
157
|
-
: await
|
|
161
|
+
: await connectStep({
|
|
158
162
|
target: answers.target,
|
|
159
163
|
siteId: answers.siteId,
|
|
160
164
|
committed: result.committed,
|
|
@@ -165,11 +169,15 @@ At the end it can create or link the site on your Foliwake server (--create-site
|
|
|
165
169
|
relink: flags.relink,
|
|
166
170
|
org: flags.org,
|
|
167
171
|
prompts: { input: process.stdin, output: process.stdout },
|
|
168
|
-
signIn: (
|
|
172
|
+
signIn: (origin) => signInHere(origin, { insecureStorage: flags['insecure-storage'] }),
|
|
173
|
+
onServer: (origin) => (known = origin),
|
|
169
174
|
}, io);
|
|
170
175
|
if (connected === null) {
|
|
171
|
-
|
|
172
|
-
|
|
176
|
+
const { localSiteId } = await import('create-foliwake');
|
|
177
|
+
const site = flags['link-site'] ?? localSiteId(answers.siteId, answers.target);
|
|
178
|
+
const later = { cwd: process.cwd(), target: answers.target, site, server: known };
|
|
179
|
+
io.out('To publish it, link the folder to a site on your Foliwake server (it signs in when needed):');
|
|
180
|
+
io.out(` ${laterConnectCommand({ ...later, link: flags['link-site'] !== undefined })}`);
|
|
173
181
|
}
|
|
174
182
|
return connected === null || connected === 0 ? result.status : connected;
|
|
175
183
|
}
|
package/dist/commands/login.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Args, Command, Flags } from '@oclif/core';
|
|
2
2
|
import { CLOUD, CLOUD_NOTE, ServerAddressError, cloudConfigured, nameOrigin, readProfiles, serverDisplay, serverOrigin, } from '../remote/servers.js';
|
|
3
3
|
import { COMMAND } from '../settings.js';
|
|
4
|
+
import { insecureStorageFlag } from '../sign-in-flags.js';
|
|
4
5
|
const NO_SERVER = `Name the server to sign in to: \`${COMMAND} login <origin or host>\`, e.g. https://foliwake.example.`;
|
|
5
6
|
/** Asks for the server's address in a terminal; null when cancelled or empty. */
|
|
6
7
|
async function askServer() {
|
|
@@ -30,9 +31,7 @@ export default class Login extends Command {
|
|
|
30
31
|
default: Flags.boolean({
|
|
31
32
|
description: 'make this server the default of other commands (the first sign-in always is)',
|
|
32
33
|
}),
|
|
33
|
-
'insecure-storage':
|
|
34
|
-
description: 'keep the token in a 0600 file when there is no system keychain',
|
|
35
|
-
}),
|
|
34
|
+
'insecure-storage': insecureStorageFlag(),
|
|
36
35
|
};
|
|
37
36
|
async run() {
|
|
38
37
|
const { args, flags } = await this.parse(Login);
|
package/dist/commands/logout.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Args, Command, Flags } from '@oclif/core';
|
|
2
|
+
import { TOKEN_LIFETIME } from '../remote/device-login.js';
|
|
2
3
|
import { ServerAddressError, defaultOrigin, serverDisplay, serverOrigin } from '../remote/servers.js';
|
|
3
4
|
import { COMMAND } from '../settings.js';
|
|
4
|
-
const NOT_REVOKED_HINT =
|
|
5
|
+
const NOT_REVOKED_HINT = `it then stays valid until ${TOKEN_LIFETIME}`;
|
|
5
6
|
export default class Logout extends Command {
|
|
6
7
|
static description = 'Revoke this computer’s sign-in to a Foliwake server and forget its token';
|
|
7
8
|
static examples = [
|
|
@@ -40,7 +41,7 @@ export default class Logout extends Command {
|
|
|
40
41
|
if (!revoked && signIn.token !== null && !flags['forget-only']) {
|
|
41
42
|
this.error(`${issuer ?? origin} could not revoke the token, so it is kept here to try again. Run ` +
|
|
42
43
|
`\`${COMMAND} logout\` again later, or pass --forget-only to forget it anyway ` +
|
|
43
|
-
|
|
44
|
+
`(it then stays valid there until ${TOKEN_LIFETIME}).`, { exit: 1 });
|
|
44
45
|
}
|
|
45
46
|
forgetSignIn(origin);
|
|
46
47
|
if (!revoked)
|
package/dist/create-connect.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export interface ConnectStep {
|
|
|
18
18
|
repository?: string | undefined;
|
|
19
19
|
prompts: PromptStreams;
|
|
20
20
|
signIn: (origin: string) => Promise<boolean>;
|
|
21
|
+
/** In a terminal: told the server once it is known (--server, FOLIWAKE_SERVER or the answer). */
|
|
22
|
+
onServer?: (origin: string) => void;
|
|
21
23
|
/** Told the server and site the folder was linked to (`connect --json`). */
|
|
22
24
|
onLinked?: (linked: {
|
|
23
25
|
server: string | null;
|
|
@@ -27,3 +29,15 @@ export interface ConnectStep {
|
|
|
27
29
|
}
|
|
28
30
|
/** The connect step; its exit status, or null when nothing ran (not now, a failed sign-in, no answer). */
|
|
29
31
|
export declare function connectStep(step: ConnectStep, io: ConnectIO): Promise<number | null>;
|
|
32
|
+
export interface LaterConnect {
|
|
33
|
+
/** Where the command runs, and the new project folder. */
|
|
34
|
+
cwd: string;
|
|
35
|
+
target: string;
|
|
36
|
+
/** The site to create, or the existing one to link (`--link-site`). */
|
|
37
|
+
site: string;
|
|
38
|
+
link: boolean;
|
|
39
|
+
/** The server, when it is known. */
|
|
40
|
+
server: string | null;
|
|
41
|
+
}
|
|
42
|
+
/** The exact command that links the new folder later: `cd <folder> && foliwake connect --create <id> …`. */
|
|
43
|
+
export declare function laterConnectCommand({ cwd, target, site, link, server }: LaterConnect): string;
|
package/dist/create-connect.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// sign-in when it is needed (as `login`), then a new site (offered to admins), an existing one, or not now.
|
|
4
4
|
// Without a terminal: only --create-site or --link-site, on the server `site create` would choose. Runs the
|
|
5
5
|
// same code as `site create` and `site link` in the new folder.
|
|
6
|
+
import path from 'node:path';
|
|
6
7
|
import { localSiteId } from 'create-foliwake';
|
|
7
8
|
import { connectChoices, createSite, knownServers, linkSite } from './connect.js';
|
|
8
9
|
import { terminalQuestions } from './connect-prompts.js';
|
|
@@ -65,6 +66,7 @@ export async function connectStep(step, io) {
|
|
|
65
66
|
const server = step.server ?? presetServer() ?? (await askServer(step.prompts, known, (typed) => serverOrigin(typed)));
|
|
66
67
|
if (server === null)
|
|
67
68
|
return null;
|
|
69
|
+
step.onServer?.(server);
|
|
68
70
|
if (!known.some((entry) => entry.origin === server && entry.signedIn)) {
|
|
69
71
|
io.out(`\nSigning in to ${server} (as \`${COMMAND} login ${server}\`).`);
|
|
70
72
|
if (!(await step.signIn(server)))
|
|
@@ -91,3 +93,15 @@ async function linkWith(step, io, site, server) {
|
|
|
91
93
|
step.onLinked?.({ server: linked.server, site, created: false });
|
|
92
94
|
return linked.exit;
|
|
93
95
|
}
|
|
96
|
+
/** A word for a POSIX shell: as it is when that is safe, else in single quotes. */
|
|
97
|
+
function shellWord(word) {
|
|
98
|
+
return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
|
|
99
|
+
}
|
|
100
|
+
/** The exact command that links the new folder later: `cd <folder> && foliwake connect --create <id> …`. */
|
|
101
|
+
export function laterConnectCommand({ cwd, target, site, link, server }) {
|
|
102
|
+
const folder = path.relative(cwd, target);
|
|
103
|
+
const cd = folder ? `cd ${shellWord(folder)} && ` : '';
|
|
104
|
+
const what = `${link ? '--link' : '--create'} ${shellWord(site)}`;
|
|
105
|
+
const on = server ? ` --server ${shellWord(server)}` : '';
|
|
106
|
+
return `${cd}${COMMAND} connect ${what}${on}`;
|
|
107
|
+
}
|
|
@@ -7,9 +7,25 @@ export interface CredentialStore {
|
|
|
7
7
|
}
|
|
8
8
|
export declare class CredentialStoreError extends Error {
|
|
9
9
|
}
|
|
10
|
+
/** The 0600 file of the file store. */
|
|
11
|
+
export declare function credentialsFile(env?: NodeJS.ProcessEnv): string;
|
|
10
12
|
/** The store of this kind, as a profile names it. */
|
|
11
13
|
export declare function credentialStore(kind: CredentialStoreKind, env?: NodeJS.ProcessEnv): CredentialStore;
|
|
12
|
-
/** The store a new sign-in uses
|
|
13
|
-
export
|
|
14
|
+
/** The store a new sign-in uses, and the line that says so when it is the file. */
|
|
15
|
+
export interface StoreChoice {
|
|
16
|
+
store: CredentialStore;
|
|
17
|
+
notice: string | null;
|
|
18
|
+
}
|
|
19
|
+
export interface StoreOptions {
|
|
20
|
+
/** Use the file even when a system keychain works (`--insecure-storage`). */
|
|
14
21
|
insecureStorage: boolean;
|
|
15
|
-
|
|
22
|
+
/** For tests: the platform to choose for (default: this one). */
|
|
23
|
+
platform?: NodeJS.Platform;
|
|
24
|
+
}
|
|
25
|
+
/** "kept in <file> (readable only by you)": 0600 means that on POSIX systems, not on Windows. */
|
|
26
|
+
export declare function fileWhere(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform): string;
|
|
27
|
+
/**
|
|
28
|
+
* The store a new sign-in uses: the OS keychain when it works, else the 0600 file with a notice; the file
|
|
29
|
+
* also when `insecureStorage` asks for it. Never throws: probing a keychain that fails means the file.
|
|
30
|
+
*/
|
|
31
|
+
export declare function storeForSignIn(options: StoreOptions, env?: NodeJS.ProcessEnv): StoreChoice;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// Where this computer keeps the CLI token of each server: the OS keychain (macOS Keychain, Linux Secret
|
|
2
|
-
// Service),
|
|
3
|
-
// so either CLI reads a sign-in the other made. `FOLIWAKE_TOKEN` (CI) is read by the
|
|
1
|
+
// Where this computer keeps the CLI token of each server: the OS keychain (macOS Keychain, a Linux Secret
|
|
2
|
+
// Service that stores), else a 0600 file (also when asked for: --insecure-storage). The service name and account
|
|
3
|
+
// keys are the engine CLI's, so either CLI reads a sign-in the other made. `FOLIWAKE_TOKEN` (CI) is read by the
|
|
4
|
+
// caller and stores nothing.
|
|
4
5
|
import { spawnSync } from 'node:child_process';
|
|
5
6
|
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
6
7
|
import path from 'node:path';
|
|
@@ -8,12 +9,15 @@ import { configDir, setting } from '../settings.js';
|
|
|
8
9
|
export class CredentialStoreError extends Error {
|
|
9
10
|
}
|
|
10
11
|
const serviceName = (env) => setting('KEYCHAIN_SERVICE', env) ?? 'foliwake';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
/** Runs a keychain's tool with this environment (its PATH finds `secret-tool`). */
|
|
13
|
+
function runner(env) {
|
|
14
|
+
return (command, args, input) => {
|
|
15
|
+
const options = { input, env: { ...process.env, ...env }, encoding: 'utf8', timeout: 15_000 };
|
|
16
|
+
const result = spawnSync(command, args, { ...options, stdio: 'pipe' });
|
|
17
|
+
if (result.error)
|
|
18
|
+
throw new CredentialStoreError(`${command} could not run: ${result.error.message}`);
|
|
19
|
+
return { status: result.status, stdout: result.stdout };
|
|
20
|
+
};
|
|
17
21
|
}
|
|
18
22
|
/** Keys and tokens hold no quotes (a key is an origin or "cloud"; a token is base64url). */
|
|
19
23
|
function quoted(value) {
|
|
@@ -22,7 +26,7 @@ function quoted(value) {
|
|
|
22
26
|
return `"${value}"`;
|
|
23
27
|
}
|
|
24
28
|
// Values go through `security -i` on stdin, so the token is never in a process's arguments.
|
|
25
|
-
function keychain(service) {
|
|
29
|
+
function keychain(service, run) {
|
|
26
30
|
const security = '/usr/bin/security';
|
|
27
31
|
return {
|
|
28
32
|
kind: 'keychain',
|
|
@@ -41,7 +45,9 @@ function keychain(service) {
|
|
|
41
45
|
},
|
|
42
46
|
};
|
|
43
47
|
}
|
|
44
|
-
|
|
48
|
+
const SECRET_SERVICE_REFUSED = 'The Secret Service refused to store the token: it needs a running Secret Service (such as gnome-keyring), ' +
|
|
49
|
+
'not only libsecret-tools.';
|
|
50
|
+
function secretService(service, run) {
|
|
45
51
|
return {
|
|
46
52
|
kind: 'secret-service',
|
|
47
53
|
get(key) {
|
|
@@ -52,7 +58,7 @@ function secretService(service) {
|
|
|
52
58
|
const args = ['store', `--label=Foliwake ${key}`, 'service', service, 'account', key];
|
|
53
59
|
const result = run('secret-tool', args, token);
|
|
54
60
|
if (result.status !== 0)
|
|
55
|
-
throw new CredentialStoreError(
|
|
61
|
+
throw new CredentialStoreError(SECRET_SERVICE_REFUSED);
|
|
56
62
|
},
|
|
57
63
|
delete(key) {
|
|
58
64
|
run('secret-tool', ['clear', 'service', service, 'account', key]);
|
|
@@ -75,8 +81,12 @@ function writeFileStore(where, all) {
|
|
|
75
81
|
writeFileSync(temp, `${JSON.stringify(all, null, 2)}\n`, { mode: 0o600 });
|
|
76
82
|
renameSync(temp, where);
|
|
77
83
|
}
|
|
84
|
+
/** The 0600 file of the file store. */
|
|
85
|
+
export function credentialsFile(env = process.env) {
|
|
86
|
+
return path.join(configDir(env), 'credentials.json');
|
|
87
|
+
}
|
|
78
88
|
function file(env) {
|
|
79
|
-
const where =
|
|
89
|
+
const where = credentialsFile(env);
|
|
80
90
|
return {
|
|
81
91
|
kind: 'file',
|
|
82
92
|
get: (key) => readFileStore(where)[key] ?? null,
|
|
@@ -90,23 +100,62 @@ function file(env) {
|
|
|
90
100
|
},
|
|
91
101
|
};
|
|
92
102
|
}
|
|
93
|
-
const hasSecretTool = () => spawnSync('secret-tool', ['--version'], { stdio: 'ignore' }).error === undefined;
|
|
94
103
|
/** The store of this kind, as a profile names it. */
|
|
95
104
|
export function credentialStore(kind, env = process.env) {
|
|
96
105
|
if (kind === 'keychain')
|
|
97
|
-
return keychain(serviceName(env));
|
|
106
|
+
return keychain(serviceName(env), runner(env));
|
|
98
107
|
if (kind === 'secret-service')
|
|
99
|
-
return secretService(serviceName(env));
|
|
108
|
+
return secretService(serviceName(env), runner(env));
|
|
100
109
|
return file(env);
|
|
101
110
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
111
|
+
const PROBE_ACCOUNT = 'foliwake-probe';
|
|
112
|
+
const PROBE_VALUE = 'probe';
|
|
113
|
+
/** Whether this Secret Service stores: a dummy item is stored, read back and cleared. */
|
|
114
|
+
function secretServiceStores(store) {
|
|
115
|
+
try {
|
|
116
|
+
store.set(PROBE_ACCOUNT, PROBE_VALUE);
|
|
117
|
+
return store.get(PROBE_ACCOUNT) === PROBE_VALUE;
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
clearProbe(store);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function clearProbe(store) {
|
|
127
|
+
try {
|
|
128
|
+
store.delete(PROBE_ACCOUNT);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// No tool to clear with: nothing was stored either.
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** The OS keychain of this computer, or null: macOS Keychain; a Linux Secret Service only when it stores. */
|
|
135
|
+
function systemKeychain(platform, env) {
|
|
136
|
+
if (platform === 'darwin')
|
|
137
|
+
return credentialStore('keychain', env);
|
|
138
|
+
if (platform !== 'linux')
|
|
139
|
+
return null;
|
|
140
|
+
const store = credentialStore('secret-service', env);
|
|
141
|
+
return secretServiceStores(store) ? store : null;
|
|
142
|
+
}
|
|
143
|
+
/** "kept in <file> (readable only by you)": 0600 means that on POSIX systems, not on Windows. */
|
|
144
|
+
export function fileWhere(env = process.env, platform = process.platform) {
|
|
145
|
+
const only = platform === 'win32' ? '' : ' (readable only by you)';
|
|
146
|
+
return `kept in ${credentialsFile(env)}${only}`;
|
|
147
|
+
}
|
|
148
|
+
function noKeychainNotice(env, platform) {
|
|
149
|
+
return `No system keychain here: the token is ${fileWhere(env, platform)}.`;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The store a new sign-in uses: the OS keychain when it works, else the 0600 file with a notice; the file
|
|
153
|
+
* also when `insecureStorage` asks for it. Never throws: probing a keychain that fails means the file.
|
|
154
|
+
*/
|
|
155
|
+
export function storeForSignIn(options, env = process.env) {
|
|
156
|
+
const platform = options.platform ?? process.platform;
|
|
157
|
+
if (options.insecureStorage)
|
|
158
|
+
return { store: file(env), notice: `The token is ${fileWhere(env, platform)}.` };
|
|
159
|
+
const system = systemKeychain(platform, env);
|
|
160
|
+
return system ? { store: system, notice: null } : { store: file(env), notice: noKeychainNotice(env, platform) };
|
|
112
161
|
}
|
|
@@ -14,6 +14,8 @@ export declare class SignInError extends Error {
|
|
|
14
14
|
export declare class SignInCancelled extends SignInError {
|
|
15
15
|
constructor();
|
|
16
16
|
}
|
|
17
|
+
/** How long a CLI token the server could not revoke stays valid (engine: src/http/key-classes.ts). */
|
|
18
|
+
export declare const TOKEN_LIFETIME = "90 days after its last use (30 for an account with installation authority)";
|
|
17
19
|
export declare function startDeviceSignIn(origin: string): Promise<DeviceStart>;
|
|
18
20
|
/** A sleep that ends early with SignInCancelled when `signal` aborts. */
|
|
19
21
|
export declare function abortableSleep(signal: AbortSignal): (ms: number) => Promise<void>;
|
|
@@ -15,6 +15,8 @@ export class SignInCancelled extends SignInError {
|
|
|
15
15
|
super('SIGN_IN_CANCELLED', 'The sign-in was cancelled.');
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
+
/** How long a CLI token the server could not revoke stays valid (engine: src/http/key-classes.ts). */
|
|
19
|
+
export const TOKEN_LIFETIME = '90 days after its last use (30 for an account with installation authority)';
|
|
18
20
|
/** One request is bounded: a server that hangs is a transient failure, not a stuck CLI. */
|
|
19
21
|
const REQUEST_TIMEOUT_MS = 15_000;
|
|
20
22
|
/** Backoff on transient failures while polling (RFC 8628 §3.5): doubled, at most this. */
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
export interface SignInHereOptions {
|
|
2
|
+
/** Keep the token in the 0600 file even when a system keychain works (`--insecure-storage`). */
|
|
3
|
+
insecureStorage: boolean;
|
|
4
|
+
}
|
|
1
5
|
/**
|
|
2
6
|
* `foliwake login <origin>` inline, for `create` and `connect`: Ctrl+C stops only the wait, a failure is printed;
|
|
3
7
|
* false in both cases.
|
|
4
8
|
*/
|
|
5
|
-
export declare function signInHere(origin: string): Promise<boolean>;
|
|
9
|
+
export declare function signInHere(origin: string, { insecureStorage }: SignInHereOptions): Promise<boolean>;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/** An error's message without its final full stop, to end a sentence of ours. */
|
|
2
|
+
const withoutStop = (message) => message.replace(/\.+$/, '');
|
|
2
3
|
/**
|
|
3
4
|
* `foliwake login <origin>` inline, for `create` and `connect`: Ctrl+C stops only the wait, a failure is printed;
|
|
4
5
|
* false in both cases.
|
|
5
6
|
*/
|
|
6
|
-
export async function signInHere(origin) {
|
|
7
|
+
export async function signInHere(origin, { insecureStorage }) {
|
|
7
8
|
const { deviceSignIn } = await import('./sign-in.js');
|
|
8
9
|
const controller = new AbortController();
|
|
9
10
|
const onInterrupt = () => controller.abort();
|
|
@@ -12,7 +13,7 @@ export async function signInHere(origin) {
|
|
|
12
13
|
const signed = await deviceSignIn(origin, {
|
|
13
14
|
browser: true,
|
|
14
15
|
tty: process.stdout.isTTY === true,
|
|
15
|
-
insecureStorage
|
|
16
|
+
insecureStorage,
|
|
16
17
|
log: (text) => void process.stderr.write(text),
|
|
17
18
|
signal: controller.signal,
|
|
18
19
|
});
|
|
@@ -23,7 +24,7 @@ export async function signInHere(origin) {
|
|
|
23
24
|
catch (error) {
|
|
24
25
|
const { SignInCancelled } = await import('./device-login.js');
|
|
25
26
|
const cancelled = error instanceof SignInCancelled;
|
|
26
|
-
const why = cancelled ? 'was cancelled' : `failed: ${error.message}`;
|
|
27
|
+
const why = cancelled ? 'was cancelled' : `failed: ${withoutStop(error.message)}`;
|
|
27
28
|
process.stderr.write(`The sign-in to ${origin} ${why}.\n`);
|
|
28
29
|
return false;
|
|
29
30
|
}
|
package/dist/remote/sign-in.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface SignInOptions {
|
|
|
4
4
|
/** Open the approval page in the browser (only when `tty`). */
|
|
5
5
|
browser: boolean;
|
|
6
6
|
tty: boolean;
|
|
7
|
+
/** Keep the token in the 0600 file even when a system keychain works. */
|
|
7
8
|
insecureStorage: boolean;
|
|
8
9
|
/** Where the code, the address and warnings go. */
|
|
9
10
|
log: (text: string) => void;
|
|
@@ -12,6 +13,8 @@ export interface SignInOptions {
|
|
|
12
13
|
/** Aborting stops the wait (SignInCancelled). */
|
|
13
14
|
signal?: AbortSignal;
|
|
14
15
|
env?: NodeJS.ProcessEnv;
|
|
16
|
+
/** For tests: the platform whose keychain is used (default: this one). */
|
|
17
|
+
platform?: NodeJS.Platform;
|
|
15
18
|
}
|
|
16
19
|
export interface SignedIn {
|
|
17
20
|
origin: string;
|
|
@@ -19,7 +22,10 @@ export interface SignedIn {
|
|
|
19
22
|
email: string | null;
|
|
20
23
|
store: CredentialStore['kind'];
|
|
21
24
|
}
|
|
22
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Signs this computer in to `origin`; throws SignInError (SignInCancelled too) or CredentialStoreError.
|
|
27
|
+
* The store is chosen (and a Linux keychain probed) before the code is shown.
|
|
28
|
+
*/
|
|
23
29
|
export declare function deviceSignIn(origin: string, options: SignInOptions): Promise<SignedIn>;
|
|
24
30
|
/** The cloud is not available yet: say so instead of only "cannot be reached". */
|
|
25
31
|
export declare function cloudHint(error: unknown, origin: string, env?: NodeJS.ProcessEnv): unknown;
|
package/dist/remote/sign-in.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
// The device-code sign-in of `foliwake login`, shared with `
|
|
1
|
+
// The device-code sign-in of `foliwake login`, shared with `create` and `connect` when they need one: the code
|
|
2
2
|
// and the approval page on stderr, the browser opened in a terminal, the token kept in the credential store
|
|
3
|
-
// and the server made the default. It never prints the token.
|
|
3
|
+
// and the server made the default. It never prints the token, and never leaves an issued token it did not keep.
|
|
4
4
|
import { COMMAND, openBrowser } from '../settings.js';
|
|
5
|
-
import { storeForSignIn } from './credentials.js';
|
|
6
|
-
import { SignInError, abortableSleep, revokeToken, startDeviceSignIn, waitForSignIn } from './device-login.js';
|
|
5
|
+
import { CredentialStoreError, credentialStore, fileWhere, storeForSignIn, } from './credentials.js';
|
|
6
|
+
import { SignInError, TOKEN_LIFETIME, abortableSleep, revokeToken, startDeviceSignIn, waitForSignIn, } from './device-login.js';
|
|
7
7
|
import { createRemoteCaller } from './remote-caller.js';
|
|
8
8
|
import { CLOUD_NOTE, cloudOrigin } from './servers.js';
|
|
9
9
|
import { rehomeMovedSignIn, saveSignIn } from './session.js';
|
|
@@ -22,12 +22,69 @@ async function settleMovedSignIn(origin, log, env) {
|
|
|
22
22
|
log(revoked
|
|
23
23
|
? `Your earlier sign-in, made at ${issuer}, was revoked there and replaced.\n`
|
|
24
24
|
: `Warning: your earlier sign-in${where} could not be revoked and is replaced here; ` +
|
|
25
|
-
|
|
25
|
+
`it stays valid there until ${TOKEN_LIFETIME}.\n`);
|
|
26
26
|
}
|
|
27
|
-
/**
|
|
27
|
+
/** Saves into the 0600 file; a failure there is a CredentialStoreError naming the file. */
|
|
28
|
+
function saveInFile(signIn, env) {
|
|
29
|
+
const store = credentialStore('file', env);
|
|
30
|
+
try {
|
|
31
|
+
return { superseded: saveSignIn({ ...signIn, store }, env), store };
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw new CredentialStoreError(`The token could not be saved in the file: ${error.message}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Saves the sign-in; a keychain that refuses it after all (after the approval) gives way to the file. */
|
|
38
|
+
function saveOrFallBack(signIn, log, env, platform) {
|
|
39
|
+
if (signIn.store.kind === 'file')
|
|
40
|
+
return saveInFile(signIn, env);
|
|
41
|
+
try {
|
|
42
|
+
return { superseded: saveSignIn(signIn, env), store: signIn.store };
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
const saved = saveInFile(signIn, env);
|
|
46
|
+
const refused = error.message.replace(/\.$/, '');
|
|
47
|
+
log(`${refused}. The token is ${fileWhere(env, platform)} instead.\n`);
|
|
48
|
+
return saved;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** The issued token could not be kept here: revoke it rather than leave it valid and unknown. */
|
|
52
|
+
async function revokeUnkept(origin, token, log) {
|
|
53
|
+
const revoked = await revokeToken(origin, token);
|
|
54
|
+
log(revoked
|
|
55
|
+
? `The sign-in was not kept on this computer; ${origin} revoked its new token.\n`
|
|
56
|
+
: `The sign-in was not kept on this computer and ${origin} could not revoke its new token; ` +
|
|
57
|
+
`it expires ${TOKEN_LIFETIME}.\n`);
|
|
58
|
+
}
|
|
59
|
+
async function keepIssued(issued, store, options) {
|
|
60
|
+
const { origin, token, email } = issued;
|
|
61
|
+
const { log, makeDefault = false, env = process.env, platform } = options;
|
|
62
|
+
try {
|
|
63
|
+
const grant = await createRemoteCaller({ origin, token }).whoami();
|
|
64
|
+
await settleMovedSignIn(origin, log, env);
|
|
65
|
+
const saved = saveOrFallBack({ origin, token, grant, store, email, makeDefault }, log, env, platform);
|
|
66
|
+
return { ...saved, grant };
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
await revokeUnkept(origin, token, log);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** The earlier sign-in of this computer would stay valid on the server until it expired. */
|
|
74
|
+
async function revokeSuperseded(origin, superseded, log) {
|
|
75
|
+
if (!superseded || (await revokeToken(origin, superseded)))
|
|
76
|
+
return;
|
|
77
|
+
log(`Warning: ${origin} could not revoke this computer's earlier token; it expires ${TOKEN_LIFETIME}.\n`);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Signs this computer in to `origin`; throws SignInError (SignInCancelled too) or CredentialStoreError.
|
|
81
|
+
* The store is chosen (and a Linux keychain probed) before the code is shown.
|
|
82
|
+
*/
|
|
28
83
|
export async function deviceSignIn(origin, options) {
|
|
29
|
-
const { browser, tty, insecureStorage, log, signal,
|
|
30
|
-
const
|
|
84
|
+
const { browser, tty, insecureStorage, log, signal, env = process.env, platform } = options;
|
|
85
|
+
const choice = storeForSignIn({ insecureStorage, platform }, env);
|
|
86
|
+
if (choice.notice)
|
|
87
|
+
log(`${choice.notice}\n`);
|
|
31
88
|
const start = await startDeviceSignIn(origin).catch((error) => {
|
|
32
89
|
throw cloudHint(error, origin, env);
|
|
33
90
|
});
|
|
@@ -36,14 +93,9 @@ export async function deviceSignIn(origin, options) {
|
|
|
36
93
|
openBrowser(start.verificationUriComplete);
|
|
37
94
|
log('Waiting for approval…\n');
|
|
38
95
|
const { token, email } = await waitForSignIn(origin, start, signal ? { sleep: abortableSleep(signal) } : {});
|
|
39
|
-
const
|
|
40
|
-
await
|
|
41
|
-
|
|
42
|
-
// The earlier sign-in of this computer would stay valid on the server until it expired.
|
|
43
|
-
if (superseded && !(await revokeToken(origin, superseded))) {
|
|
44
|
-
log(`Warning: ${origin} could not revoke this computer's earlier token; it expires 90 days after its last use.\n`);
|
|
45
|
-
}
|
|
46
|
-
return { origin, grant, email, store: store.kind };
|
|
96
|
+
const kept = await keepIssued({ origin, token, email }, choice.store, options);
|
|
97
|
+
await revokeSuperseded(origin, kept.superseded, log);
|
|
98
|
+
return { origin, grant: kept.grant, email, store: kept.store.kind };
|
|
47
99
|
}
|
|
48
100
|
/** The cloud is not available yet: say so instead of only "cannot be reached". */
|
|
49
101
|
export function cloudHint(error, origin, env = process.env) {
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// The flag of every command that may sign this computer in (`login`, `create`, `connect`).
|
|
2
|
+
import { Flags } from '@oclif/core';
|
|
3
|
+
/** `--insecure-storage` (the name `gh` uses): the 0600 file even where a system keychain works. */
|
|
4
|
+
export const insecureStorageFlag = () => Flags.boolean({
|
|
5
|
+
description: 'keep the token of a sign-in in a 0600 file even when a system keychain works (without one, the file is ' +
|
|
6
|
+
'used anyway)',
|
|
7
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@foliwake/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "The foliwake command without the engine: install a Foliwake server in Docker, sign in to it and run its commands there (site status, sync, preview, publish, code deploy, branches, releases, sources, credentials, CI keys), and develop a site locally (in Docker) or against a remote preview.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -108,12 +108,12 @@
|
|
|
108
108
|
"chokidar": "6.0.0",
|
|
109
109
|
"parse5": "7.3.0",
|
|
110
110
|
"yaml": "2.9.1",
|
|
111
|
-
"@foliwake/block-kit": "0.
|
|
112
|
-
"@foliwake/
|
|
113
|
-
"@foliwake/
|
|
114
|
-
"@foliwake/
|
|
115
|
-
"
|
|
116
|
-
"
|
|
111
|
+
"@foliwake/block-kit": "0.12.0",
|
|
112
|
+
"@foliwake/core-contract": "0.12.0",
|
|
113
|
+
"@foliwake/blocks-contract": "0.12.0",
|
|
114
|
+
"@foliwake/site-check": "0.12.0",
|
|
115
|
+
"create-foliwake": "0.12.0",
|
|
116
|
+
"@foliwake/server": "0.12.0"
|
|
117
117
|
},
|
|
118
118
|
"files": [
|
|
119
119
|
"dist/",
|