diff --git a/package-lock.json b/package-lock.json index 5a38796..eae6f3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,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", @@ -2997,6 +2997,29 @@ "@babel/runtime": "7.x" } }, + "node_modules/@toruslabs/fetch-node-details/node_modules/@toruslabs/http-helpers": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@toruslabs/http-helpers/-/http-helpers-5.0.0.tgz", + "integrity": "sha512-GmezWz9JeF6YyhjLSm+9XDF4YaeICEckY0Jbo43i86SjhfJYgRWqEi63VSiNsaqc/z810Q0FQvEk1TnBRX2tgA==", + "dev": true, + "dependencies": { + "lodash.merge": "^4.6.2", + "loglevel": "^1.8.1" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "^7.x", + "@sentry/types": "^7.x" + }, + "peerDependenciesMeta": { + "@sentry/types": { + "optional": true + } + } + }, "node_modules/@toruslabs/fnd-base": { "version": "13.1.1", "resolved": "https://registry.npmjs.org/@toruslabs/fnd-base/-/fnd-base-13.1.1.tgz", @@ -3014,9 +3037,9 @@ } }, "node_modules/@toruslabs/http-helpers": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/@toruslabs/http-helpers/-/http-helpers-5.0.0.tgz", - "integrity": "sha512-GmezWz9JeF6YyhjLSm+9XDF4YaeICEckY0Jbo43i86SjhfJYgRWqEi63VSiNsaqc/z810Q0FQvEk1TnBRX2tgA==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/@toruslabs/http-helpers/-/http-helpers-6.0.0.tgz", + "integrity": "sha512-/KrISB9fGV2TM+5Z+0CGj24d/G08kqbB+kodO3nbwNcS0a55dpy+RiB0naF3H1gbEt4Ah5YH8qCDyAZ+zcR2FQ==", "dependencies": { "lodash.merge": "^4.6.2", "loglevel": "^1.8.1" diff --git a/package.json b/package.json index 0e293f2..f46ccb1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/torus.ts b/src/torus.ts index 44a9156..ea916ad 100644 --- a/src/torus.ts +++ b/src/torus.ts @@ -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; @@ -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; } @@ -137,7 +143,10 @@ class Torus { verifierParams, idToken, importedShares: [], - extraParams, + extraParams: { + ...extraParams, + session_token_exp_second: Torus.sessionTime, + }, }); } @@ -222,7 +231,10 @@ class Torus { verifierParams, idToken, importedShares: sharesData, - extraParams, + extraParams: { + ...extraParams, + session_token_exp_second: Torus.sessionTime, + }, }); } diff --git a/test/sapphire_devnet.test.ts b/test/sapphire_devnet.test.ts index 41d8de4..24d6f7d 100644 --- a/test/sapphire_devnet.test.ts +++ b/test/sapphire_devnet.test.ts @@ -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"); diff --git a/test/sapphire_mainnet.test.ts b/test/sapphire_mainnet.test.ts index 6410d65..db3f1c7 100644 --- a/test/sapphire_mainnet.test.ts +++ b/test/sapphire_mainnet.test.ts @@ -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); + }); + }); }); diff --git a/tsconfig.json b/tsconfig.json index 2951c72..22d236c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" + } + } }