@toruslabs/session-manager
Version:
109 lines (106 loc) • 3.74 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { generatePrivate, getPublic, sign } from '@toruslabs/eccrypto';
import { encryptData, keccak256, decryptData } from '@toruslabs/metadata-helpers';
import { BaseSessionManager } from './base.js';
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 { SessionManager };