ravensight-playtest 0.21.1 → 0.21.2

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 CHANGED
@@ -295,7 +295,8 @@ ravensight-playtest login
295
295
 
296
296
  Nothing is downloaded until you need it. Playwright is an optional dependency,
297
297
  so `login`, `check`, `init` and `brief` work without a browser installed;
298
- `run --driver playwright_web` is what asks for one. `keytar` is optional too,
298
+ `run --driver playwright_web` is what asks for one. The keychain module
299
+ (`@napi-rs/keyring`, a prebuilt binary with no install script) is optional too,
299
300
  which is why a machine with no keychain still installs cleanly.
300
301
 
301
302
  ## First run
@@ -310,7 +311,7 @@ ravensight-playtest run # prints the price, asks, then plays
310
311
  ```
311
312
 
312
313
  `login` stores the token in your OS keychain (macOS Keychain, Windows
313
- Credential Manager, libsecret on Linux). With no keychain available it falls
314
+ Credential Manager, the Secret Service on Linux). With no keychain available it falls
314
315
  back to a file with 0600 permissions under your home directory, and says so.
315
316
  In CI, set `RAVENSIGHT_TOKEN` instead and skip `login` entirely.
316
317
 
@@ -379,7 +380,7 @@ stores the token.
379
380
  | `--no-browser` | Do not try to open a browser. `RAVENSIGHT_NO_BROWSER=1` does the same thing. |
380
381
 
381
382
  The token goes in your OS keychain (macOS Keychain, Windows Credential Manager,
382
- libsecret on Linux). With no keychain available it falls back to a file with 0600
383
+ the Secret Service on Linux). With no keychain available it falls back to a file with 0600
383
384
  permissions under `RAVENSIGHT_HOME`, and says which store it used. In CI, set
384
385
  `RAVENSIGHT_TOKEN` and skip `login` entirely.
385
386
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ravensight-playtest",
3
- "version": "0.21.1",
3
+ "version": "0.21.2",
4
4
  "description": "Local AI playtests and seeded game simulations with reports in Ravensight.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://ravensight.io/docs/playtest/",
@@ -75,7 +75,7 @@
75
75
  "commander": "^13.0.0"
76
76
  },
77
77
  "optionalDependencies": {
78
- "keytar": "^7.9.0",
78
+ "@napi-rs/keyring": "^2.1.2",
79
79
  "playwright": "^1.51.0"
80
80
  }
81
81
  }
@@ -1,52 +1,187 @@
1
- import { mkdir } from 'node:fs/promises';
2
- import { unlink } from 'node:fs/promises';
1
+ import { execFile } from 'node:child_process';
2
+ import { mkdir, unlink } from 'node:fs/promises';
3
3
  import { paths } from '../paths.js';
4
4
  import { readJson, writeJson } from '../fsutil.js';
5
5
 
6
6
  export const KEYCHAIN_SERVICE = 'ravensight-playtest';
7
7
  const FILE_MODE = 0o600;
8
8
 
9
- let keytarModule;
10
- let keytarTried = false;
9
+ let backendModule;
10
+ let backendTried = false;
11
+ let service = KEYCHAIN_SERVICE;
11
12
 
12
13
  /**
13
- * `keytar` is a native module, and every realistic reason it is missing is a
14
- * reason the CLI still has to work: it is an optional dependency whose install
15
- * script may not have run, a headless Linux box with no libsecret, or a CI
16
- * container. So it is required lazily, once, inside a try, and a failure means
17
- * "use the file backend" rather than "crash".
14
+ * The OS keychain, through `@napi-rs/keyring`. It ships a prebuilt binary per
15
+ * platform as an optional package, so there is no install script, and every
16
+ * realistic reason it is missing is a reason the CLI still has to work: an
17
+ * optional dependency that was skipped, a platform with no prebuilt binary, a
18
+ * headless Linux box with no Secret Service, or a CI container. So it is loaded
19
+ * lazily, once, inside a try, and a failure means "use the file backend"
20
+ * rather than "crash".
21
+ *
22
+ * On Linux the entry is pinned to the Secret Service. Left to itself the module
23
+ * would fall back to the kernel keyring, which forgets everything at a reboot;
24
+ * a token that vanishes at a reboot is worse than the 0600 file, so a machine
25
+ * with no Secret Service uses the file, as it did before.
18
26
  *
19
27
  * @returns {Promise<object|null>}
20
28
  */
21
- async function keytar() {
22
- if (keytarTried) return keytarModule || null;
23
- keytarTried = true;
29
+ async function keychain() {
30
+ if (backendTried) return backendModule || null;
31
+ backendTried = true;
32
+ try {
33
+ const mod = await import('@napi-rs/keyring');
34
+ backendModule = keyringBackend(mod.default || mod);
35
+ } catch {
36
+ backendModule = null;
37
+ }
38
+ return backendModule;
39
+ }
40
+
41
+ /**
42
+ * Wrap the module in the small shape this file uses, or null when it loaded
43
+ * but cannot work here. Exported for tests, with the platform and the legacy
44
+ * Linux lookup injectable, so every platform's branch runs on any machine.
45
+ *
46
+ * Entries keep the names the CLI used before (service `ravensight-playtest`,
47
+ * account the API host), so on macOS a token stored by the old keychain module
48
+ * is the same Keychain item and is read with no migration at all. Windows and
49
+ * Linux name their entries differently in the two modules, so a token the old
50
+ * module stored is read from its old place when the new one has none, and
51
+ * logout clears both (see `readLegacy` and `deleteLegacy`).
52
+ *
53
+ * @param {object} mod the loaded `@napi-rs/keyring`
54
+ * @param {{platform?: string, secretTool?: Function}} [options]
55
+ * @returns {object|null}
56
+ */
57
+ export function keyringBackend(mod, options = {}) {
58
+ const platform = options.platform || process.platform;
59
+ const { AsyncEntry } = mod || {};
60
+ if (typeof AsyncEntry !== 'function') return null;
61
+ const entryOptions = platform === 'linux' ? { linux: { store: 'secret-service' } } : undefined;
62
+ const entry = (svc, account) => new AsyncEntry(svc, account, entryOptions);
63
+ // An import that resolves but cannot open the store (no Secret Service on
64
+ // Linux) is not a working backend: treat it exactly like an absent module.
65
+ // Making an entry touches no stored item on any platform.
24
66
  try {
25
- const mod = await import('keytar');
26
- const candidate = mod.default || mod;
27
- // An import that resolves but has no getPassword is a broken build, not a
28
- // working backend: treat it exactly like an absent module.
29
- keytarModule = typeof candidate.getPassword === 'function' ? candidate : null;
67
+ entry(service, 'probe');
30
68
  } catch {
31
- keytarModule = null;
69
+ return null;
32
70
  }
33
- return keytarModule;
71
+ const legacy = legacyStore(platform, mod, options.secretTool || secretTool);
72
+ return {
73
+ getPassword: (svc, account) => entry(svc, account).getPassword(),
74
+ setPassword: (svc, account, value) => entry(svc, account).setPassword(value),
75
+ deletePassword: (svc, account) => entry(svc, account).deletePassword(),
76
+ readLegacy: legacy.read,
77
+ deleteLegacy: legacy.remove
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Where the old keychain module (keytar, archived) kept a token on each
83
+ * platform, read and cleared without loading it.
84
+ *
85
+ * - macOS: the same generic password item (service, account), so nothing.
86
+ * - Windows: a generic credential named `<service>/<account>`, holding the
87
+ * token's UTF-8 bytes. The new module names its own `<account>.<service>`.
88
+ * It is found by enumerating that exact name, never by making an entry for
89
+ * it: the module writes an empty secret into a named entry as it makes it,
90
+ * which would wipe the very token being read. The module decodes the bytes
91
+ * as UTF-16, so they are turned back into the bytes and read as UTF-8.
92
+ * - Linux: a Secret Service item with the attributes `service` and `account`,
93
+ * where the new module uses `username`. Read and cleared with `secret-tool`
94
+ * when it is installed; without it the old token is not found and one more
95
+ * login stores it in the new place.
96
+ */
97
+ function legacyStore(platform, mod, runSecretTool) {
98
+ const none = { read: async () => null, remove: async () => false };
99
+ if (platform === 'win32') {
100
+ const { AsyncEntry, findCredentialsAsync } = mod;
101
+ if (typeof findCredentialsAsync !== 'function') return none;
102
+ const target = (svc, account) => `${svc}/${account}`;
103
+ const find = (svc, account) => findCredentialsAsync(svc, target(svc, account));
104
+ return {
105
+ async read(svc, account) {
106
+ const found = (await find(svc, account)).find(c => c && c.account === account);
107
+ return found ? fromWideString(found.password) : null;
108
+ },
109
+ async remove(svc, account) {
110
+ let present;
111
+ try {
112
+ present = (await find(svc, account)).length > 0;
113
+ } catch {
114
+ // Found but unreadable (an odd number of bytes) is still there.
115
+ present = true;
116
+ }
117
+ if (!present) return false;
118
+ return Boolean(await AsyncEntry.withTarget(target(svc, account), svc, account).deleteCredential());
119
+ }
120
+ };
121
+ }
122
+ if (platform === 'linux') {
123
+ const attributes = (svc, account) => ['service', svc, 'account', account];
124
+ return {
125
+ async read(svc, account) {
126
+ const out = await runSecretTool(['lookup', ...attributes(svc, account)]);
127
+ return out ? out.replace(/\r?\n$/, '') || null : null;
128
+ },
129
+ async remove(svc, account) {
130
+ if (!(await runSecretTool(['lookup', ...attributes(svc, account)]))) return false;
131
+ await runSecretTool(['clear', ...attributes(svc, account)]);
132
+ return true;
133
+ }
134
+ };
135
+ }
136
+ return none;
137
+ }
138
+
139
+ /**
140
+ * The token keytar stored on Windows, from the string the new module read it
141
+ * as. A token is printable ASCII, so anything else is not one.
142
+ * @param {string} value
143
+ * @returns {string|null}
144
+ */
145
+ export function fromWideString(value) {
146
+ if (typeof value !== 'string' || !value) return null;
147
+ const decoded = Buffer.from(value, 'utf16le').toString('utf8');
148
+ return /^[\x21-\x7e]+$/.test(decoded) ? decoded : null;
149
+ }
150
+
151
+ /**
152
+ * `secret-tool` with arguments, never a shell. Its stdout, or null when it is
153
+ * not installed, found nothing or failed.
154
+ * @param {string[]} args
155
+ * @returns {Promise<string|null>}
156
+ */
157
+ function secretTool(args) {
158
+ return new Promise(resolve => {
159
+ execFile('secret-tool', args, { timeout: 5000, windowsHide: true }, (error, stdout) => {
160
+ resolve(error ? null : String(stdout || ''));
161
+ });
162
+ });
34
163
  }
35
164
 
36
- /** Test seam: forget the cached probe so a stub can be installed. */
37
- export function resetKeychainProbe(stub) {
38
- keytarTried = stub !== undefined;
39
- keytarModule = stub === undefined ? undefined : stub;
165
+ /**
166
+ * Test seam: forget the cached probe so a stub can be installed, and
167
+ * optionally name a test-only keychain service so a test against the real
168
+ * keychain can never touch the developer's own token.
169
+ */
170
+ export function resetKeychainProbe(stub, testService) {
171
+ backendTried = stub !== undefined;
172
+ backendModule = stub === undefined ? undefined : stub;
173
+ service = testService || KEYCHAIN_SERVICE;
40
174
  }
41
175
 
42
176
  /**
43
- * Which backend is in use, for `check` to report. The OS keychain when keytar
44
- * loaded, otherwise the 0600 file, and `env` is not a backend at all: it is a
45
- * credential this process was handed and will not store.
177
+ * Which backend is in use, for `check` to report. The OS keychain when the
178
+ * keyring module loaded and can open the store, otherwise the 0600 file, and
179
+ * `env` is not a backend at all: it is a credential this process was handed
180
+ * and will not store.
46
181
  * @returns {Promise<'keychain'|'file'>}
47
182
  */
48
183
  export async function backendName() {
49
- return (await keytar()) ? 'keychain' : 'file';
184
+ return (await keychain()) ? 'keychain' : 'file';
50
185
  }
51
186
 
52
187
  async function fileStore() {
@@ -76,15 +211,27 @@ async function writeFileStore(store) {
76
211
  * @returns {Promise<{token: string|null, backend: 'keychain'|'file'|null}>}
77
212
  */
78
213
  export async function readStoredToken(host) {
79
- const kt = await keytar();
80
- if (kt) {
214
+ const kc = await keychain();
215
+ if (kc) {
81
216
  try {
82
- const found = await kt.getPassword(KEYCHAIN_SERVICE, host);
217
+ const found = await kc.getPassword(service, host);
83
218
  if (found) return { token: found, backend: 'keychain' };
84
219
  } catch {
85
220
  // A locked or unavailable keychain falls through to the file, rather than
86
221
  // failing a command that could have worked.
87
222
  }
223
+ if (kc.readLegacy) {
224
+ try {
225
+ // A token the previous keychain module stored, where this one names
226
+ // its entries differently. Read only: it is not copied or moved, so
227
+ // a command that only reads changes nothing, and the next login or
228
+ // logout clears it.
229
+ const legacy = await kc.readLegacy(service, host);
230
+ if (legacy) return { token: legacy, backend: 'keychain' };
231
+ } catch {
232
+ // Same as above: the file is still worth a look.
233
+ }
234
+ }
88
235
  }
89
236
  const store = await fileStore();
90
237
  const entry = store[host];
@@ -109,10 +256,13 @@ export async function getStoredToken(host) {
109
256
  * @returns {Promise<{backend: 'keychain'|'file'}>}
110
257
  */
111
258
  export async function setStoredToken(host, token) {
112
- const kt = await keytar();
113
- if (kt) {
259
+ const kc = await keychain();
260
+ if (kc) {
114
261
  try {
115
- await kt.setPassword(KEYCHAIN_SERVICE, host, token);
262
+ await kc.setPassword(service, host, token);
263
+ // The new token is stored, so an old one the previous module kept for
264
+ // this host is only a stale credential left behind: clear it.
265
+ await deleteLegacy(kc, host);
116
266
  return { backend: 'keychain' };
117
267
  } catch {
118
268
  // Fall through: a token we cannot store is a login the developer has to
@@ -126,20 +276,37 @@ export async function setStoredToken(host, token) {
126
276
  }
127
277
 
128
278
  /**
129
- * Forget the token for one host. Answers whether anything was there, so
130
- * `logout` can tell "revoked and cleared" from "nothing to clear".
279
+ * Clear the previous module's entry for one host, where it differs from this
280
+ * module's. Never throws: a stale entry that could not be cleared must not
281
+ * fail a login or a logout.
282
+ * @returns {Promise<boolean>} whether one was there and is gone
283
+ */
284
+ async function deleteLegacy(kc, host) {
285
+ if (!kc.deleteLegacy) return false;
286
+ try {
287
+ return Boolean(await kc.deleteLegacy(service, host));
288
+ } catch {
289
+ return false;
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Forget the token for one host: this module's keychain entry, the previous
295
+ * module's entry where it differs, and the file. Answers whether anything was
296
+ * there, so `logout` can tell "revoked and cleared" from "nothing to clear".
131
297
  * @param {string} host
132
298
  * @returns {Promise<boolean>}
133
299
  */
134
300
  export async function deleteStoredToken(host) {
135
301
  let removed = false;
136
- const kt = await keytar();
137
- if (kt) {
302
+ const kc = await keychain();
303
+ if (kc) {
138
304
  try {
139
- removed = Boolean(await kt.deletePassword(KEYCHAIN_SERVICE, host));
305
+ removed = Boolean(await kc.deletePassword(service, host));
140
306
  } catch {
141
307
  removed = false;
142
308
  }
309
+ if (await deleteLegacy(kc, host)) removed = true;
143
310
  }
144
311
  const store = await fileStore();
145
312
  if (Object.prototype.hasOwnProperty.call(store, host)) {
@@ -463,7 +463,7 @@ export async function runChecks(flags = {}, deps = {}) {
463
463
  'pass',
464
464
  storeDetail(),
465
465
  source === 'file' || (source === null && backend === 'file')
466
- ? 'install libsecret for a real keychain on Linux'
466
+ ? 'on Linux, run a Secret Service (GNOME Keyring or KWallet) for a real keychain'
467
467
  : null
468
468
  ));
469
469