@enbox/auth 0.4.0 → 0.5.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.
Files changed (50) hide show
  1. package/dist/esm/auth-manager.js +200 -4
  2. package/dist/esm/auth-manager.js.map +1 -1
  3. package/dist/esm/flows/dwn-discovery.js +96 -81
  4. package/dist/esm/flows/dwn-discovery.js.map +1 -1
  5. package/dist/esm/flows/dwn-registration.js +49 -3
  6. package/dist/esm/flows/dwn-registration.js.map +1 -1
  7. package/dist/esm/flows/import-identity.js +2 -0
  8. package/dist/esm/flows/import-identity.js.map +1 -1
  9. package/dist/esm/flows/local-connect.js +25 -8
  10. package/dist/esm/flows/local-connect.js.map +1 -1
  11. package/dist/esm/flows/session-restore.js +13 -2
  12. package/dist/esm/flows/session-restore.js.map +1 -1
  13. package/dist/esm/flows/wallet-connect.js +5 -4
  14. package/dist/esm/flows/wallet-connect.js.map +1 -1
  15. package/dist/esm/index.js +5 -1
  16. package/dist/esm/index.js.map +1 -1
  17. package/dist/esm/password-provider.js +319 -0
  18. package/dist/esm/password-provider.js.map +1 -0
  19. package/dist/esm/types.js +9 -1
  20. package/dist/esm/types.js.map +1 -1
  21. package/dist/types/auth-manager.d.ts +67 -2
  22. package/dist/types/auth-manager.d.ts.map +1 -1
  23. package/dist/types/flows/dwn-discovery.d.ts +40 -53
  24. package/dist/types/flows/dwn-discovery.d.ts.map +1 -1
  25. package/dist/types/flows/dwn-registration.d.ts +20 -1
  26. package/dist/types/flows/dwn-registration.d.ts.map +1 -1
  27. package/dist/types/flows/import-identity.d.ts.map +1 -1
  28. package/dist/types/flows/local-connect.d.ts +2 -0
  29. package/dist/types/flows/local-connect.d.ts.map +1 -1
  30. package/dist/types/flows/session-restore.d.ts +2 -0
  31. package/dist/types/flows/session-restore.d.ts.map +1 -1
  32. package/dist/types/flows/wallet-connect.d.ts +2 -2
  33. package/dist/types/flows/wallet-connect.d.ts.map +1 -1
  34. package/dist/types/index.d.ts +5 -2
  35. package/dist/types/index.d.ts.map +1 -1
  36. package/dist/types/password-provider.d.ts +194 -0
  37. package/dist/types/password-provider.d.ts.map +1 -0
  38. package/dist/types/types.d.ts +86 -1
  39. package/dist/types/types.d.ts.map +1 -1
  40. package/package.json +8 -9
  41. package/src/auth-manager.ts +236 -8
  42. package/src/flows/dwn-discovery.ts +99 -79
  43. package/src/flows/dwn-registration.ts +60 -5
  44. package/src/flows/import-identity.ts +2 -0
  45. package/src/flows/local-connect.ts +24 -3
  46. package/src/flows/session-restore.ts +15 -2
  47. package/src/flows/wallet-connect.ts +5 -4
  48. package/src/index.ts +10 -1
  49. package/src/password-provider.ts +383 -0
  50. package/src/types.ts +93 -1
