@toruslabs/customauth
Version:
CustomAuth login with torus to get user private key
103 lines (99 loc) • 4.32 kB
JavaScript
;
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var constants = require('@toruslabs/constants');
var eccrypto = require('@toruslabs/eccrypto');
var httpHelpers = require('@toruslabs/http-helpers');
var metadataHelpers = require('@toruslabs/metadata-helpers');
var enums = require('./enums.js');
var helpers = require('./helpers.js');
var loglevel = require('./loglevel.js');
class StorageHelper {
constructor(serverUrl) {
_defineProperty(this, "currentStorageMethod", enums.REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE);
_defineProperty(this, "isInitialized", false);
_defineProperty(this, "storageServerUrl", constants.SESSION_SERVER_API_URL);
_defineProperty(this, "localStorageAvailable", true);
this.storageServerUrl = serverUrl;
}
get storageMethodUsed() {
return this.currentStorageMethod;
}
init() {
// const support = are3PCSupported();
const localStorageAvailable = helpers.storageAvailable(enums.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;
// } else {
// // use server store as default for storing stuff
// this.currentStorageMethod = REDIRECT_PARAMS_STORAGE_METHOD.SERVER;
// }
this.isInitialized = true;
}
async storeData(key, params) {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.localStorageAvailable) window.localStorage.setItem(key, JSON.stringify(params));
// if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.SERVER) {
const privKey = metadataHelpers.keccak256(Buffer.from(key, "utf8"));
const privKeyHex = privKey.toString("hex");
const publicKeyHex = eccrypto.getPublic(privKey).toString("hex");
const encData = await metadataHelpers.encryptData(privKeyHex, params);
const signature = (await eccrypto.sign(privKey, metadataHelpers.keccak256(Buffer.from(encData, "utf8")))).toString("hex");
await httpHelpers.post(`${this.storageServerUrl}/v2/store/set`, {
key: publicKeyHex,
data: encData,
signature,
allowedOrigin: true
});
// }
}
async retrieveData(key) {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.localStorageAvailable) {
const data = window.localStorage.getItem(key);
if (data) {
this.currentStorageMethod = enums.REDIRECT_PARAMS_STORAGE_METHOD.LOCAL_STORAGE;
return JSON.parse(data || "{}");
}
}
// if (this.currentStorageMethod === REDIRECT_PARAMS_STORAGE_METHOD.SERVER) {
this.currentStorageMethod = enums.REDIRECT_PARAMS_STORAGE_METHOD.SERVER;
const privKey = metadataHelpers.keccak256(Buffer.from(key, "utf8"));
const privKeyHex = privKey.toString("hex");
const publicKeyHex = eccrypto.getPublic(privKey).toString("hex");
try {
const encData = await httpHelpers.post(`${this.storageServerUrl}/v2/store/get`, {
key: publicKeyHex
});
if (encData.message) {
const data = await metadataHelpers.decryptData(privKeyHex, encData.message);
return data;
}
} catch (error) {
if (error.status === 404) {
loglevel.warn(error, "Session likely expired");
} else {
throw error;
}
}
// }
}
clearStorage(key) {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (this.localStorageAvailable) window.localStorage.removeItem(key);
// No need to clear server details cause they auto expire and scope is never re-used for different login attempts
}
clearOrphanedData(baseKey) {
if (!this.isInitialized) throw new Error("StorageHelper is not initialized");
if (!this.localStorageAvailable) return;
const allStorageKeys = Object.keys(window.localStorage);
allStorageKeys.forEach(key => {
if (key.startsWith(baseKey)) {
window.localStorage.removeItem(key);
}
});
// No need to clear server details cause they auto expire and scope is never re-used for different login attempts
}
}
exports.StorageHelper = StorageHelper;