Skip to content

Commit ed24b7d

Browse files
committed
variable sessionTime for sessionTokenData
1 parent c8954c9 commit ed24b7d

6 files changed

+137
-10
lines changed

package-lock.json

+27-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"dependencies": {
2626
"@toruslabs/constants": "^13.1.0",
2727
"@toruslabs/eccrypto": "^4.0.0",
28-
"@toruslabs/http-helpers": "^5.0.0",
28+
"@toruslabs/http-helpers": "^6.0.0",
2929
"bn.js": "^5.2.1",
3030
"elliptic": "^6.5.4",
3131
"ethereum-cryptography": "^2.1.2",

src/torus.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import { Some } from "./some";
5454
// Implement threshold logic wrappers around public APIs
5555
// of Torus nodes to handle malicious node responses
5656
class Torus {
57+
private static sessionTime: number = 2480; // 2480s = 24 hour
58+
5759
public allowHost: string;
5860

5961
public serverTimeOffset: number;
@@ -104,6 +106,10 @@ class Torus {
104106
setEmbedHost(embedHost);
105107
}
106108

109+
static setSessionTime(sessionTime: number): void {
110+
Torus.sessionTime = sessionTime;
111+
}
112+
107113
static isGetOrSetNonceError(err: unknown): boolean {
108114
return err instanceof GetOrSetNonceError;
109115
}
@@ -137,7 +143,10 @@ class Torus {
137143
verifierParams,
138144
idToken,
139145
importedShares: [],
140-
extraParams,
146+
extraParams: {
147+
...extraParams,
148+
session_token_exp_second: Torus.sessionTime,
149+
},
141150
});
142151
}
143152

@@ -222,7 +231,10 @@ class Torus {
222231
verifierParams,
223232
idToken,
224233
importedShares: sharesData,
225-
extraParams,
234+
extraParams: {
235+
...extraParams,
236+
session_token_exp_second: Torus.sessionTime,
237+
},
226238
});
227239
}
228240

test/sapphire_devnet.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,34 @@ describe("torus utils sapphire devnet", function () {
373373
nodesData: result.nodesData,
374374
});
375375
});
376+
377+
it("should be able to update the `sessionTime` of the token signature data", async function () {
378+
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");
379+
380+
const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL });
381+
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
382+
torusNodeEndpoints[1] = "https://example.com";
383+
384+
const customSessionTime = 3600;
385+
TorusUtils.setSessionTime(customSessionTime); // 1hr
386+
387+
const result = await torus.retrieveShares(
388+
torusNodeEndpoints,
389+
nodeDetails.torusIndexes,
390+
TORUS_TEST_VERIFIER,
391+
{ verifier_id: TORUS_TEST_EMAIL },
392+
token
393+
);
394+
395+
const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));
396+
397+
const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
398+
parsedSigsData.forEach((ps) => {
399+
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
400+
expect(sessionTime).eql(customSessionTime);
401+
});
402+
});
403+
376404
it.skip("should be able to import a key for a new user", async function () {
377405
const email = faker.internet.email();
378406
const token = generateIdToken(email, "ES256");

test/sapphire_mainnet.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,32 @@ describe("torus utils sapphire mainnet", function () {
343343
expect(result.metadata.nonce).to.not.equal(null);
344344
expect(result.metadata.upgraded).to.equal(false);
345345
});
346+
347+
it("should be able to update the `sessionTime` of the token signature data", async function () {
348+
const email = faker.internet.email();
349+
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");
350+
351+
const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierId: email });
352+
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
353+
torusNodeEndpoints[1] = "https://example.com";
354+
355+
const customSessionTime = 3600;
356+
TorusUtils.setSessionTime(customSessionTime); // 1hr
357+
358+
const result = await torus.retrieveShares(
359+
torusNodeEndpoints,
360+
nodeDetails.torusIndexes,
361+
TORUS_TEST_VERIFIER,
362+
{ verifier_id: TORUS_TEST_EMAIL },
363+
token
364+
);
365+
366+
const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));
367+
368+
const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
369+
parsedSigsData.forEach((ps) => {
370+
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
371+
expect(sessionTime).eql(customSessionTime);
372+
});
373+
});
346374
});

tsconfig.json

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
1+
// {
2+
// "extends": "@toruslabs/config/tsconfig.default.json",
3+
// "include": ["src", "test"],
4+
// "compilerOptions": {},
5+
// "ts-node": {
6+
// "compilerOptions": {
7+
// "module": "CommonJS",
8+
// "target": "ES2015",
9+
// }
10+
// }
11+
// }
12+
113
{
2-
"extends": "@toruslabs/config/tsconfig.default.json",
3-
"include": ["src", "test"],
4-
"compilerOptions": {}
14+
"$schema": "https://json.schemastore.org/tsconfig",
15+
"compilerOptions": {
16+
"moduleResolution": "bundler",
17+
"strict": true,
18+
"module": "esnext",
19+
"target": "esnext",
20+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
21+
"sourceMap": true,
22+
"esModuleInterop": true,
23+
"noImplicitThis": true,
24+
"declaration": true,
25+
"allowSyntheticDefaultImports": true,
26+
"resolveJsonModule": true,
27+
"noUncheckedIndexedAccess": true,
28+
"noUnusedLocals": true,
29+
"noUnusedParameters": true,
30+
"isolatedModules": true,
31+
"noImplicitAny": true,
32+
"strictNullChecks": false,
33+
"skipLibCheck": true
34+
},
35+
"ts-node": {
36+
"compilerOptions": {
37+
"module": "CommonJS",
38+
"target": "ES2015"
39+
}
40+
}
541
}

0 commit comments

Comments
 (0)