@@ -0,0 +1,319 @@
1
+ /**
2
+ * PasswordProvider — composable password acquisition strategies.
3
+ *
4
+ * Replaces ad-hoc password prompting scattered across CLI consumers
5
+ * (env vars, raw-mode TTY, `/dev/tty` + `stty`, `@clack/prompts`, etc.)
6
+ * with a single, composable abstraction.
7
+ *
8
+ * @example Chained provider (env first, fall back to TTY)
9
+ * ```ts
10
+ * import { PasswordProvider } from '@enbox/auth';
11
+ *
12
+ * const provider = PasswordProvider.chain([
13
+ * PasswordProvider.fromEnv('ENBOX_PASSWORD'),
14
+ * PasswordProvider.fromTty({ prompt: 'Vault password: ' }),
15
+ * ]);
16
+ *
17
+ * const auth = await AuthManager.create({ passwordProvider: provider });
18
+ * ```
19
+ *
20
+ * @module
21
+ */
22
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
23
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
+ return new (P || (P = Promise))(function (resolve, reject) {
25
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
29
+ });
30
+ };
31
+ // ─── Internal helpers ────────────────────────────────────────────
32
+ /**
33
+ * Read a password from a raw-mode TTY stream.
34
+ *
35
+ * Reads character-by-character with no echo. Handles Enter (resolve),
36
+ * Ctrl-C (reject), backspace, and printable characters.
37
+ *
38
+ * @internal Exported for testing only.
39
+ */
40
+ export function readPasswordRawMode(stdin, stdout, prompt) {
41
+ stdout.write(prompt);
42
+ return new Promise((resolve, reject) => {
43
+ let buf = '';
44
+ stdin.setRawMode(true);
45
+ stdin.setEncoding('utf8');
46
+ stdin.resume();
47
+ const onData = (ch) => {
48
+ const code = ch.charCodeAt(0);
49
+ if (ch === '\r' || ch === '\n') {
50
+ // Enter — done.
51
+ stdin.setRawMode(false);
52
+ stdin.pause();
53
+ stdin.removeListener('data', onData);
54
+ stdout.write('\n');
55
+ resolve(buf);
56
+ }
57
+ else if (code === 3) {
58
+ // Ctrl-C — abort.
59
+ stdin.setRawMode(false);
60
+ stdin.pause();
61
+ stdin.removeListener('data', onData);
62
+ stdout.write('\n');
63
+ reject(new Error('[@enbox/auth] PasswordProvider.fromTty: cancelled by user.'));
64
+ }
65
+ else if (code === 127 || code === 8) {
66
+ // Backspace / Delete.
67
+ if (buf.length > 0) {
68
+ buf = buf.slice(0, -1);
69
+ }
70
+ }
71
+ else if (code >= 32) {
72
+ // Printable character.
73
+ buf += ch;
74
+ }
75
+ };
76
+ stdin.on('data', onData);
77
+ });
78
+ }
79
+ /**
80
+ * Read a password from `/dev/tty` using synchronous I/O.
81
+ *
82
+ * Opens `/dev/tty` directly, uses `stty -echo` to suppress input,
83
+ * reads until newline, then restores echo and closes file descriptors.
84
+ *
85
+ * @param prompt - The prompt string to display.
86
+ * @param io - Injectable I/O functions (defaults to `node:fs` + `node:child_process`).
87
+ * @internal Exported for testing only.
88
+ */
89
+ export function readPasswordDevTty(prompt, io) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ // Use injected I/O or import real modules.
92
+ let fsIo;
93
+ if (io) {
94
+ fsIo = io;
95
+ }
96
+ else {
97
+ const { openSync, readSync, writeSync, closeSync } = yield import('node:fs');
98
+ const { execSync } = yield import('node:child_process');
99
+ fsIo = {
100
+ openSync,
101
+ readSync,
102
+ writeSync,
103
+ closeSync,
104
+ execSync: (cmd, opts) => { execSync(cmd, opts); },
105
+ };
106
+ }
107
+ let readFd;
108
+ let writeFd;
109
+ try {
110
+ readFd = fsIo.openSync('/dev/tty', 'r');
111
+ writeFd = fsIo.openSync('/dev/tty', 'w');
112
+ }
113
+ catch (_a) {
114
+ throw new Error('[@enbox/auth] PasswordProvider.fromDevTty: cannot open /dev/tty. ' +
115
+ 'No controlling terminal available.');
116
+ }
117
+ try {
118
+ // Suppress echo.
119
+ try {
120
+ fsIo.execSync('stty -echo < /dev/tty', { stdio: 'ignore' });
121
+ }
122
+ catch (_b) {
123
+ // Continue — the user sees their password but the flow works.
124
+ }
125
+ fsIo.writeSync(writeFd, prompt);
126
+ // Cooked-mode read (line-buffered; terminal handles backspace).
127
+ const readBuf = new Uint8Array(256);
128
+ const decoder = new TextDecoder('utf-8');
129
+ let password = '';
130
+ while (true) {
131
+ const bytesRead = fsIo.readSync(readFd, readBuf, 0, readBuf.length, null);
132
+ if (bytesRead === 0) {
133
+ break;
134
+ }
135
+ password += decoder.decode(readBuf.subarray(0, bytesRead), { stream: true });
136
+ const nlIdx = password.indexOf('\n');
137
+ if (nlIdx !== -1) {
138
+ password = password.slice(0, nlIdx);
139
+ break;
140
+ }
141
+ const crIdx = password.indexOf('\r');
142
+ if (crIdx !== -1) {
143
+ password = password.slice(0, crIdx);
144
+ break;
145
+ }
146
+ }
147
+ fsIo.writeSync(writeFd, '\n');
148
+ return password;
149
+ }
150
+ finally {
151
+ // Restore echo.
152
+ try {
153
+ fsIo.execSync('stty echo < /dev/tty', { stdio: 'ignore' });
154
+ }
155
+ catch ( /* best-effort */_c) { /* best-effort */ }
156
+ fsIo.closeSync(readFd);
157
+ fsIo.closeSync(writeFd);
158
+ }
159
+ });
160
+ }
161
+ // ─── Factory functions ───────────────────────────────────────────
162
+ export var PasswordProvider;
163
+ (function (PasswordProvider) {
164
+ /**
165
+ * Read the password from an environment variable.
166
+ *
167
+ * Throws if the variable is not set or is empty, allowing `chain()`
168
+ * to fall through to the next provider.
169
+ *
170
+ * @param envVar - Name of the environment variable. Default: `'ENBOX_PASSWORD'`.
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * const provider = PasswordProvider.fromEnv('MY_APP_PASSWORD');
175
+ * ```
176
+ */
177
+ function fromEnv(envVar = 'ENBOX_PASSWORD') {
178
+ return {
179
+ getPassword() {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ const value = process.env[envVar];
182
+ if (!value) {
183
+ throw new Error(`[@enbox/auth] PasswordProvider.fromEnv: environment variable '${envVar}' is not set.`);
184
+ }
185
+ return value;
186
+ });
187
+ },
188
+ };
189
+ }
190
+ PasswordProvider.fromEnv = fromEnv;
191
+ /**
192
+ * Wrap an async callback as a password provider.
193
+ *
194
+ * This is the escape hatch for custom UI (e.g. `@clack/prompts`,
195
+ * Electron dialog, browser modal).
196
+ *
197
+ * @param callback - Called with the password context; must return a password string.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * const provider = PasswordProvider.fromCallback(async ({ reason }) => {
202
+ * if (reason === 'create') {
203
+ * return await showCreatePasswordDialog();
204
+ * }
205
+ * return await showUnlockDialog();
206
+ * });
207
+ * ```
208
+ */
209
+ function fromCallback(callback) {
210
+ return { getPassword: callback };
211
+ }
212
+ PasswordProvider.fromCallback = fromCallback;
213
+ /**
214
+ * Prompt for a password via `process.stdin` in raw mode.
215
+ *
216
+ * Input is read character-by-character with no echo. Handles
217
+ * backspace and Ctrl-C (rejects with an error). Only works when
218
+ * `process.stdin.isTTY` is `true`; throws otherwise so `chain()`
219
+ * can fall through to the next provider.
220
+ *
221
+ * Suitable for main CLI processes that own stdin/stdout.
222
+ *
223
+ * @param options - Optional configuration.
224
+ * @param options.prompt - Text to display before reading. Default: `'Vault password: '`.
225
+ *
226
+ * @example
227
+ * ```ts
228
+ * const provider = PasswordProvider.fromTty({ prompt: 'Password: ' });
229
+ * ```
230
+ */
231
+ function fromTty(options = {}) {
232
+ var _a;
233
+ const prompt = (_a = options.prompt) !== null && _a !== void 0 ? _a : 'Vault password: ';
234
+ return {
235
+ getPassword() {
236
+ return __awaiter(this, void 0, void 0, function* () {
237
+ if (!process.stdin.isTTY) {
238
+ throw new Error('[@enbox/auth] PasswordProvider.fromTty: stdin is not a TTY.');
239
+ }
240
+ return readPasswordRawMode(process.stdin, process.stdout, prompt);
241
+ });
242
+ },
243
+ };
244
+ }
245
+ PasswordProvider.fromTty = fromTty;
246
+ /**
247
+ * Prompt for a password via `/dev/tty` (Unix only).
248
+ *
249
+ * Opens `/dev/tty` directly, bypassing `process.stdin`. This is
250
+ * essential for subprocesses where stdin is owned by the parent
251
+ * (e.g. Git credential helpers, SSH, GPG). Uses `stty -echo` to
252
+ * suppress input echo.
253
+ *
254
+ * Throws if `/dev/tty` cannot be opened (e.g. non-Unix platform,
255
+ * no controlling terminal), allowing `chain()` to fall through.
256
+ *
257
+ * @param options - Optional configuration.
258
+ * @param options.prompt - Text to display before reading. Default: `'Vault password: '`.
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * // For git credential helpers:
263
+ * const provider = PasswordProvider.fromDevTty();
264
+ * ```
265
+ */
266
+ function fromDevTty(options = {}) {
267
+ var _a;
268
+ const prompt = (_a = options.prompt) !== null && _a !== void 0 ? _a : 'Vault password: ';
269
+ return {
270
+ getPassword() {
271
+ return __awaiter(this, void 0, void 0, function* () {
272
+ return readPasswordDevTty(prompt);
273
+ });
274
+ },
275
+ };
276
+ }
277
+ PasswordProvider.fromDevTty = fromDevTty;
278
+ /**
279
+ * Compose multiple providers with automatic fallback.
280
+ *
281
+ * Tries each provider in order. If a provider throws, the next one
282
+ * is tried. If all providers fail, the last error is rethrown.
283
+ *
284
+ * @param providers - Ordered list of providers to try.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * // Try env var first, then interactive TTY, then /dev/tty for subprocesses.
289
+ * const provider = PasswordProvider.chain([
290
+ * PasswordProvider.fromEnv('ENBOX_PASSWORD'),
291
+ * PasswordProvider.fromTty(),
292
+ * PasswordProvider.fromDevTty(),
293
+ * ]);
294
+ * ```
295
+ */
296
+ function chain(providers) {
297
+ if (providers.length === 0) {
298
+ throw new Error('[@enbox/auth] PasswordProvider.chain: at least one provider is required.');
299
+ }
300
+ return {
301
+ getPassword(context) {
302
+ return __awaiter(this, void 0, void 0, function* () {
303
+ let lastError;
304
+ for (const provider of providers) {
305
+ try {
306
+ return yield provider.getPassword(context);
307
+ }
308
+ catch (err) {
309
+ lastError = err instanceof Error ? err : new Error(String(err));
310
+ }
311
+ }
312
+ throw lastError !== null && lastError !== void 0 ? lastError : new Error('[@enbox/auth] PasswordProvider.chain: all providers failed.');
313
+ });
314
+ },
315
+ };
316
+ }
317
+ PasswordProvider.chain = chain;
318
+ })(PasswordProvider || (PasswordProvider = {}));
319
+ //# sourceMappingURL=password-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"password-provider.js","sourceRoot":"","sources":["../../src/password-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;AAsDH,oEAAoE;AAEpE;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CACjC,KAAkB,EAClB,MAAmB,EACnB,MAAc;IAEd,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAErB,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC7C,IAAI,GAAG,GAAG,EAAE,CAAC;QACb,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QACvB,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QAC1B,KAAK,CAAC,MAAM,EAAE,CAAC;QAEf,MAAM,MAAM,GAAG,CAAC,EAAU,EAAQ,EAAE;YAClC,MAAM,IAAI,GAAG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AI,kBAAkB,CAAC;QAEpD,OAAO;YACC,WAAW;;oBACf,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC;gBACpC,CAAC;aAAA;SACF,CAAC;IACJ,CAAC;IARe,2BAAU,aAQzB,CAAA;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAgB,KAAK,CAAC,SAA6B;QACjD,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,0EAA0E,CAAC,CAAC;QAC9F,CAAC;QAED,OAAO;YACC,WAAW,CAAC,OAAwB;;oBACxC,IAAI,SAA4B,CAAC;oBAEjC,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;wBACjC,IAAI,CAAC;4BACH,OAAO,MAAM,QAAQ,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;wBAC7C,CAAC;wBAAC,OAAO,GAAG,EAAE,CAAC;4BACb,SAAS,GAAG,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;wBAClE,CAAC;oBACH,CAAC;oBAED,MAAM,SAAS,aAAT,SAAS,cAAT,SAAS,GAAI,IAAI,KAAK,CAAC,6DAA6D,CAAC,CAAC;gBAC9F,CAAC;aAAA;SACF,CAAC;IACJ,CAAC;IApBe,sBAAK,QAoBpB,CAAA;AACH,CAAC,EAhKgB,gBAAgB,KAAhB,gBAAgB,QAgKhC"}
package/dist/esm/types.js CHANGED
@@ -19,12 +19,20 @@ export const STORAGE_KEYS = {
19
19
  /** The connected DID (for wallet-connected sessions). */
20
20
  CONNECTED_DID: 'enbox:auth:connectedDid',
21
21
  /**
22
- * The base URL of the local DWN server discovered via the `dwn://register`
22
+ * The base URL of the local DWN server discovered via the `dwn://connect`
23
23
  * browser redirect flow. Persisted so subsequent page loads can skip the
24
24
  * redirect and inject the endpoint directly.
25
25
  *
26
26
  * @see https://github.com/enboxorg/enbox/issues/589
27
27
  */
28
28
  LOCAL_DWN_ENDPOINT: 'enbox:auth:localDwnEndpoint',
29
+ /**
30
+ * JSON-serialised `Record<string, RegistrationTokenData>` for DWN endpoint
31
+ * registration tokens. Automatically loaded before registration and saved
32
+ * after new/refreshed tokens are obtained when `persistTokens` is enabled.
33
+ *
34
+ * @see https://github.com/enboxorg/enbox/issues/690
35
+ */
36
+ REGISTRATION_TOKENS: 'enbox:auth:registrationTokens',
29
37
  };
