@aragon-agent/cli 0.6.71 → 0.6.73
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 +29 -0
- package/README.md +20 -0
- package/dist/account/config.d.ts +61 -0
- package/dist/account/config.js +83 -0
- package/dist/account/device-client.d.ts +113 -0
- package/dist/account/device-client.js +425 -0
- package/dist/account/format.d.ts +51 -0
- package/dist/account/format.js +102 -0
- package/dist/account/login-flow.d.ts +68 -0
- package/dist/account/login-flow.js +305 -0
- package/dist/account/session.d.ts +109 -0
- package/dist/account/session.js +109 -0
- package/dist/account/store.d.ts +86 -0
- package/dist/account/store.js +231 -0
- package/dist/app/App.js +397 -8
- package/dist/commands/account-command.d.ts +96 -0
- package/dist/commands/account-command.js +194 -0
- package/dist/commands/builtins.js +19 -0
- package/dist/commands/catalog.js +24 -0
- package/dist/config/app-paths.d.ts +15 -0
- package/dist/config/app-paths.js +17 -0
- package/package.json +3 -3
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<home>/account.json` - the on-disk credential for the device-flow login (C3).
|
|
3
|
+
*
|
|
4
|
+
* FILE SHAPE (version 1, and that version field is the whole migration story):
|
|
5
|
+
*
|
|
6
|
+
* {
|
|
7
|
+
* "version": 1,
|
|
8
|
+
* "baseUrl": "https://aragonagent.com",
|
|
9
|
+
* "token": "<plaintext bearer token>",
|
|
10
|
+
* "tokenPrefix": "aragon_pat_ab12cd34ef56",
|
|
11
|
+
* "user": { "id": "usr_...", "username": "ada", "email": "ada@example.com" },
|
|
12
|
+
* "scopes": ["account:read"],
|
|
13
|
+
* "issuedAt": 1767000000000,
|
|
14
|
+
* "expiresAt": 1768728000000
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* THE TOKEN IS PLAINTEXT ON PURPOSE. An OS keychain would be better and is not
|
|
18
|
+
* reachable from a CLI that must also run under WSL, in CI and over SSH; what is
|
|
19
|
+
* reachable is 0600 plus an atomic write, which is what `~/.aws/credentials`,
|
|
20
|
+
* `~/.npmrc` and `gh` all do. The file never holds a refresh token, so the blast
|
|
21
|
+
* radius of a leak is one revocable credential with a 20-day ceiling.
|
|
22
|
+
*
|
|
23
|
+
* NEVER THROWS. Every failure mode here - missing file, garbage bytes, a JSON
|
|
24
|
+
* document of the wrong shape, an unwritable home - is an ANSWER, because the
|
|
25
|
+
* only caller that matters runs at startup and a throw there is a CLI that will
|
|
26
|
+
* not start because of a credential file. `readAccount()` answers `null`,
|
|
27
|
+
* `writeAccount()` answers `false`, `clearAccount()` answers whether the file is
|
|
28
|
+
* gone afterwards.
|
|
29
|
+
*/
|
|
30
|
+
import { randomUUID } from 'node:crypto';
|
|
31
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
|
|
32
|
+
import process from 'node:process';
|
|
33
|
+
import { registerSecret } from '@aragon-agent/tui/model/logging/secret-registry';
|
|
34
|
+
import { getAccountPath, getHomeRoot } from '../config/app-paths.js';
|
|
35
|
+
import { getLogger } from '../logging/logger.js';
|
|
36
|
+
/** The only shape this module reads or writes (C3). */
|
|
37
|
+
export const ACCOUNT_SCHEMA_VERSION = 1;
|
|
38
|
+
/** POSIX mode for a file holding a bearer token. Ignored on win32. */
|
|
39
|
+
export const ACCOUNT_FILE_MODE = 0o600;
|
|
40
|
+
function isNonEmptyString(value) {
|
|
41
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
42
|
+
}
|
|
43
|
+
function isEpochMs(value) {
|
|
44
|
+
return typeof value === 'number' && Number.isFinite(value) && value > 0;
|
|
45
|
+
}
|
|
46
|
+
function readUser(value) {
|
|
47
|
+
if (!value || typeof value !== 'object')
|
|
48
|
+
return null;
|
|
49
|
+
const raw = value;
|
|
50
|
+
if (!isNonEmptyString(raw.id) || !isNonEmptyString(raw.username))
|
|
51
|
+
return null;
|
|
52
|
+
return {
|
|
53
|
+
id: raw.id,
|
|
54
|
+
username: raw.username,
|
|
55
|
+
...(isNonEmptyString(raw.email) ? { email: raw.email } : {}),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function readScopes(value) {
|
|
59
|
+
if (!Array.isArray(value))
|
|
60
|
+
return null;
|
|
61
|
+
if (!value.every((entry) => typeof entry === 'string'))
|
|
62
|
+
return null;
|
|
63
|
+
return [...value];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Validate a parsed document against C3, or answer `null`.
|
|
67
|
+
*
|
|
68
|
+
* STRICT ON PURPOSE, NOT `Partial<>`-TOLERANT: every field here answers a
|
|
69
|
+
* question the user asks out loud ("who am I signed in as", "does this token
|
|
70
|
+
* still work"), so a document missing one of them is not a credential with a
|
|
71
|
+
* gap - it is a file this build does not understand, and `null` sends the caller
|
|
72
|
+
* down the documented `signed-out` path instead of printing `undefined`.
|
|
73
|
+
*
|
|
74
|
+
* AN UNKNOWN `version` IS ALSO `null`, deliberately: `2` means a newer CLI wrote
|
|
75
|
+
* a shape this build does not know, and guessing at it would be the one way to
|
|
76
|
+
* make an upgrade lose someone's login instead of asking them to log in again.
|
|
77
|
+
*/
|
|
78
|
+
function toAccountRecord(value) {
|
|
79
|
+
if (!value || typeof value !== 'object')
|
|
80
|
+
return null;
|
|
81
|
+
const raw = value;
|
|
82
|
+
if (raw.version !== ACCOUNT_SCHEMA_VERSION)
|
|
83
|
+
return null;
|
|
84
|
+
if (!isNonEmptyString(raw.baseUrl))
|
|
85
|
+
return null;
|
|
86
|
+
if (!isNonEmptyString(raw.token))
|
|
87
|
+
return null;
|
|
88
|
+
if (!isNonEmptyString(raw.tokenPrefix))
|
|
89
|
+
return null;
|
|
90
|
+
const user = readUser(raw.user);
|
|
91
|
+
if (user === null)
|
|
92
|
+
return null;
|
|
93
|
+
const scopes = readScopes(raw.scopes);
|
|
94
|
+
if (scopes === null)
|
|
95
|
+
return null;
|
|
96
|
+
if (!isEpochMs(raw.issuedAt) || !isEpochMs(raw.expiresAt))
|
|
97
|
+
return null;
|
|
98
|
+
return {
|
|
99
|
+
version: ACCOUNT_SCHEMA_VERSION,
|
|
100
|
+
baseUrl: raw.baseUrl,
|
|
101
|
+
token: raw.token,
|
|
102
|
+
tokenPrefix: raw.tokenPrefix,
|
|
103
|
+
user,
|
|
104
|
+
scopes,
|
|
105
|
+
issuedAt: raw.issuedAt,
|
|
106
|
+
expiresAt: raw.expiresAt,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Register the token with the redactor the moment it enters the process.
|
|
111
|
+
*
|
|
112
|
+
* Belt AND braces: `login-flow.ts` registers the token it receives, and this
|
|
113
|
+
* does it for a token READ BACK from disk, because the two cases that leak are
|
|
114
|
+
* "a stack trace printed the request" and "a debug record carried the parsed
|
|
115
|
+
* object", and both happen after a read. Registration is idempotent and
|
|
116
|
+
* process-lifetime (see `secret-registry.ts`).
|
|
117
|
+
*/
|
|
118
|
+
function rememberSecret(token) {
|
|
119
|
+
registerSecret(token);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The credential on disk, or `null`.
|
|
123
|
+
*
|
|
124
|
+
* `null` covers: no file, an empty file, a half-written file, a document that is
|
|
125
|
+
* not JSON, a JSON scalar, a document of the wrong shape and a document from a
|
|
126
|
+
* newer schema. Callers treat all of them the same way - "not signed in" - which
|
|
127
|
+
* is the only answer that cannot be wrong about a file it could not parse.
|
|
128
|
+
*/
|
|
129
|
+
export function readAccount() {
|
|
130
|
+
const path = getAccountPath();
|
|
131
|
+
if (!existsSync(path))
|
|
132
|
+
return null;
|
|
133
|
+
let text;
|
|
134
|
+
try {
|
|
135
|
+
text = readFileSync(path, 'utf-8');
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
getLogger().warn('cli', 'account.read_failed', { path, code: errorCodeOf(error) });
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
let parsed;
|
|
142
|
+
try {
|
|
143
|
+
parsed = JSON.parse(text);
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
// Deliberately not an error record: a torn file is expected after a crash and
|
|
147
|
+
// the user-visible outcome is "signed out", which is not a fault to report.
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const record = toAccountRecord(parsed);
|
|
151
|
+
if (record === null)
|
|
152
|
+
return null;
|
|
153
|
+
rememberSecret(record.token);
|
|
154
|
+
return record;
|
|
155
|
+
}
|
|
156
|
+
function errorCodeOf(error) {
|
|
157
|
+
const code = error?.code;
|
|
158
|
+
return typeof code === 'string' ? code : 'unknown';
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Write the credential atomically: temp file in the same directory, then rename.
|
|
162
|
+
*
|
|
163
|
+
* WHY RENAME AND NOT `writeFileSync(path)`: a write that dies half-way (a full
|
|
164
|
+
* disk, a killed process, a power cut) leaves a TRUNCATED JSON document, and the
|
|
165
|
+
* reader above would then report `signed-out` for a user who did nothing wrong.
|
|
166
|
+
* `rename` is atomic within a directory, so a reader sees either the old file or
|
|
167
|
+
* the new one - never a torn one.
|
|
168
|
+
*
|
|
169
|
+
* THE TEMP NAME CARRIES pid AND A RANDOM SUFFIX: two `aragon` processes logging
|
|
170
|
+
* in at once (a terminal and a desktop host) must not write the same temp file.
|
|
171
|
+
*
|
|
172
|
+
* `chmodSync` AFTER the rename, matching `config/ui-state.ts`: `rename` does not
|
|
173
|
+
* preserve the mode on every platform, and a 0600 temp file renamed into place
|
|
174
|
+
* is only 0600 by luck otherwise. win32 has no POSIX mode to set, so it is
|
|
175
|
+
* skipped there rather than warned about.
|
|
176
|
+
*/
|
|
177
|
+
export function writeAccount(record) {
|
|
178
|
+
const path = getAccountPath();
|
|
179
|
+
const tmp = `${path}.tmp-${process.pid}-${randomUUID().slice(0, 8)}`;
|
|
180
|
+
const body = `${JSON.stringify({ ...record, version: ACCOUNT_SCHEMA_VERSION }, null, 2)}\n`;
|
|
181
|
+
try {
|
|
182
|
+
mkdirSync(getHomeRoot(), { recursive: true });
|
|
183
|
+
writeFileSync(tmp, body, { encoding: 'utf-8', mode: ACCOUNT_FILE_MODE });
|
|
184
|
+
renameSync(tmp, path);
|
|
185
|
+
if (process.platform !== 'win32')
|
|
186
|
+
chmodSync(path, ACCOUNT_FILE_MODE);
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
try {
|
|
190
|
+
rmSync(tmp, { force: true });
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
// The temp file is already gone, or the directory is unwritable; either way
|
|
194
|
+
// there is nothing further to try and the answer is still `false`.
|
|
195
|
+
}
|
|
196
|
+
getLogger().warn('cli', 'account.write_failed', { path, code: errorCodeOf(error) });
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
rememberSecret(record.token);
|
|
200
|
+
getLogger().info('cli', 'account.saved', {
|
|
201
|
+
path,
|
|
202
|
+
username: record.user.username,
|
|
203
|
+
expiresAt: record.expiresAt,
|
|
204
|
+
scopes: record.scopes.length,
|
|
205
|
+
});
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Delete the credential. IDEMPOTENT: `true` means "there is no credential file
|
|
210
|
+
* now", which is also the answer when there never was one.
|
|
211
|
+
*
|
|
212
|
+
* `false` means the file is still there, and the only cause is a filesystem
|
|
213
|
+
* refusal (a Windows share with an open handle, a read-only directory). The
|
|
214
|
+
* caller may then say "sign-out failed" instead of lying.
|
|
215
|
+
*/
|
|
216
|
+
export function clearAccount() {
|
|
217
|
+
const path = getAccountPath();
|
|
218
|
+
const existed = existsSync(path);
|
|
219
|
+
try {
|
|
220
|
+
rmSync(path, { force: true });
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
getLogger().warn('cli', 'account.clear_failed', { path, code: errorCodeOf(error) });
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
if (existsSync(path))
|
|
227
|
+
return false;
|
|
228
|
+
if (existed)
|
|
229
|
+
getLogger().info('cli', 'account.cleared', { path });
|
|
230
|
+
return true;
|
|
231
|
+
}
|