@enbox/auth 0.5.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.
- package/dist/esm/auth-manager.js +94 -167
- package/dist/esm/auth-manager.js.map +1 -1
- package/dist/esm/connect/import.js +131 -0
- package/dist/esm/connect/import.js.map +1 -0
- package/dist/esm/connect/lifecycle.js +235 -0
- package/dist/esm/connect/lifecycle.js.map +1 -0
- package/dist/esm/connect/local.js +91 -0
- package/dist/esm/connect/local.js.map +1 -0
- package/dist/esm/{flows/session-restore.js → connect/restore.js} +34 -56
- package/dist/esm/connect/restore.js.map +1 -0
- package/dist/esm/{flows/wallet-connect.js → connect/wallet.js} +28 -35
- package/dist/esm/connect/wallet.js.map +1 -0
- package/dist/esm/{flows/dwn-discovery.js → discovery.js} +2 -2
- package/dist/esm/discovery.js.map +1 -0
- package/dist/esm/index.js +4 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/{flows/dwn-registration.js → registration.js} +2 -2
- package/dist/esm/registration.js.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/wallet-connect-client.js +188 -0
- package/dist/esm/wallet-connect-client.js.map +1 -0
- package/dist/types/auth-manager.d.ts +20 -6
- package/dist/types/auth-manager.d.ts.map +1 -1
- package/dist/types/connect/import.d.ts +25 -0
- package/dist/types/connect/import.d.ts.map +1 -0
- package/dist/types/connect/lifecycle.d.ts +152 -0
- package/dist/types/connect/lifecycle.d.ts.map +1 -0
- package/dist/types/connect/local.d.ts +18 -0
- package/dist/types/connect/local.d.ts.map +1 -0
- package/dist/types/connect/restore.d.ts +18 -0
- package/dist/types/connect/restore.d.ts.map +1 -0
- package/dist/types/{flows/wallet-connect.d.ts → connect/wallet.d.ts} +5 -14
- package/dist/types/connect/wallet.d.ts.map +1 -0
- package/dist/types/{flows/dwn-discovery.d.ts → discovery.d.ts} +3 -3
- package/dist/types/discovery.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/{flows/dwn-registration.d.ts → registration.d.ts} +2 -2
- package/dist/types/registration.d.ts.map +1 -0
- package/dist/types/types.d.ts +6 -3
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/wallet-connect-client.d.ts +89 -0
- package/dist/types/wallet-connect-client.d.ts.map +1 -0
- package/package.json +9 -5
- package/src/auth-manager.ts +94 -188
- package/src/connect/import.ts +148 -0
- package/src/connect/lifecycle.ts +321 -0
- package/src/connect/local.ts +101 -0
- package/src/connect/restore.ts +117 -0
- package/src/{flows/wallet-connect.ts → connect/wallet.ts} +37 -54
- package/src/{flows/dwn-discovery.ts → discovery.ts} +4 -3
- package/src/index.ts +5 -4
- package/src/{flows/dwn-registration.ts → registration.ts} +2 -2
- package/src/types.ts +7 -3
- package/src/wallet-connect-client.ts +278 -0
- package/dist/esm/flows/dwn-discovery.js.map +0 -1
- package/dist/esm/flows/dwn-registration.js.map +0 -1
- package/dist/esm/flows/import-identity.js +0 -177
- package/dist/esm/flows/import-identity.js.map +0 -1
- package/dist/esm/flows/local-connect.js +0 -158
- package/dist/esm/flows/local-connect.js.map +0 -1
- package/dist/esm/flows/session-restore.js.map +0 -1
- package/dist/esm/flows/wallet-connect.js.map +0 -1
- package/dist/esm/vault/vault-manager.js +0 -95
- package/dist/esm/vault/vault-manager.js.map +0 -1
- package/dist/types/flows/dwn-discovery.d.ts.map +0 -1
- package/dist/types/flows/dwn-registration.d.ts.map +0 -1
- package/dist/types/flows/import-identity.d.ts +0 -35
- package/dist/types/flows/import-identity.d.ts.map +0 -1
- package/dist/types/flows/local-connect.d.ts +0 -31
- package/dist/types/flows/local-connect.d.ts.map +0 -1
- package/dist/types/flows/session-restore.d.ts +0 -29
- package/dist/types/flows/session-restore.d.ts.map +0 -1
- package/dist/types/flows/wallet-connect.d.ts.map +0 -1
- package/dist/types/vault/vault-manager.d.ts +0 -57
- package/dist/types/vault/vault-manager.d.ts.map +0 -1
- package/src/flows/import-identity.ts +0 -219
- package/src/flows/local-connect.ts +0 -192
- package/src/flows/session-restore.ts +0 -155
- package/src/vault/vault-manager.ts +0 -89
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* VaultManager wraps {@link HdIdentityVault} with a high-level API
|
|
3
|
-
* and emits events on lock/unlock.
|
|
4
|
-
* @module
|
|
5
|
-
*/
|
|
6
|
-
import type { HdIdentityVault, IdentityVaultBackup } from '@enbox/agent';
|
|
7
|
-
import type { AuthEventEmitter } from '../events.js';
|
|
8
|
-
/**
|
|
9
|
-
* Manages the encrypted identity vault lifecycle.
|
|
10
|
-
*
|
|
11
|
-
* The vault stores the agent's DID and content encryption key (CEK),
|
|
12
|
-
* protected by a user password using PBES2-HS512+A256KW with a 210K
|
|
13
|
-
* iteration work factor. The vault supports HD key derivation from
|
|
14
|
-
* a BIP-39 mnemonic for recovery.
|
|
15
|
-
*/
|
|
16
|
-
export declare class VaultManager {
|
|
17
|
-
private readonly _vault;
|
|
18
|
-
private readonly _emitter;
|
|
19
|
-
constructor(vault: HdIdentityVault, emitter: AuthEventEmitter);
|
|
20
|
-
/** The underlying vault instance (for advanced usage). */
|
|
21
|
-
get raw(): HdIdentityVault;
|
|
22
|
-
/** Whether the vault has been initialized (has encrypted data). */
|
|
23
|
-
isInitialized(): Promise<boolean>;
|
|
24
|
-
/** Whether the vault is currently locked (synchronous check). */
|
|
25
|
-
get isLocked(): boolean;
|
|
26
|
-
/**
|
|
27
|
-
* Unlock the vault with the given password.
|
|
28
|
-
* Decrypts the CEK into memory so the agent DID can be retrieved.
|
|
29
|
-
*
|
|
30
|
-
* @throws If the password is incorrect or vault is not initialized.
|
|
31
|
-
*/
|
|
32
|
-
unlock(password: string): Promise<void>;
|
|
33
|
-
/**
|
|
34
|
-
* Lock the vault, clearing the CEK from memory.
|
|
35
|
-
* After locking, the password must be provided again to unlock.
|
|
36
|
-
*/
|
|
37
|
-
lock(): Promise<void>;
|
|
38
|
-
/**
|
|
39
|
-
* Change the vault password. Re-encrypts the CEK with the new password.
|
|
40
|
-
*
|
|
41
|
-
* @throws If the old password is incorrect or vault is locked.
|
|
42
|
-
*/
|
|
43
|
-
changePassword(oldPassword: string, newPassword: string): Promise<void>;
|
|
44
|
-
/**
|
|
45
|
-
* Create a backup of the vault.
|
|
46
|
-
*
|
|
47
|
-
* @throws If the vault is not initialized or is locked.
|
|
48
|
-
*/
|
|
49
|
-
backup(): Promise<IdentityVaultBackup>;
|
|
50
|
-
/**
|
|
51
|
-
* Restore the vault from a backup.
|
|
52
|
-
*
|
|
53
|
-
* @throws If the password doesn't match the backup's encryption.
|
|
54
|
-
*/
|
|
55
|
-
restore(backup: IdentityVaultBackup, password: string): Promise<void>;
|
|
56
|
-
}
|
|
57
|
-
//# sourceMappingURL=vault-manager.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"vault-manager.d.ts","sourceRoot":"","sources":["../../../src/vault/vault-manager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD;;;;;;;GAOG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkB;IACzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;gBAEhC,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,gBAAgB;IAK7D,0DAA0D;IAC1D,IAAI,GAAG,IAAI,eAAe,CAEzB;IAED,mEAAmE;IAC7D,aAAa,IAAI,OAAO,CAAC,OAAO,CAAC;IAIvC,iEAAiE;IACjE,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED;;;;;OAKG;IACG,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK7C;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAK3B;;;;OAIG;IACG,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7E;;;;OAIG;IACG,MAAM,IAAI,OAAO,CAAC,mBAAmB,CAAC;IAI5C;;;;OAIG;IACG,OAAO,CAAC,MAAM,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAG5E"}
|
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Identity import flows.
|
|
3
|
-
*
|
|
4
|
-
* - Import from BIP-39 recovery phrase (re-derive vault + identity).
|
|
5
|
-
* - Import from PortableIdentity JSON.
|
|
6
|
-
* @module
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { EnboxUserAgent } from '@enbox/agent';
|
|
10
|
-
|
|
11
|
-
import type { AuthEventEmitter } from '../events.js';
|
|
12
|
-
import { AuthSession } from '../identity-session.js';
|
|
13
|
-
import { registerWithDwnEndpoints } from './dwn-registration.js';
|
|
14
|
-
import { STORAGE_KEYS } from '../types.js';
|
|
15
|
-
import type {
|
|
16
|
-
ImportFromPhraseOptions,
|
|
17
|
-
ImportFromPortableOptions,
|
|
18
|
-
RegistrationOptions,
|
|
19
|
-
StorageAdapter,
|
|
20
|
-
SyncOption,
|
|
21
|
-
} from '../types.js';
|
|
22
|
-
|
|
23
|
-
/** @internal */
|
|
24
|
-
export interface ImportContext {
|
|
25
|
-
userAgent: EnboxUserAgent;
|
|
26
|
-
emitter: AuthEventEmitter;
|
|
27
|
-
storage: StorageAdapter;
|
|
28
|
-
defaultSync?: SyncOption;
|
|
29
|
-
defaultDwnEndpoints?: string[];
|
|
30
|
-
registration?: RegistrationOptions;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Import (or recover) an identity from a BIP-39 recovery phrase.
|
|
35
|
-
*
|
|
36
|
-
* This re-initializes the vault with the given phrase and password,
|
|
37
|
-
* recovering the agent DID and all derived keys.
|
|
38
|
-
*/
|
|
39
|
-
export async function importFromPhrase(
|
|
40
|
-
ctx: ImportContext,
|
|
41
|
-
options: ImportFromPhraseOptions,
|
|
42
|
-
): Promise<AuthSession> {
|
|
43
|
-
const { userAgent, emitter, storage } = ctx;
|
|
44
|
-
const { recoveryPhrase, password } = options;
|
|
45
|
-
const sync = options.sync ?? ctx.defaultSync;
|
|
46
|
-
const dwnEndpoints = options.dwnEndpoints ?? ctx.defaultDwnEndpoints ?? ['https://enbox-dwn.fly.dev'];
|
|
47
|
-
|
|
48
|
-
// Initialize the vault with the recovery phrase.
|
|
49
|
-
// This re-derives the same agent DID and CEK from the mnemonic.
|
|
50
|
-
if (await userAgent.firstLaunch()) {
|
|
51
|
-
await userAgent.initialize({
|
|
52
|
-
password,
|
|
53
|
-
recoveryPhrase,
|
|
54
|
-
dwnEndpoints,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
await userAgent.start({ password });
|
|
59
|
-
emitter.emit('vault-unlocked', {});
|
|
60
|
-
|
|
61
|
-
// The recovery phrase re-derives the same agent DID,
|
|
62
|
-
// but the user identity might not exist yet — create one if needed.
|
|
63
|
-
const identities = await userAgent.identity.list();
|
|
64
|
-
let identity = identities[0];
|
|
65
|
-
let isNewIdentity = false;
|
|
66
|
-
|
|
67
|
-
if (!identity) {
|
|
68
|
-
isNewIdentity = true;
|
|
69
|
-
identity = await userAgent.identity.create({
|
|
70
|
-
didMethod : 'dht',
|
|
71
|
-
metadata : { name: 'Default' },
|
|
72
|
-
didOptions : {
|
|
73
|
-
services: [
|
|
74
|
-
{
|
|
75
|
-
id : 'dwn',
|
|
76
|
-
type : 'DecentralizedWebNode',
|
|
77
|
-
serviceEndpoint : dwnEndpoints,
|
|
78
|
-
enc : '#enc',
|
|
79
|
-
sig : '#sig',
|
|
80
|
-
}
|
|
81
|
-
],
|
|
82
|
-
verificationMethods: [
|
|
83
|
-
{
|
|
84
|
-
algorithm : 'Ed25519',
|
|
85
|
-
id : 'sig',
|
|
86
|
-
purposes : ['assertionMethod', 'authentication'],
|
|
87
|
-
},
|
|
88
|
-
{
|
|
89
|
-
algorithm : 'X25519',
|
|
90
|
-
id : 'enc',
|
|
91
|
-
purposes : ['keyAgreement'],
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const connectedDid = identity.did.uri;
|
|
99
|
-
|
|
100
|
-
// Register with DWN endpoints (if registration options are provided).
|
|
101
|
-
if (ctx.registration) {
|
|
102
|
-
await registerWithDwnEndpoints(
|
|
103
|
-
{
|
|
104
|
-
userAgent : userAgent,
|
|
105
|
-
dwnEndpoints,
|
|
106
|
-
agentDid : userAgent.agentDid.uri,
|
|
107
|
-
connectedDid,
|
|
108
|
-
storage : storage,
|
|
109
|
-
},
|
|
110
|
-
ctx.registration,
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Register and start sync.
|
|
115
|
-
if (isNewIdentity && sync !== 'off') {
|
|
116
|
-
await userAgent.sync.registerIdentity({ did: connectedDid, options: { protocols: [] } });
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (sync !== 'off') {
|
|
120
|
-
const syncMode = sync === undefined ? 'live' : 'poll';
|
|
121
|
-
const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
|
|
122
|
-
userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
|
|
123
|
-
.catch((err: unknown) => console.error('[@enbox/auth] Sync failed:', err));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
await storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
|
|
127
|
-
await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
|
|
128
|
-
|
|
129
|
-
const identityInfo = {
|
|
130
|
-
didUri : connectedDid,
|
|
131
|
-
name : identity.metadata.name,
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const session = new AuthSession({
|
|
135
|
-
agent : userAgent,
|
|
136
|
-
did : connectedDid,
|
|
137
|
-
identity : identityInfo,
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
emitter.emit('identity-added', { identity: identityInfo });
|
|
141
|
-
emitter.emit('session-start', {
|
|
142
|
-
session: { did: connectedDid, identity: identityInfo },
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
return session;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Import an identity from a PortableIdentity JSON object.
|
|
150
|
-
*
|
|
151
|
-
* The portable identity contains the DID's private keys and metadata,
|
|
152
|
-
* allowing it to be used on this device.
|
|
153
|
-
*/
|
|
154
|
-
export async function importFromPortable(
|
|
155
|
-
ctx: ImportContext,
|
|
156
|
-
options: ImportFromPortableOptions,
|
|
157
|
-
): Promise<AuthSession> {
|
|
158
|
-
const { userAgent, emitter, storage } = ctx;
|
|
159
|
-
const sync = options.sync ?? ctx.defaultSync;
|
|
160
|
-
|
|
161
|
-
const identity = await userAgent.identity.import({
|
|
162
|
-
portableIdentity: options.portableIdentity,
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
|
|
166
|
-
const delegateDid = identity.metadata.connectedDid ? identity.did.uri : undefined;
|
|
167
|
-
|
|
168
|
-
// Register with DWN endpoints (if registration options are provided).
|
|
169
|
-
// For portable imports, extract endpoints from the DID document's DWN service.
|
|
170
|
-
if (ctx.registration) {
|
|
171
|
-
const dwnEndpoints = ctx.defaultDwnEndpoints ?? ['https://enbox-dwn.fly.dev'];
|
|
172
|
-
await registerWithDwnEndpoints(
|
|
173
|
-
{
|
|
174
|
-
userAgent : userAgent,
|
|
175
|
-
dwnEndpoints,
|
|
176
|
-
agentDid : userAgent.agentDid.uri,
|
|
177
|
-
connectedDid,
|
|
178
|
-
storage : storage,
|
|
179
|
-
},
|
|
180
|
-
ctx.registration,
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Register and start sync.
|
|
185
|
-
if (sync !== 'off') {
|
|
186
|
-
await userAgent.sync.registerIdentity({
|
|
187
|
-
did : connectedDid,
|
|
188
|
-
options : { delegateDid, protocols: [] },
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
const syncMode = sync === undefined ? 'live' : 'poll';
|
|
192
|
-
const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
|
|
193
|
-
userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
|
|
194
|
-
.catch((err: unknown) => console.error('[@enbox/auth] Sync failed:', err));
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
await storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
|
|
198
|
-
await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
|
|
199
|
-
|
|
200
|
-
const identityInfo = {
|
|
201
|
-
didUri : connectedDid,
|
|
202
|
-
name : identity.metadata.name,
|
|
203
|
-
connectedDid : identity.metadata.connectedDid,
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
const session = new AuthSession({
|
|
207
|
-
agent : userAgent,
|
|
208
|
-
did : connectedDid,
|
|
209
|
-
delegateDid,
|
|
210
|
-
identity : identityInfo,
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
emitter.emit('identity-added', { identity: identityInfo });
|
|
214
|
-
emitter.emit('session-start', {
|
|
215
|
-
session: { did: connectedDid, delegateDid, identity: identityInfo },
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
return session;
|
|
219
|
-
}
|
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local DID connect flow.
|
|
3
|
-
*
|
|
4
|
-
* Creates or reconnects a local identity with vault-protected keys.
|
|
5
|
-
* This replaces the "Mode D/E" paths in Enbox.connect().
|
|
6
|
-
* @module
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { EnboxUserAgent } from '@enbox/agent';
|
|
10
|
-
|
|
11
|
-
import type { AuthEventEmitter } from '../events.js';
|
|
12
|
-
import type { PasswordProvider } from '../password-provider.js';
|
|
13
|
-
import type { LocalConnectOptions, RegistrationOptions, StorageAdapter, SyncOption } from '../types.js';
|
|
14
|
-
|
|
15
|
-
import { applyLocalDwnDiscovery } from './dwn-discovery.js';
|
|
16
|
-
import { AuthSession } from '../identity-session.js';
|
|
17
|
-
import { registerWithDwnEndpoints } from './dwn-registration.js';
|
|
18
|
-
import { INSECURE_DEFAULT_PASSWORD, STORAGE_KEYS } from '../types.js';
|
|
19
|
-
|
|
20
|
-
/** @internal */
|
|
21
|
-
export interface LocalConnectContext {
|
|
22
|
-
userAgent: EnboxUserAgent;
|
|
23
|
-
emitter: AuthEventEmitter;
|
|
24
|
-
storage: StorageAdapter;
|
|
25
|
-
defaultPassword?: string;
|
|
26
|
-
passwordProvider?: PasswordProvider;
|
|
27
|
-
defaultSync?: SyncOption;
|
|
28
|
-
defaultDwnEndpoints?: string[];
|
|
29
|
-
registration?: RegistrationOptions;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Execute the local connect flow.
|
|
34
|
-
*
|
|
35
|
-
* - On first launch: initializes the vault, creates a new DID, returns recovery phrase.
|
|
36
|
-
* - On subsequent launches: unlocks the vault and reconnects to the existing identity.
|
|
37
|
-
*/
|
|
38
|
-
export async function localConnect(
|
|
39
|
-
ctx: LocalConnectContext,
|
|
40
|
-
options: LocalConnectOptions = {},
|
|
41
|
-
): Promise<AuthSession> {
|
|
42
|
-
const { userAgent, emitter, storage } = ctx;
|
|
43
|
-
|
|
44
|
-
// Resolve password: explicit option → provider → manager default → insecure fallback.
|
|
45
|
-
const isFirstLaunch = await userAgent.firstLaunch();
|
|
46
|
-
let password = options.password ?? ctx.defaultPassword;
|
|
47
|
-
|
|
48
|
-
if (!password && ctx.passwordProvider) {
|
|
49
|
-
try {
|
|
50
|
-
password = await ctx.passwordProvider.getPassword({
|
|
51
|
-
reason: isFirstLaunch ? 'create' : 'unlock',
|
|
52
|
-
});
|
|
53
|
-
} catch {
|
|
54
|
-
// Provider failed — fall through to insecure default.
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
password ??= INSECURE_DEFAULT_PASSWORD;
|
|
59
|
-
|
|
60
|
-
const sync = options.sync ?? ctx.defaultSync;
|
|
61
|
-
const dwnEndpoints = options.dwnEndpoints ?? ctx.defaultDwnEndpoints ?? ['https://enbox-dwn.fly.dev'];
|
|
62
|
-
|
|
63
|
-
// Warn if using insecure default.
|
|
64
|
-
if (password === INSECURE_DEFAULT_PASSWORD) {
|
|
65
|
-
console.warn(
|
|
66
|
-
'[@enbox/auth] SECURITY WARNING: No password set. Using insecure default. ' +
|
|
67
|
-
'Set a password via AuthManager.create({ password }) or connect({ password }) ' +
|
|
68
|
-
'to protect your identity vault.'
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
let recoveryPhrase: string | undefined;
|
|
73
|
-
|
|
74
|
-
// Initialize vault on first launch.
|
|
75
|
-
if (isFirstLaunch) {
|
|
76
|
-
recoveryPhrase = await userAgent.initialize({
|
|
77
|
-
password,
|
|
78
|
-
recoveryPhrase: options.recoveryPhrase,
|
|
79
|
-
dwnEndpoints,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Start the agent (unlocks vault if locked, sets agentDid).
|
|
84
|
-
await userAgent.start({ password });
|
|
85
|
-
emitter.emit('vault-unlocked', {});
|
|
86
|
-
|
|
87
|
-
// Apply local DWN discovery (browser redirect payload or persisted endpoint).
|
|
88
|
-
// In remote mode, discovery already ran before agent creation — skip.
|
|
89
|
-
if (!userAgent.dwn.isRemoteMode) {
|
|
90
|
-
await applyLocalDwnDiscovery(userAgent, storage, emitter);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Find or create the user identity.
|
|
94
|
-
const identities = await userAgent.identity.list();
|
|
95
|
-
let identity = identities[0];
|
|
96
|
-
let isNewIdentity = false;
|
|
97
|
-
|
|
98
|
-
if (!identity) {
|
|
99
|
-
isNewIdentity = true;
|
|
100
|
-
identity = await userAgent.identity.create({
|
|
101
|
-
didMethod : 'dht',
|
|
102
|
-
metadata : { name: options.metadata?.name ?? 'Default' },
|
|
103
|
-
didOptions : {
|
|
104
|
-
services: [
|
|
105
|
-
{
|
|
106
|
-
id : 'dwn',
|
|
107
|
-
type : 'DecentralizedWebNode',
|
|
108
|
-
serviceEndpoint : dwnEndpoints,
|
|
109
|
-
enc : '#enc',
|
|
110
|
-
sig : '#sig',
|
|
111
|
-
}
|
|
112
|
-
],
|
|
113
|
-
verificationMethods: [
|
|
114
|
-
{
|
|
115
|
-
algorithm : 'Ed25519',
|
|
116
|
-
id : 'sig',
|
|
117
|
-
purposes : ['assertionMethod', 'authentication'],
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
algorithm : 'X25519',
|
|
121
|
-
id : 'enc',
|
|
122
|
-
purposes : ['keyAgreement'],
|
|
123
|
-
},
|
|
124
|
-
],
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
|
|
130
|
-
const delegateDid = identity.metadata.connectedDid ? identity.did.uri : undefined;
|
|
131
|
-
|
|
132
|
-
// Register with DWN endpoints (if registration options are provided).
|
|
133
|
-
if (ctx.registration) {
|
|
134
|
-
await registerWithDwnEndpoints(
|
|
135
|
-
{
|
|
136
|
-
userAgent : userAgent,
|
|
137
|
-
dwnEndpoints,
|
|
138
|
-
agentDid : userAgent.agentDid.uri,
|
|
139
|
-
connectedDid,
|
|
140
|
-
storage : storage,
|
|
141
|
-
},
|
|
142
|
-
ctx.registration,
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Register sync for new identities.
|
|
147
|
-
if (isNewIdentity && sync !== 'off') {
|
|
148
|
-
await userAgent.sync.registerIdentity({
|
|
149
|
-
did : connectedDid,
|
|
150
|
-
options : { delegateDid, protocols: [] },
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Start sync.
|
|
155
|
-
if (sync !== 'off') {
|
|
156
|
-
const syncMode = sync === undefined ? 'live' : 'poll';
|
|
157
|
-
const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
|
|
158
|
-
userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
|
|
159
|
-
.catch((error: unknown) => {
|
|
160
|
-
console.error('[@enbox/auth] Sync failed:', error);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Persist session info.
|
|
165
|
-
await storage.set(STORAGE_KEYS.PREVIOUSLY_CONNECTED, 'true');
|
|
166
|
-
await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
|
|
167
|
-
|
|
168
|
-
const identityInfo = {
|
|
169
|
-
didUri : connectedDid,
|
|
170
|
-
name : identity.metadata.name,
|
|
171
|
-
connectedDid : identity.metadata.connectedDid,
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
const session = new AuthSession({
|
|
175
|
-
agent : userAgent,
|
|
176
|
-
did : connectedDid,
|
|
177
|
-
delegateDid,
|
|
178
|
-
recoveryPhrase,
|
|
179
|
-
identity : identityInfo,
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
emitter.emit('identity-added', { identity: identityInfo });
|
|
183
|
-
emitter.emit('session-start', {
|
|
184
|
-
session: {
|
|
185
|
-
did : session.did,
|
|
186
|
-
delegateDid,
|
|
187
|
-
identity : identityInfo,
|
|
188
|
-
},
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
return session;
|
|
192
|
-
}
|
|
@@ -1,155 +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 { PasswordProvider } from '../password-provider.js';
|
|
13
|
-
import type { RestoreSessionOptions, StorageAdapter, SyncOption } from '../types.js';
|
|
14
|
-
|
|
15
|
-
import { applyLocalDwnDiscovery } from './dwn-discovery.js';
|
|
16
|
-
import { AuthSession } from '../identity-session.js';
|
|
17
|
-
import { INSECURE_DEFAULT_PASSWORD, STORAGE_KEYS } from '../types.js';
|
|
18
|
-
|
|
19
|
-
/** @internal */
|
|
20
|
-
export interface SessionRestoreContext {
|
|
21
|
-
userAgent: EnboxUserAgent;
|
|
22
|
-
emitter: AuthEventEmitter;
|
|
23
|
-
storage: StorageAdapter;
|
|
24
|
-
defaultPassword?: string;
|
|
25
|
-
passwordProvider?: PasswordProvider;
|
|
26
|
-
defaultSync?: SyncOption;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Attempt to restore a previous session.
|
|
31
|
-
*
|
|
32
|
-
* Returns `undefined` if no previous session exists.
|
|
33
|
-
* Returns an `AuthSession` if the session was successfully restored.
|
|
34
|
-
*/
|
|
35
|
-
export async function restoreSession(
|
|
36
|
-
ctx: SessionRestoreContext,
|
|
37
|
-
options: RestoreSessionOptions = {},
|
|
38
|
-
): Promise<AuthSession | undefined> {
|
|
39
|
-
const { userAgent, emitter, storage } = ctx;
|
|
40
|
-
|
|
41
|
-
// Check if there was a previous session.
|
|
42
|
-
const previouslyConnected = await storage.get(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
|
|
43
|
-
if (previouslyConnected !== 'true') {
|
|
44
|
-
return undefined;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Resolve password: explicit option → callback → provider → manager default → insecure fallback.
|
|
48
|
-
let password = options.password ?? ctx.defaultPassword;
|
|
49
|
-
|
|
50
|
-
if (!password && options.onPasswordRequired) {
|
|
51
|
-
password = await options.onPasswordRequired();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (!password && ctx.passwordProvider) {
|
|
55
|
-
try {
|
|
56
|
-
password = await ctx.passwordProvider.getPassword({ reason: 'unlock' });
|
|
57
|
-
} catch {
|
|
58
|
-
// Provider failed — fall through to insecure default.
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
password ??= INSECURE_DEFAULT_PASSWORD;
|
|
63
|
-
|
|
64
|
-
// Warn if using insecure default.
|
|
65
|
-
if (password === INSECURE_DEFAULT_PASSWORD) {
|
|
66
|
-
console.warn(
|
|
67
|
-
'[@enbox/auth] SECURITY WARNING: No password set. Using insecure default. ' +
|
|
68
|
-
'Set a password to protect your identity vault.'
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Start the agent (initializes + unlocks vault).
|
|
73
|
-
if (await userAgent.firstLaunch()) {
|
|
74
|
-
// Vault doesn't exist yet — this shouldn't happen if previouslyConnected is true.
|
|
75
|
-
// Clean up the stale flag and return undefined.
|
|
76
|
-
await storage.remove(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
|
|
77
|
-
return undefined;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
await userAgent.start({ password });
|
|
81
|
-
emitter.emit('vault-unlocked', {});
|
|
82
|
-
|
|
83
|
-
// Apply local DWN discovery (browser redirect payload or persisted endpoint).
|
|
84
|
-
// In remote mode, discovery already ran before agent creation — skip.
|
|
85
|
-
if (!userAgent.dwn.isRemoteMode) {
|
|
86
|
-
await applyLocalDwnDiscovery(userAgent, storage, emitter);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Determine which identity to reconnect.
|
|
90
|
-
const activeIdentityDid = await storage.get(STORAGE_KEYS.ACTIVE_IDENTITY);
|
|
91
|
-
const storedDelegateDid = await storage.get(STORAGE_KEYS.DELEGATE_DID);
|
|
92
|
-
|
|
93
|
-
// First try the connected identity (wallet-connected sessions).
|
|
94
|
-
let identity = await userAgent.identity.connectedIdentity();
|
|
95
|
-
|
|
96
|
-
if (!identity) {
|
|
97
|
-
// Try to find the specific active identity.
|
|
98
|
-
if (activeIdentityDid) {
|
|
99
|
-
identity = await userAgent.identity.get({ didUri: activeIdentityDid });
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Fall back to the first available identity.
|
|
103
|
-
if (!identity) {
|
|
104
|
-
const identities = await userAgent.identity.list();
|
|
105
|
-
identity = identities[0];
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (!identity) {
|
|
110
|
-
// No identity found — clean up stale session data.
|
|
111
|
-
await storage.remove(STORAGE_KEYS.PREVIOUSLY_CONNECTED);
|
|
112
|
-
await storage.remove(STORAGE_KEYS.ACTIVE_IDENTITY);
|
|
113
|
-
await storage.remove(STORAGE_KEYS.DELEGATE_DID);
|
|
114
|
-
await storage.remove(STORAGE_KEYS.CONNECTED_DID);
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const connectedDid = identity.metadata.connectedDid ?? identity.did.uri;
|
|
119
|
-
const delegateDid = identity.metadata.connectedDid
|
|
120
|
-
? identity.did.uri
|
|
121
|
-
: (storedDelegateDid ?? undefined);
|
|
122
|
-
|
|
123
|
-
// Start sync.
|
|
124
|
-
const sync = ctx.defaultSync;
|
|
125
|
-
if (sync !== 'off') {
|
|
126
|
-
const syncMode = sync === undefined ? 'live' : 'poll';
|
|
127
|
-
const syncInterval = sync ?? (syncMode === 'live' ? '5m' : '2m');
|
|
128
|
-
userAgent.sync.startSync({ mode: syncMode, interval: syncInterval })
|
|
129
|
-
.catch((err: unknown) => {
|
|
130
|
-
console.error('[@enbox/auth] Sync failed:', err);
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Update persisted session info.
|
|
135
|
-
await storage.set(STORAGE_KEYS.ACTIVE_IDENTITY, connectedDid);
|
|
136
|
-
|
|
137
|
-
const identityInfo = {
|
|
138
|
-
didUri : connectedDid,
|
|
139
|
-
name : identity.metadata.name,
|
|
140
|
-
connectedDid : identity.metadata.connectedDid,
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
const session = new AuthSession({
|
|
144
|
-
agent : userAgent,
|
|
145
|
-
did : connectedDid,
|
|
146
|
-
delegateDid,
|
|
147
|
-
identity : identityInfo,
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
emitter.emit('session-start', {
|
|
151
|
-
session: { did: connectedDid, delegateDid, identity: identityInfo },
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
return session;
|
|
155
|
-
}
|