30
38
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAuXH,oEAAoE;AAEpE,gEAAgE;AAChE,MAAM,CAAC,MAAM,yBAAyB,GAAG,wBAAwB,CAAC;AAElE;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,oDAAoD;IACpD,oBAAoB,EAAE,gCAAgC;IAEtD,+CAA+C;IAC/C,eAAe,EAAE,2BAA2B;IAE5C,4DAA4D;IAC5D,YAAY,EAAE,wBAAwB;IAEtC,yDAAyD;IACzD,aAAa,EAAE,yBAAyB;IAExC;;;;;;OAMG;IACH,kBAAkB,EAAE,6BAA6B;CACzC,CAAC"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA0cH,oEAAoE;AAEpE,gEAAgE;AAChE,MAAM,CAAC,MAAM,yBAAyB,GAAG,wBAAwB,CAAC;AAElE;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,oDAAoD;IACpD,oBAAoB,EAAE,gCAAgC;IAEtD,+CAA+C;IAC/C,eAAe,EAAE,2BAA2B;IAE5C,4DAA4D;IAC5D,YAAY,EAAE,wBAAwB;IAEtC,yDAAyD;IACzD,aAAa,EAAE,yBAAyB;IAExC;;;;;;OAMG;IACH,kBAAkB,EAAE,6BAA6B;IAEjD;;;;;;OAMG;IACH,mBAAmB,EAAE,+BAA+B;CAC5C,CAAC"}
@@ -9,7 +9,7 @@ import { EnboxUserAgent } from '@enbox/agent';
9
9
  import type { PortableIdentity } from '@enbox/agent';
