enseal 0.1.0 → 0.2.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 +9 -4
- package/bin/enseal.mjs +4 -4
- package/lib/config.mjs +44 -9
- package/lib/secretstore.mjs +127 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -92,10 +92,15 @@ Tokens expire after 90 days unless you choose otherwise. A token stops
|
|
|
92
92
|
working when it is revoked, when its creator leaves the workspace, and when
|
|
93
93
|
its creator's password is reset.
|
|
94
94
|
|
|
95
|
-
The token is only ever sent over HTTPS
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
95
|
+
The token is only ever sent over HTTPS, and a saved token only to the
|
|
96
|
+
server it was saved for. `enseal login` keeps it in your macOS keychain,
|
|
97
|
+
encrypted for your Windows account (DPAPI), or in your Linux desktop keyring.
|
|
98
|
+
Where there is none of those (a server, say), it goes in
|
|
99
|
+
`~/.config/enseal/config.json`, readable only by you (mode 0600). The
|
|
100
|
+
PowerShell module uses the same places, so either can use a sign-in made by
|
|
101
|
+
the other. `enseal logout` forgets it on this computer; revoke it in the
|
|
102
|
+
dashboard to end it everywhere. You are emailed when a token is first used,
|
|
103
|
+
and whenever it is used from a network it has not been used from before.
|
|
99
104
|
|
|
100
105
|
## License
|
|
101
106
|
|
package/bin/enseal.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import { sendSeal, envelopeForText, envelopeForFile, envelopeForLogin, toB64url
|
|
|
15
15
|
import { sendMessage, requestMessage, mailtoHref, messageText } from "../lib/handoffmail.js";
|
|
16
16
|
import { assessFile } from "../lib/filerisk.js";
|
|
17
17
|
import { api, VERSION } from "../lib/api.mjs";
|
|
18
|
-
import { ctx,
|
|
18
|
+
import { ctx, saveLogin, clearConfig, checkBaseUrl } from "../lib/config.mjs";
|
|
19
19
|
import { CliError } from "../lib/errors.mjs";
|
|
20
20
|
import { prompt, promptHidden, readStdin } from "../lib/prompt.mjs";
|
|
21
21
|
import { copyToClipboard, openMailto } from "../lib/clipboard.mjs";
|
|
@@ -82,9 +82,9 @@ async function cmdLogin(args) {
|
|
|
82
82
|
const token = await promptHidden(`API token for ${baseUrl} (from Settings > API tokens): `);
|
|
83
83
|
if (!token.startsWith("esk_")) throw new CliError("That doesn't look like an Enseal API token (esk_…).", 4);
|
|
84
84
|
const me = requireIdentity(await whoami({ baseUrl, token }));
|
|
85
|
-
const
|
|
85
|
+
const { where } = saveLogin({ baseUrl, token, userName: me.name });
|
|
86
86
|
out(`Signed in as ${me.name} (${me.email}), workspace "${me.workspace.name}".`);
|
|
87
|
-
out(`Token scopes: ${me.token.scopes.join(", ")}. Saved
|
|
87
|
+
out(`Token scopes: ${me.token.scopes.join(", ")}. Saved in ${where}.`);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
async function cmdWhoami(args) {
|
|
@@ -349,7 +349,7 @@ async function main() {
|
|
|
349
349
|
const [cmd, ...rest] = process.argv.slice(2);
|
|
350
350
|
switch (cmd) {
|
|
351
351
|
case "login": return cmdLogin(rest);
|
|
352
|
-
case "logout": clearConfig(); return out("Signed out; the saved token is gone.");
|
|
352
|
+
case "logout": clearConfig(); return out("Signed out; the saved token is gone from this computer. Revoke it in Settings to end it everywhere.");
|
|
353
353
|
case "whoami": return cmdWhoami(rest);
|
|
354
354
|
case "send": return cmdSend(rest);
|
|
355
355
|
case "sends": return cmdSends(rest);
|
package/lib/config.mjs
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Where the CLI keeps its sign-in: ~/.config/enseal/config.json (0600), or
|
|
3
|
-
* the XDG config home when set
|
|
4
|
-
*
|
|
3
|
+
* the XDG config home when set, holds the server and which store has the
|
|
4
|
+
* token; the token itself goes into the system's secure store when there is
|
|
5
|
+
* one (secretstore.mjs). Environment variables win over both, so CI and
|
|
6
|
+
* scripts never need a config at all:
|
|
5
7
|
*
|
|
6
8
|
* ENSEAL_TOKEN the API token (skips `enseal login` entirely)
|
|
7
9
|
* ENSEAL_BASE_URL the server (default https://enseal.app)
|
|
8
10
|
*
|
|
9
11
|
* The token is only ever sent over HTTPS: a plain-http server address is
|
|
10
|
-
* refused, except on this computer itself (localhost), for development.
|
|
12
|
+
* refused, except on this computer itself (localhost), for development. A
|
|
13
|
+
* SAVED token is only ever sent to the server it was saved for.
|
|
11
14
|
*/
|
|
12
15
|
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
13
16
|
import { homedir } from "node:os";
|
|
14
17
|
import { join } from "node:path";
|
|
15
18
|
import { CliError } from "./errors.mjs";
|
|
19
|
+
import { secretStore } from "./secretstore.mjs";
|
|
16
20
|
|
|
17
21
|
const configDir = () => join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "enseal");
|
|
18
22
|
const configFile = () => join(configDir(), "config.json");
|
|
@@ -32,7 +36,37 @@ export function saveConfig(cfg) {
|
|
|
32
36
|
return configFile();
|
|
33
37
|
}
|
|
34
38
|
|
|
39
|
+
/** Save a sign-in: the token in the system's store when it takes it, the
|
|
40
|
+
* plain file otherwise. Returns where it went, for telling the person. */
|
|
41
|
+
export function saveLogin({ baseUrl, token, userName }) {
|
|
42
|
+
const old = loadConfig();
|
|
43
|
+
const store = secretStore();
|
|
44
|
+
const kept = store?.save(baseUrl, token) ?? null;
|
|
45
|
+
// a token saved earlier for another server, or in another store, goes
|
|
46
|
+
if (old.baseUrl && (old.baseUrl !== baseUrl || !kept) && ["keychain", "secret-service"].includes(old.store)) {
|
|
47
|
+
secretStore()?.remove(old.baseUrl);
|
|
48
|
+
}
|
|
49
|
+
const cfg = kept
|
|
50
|
+
? { baseUrl, userName, store: kept.kind, ...kept.saved }
|
|
51
|
+
: { baseUrl, userName, store: "file", token };
|
|
52
|
+
const file = saveConfig(cfg);
|
|
53
|
+
return { file, where: kept ? store.label : `${file} (readable only by you)` };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The saved token, from wherever it was saved; null when there is none. */
|
|
57
|
+
export function savedToken(cfg) {
|
|
58
|
+
if (!cfg.store || cfg.store === "file") return cfg.token || null;
|
|
59
|
+
const store = secretStore();
|
|
60
|
+
const token = store && store.kind === cfg.store ? store.load(cfg.baseUrl, cfg) : null;
|
|
61
|
+
if (!token) {
|
|
62
|
+
throw new CliError(`The saved sign-in could not be read from ${store?.label ?? cfg.store}. Run \`enseal login\` again.`, 3);
|
|
63
|
+
}
|
|
64
|
+
return token;
|
|
65
|
+
}
|
|
66
|
+
|
|
35
67
|
export function clearConfig() {
|
|
68
|
+
const cfg = loadConfig();
|
|
69
|
+
if (cfg.baseUrl && ["keychain", "secret-service"].includes(cfg.store)) secretStore()?.remove(cfg.baseUrl);
|
|
36
70
|
try {
|
|
37
71
|
writeFileSync(configFile(), "{}\n", { mode: 0o600 });
|
|
38
72
|
} catch {
|
|
@@ -61,15 +95,16 @@ export function checkBaseUrl(raw) {
|
|
|
61
95
|
/** Token + server for this invocation. Never prints the token anywhere. */
|
|
62
96
|
export function ctx() {
|
|
63
97
|
const cfg = loadConfig();
|
|
64
|
-
const
|
|
98
|
+
const baseUrl = checkBaseUrl(process.env.ENSEAL_BASE_URL || cfg.baseUrl || "https://enseal.app");
|
|
99
|
+
if (process.env.ENSEAL_TOKEN) return { token: process.env.ENSEAL_TOKEN, baseUrl, cfg };
|
|
100
|
+
if (cfg.baseUrl && checkBaseUrl(cfg.baseUrl) !== baseUrl) {
|
|
101
|
+
throw new CliError(`The saved sign-in is for ${cfg.baseUrl}, not ${baseUrl}. Set ENSEAL_TOKEN for that server, or run \`enseal login --base-url ${baseUrl}\`.`, 3);
|
|
102
|
+
}
|
|
103
|
+
const token = savedToken(cfg);
|
|
65
104
|
if (!token) {
|
|
66
105
|
throw new CliError("Not signed in. Run `enseal login`, or set ENSEAL_TOKEN.", 3);
|
|
67
106
|
}
|
|
68
|
-
return {
|
|
69
|
-
token,
|
|
70
|
-
baseUrl: checkBaseUrl(process.env.ENSEAL_BASE_URL || cfg.baseUrl || "https://enseal.app"),
|
|
71
|
-
cfg,
|
|
72
|
-
};
|
|
107
|
+
return { token, baseUrl, cfg };
|
|
73
108
|
}
|
|
74
109
|
|
|
75
110
|
export { trimSlash };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a saved API token is kept: the system's own store when there is one,
|
|
3
|
+
* so the token is never a plain line in a file (backups, synced dotfiles, a
|
|
4
|
+
* copied home folder):
|
|
5
|
+
*
|
|
6
|
+
* macOS the login keychain (`security`)
|
|
7
|
+
* Windows encrypted for this Windows account with DPAPI, the protection
|
|
8
|
+
* Windows' own Credential Manager uses; the encrypted text sits
|
|
9
|
+
* in the config file and only this account on this computer can
|
|
10
|
+
* open it
|
|
11
|
+
* Linux the desktop keyring (Secret Service, `secret-tool`), when one
|
|
12
|
+
* is running
|
|
13
|
+
* anywhere else, or when the store refuses: the config file, readable
|
|
14
|
+
* only by its owner (0600), as before
|
|
15
|
+
*
|
|
16
|
+
* The token itself never appears in any command's arguments (which other
|
|
17
|
+
* programs can see in the process list): it always goes in through stdin.
|
|
18
|
+
* The PowerShell module reads and writes the same places in the same way,
|
|
19
|
+
* so either can use a sign-in made by the other.
|
|
20
|
+
*
|
|
21
|
+
* ENSEAL_SECRET_STORE=file forces the plain file (servers, CI, tests);
|
|
22
|
+
* ENSEAL_KEYCHAIN names a macOS keychain file to use instead of the login
|
|
23
|
+
* keychain (tests).
|
|
24
|
+
*/
|
|
25
|
+
import { spawnSync } from "node:child_process";
|
|
26
|
+
|
|
27
|
+
export const SERVICE = "enseal";
|
|
28
|
+
|
|
29
|
+
export const defaultRun = (cmd, args, input, env) => {
|
|
30
|
+
try {
|
|
31
|
+
const r = spawnSync(cmd, args, { input: input ?? "", encoding: "utf8", stdio: ["pipe", "pipe", "pipe"], windowsHide: true, timeout: 20_000, ...(env ? { env } : {}) });
|
|
32
|
+
return { ok: r.status === 0 && !r.error, out: (r.stdout ?? "").trim(), err: (r.stderr ?? "").trim(), missing: r.error?.code === "ENOENT" };
|
|
33
|
+
} catch {
|
|
34
|
+
return { ok: false, out: "", err: "", missing: true };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** For `security -i`, which reads commands from stdin: one quoted word. */
|
|
39
|
+
const q = (s) => `"${String(s).replace(/["\\]/g, (c) => `\\${c}`)}"`;
|
|
40
|
+
|
|
41
|
+
export const PS = ["-NoProfile", "-NonInteractive", "-Command"];
|
|
42
|
+
/** The environment for powershell.exe (Windows PowerShell 5.1). Run from a
|
|
43
|
+
* PowerShell 7 window (or anything started from one), it would inherit 7's
|
|
44
|
+
* PSModulePath, try to load 7's copy of its own security module, and fail
|
|
45
|
+
* ("the module could not be loaded"). Without PSModulePath it uses its own. */
|
|
46
|
+
export const ps51Env = (env = process.env) => Object.fromEntries(Object.entries(env).filter(([k]) => k.toLowerCase() !== "psmodulepath"));
|
|
47
|
+
// What was piped in. Windows PowerShell 5.1 (powershell.exe, which this
|
|
48
|
+
// calls) hands piped text to the script as $input and leaves the console's
|
|
49
|
+
// own input empty; PowerShell 7 offers both. Read whichever has it.
|
|
50
|
+
const STDIN = "$x=(@($input) -join [char]10).Trim(); if (-not $x) { $x=[Console]::In.ReadToEnd().Trim() }; ";
|
|
51
|
+
export const PROTECT = STDIN + "ConvertTo-SecureString $x -AsPlainText -Force | ConvertFrom-SecureString";
|
|
52
|
+
export const UNPROTECT =
|
|
53
|
+
STDIN + "$s=ConvertTo-SecureString $x; " +
|
|
54
|
+
"$p=[Runtime.InteropServices.Marshal]::SecureStringToBSTR($s); " +
|
|
55
|
+
"try { [Runtime.InteropServices.Marshal]::PtrToStringBSTR($p) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($p) }";
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The store for this computer. Each method returns null when it could not
|
|
59
|
+
* do its job, and the caller falls back to the file.
|
|
60
|
+
* save(account, token) -> { kind, saved: object to merge into the config } | null
|
|
61
|
+
* load(account, cfg) -> token | null
|
|
62
|
+
* remove(account) -> void
|
|
63
|
+
*/
|
|
64
|
+
export function secretStore({ platform = process.platform, env = process.env, run = defaultRun } = {}) {
|
|
65
|
+
if (env.ENSEAL_SECRET_STORE === "file") return null;
|
|
66
|
+
|
|
67
|
+
if (platform === "darwin") {
|
|
68
|
+
const kc = env.ENSEAL_KEYCHAIN ? [env.ENSEAL_KEYCHAIN] : [];
|
|
69
|
+
const kcq = env.ENSEAL_KEYCHAIN ? ` ${q(env.ENSEAL_KEYCHAIN)}` : "";
|
|
70
|
+
return {
|
|
71
|
+
kind: "keychain",
|
|
72
|
+
label: "your macOS keychain",
|
|
73
|
+
save(account, token) {
|
|
74
|
+
const r = run("security", ["-i"], `add-generic-password -U -s ${q(SERVICE)} -a ${q(account)} -l ${q("Enseal API token")} -w ${q(token)}${kcq}\n`);
|
|
75
|
+
if (r.missing) return null;
|
|
76
|
+
// `security -i` can report success for a command that failed: read it back
|
|
77
|
+
return this.load(account) === token ? { kind: "keychain", saved: {} } : null;
|
|
78
|
+
},
|
|
79
|
+
load(account) {
|
|
80
|
+
const r = run("security", ["find-generic-password", "-s", SERVICE, "-a", account, "-w", ...kc]);
|
|
81
|
+
return r.ok && r.out ? r.out : null;
|
|
82
|
+
},
|
|
83
|
+
remove(account) {
|
|
84
|
+
run("security", ["delete-generic-password", "-s", SERVICE, "-a", account, ...kc]);
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (platform === "win32") {
|
|
90
|
+
return {
|
|
91
|
+
kind: "dpapi",
|
|
92
|
+
label: "this Windows account (encrypted with DPAPI)",
|
|
93
|
+
save(_account, token) {
|
|
94
|
+
const r = run("powershell.exe", [...PS, PROTECT], token, ps51Env(env));
|
|
95
|
+
if (!r.ok || !/^[0-9a-f]{40,}$/i.test(r.out)) return null;
|
|
96
|
+
return this.load(_account, { tokenProtected: r.out }) === token ? { kind: "dpapi", saved: { tokenProtected: r.out } } : null;
|
|
97
|
+
},
|
|
98
|
+
load(_account, cfg) {
|
|
99
|
+
if (!cfg?.tokenProtected) return null;
|
|
100
|
+
const r = run("powershell.exe", [...PS, UNPROTECT], cfg.tokenProtected, ps51Env(env));
|
|
101
|
+
return r.ok && r.out ? r.out : null;
|
|
102
|
+
},
|
|
103
|
+
remove() {},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (platform === "linux") {
|
|
108
|
+
return {
|
|
109
|
+
kind: "secret-service",
|
|
110
|
+
label: "your desktop keyring",
|
|
111
|
+
save(account, token) {
|
|
112
|
+
const r = run("secret-tool", ["store", "--label=Enseal API token", "service", SERVICE, "account", account], token);
|
|
113
|
+
if (!r.ok) return null;
|
|
114
|
+
return this.load(account) === token ? { kind: "secret-service", saved: {} } : null;
|
|
115
|
+
},
|
|
116
|
+
load(account) {
|
|
117
|
+
const r = run("secret-tool", ["lookup", "service", SERVICE, "account", account]);
|
|
118
|
+
return r.ok && r.out ? r.out : null;
|
|
119
|
+
},
|
|
120
|
+
remove(account) {
|
|
121
|
+
run("secret-tool", ["clear", "service", SERVICE, "account", account]);
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return null;
|
|
127
|
+
}
|