@enbox/auth 0.4.0 → 0.6.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 (86) hide show
  1. package/dist/esm/auth-manager.js +244 -121
  2. package/dist/esm/auth-manager.js.map +1 -1
  3. package/dist/esm/connect/import.js +131 -0
  4. package/dist/esm/connect/import.js.map +1 -0
  5. package/dist/esm/connect/lifecycle.js +235 -0
  6. package/dist/esm/connect/lifecycle.js.map +1 -0
  7. package/dist/esm/connect/local.js +91 -0
  8. package/dist/esm/connect/local.js.map +1 -0
  9. package/dist/esm/{flows/session-restore.js → connect/restore.js} +39 -50
  10. package/dist/esm/connect/restore.js.map +1 -0
  11. package/dist/esm/{flows/wallet-connect.js → connect/wallet.js} +33 -39
  12. package/dist/esm/connect/wallet.js.map +1 -0
  13. package/dist/esm/{flows/dwn-discovery.js → discovery.js} +98 -83
  14. package/dist/esm/discovery.js.map +1 -0
  15. package/dist/esm/index.js +7 -3
  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/{flows/dwn-registration.js → registration.js} +50 -4
  20. package/dist/esm/registration.js.map +1 -0
  21. package/dist/esm/types.js +11 -1
  22. package/dist/esm/types.js.map +1 -1
  23. package/dist/esm/wallet-connect-client.js +188 -0
  24. package/dist/esm/wallet-connect-client.js.map +1 -0
  25. package/dist/types/auth-manager.d.ts +86 -7
  26. package/dist/types/auth-manager.d.ts.map +1 -1
  27. package/dist/types/connect/import.d.ts +25 -0
  28. package/dist/types/connect/import.d.ts.map +1 -0
  29. package/dist/types/connect/lifecycle.d.ts +152 -0
  30. package/dist/types/connect/lifecycle.d.ts.map +1 -0
  31. package/dist/types/connect/local.d.ts +18 -0
  32. package/dist/types/connect/local.d.ts.map +1 -0
  33. package/dist/types/connect/restore.d.ts +18 -0
  34. package/dist/types/connect/restore.d.ts.map +1 -0
  35. package/dist/types/{flows/wallet-connect.d.ts → connect/wallet.d.ts} +7 -16
  36. package/dist/types/connect/wallet.d.ts.map +1 -0
  37. package/dist/types/{flows/dwn-discovery.d.ts → discovery.d.ts} +43 -56
  38. package/dist/types/discovery.d.ts.map +1 -0
  39. package/dist/types/index.d.ts +8 -4
  40. package/dist/types/index.d.ts.map +1 -1
  41. package/dist/types/password-provider.d.ts +194 -0
  42. package/dist/types/password-provider.d.ts.map +1 -0
  43. package/dist/types/{flows/dwn-registration.d.ts → registration.d.ts} +21 -2
  44. package/dist/types/registration.d.ts.map +1 -0
  45. package/dist/types/types.d.ts +92 -4
  46. package/dist/types/types.d.ts.map +1 -1
  47. package/dist/types/wallet-connect-client.d.ts +89 -0
  48. package/dist/types/wallet-connect-client.d.ts.map +1 -0
  49. package/package.json +15 -12
  50. package/src/auth-manager.ts +279 -145
  51. package/src/connect/import.ts +148 -0
  52. package/src/connect/lifecycle.ts +321 -0
  53. package/src/connect/local.ts +101 -0
  54. package/src/connect/restore.ts +117 -0
  55. package/src/{flows/wallet-connect.ts → connect/wallet.ts} +42 -58
  56. package/src/{flows/dwn-discovery.ts → discovery.ts} +103 -82
  57. package/src/index.ts +14 -4
  58. package/src/password-provider.ts +383 -0
  59. package/src/{flows/dwn-registration.ts → registration.ts} +61 -6
  60. package/src/types.ts +100 -4
  61. package/src/wallet-connect-client.ts +278 -0
  62. package/dist/esm/flows/dwn-discovery.js.map +0 -1
  63. package/dist/esm/flows/dwn-registration.js.map +0 -1
  64. package/dist/esm/flows/import-identity.js +0 -175
  65. package/dist/esm/flows/import-identity.js.map +0 -1
  66. package/dist/esm/flows/local-connect.js +0 -141
  67. package/dist/esm/flows/local-connect.js.map +0 -1
  68. package/dist/esm/flows/session-restore.js.map +0 -1
  69. package/dist/esm/flows/wallet-connect.js.map +0 -1
  70. package/dist/esm/vault/vault-manager.js +0 -95
  71. package/dist/esm/vault/vault-manager.js.map +0 -1
  72. package/dist/types/flows/dwn-discovery.d.ts.map +0 -1
  73. package/dist/types/flows/dwn-registration.d.ts.map +0 -1
  74. package/dist/types/flows/import-identity.d.ts +0 -35
  75. package/dist/types/flows/import-identity.d.ts.map +0 -1
  76. package/dist/types/flows/local-connect.d.ts +0 -29
  77. package/dist/types/flows/local-connect.d.ts.map +0 -1
  78. package/dist/types/flows/session-restore.d.ts +0 -27
  79. package/dist/types/flows/session-restore.d.ts.map +0 -1
  80. package/dist/types/flows/wallet-connect.d.ts.map +0 -1
  81. package/dist/types/vault/vault-manager.d.ts +0 -57
  82. package/dist/types/vault/vault-manager.d.ts.map +0 -1
  83. package/src/flows/import-identity.ts +0 -217
  84. package/src/flows/local-connect.ts +0 -171
  85. package/src/flows/session-restore.ts +0 -142
  86. package/src/vault/vault-manager.ts +0 -89