10
10
  import { AuthSession } from './identity-session.js';
11
11
  import { VaultManager } from './vault/vault-manager.js';
12
- import type { AuthEvent, AuthEventHandler, AuthManagerOptions, AuthState, DisconnectOptions, IdentityInfo, ImportFromPhraseOptions, ImportFromPortableOptions, LocalConnectOptions, RestoreSessionOptions, WalletConnectOptions } from './types.js';
12
+ import type { AuthEvent, AuthEventHandler, AuthManagerOptions, AuthState, DisconnectOptions, HeadlessConnectOptions, IdentityInfo, ImportFromPhraseOptions, ImportFromPortableOptions, LocalConnectOptions, RestoreSessionOptions, ShutdownOptions, WalletConnectOptions } from './types.js';
13
13
  /**
14
14
  * The primary entry point for authentication and identity management.
15
15
  *
@@ -51,10 +51,19 @@ export declare class AuthManager {
51
51
  private _session;
52
52
  private _state;
53
53
  private _isConnecting;
54
+ private _isShutDown;
54
55
  private _defaultPassword?;
56
+ private _passwordProvider?;
55
57
  private _defaultSync?;
56
58
  private _defaultDwnEndpoints?;
57
59
  private _registration?;
60
+ /**
61
+ * The local DWN server endpoint discovered during `create()`, if any.
62
+ * `undefined` means no local server was found. This is set before any
63
+ * event listeners are attached, so consumers should check this property
64
+ * after `create()` returns rather than relying solely on events.
65
+ */
66
+ private _localDwnEndpoint?;
58
67
  private constructor();
