Skip to content

Commit 99e0553

Browse files
Merge pull request #89 from Web3Auth/feat/variable-session-time
enable variable session time for mpcCoreKit
2 parents 6066484 + 1e9fb83 commit 99e0553

9 files changed

Lines changed: 3580 additions & 2111 deletions

File tree

demo/redirect-flow-example/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/redirect-flow-example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"@types/node": "^16.18.48",
1111
"@types/react": "^18.2.21",
1212
"@types/react-dom": "^18.2.7",
13-
"@web3auth/mpc-core-kit": "file://../../",
13+
"@web3auth/mpc-core-kit": "file:../..",
1414
"browserify-zlib": "^0.2.0",
1515
"copy-webpack-plugin": "^11.0.0",
1616
"html-webpack-plugin": "^5.5.3",

demo/redirect-flow-example/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const coreKitInstance = new Web3AuthMPCCoreKit(
3333
web3AuthNetwork: selectedNetwork,
3434
uxMode: 'redirect',
3535
manualSync: true,
36-
setupProviderOnInit: false
36+
setupProviderOnInit: false,
37+
// sessionTime: 3600, // <== can provide variable session time based on user subscribed plan
3738
}
3839
);
3940

package-lock.json

Lines changed: 3450 additions & 2076 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,64 +42,66 @@
4242
"@tkey-mpc/share-serialization": "^9.1.0",
4343
"@tkey-mpc/storage-layer-torus": "^9.1.0",
4444
"@toruslabs/constants": "^13.0.1",
45-
"@toruslabs/customauth": "^16.0.6",
45+
"@toruslabs/customauth": "^18.1.0",
4646
"@toruslabs/eccrypto": "4.0.0",
47-
"@toruslabs/fetch-node-details": "^13.0.1",
47+
"@toruslabs/fetch-node-details": "^13.1.1",
4848
"@toruslabs/fnd-base": "^13.1.1",
4949
"@toruslabs/metadata-helpers": "^5.x",
5050
"@toruslabs/openlogin-session-manager": "^3.0.0",
5151
"@toruslabs/openlogin-utils": "^8.0.0",
52-
"@toruslabs/torus.js": "^12.1.0",
52+
"@toruslabs/torus.js": "12.2.0",
5353
"@toruslabs/tss-client": "^2.1.0",
5454
"@toruslabs/tss-lib": "^2.0.0",
55-
"@web3auth-mpc/ethereum-provider": "^2.3.0",
56-
"@web3auth/base": "^7.0.1",
57-
"@web3auth/base-provider": "^7.0.1",
55+
"@web3auth-mpc/ethereum-provider": "^3.1.0",
56+
"@web3auth/base": "^7.3.1",
57+
"@web3auth/base-provider": "^7.3.1",
5858
"bn.js": "^5.2.1",
5959
"bowser": "^2.11.0",
6060
"elliptic": "^6.5.4"
6161
},
6262
"devDependencies": {
63-
"@babel/register": "^7.22.15",
63+
"@babel/register": "^7.23.7",
6464
"@toruslabs/config": "^2.0.2",
65-
"@toruslabs/eslint-config-typescript": "^3.0.1",
66-
"@toruslabs/torus-scripts": "^5.0.5",
65+
"@toruslabs/eslint-config-typescript": "^3.1.0",
66+
"@toruslabs/torus-scripts": "^5.2.0",
6767
"@toruslabs/tss-lib-node": "^1.1.3",
68-
"@types/chai": "^4.3.6",
69-
"@types/elliptic": "^6.4.14",
68+
"@types/chai": "^4.3.11",
69+
"@types/elliptic": "^6.4.18",
7070
"@types/jsonwebtoken": "^9.0.5",
71-
"@types/node": "^20.6.3",
72-
"@typescript-eslint/eslint-plugin": "^6.7.0",
73-
"chai": "^4.3.8",
71+
"@types/node": "^20.11.16",
72+
"@typescript-eslint/eslint-plugin": "^6.20.0",
73+
"chai": "^5.0.3",
7474
"cross-env": "^7.0.3",
75-
"dotenv": "^16.3.1",
75+
"dotenv": "^16.4.1",
7676
"esbuild-register": "^3.5.0",
77-
"eslint": "^8.49.0",
78-
"husky": "^8.0.3",
77+
"eslint": "^8.56.0",
78+
"husky": "^9.0.10",
7979
"jsonwebtoken": "^9.0.2",
80-
"lint-staged": "^14.0.1",
80+
"lint-staged": "^15.2.1",
8181
"mocha": "^10.2.0",
8282
"node-fetch": "^3.3.2",
83-
"prettier": "^3.0.3",
84-
"rimraf": "^5.0.1",
85-
"ts-node": "^10.9.1",
83+
"prettier": "^3.2.4",
84+
"rimraf": "^5.0.5",
85+
"ts-node": "^10.9.2",
8686
"tsconfig-paths": "^4.2.0",
8787
"tsconfig-paths-webpack-plugin": "^4.1.0",
8888
"tslib": "^2.6.2",
89-
"typescript": "^5.2.2"
89+
"typescript": "^5.3.3"
9090
},
9191
"engines": {
9292
"node": ">=20.x"
9393
},
9494
"overrides": {
95+
"@toruslabs/customauth": "^18.1.0",
9596
"@tkey-mpc/storage-layer-torus": {
9697
"@toruslabs/http-helpers": "^6.0.0"
9798
},
9899
"@toruslabs/base-session-manager": {
99100
"@toruslabs/http-helpers": "^6.0.0"
100101
},
101-
"@toruslabs/customauth": {
102-
"@toruslabs/http-helpers": "^6.0.0"
102+
"@tkey-mpc/service-provider-torus": {
103+
"@toruslabs/torus.js": "^12.2.0",
104+
"@toruslabs/customauth": "^18.1.0"
103105
}
104106
},
105107
"lint-staged": {

src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ export interface Web3AuthOptions {
395395
* Setup Provider after `login success` reconstruct.
396396
*/
397397
setupProviderOnInit?: boolean;
398+
399+
serverTimeOffset?: number;
398400
}
399401

400402
export type Web3AuthOptionsWithDefaults = Required<Web3AuthOptions>;

src/mpcCoreKit.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
116116
if (typeof options.manualSync !== "boolean") options.manualSync = false;
117117
if (!options.web3AuthNetwork) options.web3AuthNetwork = WEB3AUTH_NETWORK.MAINNET;
118118
if (!options.sessionTime) options.sessionTime = 86400;
119+
if (!options.serverTimeOffset) options.serverTimeOffset = 0;
119120
if (!options.uxMode) options.uxMode = UX_MODE.REDIRECT;
120121
if (!options.redirectPathName) options.redirectPathName = "redirect";
121122
if (!options.baseUrl) options.baseUrl = isNodejsOrRN ? "https://localhost" : `${window?.location.origin}/serviceworker`;
@@ -262,6 +263,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
262263
network: this.options.web3AuthNetwork,
263264
redirectPathName: this.options.redirectPathName,
264265
locationReplaceOnRedirect: true,
266+
serverTimeOffset: this.options.serverTimeOffset,
265267
},
266268
nodeEndpoints: nodeDetails.torusNodeEndpoints,
267269
nodePubKeys: nodeDetails.torusNodePub.map((i) => ({ x: i.X, y: i.Y })),
@@ -304,7 +306,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
304306
} else if (params.rehydrate && this.sessionManager.sessionId) {
305307
// swallowed, should not throw on rehydrate timed out session
306308
const sessionResult = await this.sessionManager.authorizeSession().catch(async (err) => {
307-
log.info("rehydrate session error", err);
309+
log.error("rehydrate session error", err);
308310
});
309311

310312
// try rehydrate session
@@ -313,10 +315,12 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
313315
} else {
314316
// feature gating on no session rehydration
315317
await this.featureRequest();
318+
TorusUtils.setSessionTime(this.options.sessionTime);
316319
}
317320
} else {
318321
// feature gating if not redirect flow or session rehydration
319322
await this.featureRequest();
323+
TorusUtils.setSessionTime(this.options.sessionTime);
320324
}
321325

