@tinycloudlabs/node-sdk 1.0.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/LICENSE.md +320 -0
- package/dist/DelegatedAccess.d.ts +33 -0
- package/dist/DelegatedAccess.d.ts.map +1 -0
- package/dist/DelegatedAccess.js +61 -0
- package/dist/DelegatedAccess.js.map +1 -0
- package/dist/TinyCloudNode.d.ts +441 -0
- package/dist/TinyCloudNode.d.ts.map +1 -0
- package/dist/TinyCloudNode.js +987 -0
- package/dist/TinyCloudNode.js.map +1 -0
- package/dist/authorization/NodeUserAuthorization.d.ts +200 -0
- package/dist/authorization/NodeUserAuthorization.d.ts.map +1 -0
- package/dist/authorization/NodeUserAuthorization.js +516 -0
- package/dist/authorization/NodeUserAuthorization.js.map +1 -0
- package/dist/authorization/strategies.d.ts +57 -0
- package/dist/authorization/strategies.d.ts.map +1 -0
- package/dist/authorization/strategies.js +15 -0
- package/dist/authorization/strategies.js.map +1 -0
- package/dist/delegation.d.ts +35 -0
- package/dist/delegation.d.ts.map +1 -0
- package/dist/delegation.js +21 -0
- package/dist/delegation.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/dist/keys/WasmKeyProvider.d.ts +101 -0
- package/dist/keys/WasmKeyProvider.d.ts.map +1 -0
- package/dist/keys/WasmKeyProvider.js +113 -0
- package/dist/keys/WasmKeyProvider.js.map +1 -0
- package/dist/keys/index.d.ts +7 -0
- package/dist/keys/index.d.ts.map +1 -0
- package/dist/keys/index.js +7 -0
- package/dist/keys/index.js.map +1 -0
- package/dist/signers/PrivateKeySigner.d.ts +47 -0
- package/dist/signers/PrivateKeySigner.d.ts.map +1 -0
- package/dist/signers/PrivateKeySigner.js +89 -0
- package/dist/signers/PrivateKeySigner.js.map +1 -0
- package/dist/storage/FileSessionStorage.d.ts +59 -0
- package/dist/storage/FileSessionStorage.d.ts.map +1 -0
- package/dist/storage/FileSessionStorage.js +148 -0
- package/dist/storage/FileSessionStorage.js.map +1 -0
- package/dist/storage/MemorySessionStorage.d.ts +49 -0
- package/dist/storage/MemorySessionStorage.d.ts.map +1 -0
- package/dist/storage/MemorySessionStorage.js +88 -0
- package/dist/storage/MemorySessionStorage.js.map +1 -0
- package/package.json +43 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import { fetchPeerId, submitHostDelegation, activateSessionWithHost, checkNodeVersion, } from "@tinycloudlabs/sdk-core";
|
|
2
|
+
import { TCWSessionManager as SessionManager, prepareSession, completeSessionSetup, ensureEip55, makeSpaceId, initPanicHook, generateHostSIWEMessage, siweToDelegationHeaders, protocolVersion, } from "@tinycloudlabs/node-sdk-wasm";
|
|
3
|
+
import { defaultSignStrategy, } from "./strategies";
|
|
4
|
+
import { MemorySessionStorage } from "../storage/MemorySessionStorage";
|
|
5
|
+
/**
|
|
6
|
+
* Node.js implementation of IUserAuthorization.
|
|
7
|
+
*
|
|
8
|
+
* Supports multiple sign strategies for different use cases:
|
|
9
|
+
* - auto-sign: Automatically approve all sign requests (trusted backends)
|
|
10
|
+
* - auto-reject: Reject all sign requests (read-only mode)
|
|
11
|
+
* - callback: Delegate to a custom callback function (CLI prompts)
|
|
12
|
+
* - event-emitter: Emit sign requests as events (async workflows)
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Auto-sign for backend services
|
|
17
|
+
* const auth = new NodeUserAuthorization({
|
|
18
|
+
* signer: new PrivateKeySigner(process.env.PRIVATE_KEY),
|
|
19
|
+
* signStrategy: { type: 'auto-sign' },
|
|
20
|
+
* domain: 'api.myapp.com',
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Callback for CLI prompts
|
|
24
|
+
* const auth = new NodeUserAuthorization({
|
|
25
|
+
* signer,
|
|
26
|
+
* signStrategy: {
|
|
27
|
+
* type: 'callback',
|
|
28
|
+
* handler: async (req) => {
|
|
29
|
+
* const approved = await promptUser(`Sign for ${req.address}?`);
|
|
30
|
+
* return { approved };
|
|
31
|
+
* }
|
|
32
|
+
* },
|
|
33
|
+
* domain: 'cli.myapp.com',
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export class NodeUserAuthorization {
|
|
38
|
+
constructor(config) {
|
|
39
|
+
this.extensions = [];
|
|
40
|
+
// Initialize WASM panic hook once (improves error messages from WASM)
|
|
41
|
+
if (!NodeUserAuthorization.wasmInitialized) {
|
|
42
|
+
initPanicHook();
|
|
43
|
+
NodeUserAuthorization.wasmInitialized = true;
|
|
44
|
+
}
|
|
45
|
+
this.signer = config.signer;
|
|
46
|
+
this.signStrategy = config.signStrategy ?? defaultSignStrategy;
|
|
47
|
+
this.sessionStorage = config.sessionStorage ?? new MemorySessionStorage();
|
|
48
|
+
this.domain = config.domain;
|
|
49
|
+
this.uri = config.uri ?? `https://${config.domain}`;
|
|
50
|
+
this.statement = config.statement;
|
|
51
|
+
this.spacePrefix = config.spacePrefix ?? "default";
|
|
52
|
+
this.defaultActions = config.defaultActions ?? {
|
|
53
|
+
kv: {
|
|
54
|
+
"": [
|
|
55
|
+
"tinycloud.kv/put",
|
|
56
|
+
"tinycloud.kv/get",
|
|
57
|
+
"tinycloud.kv/del",
|
|
58
|
+
"tinycloud.kv/list",
|
|
59
|
+
"tinycloud.kv/metadata",
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
capabilities: {
|
|
63
|
+
"": ["tinycloud.capabilities/read"],
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
this.sessionExpirationMs = config.sessionExpirationMs ?? 60 * 60 * 1000;
|
|
67
|
+
this.autoCreateSpace = config.autoCreateSpace ?? false;
|
|
68
|
+
this.tinycloudHosts = config.tinycloudHosts ?? ["https://node.tinycloud.xyz"];
|
|
69
|
+
// Initialize session manager
|
|
70
|
+
this.sessionManager = new SessionManager();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The current active session (web-core compatible).
|
|
74
|
+
*/
|
|
75
|
+
get session() {
|
|
76
|
+
return this._session;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The current TinyCloud session with full delegation data.
|
|
80
|
+
* Includes spaceId, delegationHeader, and delegationCid.
|
|
81
|
+
*/
|
|
82
|
+
get tinyCloudSession() {
|
|
83
|
+
return this._tinyCloudSession;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Add an extension to the authorization flow.
|
|
87
|
+
*/
|
|
88
|
+
extend(extension) {
|
|
89
|
+
this.extensions.push(extension);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get the space ID for the current session.
|
|
93
|
+
*/
|
|
94
|
+
getSpaceId() {
|
|
95
|
+
return this._tinyCloudSession?.spaceId;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Create the space on the TinyCloud server (host delegation).
|
|
99
|
+
* This registers the user as the owner of the space.
|
|
100
|
+
*/
|
|
101
|
+
async hostSpace() {
|
|
102
|
+
if (!this._tinyCloudSession || !this._address || !this._chainId) {
|
|
103
|
+
throw new Error("Must be signed in to host space");
|
|
104
|
+
}
|
|
105
|
+
const host = this.tinycloudHosts[0];
|
|
106
|
+
const spaceId = this._tinyCloudSession.spaceId;
|
|
107
|
+
// Get peer ID from TinyCloud server
|
|
108
|
+
const peerId = await fetchPeerId(host, spaceId);
|
|
109
|
+
// Generate host SIWE message
|
|
110
|
+
const siwe = generateHostSIWEMessage({
|
|
111
|
+
address: this._address,
|
|
112
|
+
chainId: this._chainId,
|
|
113
|
+
domain: this.domain,
|
|
114
|
+
issuedAt: new Date().toISOString(),
|
|
115
|
+
spaceId,
|
|
116
|
+
peerId,
|
|
117
|
+
});
|
|
118
|
+
// Sign the message
|
|
119
|
+
const signature = await this.signMessage(siwe);
|
|
120
|
+
// Convert to delegation headers and submit
|
|
121
|
+
const headers = siweToDelegationHeaders({ siwe, signature });
|
|
122
|
+
const result = await submitHostDelegation(host, headers);
|
|
123
|
+
return result.success;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Ensure the user's space exists on the TinyCloud server.
|
|
127
|
+
* Creates the space if it doesn't exist and autoCreateSpace is enabled.
|
|
128
|
+
* If autoCreateSpace is false and space doesn't exist, silently returns
|
|
129
|
+
* (user may be using delegations to access other spaces).
|
|
130
|
+
*
|
|
131
|
+
* @throws Error if space creation fails
|
|
132
|
+
*/
|
|
133
|
+
async ensureSpaceExists() {
|
|
134
|
+
if (!this._tinyCloudSession) {
|
|
135
|
+
throw new Error("Must be signed in to ensure space exists");
|
|
136
|
+
}
|
|
137
|
+
const host = this.tinycloudHosts[0];
|
|
138
|
+
// Try to activate the session (this checks if space exists)
|
|
139
|
+
const result = await activateSessionWithHost(host, this._tinyCloudSession.delegationHeader);
|
|
140
|
+
if (result.success) {
|
|
141
|
+
// Space exists and session is activated
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (result.status === 404) {
|
|
145
|
+
// Space doesn't exist
|
|
146
|
+
if (!this.autoCreateSpace) {
|
|
147
|
+
// User didn't request space creation - silently return.
|
|
148
|
+
// They may be using delegations to access other spaces.
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
// Create the space
|
|
152
|
+
const created = await this.hostSpace();
|
|
153
|
+
if (!created) {
|
|
154
|
+
throw new Error(`Failed to create space: ${this._tinyCloudSession.spaceId}`);
|
|
155
|
+
}
|
|
156
|
+
// Small delay to allow space creation to propagate
|
|
157
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
158
|
+
// Retry activation after creating space
|
|
159
|
+
const retryResult = await activateSessionWithHost(host, this._tinyCloudSession.delegationHeader);
|
|
160
|
+
if (!retryResult.success) {
|
|
161
|
+
throw new Error(`Failed to activate session after creating space: ${retryResult.error}`);
|
|
162
|
+
}
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
// Other error
|
|
166
|
+
throw new Error(`Failed to activate session: ${result.error}`);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Sign in and create a new session.
|
|
170
|
+
*
|
|
171
|
+
* This follows the correct SIWE-ReCap flow:
|
|
172
|
+
* 1. Create session key and get JWK
|
|
173
|
+
* 2. Call prepareSession() which generates the SIWE with ReCap capabilities
|
|
174
|
+
* 3. Sign the SIWE string from prepareSession
|
|
175
|
+
* 4. Call completeSessionSetup() with the prepared session + signature
|
|
176
|
+
*/
|
|
177
|
+
async signIn() {
|
|
178
|
+
// Get signer address and chain ID
|
|
179
|
+
this._address = await this.signer.getAddress();
|
|
180
|
+
this._chainId = await this.signer.getChainId();
|
|
181
|
+
const address = ensureEip55(this._address);
|
|
182
|
+
const chainId = this._chainId;
|
|
183
|
+
// Create a session key
|
|
184
|
+
const keyId = `session-${Date.now()}`;
|
|
185
|
+
this.sessionManager.renameSessionKeyId("default", keyId);
|
|
186
|
+
// Get JWK for session key
|
|
187
|
+
const jwkString = this.sessionManager.jwk(keyId);
|
|
188
|
+
if (!jwkString) {
|
|
189
|
+
throw new Error("Failed to create session key");
|
|
190
|
+
}
|
|
191
|
+
const jwk = JSON.parse(jwkString);
|
|
192
|
+
// Create space ID
|
|
193
|
+
const spaceId = makeSpaceId(address, chainId, this.spacePrefix);
|
|
194
|
+
const now = new Date();
|
|
195
|
+
const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
|
|
196
|
+
// Prepare session - this creates the SIWE message with ReCap capabilities
|
|
197
|
+
const prepared = prepareSession({
|
|
198
|
+
abilities: this.defaultActions,
|
|
199
|
+
address,
|
|
200
|
+
chainId,
|
|
201
|
+
domain: this.domain,
|
|
202
|
+
issuedAt: now.toISOString(),
|
|
203
|
+
expirationTime: expirationTime.toISOString(),
|
|
204
|
+
spaceId,
|
|
205
|
+
jwk,
|
|
206
|
+
});
|
|
207
|
+
// Sign the SIWE message from prepareSession (NOT a separately generated SIWE)
|
|
208
|
+
const signature = await this.requestSignature({
|
|
209
|
+
address,
|
|
210
|
+
chainId,
|
|
211
|
+
message: prepared.siwe,
|
|
212
|
+
type: "siwe",
|
|
213
|
+
});
|
|
214
|
+
// Complete session setup with the prepared session + signature
|
|
215
|
+
const session = completeSessionSetup({
|
|
216
|
+
...prepared,
|
|
217
|
+
signature,
|
|
218
|
+
});
|
|
219
|
+
// Create client session (web-core compatible)
|
|
220
|
+
const clientSession = {
|
|
221
|
+
address,
|
|
222
|
+
walletAddress: address,
|
|
223
|
+
chainId,
|
|
224
|
+
sessionKey: keyId,
|
|
225
|
+
siwe: prepared.siwe,
|
|
226
|
+
signature,
|
|
227
|
+
};
|
|
228
|
+
// Create TinyCloud session with full delegation data
|
|
229
|
+
// Use sessionManager.getDID(keyId) for verificationMethod to get properly formatted DID URL
|
|
230
|
+
// The prepared.verificationMethod from Rust WASM has a bug that doubles the DID fragment
|
|
231
|
+
const tinyCloudSession = {
|
|
232
|
+
address,
|
|
233
|
+
chainId,
|
|
234
|
+
sessionKey: keyId,
|
|
235
|
+
spaceId,
|
|
236
|
+
delegationCid: session.delegationCid,
|
|
237
|
+
delegationHeader: session.delegationHeader,
|
|
238
|
+
verificationMethod: this.sessionManager.getDID(keyId),
|
|
239
|
+
jwk,
|
|
240
|
+
siwe: prepared.siwe,
|
|
241
|
+
signature,
|
|
242
|
+
};
|
|
243
|
+
// Persist session with TinyCloud-specific data
|
|
244
|
+
const persistedData = {
|
|
245
|
+
address,
|
|
246
|
+
chainId,
|
|
247
|
+
sessionKey: JSON.stringify(jwk),
|
|
248
|
+
siwe: prepared.siwe,
|
|
249
|
+
signature,
|
|
250
|
+
tinycloudSession: {
|
|
251
|
+
delegationHeader: session.delegationHeader,
|
|
252
|
+
delegationCid: session.delegationCid,
|
|
253
|
+
spaceId,
|
|
254
|
+
verificationMethod: this.sessionManager.getDID(keyId),
|
|
255
|
+
},
|
|
256
|
+
expiresAt: expirationTime.toISOString(),
|
|
257
|
+
createdAt: now.toISOString(),
|
|
258
|
+
version: "1.0",
|
|
259
|
+
};
|
|
260
|
+
await this.sessionStorage.save(address, persistedData);
|
|
261
|
+
// Set current session
|
|
262
|
+
this._session = clientSession;
|
|
263
|
+
this._tinyCloudSession = tinyCloudSession;
|
|
264
|
+
this._address = address;
|
|
265
|
+
this._chainId = chainId;
|
|
266
|
+
// Verify SDK-node protocol compatibility
|
|
267
|
+
await checkNodeVersion(this.tinycloudHosts[0], protocolVersion());
|
|
268
|
+
// Call extension hooks
|
|
269
|
+
for (const ext of this.extensions) {
|
|
270
|
+
if (ext.afterSignIn) {
|
|
271
|
+
await ext.afterSignIn(clientSession);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// Ensure space exists (creates if needed when autoCreateSpace is true)
|
|
275
|
+
await this.ensureSpaceExists();
|
|
276
|
+
return clientSession;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Sign out and clear the current session.
|
|
280
|
+
*/
|
|
281
|
+
async signOut() {
|
|
282
|
+
if (this._address) {
|
|
283
|
+
await this.clearPersistedSession(this._address);
|
|
284
|
+
}
|
|
285
|
+
this._session = undefined;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Get the current wallet/signer address.
|
|
289
|
+
*/
|
|
290
|
+
address() {
|
|
291
|
+
return this._address;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Get the current chain ID.
|
|
295
|
+
*/
|
|
296
|
+
chainId() {
|
|
297
|
+
return this._chainId;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Sign a message with the connected signer.
|
|
301
|
+
*/
|
|
302
|
+
async signMessage(message) {
|
|
303
|
+
if (!this._address) {
|
|
304
|
+
this._address = await this.signer.getAddress();
|
|
305
|
+
}
|
|
306
|
+
if (!this._chainId) {
|
|
307
|
+
this._chainId = await this.signer.getChainId();
|
|
308
|
+
}
|
|
309
|
+
return this.requestSignature({
|
|
310
|
+
address: this._address,
|
|
311
|
+
chainId: this._chainId,
|
|
312
|
+
message,
|
|
313
|
+
type: "message",
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Prepare a session for external signing.
|
|
318
|
+
*
|
|
319
|
+
* Use this method when you need to sign the SIWE message externally (e.g., via
|
|
320
|
+
* a hardware wallet, multi-sig, or external service). After obtaining the signature,
|
|
321
|
+
* call `signInWithPreparedSession()` to complete the sign-in.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* const { prepared, keyId, jwk } = await auth.prepareSessionForSigning();
|
|
326
|
+
* const signature = await externalSigner.signMessage(prepared.siwe);
|
|
327
|
+
* const session = await auth.signInWithPreparedSession(prepared, signature, keyId, jwk);
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
async prepareSessionForSigning() {
|
|
331
|
+
const address = ensureEip55(await this.signer.getAddress());
|
|
332
|
+
const chainId = await this.signer.getChainId();
|
|
333
|
+
// Create a session key
|
|
334
|
+
const keyId = `session-${Date.now()}`;
|
|
335
|
+
this.sessionManager.renameSessionKeyId("default", keyId);
|
|
336
|
+
// Get JWK for session key
|
|
337
|
+
const jwkString = this.sessionManager.jwk(keyId);
|
|
338
|
+
if (!jwkString) {
|
|
339
|
+
throw new Error("Failed to create session key");
|
|
340
|
+
}
|
|
341
|
+
const jwk = JSON.parse(jwkString);
|
|
342
|
+
// Create space ID
|
|
343
|
+
const spaceId = makeSpaceId(address, chainId, this.spacePrefix);
|
|
344
|
+
const now = new Date();
|
|
345
|
+
const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
|
|
346
|
+
// Prepare session - this creates the SIWE message with ReCap capabilities
|
|
347
|
+
const prepared = prepareSession({
|
|
348
|
+
abilities: this.defaultActions,
|
|
349
|
+
address,
|
|
350
|
+
chainId,
|
|
351
|
+
domain: this.domain,
|
|
352
|
+
issuedAt: now.toISOString(),
|
|
353
|
+
expirationTime: expirationTime.toISOString(),
|
|
354
|
+
spaceId,
|
|
355
|
+
jwk,
|
|
356
|
+
});
|
|
357
|
+
return {
|
|
358
|
+
prepared,
|
|
359
|
+
keyId,
|
|
360
|
+
jwk,
|
|
361
|
+
address,
|
|
362
|
+
chainId,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Complete sign-in with a prepared session and signature.
|
|
367
|
+
*
|
|
368
|
+
* Use this method after obtaining a signature for the SIWE message from
|
|
369
|
+
* `prepareSessionForSigning()`. The signature MUST be over `prepared.siwe`.
|
|
370
|
+
*
|
|
371
|
+
* @param prepared - The prepared session from `prepareSessionForSigning()`
|
|
372
|
+
* @param signature - The signature over `prepared.siwe`
|
|
373
|
+
* @param keyId - The session key ID from `prepareSessionForSigning()`
|
|
374
|
+
* @param jwk - The JWK from `prepareSessionForSigning()`
|
|
375
|
+
*/
|
|
376
|
+
async signInWithPreparedSession(prepared, signature, keyId, jwk) {
|
|
377
|
+
// Complete session setup with the prepared session + signature
|
|
378
|
+
const session = completeSessionSetup({
|
|
379
|
+
...prepared,
|
|
380
|
+
signature,
|
|
381
|
+
});
|
|
382
|
+
// Parse address and chainId from the prepared session
|
|
383
|
+
// The SIWE message contains this info, but we need to extract it
|
|
384
|
+
// For now, we'll get it from the signer since it should match
|
|
385
|
+
const address = ensureEip55(await this.signer.getAddress());
|
|
386
|
+
const chainId = await this.signer.getChainId();
|
|
387
|
+
// Create client session (web-core compatible)
|
|
388
|
+
const clientSession = {
|
|
389
|
+
address,
|
|
390
|
+
walletAddress: address,
|
|
391
|
+
chainId,
|
|
392
|
+
sessionKey: keyId,
|
|
393
|
+
siwe: prepared.siwe,
|
|
394
|
+
signature,
|
|
395
|
+
};
|
|
396
|
+
// Create TinyCloud session with full delegation data
|
|
397
|
+
// Use sessionManager.getDID(keyId) for properly formatted DID URL
|
|
398
|
+
const tinyCloudSession = {
|
|
399
|
+
address,
|
|
400
|
+
chainId,
|
|
401
|
+
sessionKey: keyId,
|
|
402
|
+
spaceId: prepared.spaceId,
|
|
403
|
+
delegationCid: session.delegationCid,
|
|
404
|
+
delegationHeader: session.delegationHeader,
|
|
405
|
+
verificationMethod: this.sessionManager.getDID(keyId),
|
|
406
|
+
jwk,
|
|
407
|
+
siwe: prepared.siwe,
|
|
408
|
+
signature,
|
|
409
|
+
};
|
|
410
|
+
// Extract expiration from SIWE message (parse the string)
|
|
411
|
+
const expirationMatch = prepared.siwe.match(/Expiration Time: (.+)/);
|
|
412
|
+
const issuedAtMatch = prepared.siwe.match(/Issued At: (.+)/);
|
|
413
|
+
const expiresAt = expirationMatch?.[1] ??
|
|
414
|
+
new Date(Date.now() + this.sessionExpirationMs).toISOString();
|
|
415
|
+
const createdAt = issuedAtMatch?.[1] ?? new Date().toISOString();
|
|
416
|
+
// Persist session with TinyCloud-specific data
|
|
417
|
+
const persistedData = {
|
|
418
|
+
address,
|
|
419
|
+
chainId,
|
|
420
|
+
sessionKey: JSON.stringify(jwk),
|
|
421
|
+
siwe: prepared.siwe,
|
|
422
|
+
signature,
|
|
423
|
+
tinycloudSession: {
|
|
424
|
+
delegationHeader: session.delegationHeader,
|
|
425
|
+
delegationCid: session.delegationCid,
|
|
426
|
+
spaceId: prepared.spaceId,
|
|
427
|
+
verificationMethod: this.sessionManager.getDID(keyId),
|
|
428
|
+
},
|
|
429
|
+
expiresAt,
|
|
430
|
+
createdAt,
|
|
431
|
+
version: "1.0",
|
|
432
|
+
};
|
|
433
|
+
await this.sessionStorage.save(address, persistedData);
|
|
434
|
+
// Set current session
|
|
435
|
+
this._session = clientSession;
|
|
436
|
+
this._tinyCloudSession = tinyCloudSession;
|
|
437
|
+
this._address = address;
|
|
438
|
+
this._chainId = chainId;
|
|
439
|
+
// Verify SDK-node protocol compatibility
|
|
440
|
+
await checkNodeVersion(this.tinycloudHosts[0], protocolVersion());
|
|
441
|
+
// Call extension hooks
|
|
442
|
+
for (const ext of this.extensions) {
|
|
443
|
+
if (ext.afterSignIn) {
|
|
444
|
+
await ext.afterSignIn(clientSession);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
// Ensure space exists (creates if needed when autoCreateSpace is true)
|
|
448
|
+
await this.ensureSpaceExists();
|
|
449
|
+
return clientSession;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Clear persisted session data.
|
|
453
|
+
*/
|
|
454
|
+
async clearPersistedSession(address) {
|
|
455
|
+
const targetAddress = address ?? this._address;
|
|
456
|
+
if (targetAddress) {
|
|
457
|
+
await this.sessionStorage.clear(targetAddress);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Check if a session is persisted for an address.
|
|
462
|
+
*/
|
|
463
|
+
isSessionPersisted(address) {
|
|
464
|
+
return this.sessionStorage.exists(address);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Request a signature based on the configured strategy.
|
|
468
|
+
*/
|
|
469
|
+
async requestSignature(request) {
|
|
470
|
+
switch (this.signStrategy.type) {
|
|
471
|
+
case "auto-sign":
|
|
472
|
+
return this.signer.signMessage(request.message);
|
|
473
|
+
case "auto-reject":
|
|
474
|
+
throw new Error("Sign request rejected by auto-reject strategy");
|
|
475
|
+
case "callback": {
|
|
476
|
+
const response = await this.signStrategy.handler(request);
|
|
477
|
+
if (!response.approved) {
|
|
478
|
+
throw new Error(response.reason ?? "Sign request rejected by callback");
|
|
479
|
+
}
|
|
480
|
+
// If callback provides signature, use it; otherwise sign with signer
|
|
481
|
+
return (response.signature ?? (await this.signer.signMessage(request.message)));
|
|
482
|
+
}
|
|
483
|
+
case "event-emitter": {
|
|
484
|
+
return this.requestSignatureViaEmitter(request, this.signStrategy.emitter, this.signStrategy.timeout ?? 60000);
|
|
485
|
+
}
|
|
486
|
+
default:
|
|
487
|
+
throw new Error(`Unknown sign strategy: ${this.signStrategy.type}`);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Request signature via event emitter with timeout.
|
|
492
|
+
*/
|
|
493
|
+
requestSignatureViaEmitter(request, emitter, timeout) {
|
|
494
|
+
return new Promise((resolve, reject) => {
|
|
495
|
+
const timeoutId = setTimeout(() => {
|
|
496
|
+
reject(new Error("Sign request timed out"));
|
|
497
|
+
}, timeout);
|
|
498
|
+
const respond = async (response) => {
|
|
499
|
+
clearTimeout(timeoutId);
|
|
500
|
+
if (!response.approved) {
|
|
501
|
+
reject(new Error(response.reason ?? "Sign request rejected via emitter"));
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
// If response provides signature, use it; otherwise sign with signer
|
|
505
|
+
const signature = response.signature ??
|
|
506
|
+
(await this.signer.signMessage(request.message));
|
|
507
|
+
resolve(signature);
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
emitter.emit("sign-request", request, respond);
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
/** Flag to ensure WASM panic hook is only initialized once */
|
|
515
|
+
NodeUserAuthorization.wasmInitialized = false;
|
|
516
|
+
//# sourceMappingURL=NodeUserAuthorization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NodeUserAuthorization.js","sourceRoot":"","sources":["../../src/authorization/NodeUserAuthorization.ts"],"names":[],"mappings":"AACA,OAAO,EAQL,WAAW,EACX,oBAAoB,EACpB,uBAAuB,EACvB,gBAAgB,GACjB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,iBAAiB,IAAI,cAAc,EACnC,cAAc,EACd,oBAAoB,EACpB,WAAW,EACX,WAAW,EACX,aAAa,EACb,uBAAuB,EACvB,uBAAuB,EACvB,eAAe,GAChB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAIL,mBAAmB,GACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AA8BvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,OAAO,qBAAqB;IAuBhC,YAAY,MAAmC;QANvC,eAAU,GAAgB,EAAE,CAAC;QAOnC,sEAAsE;QACtE,IA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N,qEAAqE;oBACrE,MAAM,SAAS,GACb,QAAQ,CAAC,SAAS;wBAClB,CAAC,MAAM,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;oBACnD,OAAO,CAAC,SAAS,CAAC,CAAC;gBACrB,CAAC;YACH,CAAC,CAAC;YAEF,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC;IACL,CAAC;;AAxmBD,8DAA8D;AAC/C,qCAAe,GAAG,KAAK,AAAR,CAAS"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js-specific SignStrategy types for TinyCloud authorization.
|
|
3
|
+
*
|
|
4
|
+
* This module re-exports common types from sdk-core and provides
|
|
5
|
+
* Node.js-specific implementations (e.g., NodeEventEmitterStrategy
|
|
6
|
+
* using Node's EventEmitter instead of browser EventTarget).
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import { EventEmitter } from "events";
|
|
11
|
+
export { SignRequest, SignResponse, SignCallback, AutoSignStrategy, AutoRejectStrategy, CallbackStrategy, } from "@tinycloudlabs/sdk-core";
|
|
12
|
+
import type { AutoSignStrategy, AutoRejectStrategy, CallbackStrategy } from "@tinycloudlabs/sdk-core";
|
|
13
|
+
/**
|
|
14
|
+
* Node.js event emitter strategy: emits sign requests as events.
|
|
15
|
+
*
|
|
16
|
+
* Uses Node.js EventEmitter for compatibility with Node.js applications.
|
|
17
|
+
* For browser environments, use the EventEmitterStrategy from sdk-core
|
|
18
|
+
* which uses EventTarget.
|
|
19
|
+
*
|
|
20
|
+
* Events emitted:
|
|
21
|
+
* - 'sign-request': When a sign request is received
|
|
22
|
+
*
|
|
23
|
+
* Use cases:
|
|
24
|
+
* - Async approval workflows in Node.js
|
|
25
|
+
* - External signing services
|
|
26
|
+
* - Multi-step authorization flows
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const emitter = new EventEmitter();
|
|
31
|
+
* const strategy: NodeEventEmitterStrategy = { type: 'event-emitter', emitter };
|
|
32
|
+
*
|
|
33
|
+
* emitter.on('sign-request', async (req, respond) => {
|
|
34
|
+
* const approved = await externalApprovalService.check(req);
|
|
35
|
+
* respond({ approved, signature: approved ? await sign(req.message) : undefined });
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export interface NodeEventEmitterStrategy {
|
|
40
|
+
type: "event-emitter";
|
|
41
|
+
emitter: EventEmitter;
|
|
42
|
+
/** Timeout in milliseconds for waiting on event response (default: 60000) */
|
|
43
|
+
timeout?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Node.js sign strategy union type.
|
|
47
|
+
*
|
|
48
|
+
* Determines how sign requests are handled in NodeUserAuthorization.
|
|
49
|
+
* Uses Node.js EventEmitter for the event-emitter strategy.
|
|
50
|
+
*/
|
|
51
|
+
export type SignStrategy = AutoSignStrategy | AutoRejectStrategy | CallbackStrategy | NodeEventEmitterStrategy;
|
|
52
|
+
/**
|
|
53
|
+
* Default sign strategy is auto-sign for convenience.
|
|
54
|
+
* This is the Node.js-specific version typed with SignStrategy.
|
|
55
|
+
*/
|
|
56
|
+
export declare const defaultSignStrategy: SignStrategy;
|
|
57
|
+
//# sourceMappingURL=strategies.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategies.d.ts","sourceRoot":"","sources":["../../src/authorization/strategies.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,EACL,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,KAAK,EAIV,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,yBAAyB,CAAC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,WAAW,wBAAwB;IACvC,IAAI,EAAE,eAAe,CAAC;IACtB,OAAO,EAAE,YAAY,CAAC;IACtB,6EAA6E;IAC7E,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GACpB,gBAAgB,GAChB,kBAAkB,GAClB,gBAAgB,GAChB,wBAAwB,CAAC;AAE7B;;;GAGG;AACH,eAAO,MAAM,mBAAmB,EAAE,YAAoC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js-specific SignStrategy types for TinyCloud authorization.
|
|
3
|
+
*
|
|
4
|
+
* This module re-exports common types from sdk-core and provides
|
|
5
|
+
* Node.js-specific implementations (e.g., NodeEventEmitterStrategy
|
|
6
|
+
* using Node's EventEmitter instead of browser EventTarget).
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Default sign strategy is auto-sign for convenience.
|
|
12
|
+
* This is the Node.js-specific version typed with SignStrategy.
|
|
13
|
+
*/
|
|
14
|
+
export const defaultSignStrategy = { type: "auto-sign" };
|
|
15
|
+
//# sourceMappingURL=strategies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategies.js","sourceRoot":"","sources":["../../src/authorization/strategies.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAqEH;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAiB,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Delegation } from "@tinycloudlabs/sdk-core";
|
|
2
|
+
/**
|
|
3
|
+
* A portable delegation that can be transported between users.
|
|
4
|
+
* Extends the base Delegation type with fields required for transport.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* PortableDelegation adds transport fields to Delegation:
|
|
8
|
+
* - `delegationHeader`: Structured authorization header for API calls
|
|
9
|
+
* - `ownerAddress`: Space owner's address for session creation
|
|
10
|
+
* - `chainId`: Chain ID for session creation
|
|
11
|
+
* - `host`: Optional server URL
|
|
12
|
+
*/
|
|
13
|
+
export interface PortableDelegation extends Omit<Delegation, "isRevoked"> {
|
|
14
|
+
/** The authorization header for this delegation (structured format) */
|
|
15
|
+
delegationHeader: {
|
|
16
|
+
Authorization: string;
|
|
17
|
+
};
|
|
18
|
+
/** The address of the space owner */
|
|
19
|
+
ownerAddress: string;
|
|
20
|
+
/** The chain ID */
|
|
21
|
+
chainId: number;
|
|
22
|
+
/** TinyCloud server URL where this delegation was created */
|
|
23
|
+
host?: string;
|
|
24
|
+
/** Whether the recipient is prevented from creating sub-delegations */
|
|
25
|
+
disableSubDelegation?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Serialize a PortableDelegation for transport (e.g., over network).
|
|
29
|
+
*/
|
|
30
|
+
export declare function serializeDelegation(delegation: PortableDelegation): string;
|
|
31
|
+
/**
|
|
32
|
+
* Deserialize a PortableDelegation from transport.
|
|
33
|
+
*/
|
|
34
|
+
export declare function deserializeDelegation(data: string): PortableDelegation;
|
|
35
|
+
//# sourceMappingURL=delegation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delegation.d.ts","sourceRoot":"","sources":["../src/delegation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAErD;;;;;;;;;;GAUG;AACH,MAAM,WAAW,kBAAmB,SAAQ,IAAI,CAAC,UAAU,EAAE,WAAW,CAAC;IACvE,uEAAuE;IACvE,gBAAgB,EAAE;QAAE,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC;IAE5C,qCAAqC;IACrC,YAAY,EAAE,MAAM,CAAC;IAErB,mBAAmB;IACnB,OAAO,EAAE,MAAM,CAAC;IAEhB,6DAA6D;IAC7D,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,uEAAuE;IACvE,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,kBAAkB,GAAG,MAAM,CAK1E;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,CAOtE"}
|