59
68
  /**
60
69
  * Create a new AuthManager instance.
@@ -79,7 +88,7 @@ export declare class AuthManager {
79
88
  */
80
89
  connect(options?: LocalConnectOptions): Promise<AuthSession>;
81
90
  /**
82
- * Connect to an external wallet via the OIDC/QR relay protocol.
91
+ * Connect to an external wallet via the Enbox Connect relay protocol.
83
92
  *
84
93
  * This runs the full WalletConnect flow: generates a URI for QR display,
85
94
  * validates the PIN, imports the delegated DID, and processes grants.
@@ -110,6 +119,30 @@ export declare class AuthManager {
110
119
  * This replaces the manual `previouslyConnected` localStorage pattern.
111
120
  */
112
121
  restoreSession(options?: RestoreSessionOptions): Promise<AuthSession | undefined>;
122
+ /**
123
+ * Lightweight vault unlock for one-shot utilities and subprocesses.
124
+ *
125
+ * Unlocks the vault and retrieves the active (or first available)
126
+ * identity **without** starting sync, DWN registration, or persisting
127
+ * session markers. This is the recommended replacement for calling
128
+ * `agent.start({ password })` directly.
129
+ *
130
+ * Typical use cases:
131
+ * - Git credential helpers that need to sign a token and exit
132
+ * - CLI utilities that perform a single operation
133
+ * - Any subprocess that shares a data directory with a long-running daemon
134
+ *
135
+ * @param options - Optional password override.
136
+ * @returns An active AuthSession (with sync disabled).
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * const session = await auth.connectHeadless({ password });
141
+ * const did = session.did; // ready to use
142
+ * await auth.shutdown(); // clean exit
143
+ * ```
144
+ */
145
+ connectHeadless(options?: HeadlessConnectOptions): Promise<AuthSession>;
113
146
  /** The current active session, or `undefined` if not connected. */
