Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use local storage all the time #347

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
"@toruslabs/torus.js": "^15.0.1",
"base64url": "^3.0.1",
"bowser": "^2.11.0",
"eventemitter3": "^5.0.1",
"deepmerge": "^4.3.1",
"eventemitter3": "^5.0.1",
"loglevel": "^1.9.1"
},
"devDependencies": {
"@sentry/types": "^8.22.0",
"@toruslabs/config": "^2.1.1",
"@sentry/types": "^8.23.0",
"@toruslabs/config": "^2.2.0",
"@toruslabs/eslint-config-typescript": "^3.3.3",
"@toruslabs/torus-scripts": "^6.0.3",
"@toruslabs/torus-scripts": "^6.1.0",
"eslint": "^8.57.0",
"husky": "^9.1.4",
"lint-staged": "^15.2.8",
Expand Down
2 changes: 1 addition & 1 deletion src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class CustomAuth {

if (!result)
return {
error: "Init parameters not found. It might be because storage is not available. Please retry the login in a different browser",
error: `Init parameters not found. It might be because storage is not available. Please retry the login in a different browser. Used storage method: ${this.storageHelper.storageMethodUsed}`,
state: instanceParameters || {},
method,
result: {},
Expand Down
39 changes: 22 additions & 17 deletions src/utils/StorageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ class StorageHelper {

private storageServerUrl = "https://session.web3auth.io";

private localStorageAvailable: boolean = true;

constructor(serverUrl: string) {
this.storageServerUrl = serverUrl;
}

get storageMethodUsed(): REDIRECT_PARAMS_STORAGE_METHOD_TYPE {
return this.currentStorageMethod;
}

init() {
const support = are3PCSupported();
const localStorageAvailable = storageAvailable(REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE);
this.localStorageAvailable = localStorageAvailable;
if (support && localStorageAvailable) {
// use local storage as default for storing stuff
this.currentStorageMethod = REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE;
Expand All @@ -40,22 +47,25 @@ class StorageHelper {
const encData = await encryptData(privKeyHex, params);
const signature = (await sign(privKey, keccak256(Buffer.from(encData, "utf8")))).toString("hex");
await post(`${this.storageServerUrl}/store/set`, { key: publicKeyHex, data: encData, signature });
} else {
window.localStorage.setItem(`torus_login_${scope}`, JSON.stringify(params));
}
if (this.localStorageAvailable) window.localStorage.setItem(`torus_login_${scope}`, JSON.stringify(params));
}

async retrieveLoginDetails(scope: string): Promise<LoginDetails> {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.localStorageAvailable) {
const loginDetails = window.localStorage.getItem(`torus_login_${scope}`);
if (loginDetails) return JSON.parse(loginDetails || "{}") as LoginDetails;
}
if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.SERVER) {
const privKey = keccak256(Buffer.from(scope, "utf8"));
const privKeyHex = privKey.toString("hex");
const publicKeyHex = getPublic(privKey).toString("hex");
try {
const encData: { message: string; success: boolean } = await get(`${this.storageServerUrl}/store/get?key=${publicKeyHex}`);
if (encData.message) {
const loginDetails = await decryptData<LoginDetails>(privKeyHex, encData.message);
return loginDetails;
const currentLoginDetails = await decryptData<LoginDetails>(privKeyHex, encData.message);
return currentLoginDetails;
}
} catch (error) {
if ((error as Response).status === 404) {
Expand All @@ -65,28 +75,23 @@ class StorageHelper {
}
}
}
const loginDetails = window.localStorage.getItem(`torus_login_${scope}`);
return JSON.parse(loginDetails || "{}") as LoginDetails;
}

clearLoginDetailsStorage(scope: string): void {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE) {
window.localStorage.removeItem(`torus_login_${scope}`);
}
if (this.localStorageAvailable) window.localStorage.removeItem(`torus_login_${scope}`);
// No need to clear server details cause they auto expire and scope is never re-used for different login attempts
}

clearOrphanedLoginDetails(): void {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE) {
const allStorageKeys = Object.keys(window.localStorage);
allStorageKeys.forEach((key) => {
if (key.startsWith("torus_login_")) {
window.localStorage.removeItem(key);
}
});
}
if (!this.localStorageAvailable) return;
const allStorageKeys = Object.keys(window.localStorage);
allStorageKeys.forEach((key) => {
if (key.startsWith("torus_login_")) {
window.localStorage.removeItem(key);
}
});
// No need to clear server details cause they auto expire and scope is never re-used for different login attempts
}
}
Expand Down