@toruslabs/session-manager
Version:
146 lines (141 loc) • 4.59 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { patch, put, post, get } from '@toruslabs/http-helpers';
import { generatePrivate, getPublic, sign } from '@toruslabs/eccrypto';
import { encryptData, keccak256, decryptData } from '@toruslabs/metadata-helpers';
class BaseSessionManager {
constructor() {
_defineProperty(this, "sessionId", void 0);
}
checkSessionParams() {
if (!this.sessionId) throw new Error("Session id is required");
this.sessionId = this.sessionId.padStart(64, "0");
}
/**
* Common handler method for making an http request.
*
* Note: Embed all the query parameters in the path itself.
*/
request({
method = "GET",
url,
data = {},
headers = {}
}) {
const options = {
headers
};
switch (method) {
case "GET":
return get(url, options);
case "POST":
return post(url, data, options);
case "PUT":
return put(url, data, options);
case "PATCH":
return patch(url, data, options);
}
throw new Error("Invalid method type");
}
}
const DEFAULT_SESSION_TIMEOUT = 86400;
class SessionManager extends BaseSessionManager {
constructor({
sessionServerBaseUrl,
sessionNamespace,
sessionTime,
sessionId
} = {}) {
super();
_defineProperty(this, "sessionServerBaseUrl", "https://session.web3auth.io");
_defineProperty(this, "sessionNamespace", void 0);
_defineProperty(this, "sessionTime", DEFAULT_SESSION_TIMEOUT);
_defineProperty(this, "sessionId", "");
if (sessionServerBaseUrl) {
this.sessionServerBaseUrl = sessionServerBaseUrl;
}
if (sessionNamespace) this.sessionNamespace = sessionNamespace;
if (sessionTime) this.sessionTime = sessionTime;
if (sessionId) this.sessionId = sessionId.padStart(64, "0");
}
static generateRandomSessionKey() {
return generatePrivate().toString("hex").padStart(64, "0");
}
async createSession(data) {
super.checkSessionParams();
const privKey = Buffer.from(this.sessionId, "hex");
const pubKey = getPublic(privKey).toString("hex");
const encData = await encryptData(this.sessionId, data);
const signature = (await sign(privKey, keccak256(Buffer.from(encData, "utf8")))).toString("hex");
const body = {
key: pubKey,
data: encData,
signature,
namespace: this.sessionNamespace,
timeout: this.sessionTime
};
await super.request({
method: "POST",
url: `${this.sessionServerBaseUrl}/store/set`,
data: body
});
return this.sessionId;
}
async authorizeSession() {
super.checkSessionParams();
const pubkey = getPublic(Buffer.from(this.sessionId, "hex")).toString("hex");
const url = new URL(`${this.sessionServerBaseUrl}/store/get`);
url.searchParams.append("key", pubkey);
if (this.sessionNamespace) url.searchParams.append("namespace", this.sessionNamespace);
const result = await super.request({
url: url.toString()
});
if (!result.message) {
throw new Error("Session Expired or Invalid public key");
}
const response = await decryptData(this.sessionId, result.message);
if (response.error) {
throw new Error("There was an error decrypting data.");
}
return response;
}
async updateSession(data) {
super.checkSessionParams();
const privKey = Buffer.from(this.sessionId, "hex");
const pubKey = getPublic(privKey).toString("hex");
const encData = await encryptData(this.sessionId, data);
const signature = (await sign(privKey, keccak256(Buffer.from(encData, "utf8")))).toString("hex");
const body = {
key: pubKey,
data: encData,
signature,
namespace: this.sessionNamespace
};
await super.request({
method: "PUT",
url: `${this.sessionServerBaseUrl}/store/update`,
data: body
});
}
async invalidateSession() {
super.checkSessionParams();
const privKey = Buffer.from(this.sessionId, "hex");
const pubKey = getPublic(privKey).toString("hex");
const encData = await encryptData(this.sessionId, {});
const signature = (await sign(privKey, keccak256(Buffer.from(encData, "utf8")))).toString("hex");
const data = {
key: pubKey,
data: encData,
signature,
namespace: this.sessionNamespace,
timeout: 1
};
await super.request({
method: "POST",
url: `${this.sessionServerBaseUrl}/store/set`,
data
});
this.sessionId = "";
return true;
}
}
export { BaseSessionManager, SessionManager };