114
147
  get session(): AuthSession | undefined;
115
148
  /**
@@ -137,6 +170,30 @@ export declare class AuthManager {
137
170
  * @param options.timeout - Milliseconds to wait for sync to complete.
138
171
  */
139
172
  disconnect(options?: DisconnectOptions): Promise<void>;
173
+ /**
174
+ * Gracefully shut down the auth manager, releasing all resources.
175
+ *
176
+ * This goes beyond {@link disconnect} or {@link lock}: it stops sync,
177
+ * clears the active session, locks the vault, and **closes** the
178
+ * underlying storage handles (e.g. LevelDB) so the process can exit
179
+ * without dangling timers or open file descriptors.
180
+ *
181
+ * After calling `shutdown()`, the `AuthManager` instance should not be
182
+ * reused — create a new one via {@link AuthManager.create} if needed.
183
+ *
184
+ * Idempotent: calling `shutdown()` more than once is safe.
185
+ *
186
+ * @param options - Optional shutdown configuration.
187
+ * @param options.timeout - Milliseconds to wait for sync to stop. Default: `2000`.
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * const session = await auth.connectHeadless({ password });
192
+ * // ... perform work ...
193
+ * await auth.shutdown(); // clean exit, no process.exit() needed
194
+ * ```
195
+ */
196
+ shutdown(options?: ShutdownOptions): Promise<void>;
140
197
  /**
141
198
  * List all stored identities.
142
199
  *
@@ -186,6 +243,14 @@ export declare class AuthManager {
186
243
  get isConnecting(): boolean;
187
244
  /** The underlying EnboxUserAgent (for advanced usage). */
188
245
  get agent(): EnboxUserAgent;
246
+ /**
247
+ * The local DWN server endpoint discovered during `create()`, if any.
248
+ *
249
+ * When set, the agent is operating in remote mode (no in-process DWN).
250
+ * This property is available immediately after `create()` returns,
251
+ * before any event listeners are attached.
252
+ */
253
+ get localDwnEndpoint(): string | undefined;
189
254
  private _setState;
190
255
  private _guardConcurrency;
191
256
  }
