Skip to content

Commit

Permalink
variable sessionTime for sessionTokenData
Browse files Browse the repository at this point in the history
  • Loading branch information
lwin-kyaw committed Jan 30, 2024
1 parent c8954c9 commit ed24b7d
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 10 deletions.
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 = 2480; // 2480s = 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
28 changes: 28 additions & 0 deletions test/sapphire_mainnet.test.ts
Original file line number Diff line number Diff line change
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);
});
});
});
42 changes: 39 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
// {
// "extends": "@toruslabs/config/tsconfig.default.json",
// "include": ["src", "test"],
// "compilerOptions": {},
// "ts-node": {
// "compilerOptions": {
// "module": "CommonJS",
// "target": "ES2015",
// }
// }
// }

{
"extends": "@toruslabs/config/tsconfig.default.json",
"include": ["src", "test"],
"compilerOptions": {}
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"moduleResolution": "bundler",
"strict": true,
"module": "esnext",
"target": "esnext",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"sourceMap": true,
"esModuleInterop": true,
"noImplicitThis": true,
"declaration": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"isolatedModules": true,
"noImplicitAny": true,
"strictNullChecks": false,
"skipLibCheck": true
},
"ts-node": {
"compilerOptions": {
"module": "CommonJS",
"target": "ES2015"
}
}
}

0 comments on commit ed24b7d

Please sign in to comment.