@cipherstash/jseql
Version:
Encrypted Query Language JavaScript Library
104 lines (102 loc) • 2.79 kB
JavaScript
// ../utils/logger/index.ts
function getLevelValue(level) {
switch (level) {
case "debug":
return 10;
case "info":
return 20;
case "error":
return 30;
default:
return 30;
}
}
var envLogLevel = process.env.JSEQL_LOG_LEVEL || "info";
var currentLevel = getLevelValue(envLogLevel);
function debug(...args) {
if (currentLevel <= getLevelValue("debug")) {
console.debug("[jseql] DEBUG", ...args);
}
}
function info(...args) {
if (currentLevel <= getLevelValue("info")) {
console.info("[jseql] INFO", ...args);
}
}
function error(...args) {
if (currentLevel <= getLevelValue("error")) {
console.error("[jseql] ERROR", ...args);
}
}
var logger = {
debug,
info,
error
};
// src/identify/index.ts
var LockContext = class {
ctsToken;
workspaceId;
context;
constructor({
context = { identityClaim: ["sub"] },
ctsToken
} = {}) {
if (!process.env.CS_WORKSPACE_ID) {
const errorMessage = "CS_WORKSPACE_ID environment variable is not set, and is required to initialize a LockContext.";
logger.error(errorMessage);
throw new Error(`[jseql]: ${errorMessage}`);
}
if (ctsToken) {
this.ctsToken = ctsToken;
}
this.workspaceId = process.env.CS_WORKSPACE_ID;
this.context = context;
logger.debug("Successfully initialized the EQL lock context.");
}
async identify(jwtToken) {
const workspaceId = this.workspaceId;
const ctsEndoint = process.env.CS_CTS_ENDPOINT || "https://ap-southeast-2.aws.auth.viturhosted.net";
const ctsResponse = await fetch(`${ctsEndoint}/api/authorize`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
workspaceId,
oidcToken: jwtToken
})
});
if (!ctsResponse.ok) {
throw new Error(
`[jseql]: Failed to fetch CTS token: ${ctsResponse.statusText}`
);
}
const ctsToken = await ctsResponse.json();
if (!ctsToken.accessToken) {
const errorMessage = "The response from the CipherStash API did not contain an access token. Please contact support.";
logger.error(errorMessage);
throw new Error(errorMessage);
}
this.ctsToken = ctsToken;
return this;
}
getLockContext() {
if (!this.ctsToken?.accessToken && !this.ctsToken?.expiry) {
return {
success: false,
error: "The CTS token is not set. Please call identify() with a users JWT token, or pass an existing CTS token to the LockContext constructor before calling getLockContext()."
};
}
return {
success: true,
context: this.context,
ctsToken: this.ctsToken
};
}
};
export {
logger,
LockContext
};
//# sourceMappingURL=chunk-2GFWYC6S.js.map