322326
// if not redirect flow or session rehydration, ask for factor key to login
@@ -712,7 +716,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
712716
throw new Error(`sessionAuth does not exist ${currentSession}`);
713717
}
714718

715-
const signatures = await this.getSigningSignatures(msgHash.toString("hex"));
719+
const signatures = await this.getSigningSignatures();
716720
if (!signatures) {
717721
throw new Error(`Signature does not exist ${signatures}`);
718722
}
@@ -1032,7 +1036,6 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
10321036
if (!factorKeyMetadata || factorKeyMetadata.message === "KEY_NOT_FOUND" || factorKeyMetadata.message === "SHARE_DELETED") {
10331037
return false;
10341038
}
1035-
log.info("factorKeyMetadata", factorKeyMetadata);
10361039
return true;
10371040
}
10381041

@@ -1192,9 +1195,8 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
11921195
return sessionData.map((session) => JSON.stringify({ data: session.token, sig: session.signature }));
11931196
}
11941197

1195-
private async getSigningSignatures(data: string): Promise<string[]> {
1198+
private async getSigningSignatures(): Promise<string[]> {
11961199
if (!this.signatures) throw new Error("signatures not present");
1197-
log.info("data", data);
11981200
return this.signatures;
11991201
}
12001202

@@ -1211,6 +1213,7 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
12111213
client_id: this.options.web3AuthClientId,
12121214
is_mpc_core_kit: "true",
12131215
enable_gating: "true",
1216+
session_time: this.options.sessionTime.toString(),
12141217
};
12151218
const url = new URL(`${accessUrl}/api/feature-access`);
12161219
url.search = new URLSearchParams(accessRequest).toString();