@@ -1 +1 @@
1
- {"version":3,"file":"auth-manager.d.ts","sourceRoot":"","sources":["../../src/auth-manager.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAkB,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGrE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAKpD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAExD,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAChB,kBAAkB,EAClB,SAAS,EACT,iBAAiB,EACjB,YAAY,EACZ,uBAAuB,EACvB,yBAAyB,EACzB,mBAAmB,EAEnB,qBAAqB,EAGrB,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAGpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,UAAU,CAAiB;IACnC,OAAO,CAAC,QAAQ,CAAmB;IACnC,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,QAAQ,CAA0B;IAC1C,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,aAAa,CAAS;IAG9B,OAAO,CAAC,gBAAgB,CA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owest priority)
9
9
  *
10
- * 1. **URL fragment payload** — A `dwn://register` redirect just landed
10
+ * 1. **URL fragment payload** — A `dwn://connect` redirect just landed
11
11
  * on the page with the endpoint in `#`. Highest priority because it's
12
12
  * fresh and explicit.
13
13
  * 2. **Persisted endpoint** (localStorage) — A previously discovered
14
14
  * endpoint restored and re-validated via `GET /info`.
15
- * 3. **Agent-level discovery** (transparent, runs on every `sendRequest`)
16
- * — `~/.enbox/dwn.json` discovery file (Node/Bun only; skipped in
17
- * browsers) and sequential port probing on `127.0.0.1:{3000,55500–55509}`.
18
- * This channel works even if the browser-specific functions here
19
- * return `false`.
20
15
  *
21
16
  * ## Discovery channels (CLI / native, all transparent)
22
17
  *
23
- * In Node/Bun environments, all discovery happens automatically inside
18
+ * In Node/Bun environments, the agent's `LocalDwnDiscovery` reads the
19
+ * `~/.enbox/dwn.json` discovery file automatically inside
24
20
  * `AgentDwnApi.getLocalDwnEndpoint()`. The browser-specific functions
25
21
  * in this module (`checkUrlForDwnDiscoveryPayload`, `requestLocalDwnDiscovery`)
26
- * are not needed the agent reads `~/.enbox/dwn.json` and probes ports
27
- * on its own.
22
+ * are not needed in those environments.
28
23
  *
29
- * @see https://github.com/enboxorg/enbox/issues/589
24
+ * @see https://github.com/enboxorg/enbox/issues/677
30
25
  * @module
31
26
  */
32
27
  import type { EnboxUserAgent } from '@enbox/agent';
@@ -36,7 +31,7 @@ import type { StorageAdapter } from '../types.js';
36
31
  * Check the current page URL for a `DwnDiscoveryPayload` in the fragment.
37
32
  *
38
33
  * This is called once at the start of a connection flow to detect whether
39
- * the user was just redirected back from a `dwn://register` handler. If a
34
+ * the user was just redirected back from a `dwn://connect` handler. If a
40
35
  * valid payload is found, the endpoint is persisted and the fragment is
41
36
  * cleared to prevent double-reads.
42
37
  *
@@ -44,6 +39,28 @@ import type { StorageAdapter } from '../types.js';
44
39
  * was found in the URL.
45
40
  */
46
41
  export declare function checkUrlForDwnDiscoveryPayload(): string | undefined;
42
+ /**
43
+ * Run local DWN discovery **before the agent exists**.
44
+ *
45
+ * This is the standalone counterpart of {@link applyLocalDwnDiscovery} and
46
+ * is designed to be called in `AuthManager.create()`, before
47
+ * `EnboxUserAgent.create()`, so the agent creation can decide whether to
48
+ * spin up an in-process DWN or operate in remote mode.
49
+ *
50
+ * Discovery channels (highest → lowest priority):
51
+ * 1. **URL fragment payload** — A `dwn://connect` redirect just landed.
52
+ * 2. **Persisted endpoint** (localStorage) — A previously discovered
53
+ * endpoint, re-validated via `GET /info`.
54
+ *
55
+ * When a valid endpoint is found it is persisted to storage. When a
56
+ * previously-persisted endpoint is stale, it is removed.
57
+ *
58
+ * @param storage - The auth storage adapter (for reading/writing the
59
+ * cached endpoint).
60
+ * @returns The validated endpoint URL, or `undefined` if no local DWN
61
+ * server is available.
62
+ */
63
+ export declare function discoverLocalDwn(storage: StorageAdapter): Promise<string | undefined>;
47
64
  /**
48
65
  * Persist a discovered local DWN endpoint in auth storage.
49
66
  *
@@ -76,25 +93,18 @@ export declare function restoreLocalDwnEndpoint(agent: EnboxUserAgent, storage:
76
93
  * Run the full local DWN discovery sequence for a browser connection flow.
77
94
  *
78
95
  * This function handles the **receiving** side of local DWN discovery in
79
- * the browser. It does NOT trigger the `dwn://register` redirect — use
96
+ * the browser. It does NOT trigger the `dwn://connect` redirect — use
80
97
  * {@link requestLocalDwnDiscovery} for that.
81
98
  *
82
99
  * The discovery channels, from highest to lowest priority:
83
100
  *
84
- * 1. **URL fragment payload** — A `dwn://register` redirect just landed on
101
+ * 1. **URL fragment payload** — A `dwn://connect` redirect just landed on
85
102
  * this page with the DWN endpoint in `#`. This is the highest-priority
86
103
  * signal because it's fresh and explicit.
87
104
  *
88
105
  * 2. **Persisted endpoint** (localStorage) — A previously discovered
89
106
  * endpoint is restored and re-validated via `GET /info`.
90
107
  *
91
- * 3. **Agent-level discovery** (transparent) — Even if this function
92
- * returns `false`, the agent's `LocalDwnDiscovery` will independently
93
- * try the discovery file (`~/.enbox/dwn.json`) and port probing on
94
- * every `sendRequest()` call. Those channels are not available in
95
- * browsers (no filesystem access, CORS may block probes), but they
96
- * work transparently in Node/Bun CLI environments.
97
- *
98
108
  * When an `emitter` is provided, this function emits:
99
109
  * - `'local-dwn-available'` with the endpoint when discovery succeeds.
100
110
  * - `'local-dwn-unavailable'` when no local DWN could be reached.
@@ -106,52 +116,29 @@ export declare function restoreLocalDwnEndpoint(agent: EnboxUserAgent, storage:
106
116
  */
107
117
  export declare function applyLocalDwnDiscovery(agent: EnboxUserAgent, storage: StorageAdapter, emitter?: AuthEventEmitter): Promise<boolean>;
108
118
  /**
109
- * Initiate the `dwn://register` flow by opening the register URL.
119
+ * Initiate the `dwn://connect` flow by opening the connect URL.
110
120
  *
111
- * This asks the operating system to route `dwn://register?callback=<url>`
121
+ * This asks the operating system to route `dwn://connect?callback=<url>`
112
122
  * to the registered handler (electrobun-dwn), which will redirect the
113
123
  * user's browser back to `callbackUrl` with the local DWN endpoint
114
124
  * encoded in the URL fragment.
115
125
  *
116
- * **Important:** There is no reliable cross-browser API to detect whether
117
- * a `dwn://` handler is installed. If no handler is registered, this call
118
- * will silently fail or show an OS-level error dialog. Use
119
- * {@link probeLocalDwn} first to check if a local DWN is already
120
- * reachable via port probing — if it is, you can skip the register flow
121
- * entirely and call {@link applyLocalDwnDiscovery} instead.
126
+ * **Note:** There is no reliable cross-browser API to detect whether a
127
+ * `dwn://` handler is installed. If no handler is registered, this call
128
+ * will silently fail or show an OS-level error dialog.
122
129
  *
123
130
  * @param callbackUrl - The URL to redirect back to. Defaults to the
124
131
  * current page URL (without its fragment) if running in a browser.
125
- * @returns `true` if the register URL was opened, `false` if no
132
+ * @returns `true` if the connect URL was opened, `false` if no
126
133
  * callback URL could be determined (e.g. no `globalThis.location`).
127
134
  *
128
135
  * @example
129
136
  * ```ts
130
- * // Check if local DWN is already available via direct probe.
131
- * const alreadyAvailable = await probeLocalDwn();
132
- * if (!alreadyAvailable) {
133
- * // No local DWN found trigger the dwn://register flow.
134
- * requestLocalDwnDiscovery();
135
- * // The page will reload with the endpoint in the URL fragment.
136
- * }
137
+ * // Trigger the dwn://connect flow to discover a local DWN.
138
+ * requestLocalDwnDiscovery();
139
+ * // The page will reload with the endpoint in the URL fragment.
140
+ * // On the next connect/restore, applyLocalDwnDiscovery() reads it.
137
141
  * ```
138
142
  */
139
143
  export declare function requestLocalDwnDiscovery(callbackUrl?: string): boolean;
140
- /**
141
- * Probe whether a local DWN server is reachable via direct HTTP fetch.
142
- *
143
- * Attempts `GET http://127.0.0.1:{port}/info` on the well-known port
144
- * candidates and returns the endpoint URL of the first server that
145
- * responds with a valid `@enbox/dwn-server` identity.
146
- *
147
- * This is useful in browsers to check if a local DWN is available
148
- * *before* triggering the `dwn://register` redirect flow — if the
149
- * server is already reachable (CORS permitting), the redirect is
150
- * unnecessary.
151
- *
152
- * @returns The local DWN endpoint URL, or `undefined` if no server
153
- * was found. Returns `undefined` (rather than throwing) on CORS
154
- * errors or network failures.
155
- */
156
- export declare function probeLocalDwn(): Promise<string | undefined>;
157
144
  //# sourceMappingURL=dwn-discovery.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dwn-discovery.d.ts","sourceRoot":"","sources":["../../../src/flows/dwn-discovery.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAInD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD;;;;;;;;;;GAUG;AACH,wBAAgB,8BAA8B,IAAI,MAAM,GAAG,SAAS,CAkBnE;AAED;;;;;GAKG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,cAAc,EACvB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;;;;;;GAOG;AACH,wBAAsB,qBAAqB,CACzC,O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