@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
package/src/types.ts CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  import type { ConnectPermissionRequest, EnboxUserAgent, HdIdentityVault, LocalDwnStrategy, PortableIdentity } from '@enbox/agent';
7
7
 
8
+ import type { PasswordProvider } from './password-provider.js';
9
+
8
10
  // Re-export types that consumers will need
9
11
  export type { ConnectPermissionRequest, HdIdentityVault, IdentityVaultBackup, LocalDwnStrategy, PortableIdentity } from '@enbox/agent';
10
12
 
@@ -166,14 +168,46 @@ export interface RegistrationOptions {
166
168
  * Pre-existing registration tokens from a previous session, keyed by
167
169
  * DWN endpoint URL. If a valid (non-expired) token exists for an
168
170
  * endpoint, it is used directly without re-running the auth flow.
171
+ *
172
+ * When {@link persistTokens} is `true`, this field is ignored —
173
+ * tokens are loaded automatically from the `StorageAdapter`.
169
174
  */
170
175
  registrationTokens?: Record<string, RegistrationTokenData>;
171
176
 
172
177
  /**
173
178
  * Called when new or refreshed registration tokens are obtained.
174
179
  * The app should persist these for future sessions.
180
+ *
181
+ * When {@link persistTokens} is `true`, tokens are saved automatically
182
+ * to the `StorageAdapter`. This callback is still invoked (if provided)
183
+ * **after** the automatic save, so consumers can observe token changes
184
+ * without handling persistence themselves.
175
185
  */
176
186
  onRegistrationTokens?: (tokens: Record<string, RegistrationTokenData>) => void;
187
+
188
+ /**
189
+ * Automatically persist and restore registration tokens using the
190
+ * auth manager's `StorageAdapter`.
191
+ *
192
+ * When `true`, tokens are loaded from storage before registration and
193
+ * saved back after new or refreshed tokens are obtained. This removes
194
+ * the need for consumers to implement their own token I/O via
195
+ * {@link registrationTokens} and {@link onRegistrationTokens}.
196
+ *
197
+ * Defaults to `false` for backward compatibility.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * const auth = await AuthManager.create({
202
+ * registration: {
203
+ * onSuccess: () => {},
204
+ * onFailure: (err) => console.error(err),
205
+ * persistTokens: true,
206
+ * },
207
+ * });
208
+ * ```
209
+ */
210
+ persistTokens?: boolean;
177
211
  }
178
212
 
179
213
  /** Options for {@link AuthManager.create}. */
@@ -223,9 +257,34 @@ export interface AuthManagerOptions {
223
257
  /**
224
258
  * Default password for vault operations.
225
259
  * If not provided, an insecure default is used (with a console warning).
260
+ *
261
+ * For more flexible password acquisition (env vars, TTY prompts,
262
+ * chained fallbacks), use {@link passwordProvider} instead.
226
263
  */
227
264
  password?: string;
228
265
 
266
+ /**
267
+ * A composable password provider for obtaining the vault password.
268
+ *
269
+ * When set, this provider is consulted by `connect()`,
270
+ * `restoreSession()`, and `connectHeadless()` whenever a password
271
+ * is needed and none was given explicitly. It takes precedence over
272
+ * the static {@link password} option.
273
+ *
274
+ * @example
275
+ * ```ts
276
+ * import { AuthManager, PasswordProvider } from '@enbox/auth';
277
+ *
278
+ * const auth = await AuthManager.create({
279
+ * passwordProvider: PasswordProvider.chain([
280
+ * PasswordProvider.fromEnv('ENBOX_PASSWORD'),
281
+ * PasswordProvider.fromTty(),
282
+ * ]),
283
+ * });
284
+ * ```
285
+ */
286
+ passwordProvider?: PasswordProvider;
287
+
229
288
  /**
230
289
  * Sync interval for DWN synchronization.
231
290
  * - `'off'` — disable sync
@@ -340,6 +399,21 @@ export interface RestoreSessionOptions {
340
399
  onPasswordRequired?: () => Promise<string>;
341
400
  }
342
401
 
402
+ /** Options for {@link AuthManager.connectHeadless}. */
403
+ export interface HeadlessConnectOptions {
404
+ /** Vault password (overrides manager default). */
405
+ password?: string;
406
+ }
407
+
408
+ /** Options for {@link AuthManager.shutdown}. */
409
+ export interface ShutdownOptions {
410
+ /**
411
+ * Milliseconds to wait for pending sync operations before shutting down.
412
+ * Default: `2000`.
413
+ */
414
+ timeout?: number;
415
+ }
416
+
343
417
  /** Options for {@link AuthManager.disconnect}. */
344
418
  export interface DisconnectOptions {
345
419
  /**
@@ -374,6 +448,15 @@ export interface StorageAdapter {
374
448
 
375
449
  /** Clear all stored data. */
376
450
  clear(): Promise<void>;
451
+
452
+ /**
453
+ * Close the underlying storage resources (e.g. LevelDB handles).
454
+ *
455
+ * Optional — not all adapters need cleanup. Called by
456
+ * {@link AuthManager.shutdown} to release resources so the process
457
+ * can exit cleanly.
458
+ */
459
+ close?(): Promise<void>;
377
460
  }
378
461
 
379
462
  // ─── Internal helpers ────────────────────────────────────────────
@@ -399,11 +482,20 @@ export const STORAGE_KEYS = {
399
482
  CONNECTED_DID: 'enbox:auth:connectedDid',
400
483
 
401
484
  /**
402
- * The base URL of the local DWN server discovered via the `dwn://register`
485
+ * The base URL of the local DWN server discovered via the `dwn://connect`
403
486
  * browser redirect flow. Persisted so subsequent page loads can skip the
404
487
  * redirect and inject the endpoint directly.
405
488
  *
406
489
  * @see https://github.com/enboxorg/enbox/issues/589
407
490
  */
408
491
  LOCAL_DWN_ENDPOINT: 'enbox:auth:localDwnEndpoint',
492
+
493
+ /**
494
+ * JSON-serialised `Record<string, RegistrationTokenData>` for DWN endpoint
495
+ * registration tokens. Automatically loaded before registration and saved
496
+ * after new/refreshed tokens are obtained when `persistTokens` is enabled.
497
+ *
498
+ * @see https://github.com/enboxorg/enbox/issues/690
499
+ */
500
+ REGISTRATION_TOKENS: 'enbox:auth:registrationTokens',
409
501
  } as const;