tests/login.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type TestVariable = {
2020
email: string;
2121
};
2222

23-
const defaultTestEmail = "testEmail1";
23+
const defaultTestEmail = "testEmailForLogin";
2424
const variable: TestVariable[] = [
2525
{ web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", email: defaultTestEmail },
2626
// { web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET, uxMode: UX_MODE.REDIRECT, email: defaultTestEmail },

tests/sessionTime.spec.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-disable mocha/handle-done-callback */
2+
import assert from "node:assert";
3+
import test from "node:test";
4+
5+
import { UX_MODE_TYPE } from "@toruslabs/customauth";
6+
import * as TssLib from "@toruslabs/tss-lib-node";
7+
8+
import { COREKIT_STATUS, WEB3AUTH_NETWORK, WEB3AUTH_NETWORK_TYPE, Web3AuthMPCCoreKit } from "../src";
9+
import { criticalResetAccount, mockLogin } from "./setup";
10+
11+
type TestVariable = {
12+
web3AuthNetwork: WEB3AUTH_NETWORK_TYPE;
13+
web3ClientID: string;
14+
uxMode: UX_MODE_TYPE | "nodejs";
15+
manualSync?: boolean;
16+
email: string;
17+
gated?: boolean;
18+
sessionTime?: number;
19+
};
20+
21+
const defaultTestEmail = "testEmail1";
22+
const variable: TestVariable[] = [
23+
{ web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", email: defaultTestEmail, web3ClientID: "torus-key-test", sessionTime: 3600 },
24+
{
25+
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
26+
uxMode: "nodejs",
27+
email: defaultTestEmail,
28+
web3ClientID: "BJ57yveG_XBLqZUpjtJCnJMrord0AaXpd_9OSy4HzkxpnpPn6Co73h-vR6GEI1VogtW4yMHq13GNPKmVpliFXY0",
29+
sessionTime: 7200,
30+
},
31+
{
32+
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
33+
uxMode: "nodejs",
34+
email: defaultTestEmail,
35+
web3ClientID: "BCriFlI9ihm81N-bc7x6N-xbqwBLuxfRDMmSH87spKH27QTNOPj1W9s2K3-mp9NzXuaRiqxvAGHyuGlXG5wLD1g",
36+
sessionTime: 172800,
37+
},
38+
];
39+
40+
variable.forEach((testVariable) => {
41+
const { web3AuthNetwork, uxMode, manualSync, email, web3ClientID: web3AuthClientId, sessionTime } = variable[0];
42+
const coreKitInstance = new Web3AuthMPCCoreKit({
43+
web3AuthClientId,
44+
web3AuthNetwork,
45+
baseUrl: "http://localhost:3000",
46+
uxMode,
47+
tssLib: TssLib,
48+
storageKey: "memory",
49+
manualSync,
50+
sessionTime,
51+
});
52+
53+
test(`#Variable SessionTime test : ${JSON.stringify({ sessionTime: testVariable.sessionTime })}`, async (t) => {
54+
t.before(async function () {
55+
if (coreKitInstance.status === COREKIT_STATUS.INITIALIZED) await criticalResetAccount(coreKitInstance);
56+
});
57+
58+
t.after(async function () {
59+
// after all test tear down
60+
await coreKitInstance.logout();
61+
});
62+
63+
await t.test("`sessionTime` should be equal to `sessionTokenDuration` from #Login", async function () {
64+
// mocklogin
65+
const { idToken, parsedToken } = await mockLogin(email);
66+
67+
await coreKitInstance.init({ handleRedirectResult: false });
68+
69+
await coreKitInstance.loginWithJWT({
70+
verifier: "torus-test-health",
71+
verifierId: parsedToken.email,
72+
idToken,
73+
});
74+
75+
coreKitInstance.signatures.forEach((sig) => {
76+
const parsedSig = JSON.parse(sig);
77+
const parsedSigData = JSON.parse(atob(parsedSig.data));
78+
79+
const sessionTokenDuration = parsedSigData.exp - Math.floor(Date.now() / 1000);
80+
// in success case, sessionTimeDiff (diff between provided sessionTime and generated session token duration from sss-service)
81+
// should not be more than 3s(supposed 3s as the network latency)
82+
const sessionTimeDiff = Math.abs(sessionTokenDuration - sessionTime);
83+
assert.strictEqual(sessionTimeDiff <= 3, true);
84+
});
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)