Skip to content

Commit

Permalink
fix: rename disableSessionManager
Browse files Browse the repository at this point in the history
fix tests, add tests
fix feature gating bug
  • Loading branch information
ieow committed Nov 5, 2024
1 parent d68b165 commit 3ae70a6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ export interface Web3AuthOptions {
storage: IAsyncStorage | IStorage;

/**
* @defaultValue true
* enable session manager creation
* signatures from newtorks will still expired after sessionTime if session manager is disabled
* @defaultValue false
* disable session manager creation
* signatures from web3auth newtorks will still expired after sessionTime if session manager is disabled
*/
enableSessionManager?: boolean;
disableSessionManager?: boolean;

/**
* @defaultValue 86400
Expand Down
20 changes: 9 additions & 11 deletions src/mpcCoreKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
if (!options.baseUrl) options.baseUrl = isNodejsOrRN ? "https://localhost" : `${window?.location.origin}/serviceworker`;
if (!options.disableHashedFactorKey) options.disableHashedFactorKey = false;
if (!options.hashedFactorNonce) options.hashedFactorNonce = options.web3AuthClientId;
if (options.enableSessionManager === undefined) options.enableSessionManager = true;
if (options.disableSessionManager === undefined) options.disableSessionManager = false;

this.options = options as Web3AuthOptionsWithDefaults;

this.currentStorage = new AsyncStorage(this._storageBaseKey, options.storage);

if (options.enableSessionManager) {
if (!options.disableSessionManager) {
this.sessionManager = new SessionManager<SessionData>({
sessionTime: options.sessionTime,
});
Expand Down Expand Up @@ -292,8 +292,10 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
// on failed redirect, instance is reseted.
// skip check feature gating on redirection as it was check before login
await this.handleRedirectResult();
// if not redirect flow try to rehydrate session if available
// return after redirect, the rest of the code will not be executed
return;
} else if (params.rehydrate && this.sessionManager) {
// if not redirect flow try to rehydrate session if available
const sessionId = await this.currentStorage.get<string>("sessionId");
if (sessionId) {
this.sessionManager.sessionId = sessionId;
Expand All @@ -306,15 +308,11 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
// try rehydrate session
if (sessionResult) {
await this.rehydrateSession(sessionResult);
} else {
// feature gating on no session rehydration
await this.featureRequest();
}
}
} else {
// feature gating if not redirect flow or session rehydration
await this.featureRequest();
}
// feature gating if not redirect flow or session rehydration
await this.featureRequest();
}

public async loginWithOAuth(params: OAuthLoginParams): Promise<void> {
Expand Down Expand Up @@ -1043,13 +1041,13 @@ export class Web3AuthMPCCoreKit implements ICoreKit {
userInfo: result.userInfo,
});
} catch (err) {
log.error("error trying to authorize session", err);
log.warn("failed to authorize session", err);
}
}

private async createSession() {
if (!this.sessionManager) {
log.info("sessionTime is 0, skipping session creation");
log.warn("sessionManager is not available");
return;
}

Expand Down
78 changes: 61 additions & 17 deletions tests/sessionTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ type TestVariable = {
email: string;
gated?: boolean;
sessionTime?: number;
disableSessionManager?: boolean;
};

const defaultTestEmail = "testEmail1";

const isBasePlan = (id: string) => id === "BCriFlI9ihm81N-bc7x6N-xbqwBLuxfRDMmSH87spKH27QTNOPj1W9s2K3-mp9NzXuaRiqxvAGHyuGlXG5wLD1g";
// BasePlan up to 1 day only
const variable: TestVariable[] = [
{ web3AuthNetwork: WEB3AUTH_NETWORK.DEVNET, uxMode: "nodejs", email: defaultTestEmail, web3ClientID: "torus-key-test", sessionTime: 3600 },
{
Expand All @@ -34,46 +38,78 @@ const variable: TestVariable[] = [
web3ClientID: "BCriFlI9ihm81N-bc7x6N-xbqwBLuxfRDMmSH87spKH27QTNOPj1W9s2K3-mp9NzXuaRiqxvAGHyuGlXG5wLD1g",
sessionTime: 172800,
},
{
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
uxMode: "nodejs",
email: defaultTestEmail,
web3ClientID: "BJ57yveG_XBLqZUpjtJCnJMrord0AaXpd_9OSy4HzkxpnpPn6Co73h-vR6GEI1VogtW4yMHq13GNPKmVpliFXY0",
sessionTime: 7200,
disableSessionManager : false
},

{
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
uxMode: "nodejs",
email: defaultTestEmail,
web3ClientID: "BJ57yveG_XBLqZUpjtJCnJMrord0AaXpd_9OSy4HzkxpnpPn6Co73h-vR6GEI1VogtW4yMHq13GNPKmVpliFXY0",
sessionTime: 7200,
disableSessionManager : true
},
];

const storageInstance = new MemoryStorage();
variable.forEach((testVariable) => {
const { web3AuthNetwork, uxMode, manualSync, email, web3ClientID: web3AuthClientId, sessionTime } = variable[0];
const coreKitInstance = new Web3AuthMPCCoreKit({
web3AuthClientId,
web3AuthNetwork,
baseUrl: "http://localhost:3000",
uxMode,
tssLib,
storage: storageInstance,
manualSync,
sessionTime,
});
variable.forEach(async (testVariable) => {
const { web3AuthNetwork, uxMode, manualSync, email, web3ClientID: web3AuthClientId, sessionTime, disableSessionManager } = testVariable;


await test(`#Variable SessionTime test : ${JSON.stringify({ sessionTime: testVariable.sessionTime })} - disableSessionManager: ${disableSessionManager} client_id: ${web3AuthClientId}`, async (t) => {
const coreKitInstance = new Web3AuthMPCCoreKit({
web3AuthClientId,
web3AuthNetwork,
baseUrl: "http://localhost:3000",
uxMode,
tssLib,
storage: storageInstance,
manualSync,
sessionTime,
disableSessionManager,
});

test(`#Variable SessionTime test : ${JSON.stringify({ sessionTime: testVariable.sessionTime })}`, async (t) => {
async function beforeTest() {
if (coreKitInstance.status === COREKIT_STATUS.INITIALIZED) await criticalResetAccount(coreKitInstance);
}

t.after(async function () {
// after all test tear down
await coreKitInstance.logout();
if (!isBasePlan(web3AuthClientId)) await coreKitInstance.logout();
});

await beforeTest();

await t.test("`sessionTime` should be equal to `sessionTokenDuration` from #Login", async function () {
await t.test("`sessionTime` should be equal to `sessionTokenDuration` from #Login", async function (t) {
// mocklogin
const { idToken, parsedToken } = await mockLogin(email);

await coreKitInstance.init({ handleRedirectResult: false });
await coreKitInstance.init({ handleRedirectResult: false }).catch((err) => {
if ( !isBasePlan(testVariable.web3ClientID) ) {
throw err;
}
});

await coreKitInstance.loginWithJWT({
verifier: "torus-test-health",
verifierId: parsedToken.email,
idToken,
}).catch((err) => {
if ( !isBasePlan(testVariable.web3ClientID) ) {
throw err;
}
});

if ( !isBasePlan(testVariable.web3ClientID) ) {
// skip remaining test if is BasePlan
return;
}

coreKitInstance.signatures.forEach((sig) => {
const parsedSig = JSON.parse(sig);
const parsedSigData = JSON.parse(atob(parsedSig.data));
Expand All @@ -84,6 +120,14 @@ variable.forEach((testVariable) => {
const sessionTimeDiff = Math.abs(sessionTokenDuration - sessionTime);
assert.strictEqual(sessionTimeDiff <= 3, true);
});

if (disableSessionManager) {
assert.equal(coreKitInstance.sessionId , undefined);
} else {
assert.notEqual(coreKitInstance.sessionId , undefined);
}
});


});
});

0 comments on commit 3ae70a6

Please sign in to comment.