@@ -1,142 +0,0 @@
1
- /**
2
- * Session restore flow.
3
- *
4
- * Restores a previously established session from persisted storage,
5
- * replacing the "previouslyConnected" pattern in apps.
6
- * @module
7
- */
8
-
9
- import type { EnboxUserAgent } from '@enbox/agent';
10
-
11
- import type { AuthEventEmitter } from '../events.js';
12
- import type { RestoreSessionOptions, StorageAdapter, SyncOption } from '../types.js';
13
-
14
- import { applyLocalDwnDiscovery } from './dwn-discovery.js';
15
- import { AuthSession } from '../identity-session.js';
16
- import { INSECURE_DEFAULT_PASSWORD, STORAGE_KEYS } from '../types.js';
17
-
18
- /** @internal */
19
- export interface SessionRestoreContext {
20
- userAgent: EnboxUserAgent;
21
- emitter: AuthEventEmitter;
22
- storage: StorageAdapter;
23
- defaultPassword?: string;
24
- defaultSync?: SyncOption;
25
- }
26
-
27
- /**
28
- * Attempt to restore a previous session.
29
- *
30
- * Returns `undefined` if no previous session exists.
31
- * Returns an `AuthSession` if the session was successfully restored.
32
- */
33
- export async function restoreSession(
34
- ctx: SessionRestoreContext,
35
- options: RestoreSessionOptions = {},
36
- ): Promise<AuthSession | undefined> {
37
- const { userAgent, emitter, storage } = ctx;
38
-
39
- // Check if there was a previous session.
40
- const previouslyConnected = await storage.get(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
41
- if (previouslyConnected !== 'true') {
42
- return undefined;
43
- }
44
-
45
- // Resolve password: explicit option → callback → manager default → insecure fallback.
46
- let password = options.password ?? ctx.defaultPassword;
47
-
48
- if (!password && options.onPasswordRequired) {
49
- password = await options.onPasswordRequired();
50
- }
51
-
52
- password ??= INSECURE_DEFAULT_PASSWORD;
53
-
54
- // Warn if using insecure default.
55
- if (password === INSECURE_DEFAULT_PASSWORD) {
56
- console.warn(
57
- '[@enbox/auth] SECURITY WARNING: No password set. Using insecure default. ' +
58
- 'Set a password to protect your identity vault.'
59
- );
60
- }
61
-
62
- // Start the agent (initializes + unlocks vault).
63
- if (await userAgent.firstLaunch()) {
64
- // Vault doesn't exist yet — this shouldn't happen if previouslyConnected is true.
65
- // Clean up the stale flag and return undefined.
66
- await storage.remove(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
67
- return undefined;
68
- }
69
-
70
- await userAgent.start({ password });
71
- emitter.emit('vault-unlocked', {});
72
-
73
- // Apply local DWN discovery (browser redirect payload or persisted endpoint).
74
- await applyLocalDwnDiscovery(userAgent, storage, emitter);
75
-
76
- // Determine which identity to reconnect.
77
- const activeIdentityDid = await storage.get(STORAGE_KEYS.ACTIVE_IDENTITY);
78
- const storedDelegateDid = await storage.get(STORAGE_KEYS.DELEGATE_DID);
79
-
80
- // First try the connected identity (wallet-connected sessions).
81
- let identity = await userAgent.identity.connectedIdentity();
82
-
83
- if (!identity) {
84
- // Try to find the specific active identity.
85
- if (activeIdentityDid) {
86
- identity = await userAgent.identity.get({ didUri: activeIdentityDid });
87
- }
88
-
89
- // Fall back to the first available identity.
90
- if (!identity) {
91
- const identities = await userAgent.identity.list();
92
- identity = identities[0];
93
- }
94
- }
95
-
96
- if (!identity) {
97
- // No identity found — clean up stale session data.
98
- await storage.remove(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
99
- await storage.remove(STORAGE_KEYS.ACTIVE_IDENTITY);
100
- await storage.remove(STORAGE_KEYS.DELEGATE_DID);
101
- await storage.remove(STORAGE_KEYS.CONNECTED_DID);
102
- return undefined;
103
- }
104
-
105
- const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
106
- const delegateDid = identity.metadata.connectedDid
107
- ? identity.did.uri
108
- : (storedDelegateDid ?? undefined);
109
-
110
- // Start sync.
111
- const sync = ctx.defaultSync;
112
- if (sync !== 'off') {
113
- const syncMode = sync === undefined ? 'live' : 'poll';
114
- const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
115
- userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
116
- .catch((err: unknown) => {
117
- console.error('[@enbox/auth] Sync failed:', err);
118
- });
119
- }
120
-
121
- // Update persisted session info.
122
- await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
123
-
124
- const identityInfo = {
125
- didUri : connectedDid,
126
- name : identity.metadata.name,
127
- connectedDid : identity.metadata.connectedDid,
128
- };
129
-
130
- const session = new AuthSession({
131
- agent : userAgent,
132
- did : connectedDid,
133
- delegateDid,
134
- identity : identityInfo,
135
- });
136
-
137
- emitter.emit('session-start', {
138
- session: { did: connectedDid, delegateDid, identity: identityInfo },
139
- });
140
-
141
- return session;
142
- }
@@ -1,89 +0,0 @@
1
- /**
2
- * VaultManager wraps {@link HdIdentityVault} with a high-level API
3
- * and emits events on lock/unlock.
4
- * @module
5
- */
6
-
7
- import type { HdIdentityVault, IdentityVaultBackup } from '@enbox/agent';
8
-
9
- import type { AuthEventEmitter } from '../events.js';
10
-
11
- /**
12
- * Manages the encrypted identity vault lifecycle.
13
- *
14
- * The vault stores the agent's DID and content encryption key (CEK),
15
- * protected by a user password using PBES2-HS512+A256KW with a 210K
16
- * iteration work factor. The vault supports HD key derivation from
17
- * a BIP-39 mnemonic for recovery.
18
- */
19
- export class VaultManager {
20
- private readonly _vault: HdIdentityVault;
21
- private readonly _emitter: AuthEventEmitter;
22
-
23
- constructor(vault: HdIdentityVault, emitter: AuthEventEmitter) {
24
- this._vault = vault;
25
- this._emitter = emitter;
26
- }
27
-
28
- /** The underlying vault instance (for advanced usage). */
29
- get raw(): HdIdentityVault {
30
- return this._vault;
31
- }
32
-
33
- /** Whether the vault has been initialized (has encrypted data). */
34
- async isInitialized(): Promise<boolean> {
35
- return this._vault.isInitialized();
36
- }
37
-
38
- /** Whether the vault is currently locked (synchronous check). */
39
- get isLocked(): boolean {
40
- return this._vault.isLocked();
41
- }
42
-
43
- /**
44
- * Unlock the vault with the given password.
45
- * Decrypts the CEK into memory so the agent DID can be retrieved.
46
- *
47
- * @throws If the password is incorrect or vault is not initialized.
48
- */
49
- async unlock(password: string): Promise<void> {
50
- await this._vault.unlock({ password });
51
- this._emitter.emit('vault-unlocked', {});
52
- }
53
-
54
- /**
55
- * Lock the vault, clearing the CEK from memory.
56
- * After locking, the password must be provided again to unlock.
57
- */
58
- async lock(): Promise<void> {
59
- await this._vault.lock();
60
- this._emitter.emit('vault-locked', {});
61
- }
62
-
63
- /**
64
- * Change the vault password. Re-encrypts the CEK with the new password.
65
- *
66
- * @throws If the old password is incorrect or vault is locked.
67
- */
68
- async changePassword(oldPassword: string, newPassword: string): Promise<void> {
69
- await this._vault.changePassword({ oldPassword, newPassword });
70
- }
71
-
72
- /**
73
- * Create a backup of the vault.
74
- *
75
- * @throws If the vault is not initialized or is locked.
76
- */
77
- async backup(): Promise<IdentityVaultBackup> {
78
- return this._vault.backup();
79
- }
80
-
81
- /**
82
- * Restore the vault from a backup.
83
- *
84
- * @throws If the password doesn't match the backup's encryption.
85
- */
86
- async restore(backup: IdentityVaultBackup, password: string): Promise<void> {
87
- await this._vault.restore({ backup, password });
88
- }
89
- }