Skip to content

Commit

Permalink
Merge pull request #129 from torusresearch/feat/variable-session-time
Browse files Browse the repository at this point in the history
variable sessionTime for sessionTokenData
  • Loading branch information
himanshuchawla009 authored Jan 30, 2024
2 parents c8954c9 + cdca52a commit 271733d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"editor.codeActionsOnSave": {
"source.fixAll": true
"source.fixAll": "explicit"
},
"cSpell.words": ["Mutex", "Mutexes", "toruslabs"]
}
31 changes: 27 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@toruslabs/constants": "^13.1.0",
"@toruslabs/eccrypto": "^4.0.0",
"@toruslabs/http-helpers": "^5.0.0",
"@toruslabs/http-helpers": "^6.0.0",
"bn.js": "^5.2.1",
"elliptic": "^6.5.4",
"ethereum-cryptography": "^2.1.2",
Expand Down
16 changes: 14 additions & 2 deletions src/torus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import { Some } from "./some";
// Implement threshold logic wrappers around public APIs
// of Torus nodes to handle malicious node responses
class Torus {
private static sessionTime: number = 86400; // 86400 = 24 hour

public allowHost: string;

public serverTimeOffset: number;
Expand Down Expand Up @@ -104,6 +106,10 @@ class Torus {
setEmbedHost(embedHost);
}

static setSessionTime(sessionTime: number): void {
Torus.sessionTime = sessionTime;
}

static isGetOrSetNonceError(err: unknown): boolean {
return err instanceof GetOrSetNonceError;
}
Expand Down Expand Up @@ -137,7 +143,10 @@ class Torus {
verifierParams,
idToken,
importedShares: [],
extraParams,
extraParams: {
...extraParams,
session_token_exp_second: Torus.sessionTime,
},
});
}

Expand Down Expand Up @@ -222,7 +231,10 @@ class Torus {
verifierParams,
idToken,
importedShares: sharesData,
extraParams,
extraParams: {
...extraParams,
session_token_exp_second: Torus.sessionTime,
},
});
}

Expand Down
28 changes: 28 additions & 0 deletions test/sapphire_devnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,34 @@ describe("torus utils sapphire devnet", function () {
nodesData: result.nodesData,
});
});

it("should be able to update the `sessionTime` of the token signature data", async function () {
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");

const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL });
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
torusNodeEndpoints[1] = "https://example.com";

const customSessionTime = 3600;
TorusUtils.setSessionTime(customSessionTime); // 1hr

const result = await torus.retrieveShares(
torusNodeEndpoints,
nodeDetails.torusIndexes,
TORUS_TEST_VERIFIER,
{ verifier_id: TORUS_TEST_EMAIL },
token
);

const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));

const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
parsedSigsData.forEach((ps) => {
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
expect(sessionTime).eql(customSessionTime);
});
});

it.skip("should be able to import a key for a new user", async function () {
const email = faker.internet.email();
const token = generateIdToken(email, "ES256");
Expand Down
36 changes: 32 additions & 4 deletions test/sapphire_mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const HashEnabledVerifier = "torus-test-verifierid-hash";
const TORUS_EXTENDED_VERIFIER_EMAIL = "[email protected]";
const TORUS_IMPORT_EMAIL = "[email protected]";

describe("torus utils sapphire mainnet", function () {
describe.only("torus utils sapphire mainnet", function () {
let torus: TorusUtils;
let TORUS_NODE_MANAGER: NodeManager;

Expand Down Expand Up @@ -50,7 +50,7 @@ describe("torus utils sapphire mainnet", function () {
X: "56e803db7710adbfe0ecca35bc6a3ad27e966df142e157e76e492773c88e8433",
Y: "f4168594c1126ca731756dd480f992ee73b0834ba4b787dd892a9211165f50a3",
},
nonce: new BN("f3ba568eeeaca5c285b25878a067fd85b1720b94f9099591d4274dc0a8cada9b", "hex"),
nonce: new BN("0", "hex"),
upgraded: false,
typeOfUser: "v2",
},
Expand Down Expand Up @@ -203,7 +203,7 @@ describe("torus utils sapphire mainnet", function () {
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
},
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
nonce: new BN("0", "hex"),
upgraded: false,
typeOfUser: "v2",
},
Expand Down Expand Up @@ -233,7 +233,7 @@ describe("torus utils sapphire mainnet", function () {
X: "498ed301af25a3b7136f478fa58677c79a6d6fe965bc13002a6f459b896313bd",
Y: "d6feb9a1e0d6d0627fbb1ce75682bc09ab4cf0e2da4f0f7fcac0ba9d07596c8f",
},
nonce: new BN("3c2b6ba5b54ca0ba4ae978eb48429a84c47b7b3e526b35e7d46dd716887f52bf", "hex"),
nonce: new BN("0", "hex"),
upgraded: false,
typeOfUser: "v2",
},
Expand Down Expand Up @@ -343,4 +343,32 @@ describe("torus utils sapphire mainnet", function () {
expect(result.metadata.nonce).to.not.equal(null);
expect(result.metadata.upgraded).to.equal(false);
});

it("should be able to update the `sessionTime` of the token signature data", async function () {
const email = faker.internet.email();
const token = generateIdToken(TORUS_TEST_EMAIL, "ES256");

const nodeDetails = await TORUS_NODE_MANAGER.getNodeDetails({ verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierId: email });
const torusNodeEndpoints = nodeDetails.torusNodeSSSEndpoints;
torusNodeEndpoints[1] = "https://example.com";

const customSessionTime = 3600;
TorusUtils.setSessionTime(customSessionTime); // 1hr

const result = await torus.retrieveShares(
torusNodeEndpoints,
nodeDetails.torusIndexes,
TORUS_TEST_VERIFIER,
{ verifier_id: TORUS_TEST_EMAIL },
token
);

const signatures = result.sessionData.sessionTokenData.map((s) => ({ data: s.token, sig: s.signature }));

const parsedSigsData = signatures.map((s) => JSON.parse(atob(s.data)));
parsedSigsData.forEach((ps) => {
const sessionTime = ps.exp - Math.floor(Date.now() / 1000);
expect(sessionTime).eql(customSessionTime);
});
});
});

0 comments on commit 271733d

Please sign in to comment.