Skip to content

Commit 271733d

Browse files
Merge pull request #129 from torusresearch/feat/variable-session-time
variable sessionTime for sessionTokenData
2 parents c8954c9 + cdca52a commit 271733d

6 files changed

+103
-12
lines changed

.vscode/settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"editor.codeActionsOnSave": {
3-
"source.fixAll": true
3+
"source.fixAll": "explicit"
44
},
55
"cSpell.words": ["Mutex", "Mutexes", "toruslabs"]
66
}

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 = 86400; // 86400 = 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

+32-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const HashEnabledVerifier = "torus-test-verifierid-hash";
1616
const TORUS_EXTENDED_VERIFIER_EMAIL = "[email protected]";
1717
const TORUS_IMPORT_EMAIL = "[email protected]";
1818

19-
describe("torus utils sapphire mainnet", function () {
19+
describe.only("torus utils sapphire mainnet", function () {
2020
let torus: TorusUtils;
2121
let TORUS_NODE_MANAGER: NodeManager;
2222

@@ -50,7 +50,7 @@ describe("torus utils sapphire mainnet", function () {
5050
X: "56e803db7710adbfe0ecca35bc6a3ad27e966df142e157e76e492773c88e8433",
5151
Y: "f4168594c1126ca731756dd480f992ee73b0834ba4b787dd892a9211165f50a3",
5252
},
53-
nonce: new BN("f3ba568eeeaca5c285b25878a067fd85b1720b94f9099591d4274dc0a8cada9b", "hex"),
53+
nonce: new BN("0", "hex"),
5454
upgraded: false,
5555
typeOfUser: "v2",
5656
},
@@ -203,7 +203,7 @@ describe("torus utils sapphire mainnet", function () {
203203
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
204204
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
205205
},
206-
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
206+
nonce: new BN("0", "hex"),
207207
upgraded: false,
208208
typeOfUser: "v2",
209209
},
@@ -233,7 +233,7 @@ describe("torus utils sapphire mainnet", function () {
233233
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
234234
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
235235
},
236-
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
236+
nonce: new BN("0", "hex"),
237237
upgraded: false,
238238
typeOfUser: "v2",
239239
},
@@ -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
});

0 commit comments

Comments
 (0)