@kognitivedev/workspace-auth 0.2.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +2 -0
- package/CHANGELOG.md +7 -0
- package/dist/crypto.d.ts +10 -0
- package/dist/crypto.js +41 -0
- package/dist/db.d.ts +6 -0
- package/dist/db.js +74 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +35 -0
- package/dist/schema.d.ts +817 -0
- package/dist/schema.js +80 -0
- package/dist/workspace.d.ts +197 -0
- package/dist/workspace.js +300 -0
- package/package.json +32 -0
- package/src/__tests__/crypto.test.ts +29 -0
- package/src/crypto.ts +50 -0
- package/src/db.ts +45 -0
- package/src/index.ts +36 -0
- package/src/schema.ts +82 -0
- package/src/workspace.ts +405 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
package/CHANGELOG.md
ADDED
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type EncryptedSecret = {
|
|
2
|
+
encryptedKey: string;
|
|
3
|
+
iv: string;
|
|
4
|
+
authTag: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function createRawApiKey(): string;
|
|
7
|
+
export declare function getApiKeyPrefix(rawKey: string): string;
|
|
8
|
+
export declare function hashApiKey(rawKey: string): string;
|
|
9
|
+
export declare function encryptManagedSecret(rawSecret: string, secret?: string): EncryptedSecret;
|
|
10
|
+
export declare function decryptManagedSecret(encrypted: EncryptedSecret, secret?: string): string;
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRawApiKey = createRawApiKey;
|
|
4
|
+
exports.getApiKeyPrefix = getApiKeyPrefix;
|
|
5
|
+
exports.hashApiKey = hashApiKey;
|
|
6
|
+
exports.encryptManagedSecret = encryptManagedSecret;
|
|
7
|
+
exports.decryptManagedSecret = decryptManagedSecret;
|
|
8
|
+
const node_crypto_1 = require("node:crypto");
|
|
9
|
+
function createRawApiKey() {
|
|
10
|
+
return `kog_${(0, node_crypto_1.randomBytes)(16).toString("hex")}`;
|
|
11
|
+
}
|
|
12
|
+
function getApiKeyPrefix(rawKey) {
|
|
13
|
+
return rawKey.slice(0, 8);
|
|
14
|
+
}
|
|
15
|
+
function hashApiKey(rawKey) {
|
|
16
|
+
return (0, node_crypto_1.createHash)("sha256").update(rawKey).digest("hex");
|
|
17
|
+
}
|
|
18
|
+
function getEncryptionKey(secret = process.env.KOGNITIVE_MANAGED_CREDENTIAL_SECRET) {
|
|
19
|
+
if (!secret || Buffer.byteLength(secret, "utf8") < 32) {
|
|
20
|
+
throw new Error("KOGNITIVE_MANAGED_CREDENTIAL_SECRET must be set to a stable 32+ byte secret");
|
|
21
|
+
}
|
|
22
|
+
return (0, node_crypto_1.createHash)("sha256").update(secret).digest();
|
|
23
|
+
}
|
|
24
|
+
function encryptManagedSecret(rawSecret, secret) {
|
|
25
|
+
const iv = (0, node_crypto_1.randomBytes)(12);
|
|
26
|
+
const cipher = (0, node_crypto_1.createCipheriv)("aes-256-gcm", getEncryptionKey(secret), iv);
|
|
27
|
+
const encrypted = Buffer.concat([cipher.update(rawSecret, "utf8"), cipher.final()]);
|
|
28
|
+
return {
|
|
29
|
+
encryptedKey: encrypted.toString("base64"),
|
|
30
|
+
iv: iv.toString("base64"),
|
|
31
|
+
authTag: cipher.getAuthTag().toString("base64"),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function decryptManagedSecret(encrypted, secret) {
|
|
35
|
+
const decipher = (0, node_crypto_1.createDecipheriv)("aes-256-gcm", getEncryptionKey(secret), Buffer.from(encrypted.iv, "base64"));
|
|
36
|
+
decipher.setAuthTag(Buffer.from(encrypted.authTag, "base64"));
|
|
37
|
+
return Buffer.concat([
|
|
38
|
+
decipher.update(Buffer.from(encrypted.encryptedKey, "base64")),
|
|
39
|
+
decipher.final(),
|
|
40
|
+
]).toString("utf8");
|
|
41
|
+
}
|
package/dist/db.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import postgres from "postgres";
|
|
2
|
+
import * as schema from "./schema";
|
|
3
|
+
export declare function getWorkspaceAuthDb(): import("drizzle-orm/postgres-js").PostgresJsDatabase<typeof schema> & {
|
|
4
|
+
$client: postgres.Sql<{}>;
|
|
5
|
+
};
|
|
6
|
+
export type WorkspaceAuthDb = ReturnType<typeof getWorkspaceAuthDb>;
|
package/dist/db.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.getWorkspaceAuthDb = getWorkspaceAuthDb;
|
|
40
|
+
const postgres_js_1 = require("drizzle-orm/postgres-js");
|
|
41
|
+
const postgres_1 = __importDefault(require("postgres"));
|
|
42
|
+
const schema = __importStar(require("./schema"));
|
|
43
|
+
const globalForWorkspaceAuthDb = globalThis;
|
|
44
|
+
function readPositiveInteger(value, fallback) {
|
|
45
|
+
if (!value)
|
|
46
|
+
return fallback;
|
|
47
|
+
const parsed = Number.parseInt(value, 10);
|
|
48
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
49
|
+
}
|
|
50
|
+
function shouldPrepareStatements(connectionString) {
|
|
51
|
+
var _a;
|
|
52
|
+
const explicit = process.env.DATABASE_PREPARE_STATEMENTS;
|
|
53
|
+
if (explicit) {
|
|
54
|
+
return explicit !== "0" && explicit.toLowerCase() !== "false";
|
|
55
|
+
}
|
|
56
|
+
const poolingMode = (_a = process.env.DATABASE_POOL_MODE) !== null && _a !== void 0 ? _a : process.env.PGBOUNCER_POOL_MODE;
|
|
57
|
+
if ((poolingMode === null || poolingMode === void 0 ? void 0 : poolingMode.toLowerCase()) === "transaction")
|
|
58
|
+
return false;
|
|
59
|
+
return !connectionString.toLowerCase().includes("pgbouncer");
|
|
60
|
+
}
|
|
61
|
+
function getWorkspaceAuthDb() {
|
|
62
|
+
var _a, _b, _c;
|
|
63
|
+
const connectionString = process.env.DATABASE_URL || "postgres://postgres:password@localhost:5432/cognitive_layer";
|
|
64
|
+
const client = (_a = globalForWorkspaceAuthDb.workspaceAuthDbClient) !== null && _a !== void 0 ? _a : (0, postgres_1.default)(connectionString, {
|
|
65
|
+
max: readPositiveInteger((_b = process.env.DATABASE_POOL_MAX) !== null && _b !== void 0 ? _b : process.env.POSTGRES_POOL_MAX, 20),
|
|
66
|
+
idle_timeout: readPositiveInteger(process.env.DATABASE_IDLE_TIMEOUT_SECONDS, 30),
|
|
67
|
+
connect_timeout: readPositiveInteger(process.env.DATABASE_CONNECT_TIMEOUT_SECONDS, 10),
|
|
68
|
+
prepare: shouldPrepareStatements(connectionString),
|
|
69
|
+
});
|
|
70
|
+
const db = (_c = globalForWorkspaceAuthDb.workspaceAuthDb) !== null && _c !== void 0 ? _c : (0, postgres_js_1.drizzle)(client, { schema });
|
|
71
|
+
globalForWorkspaceAuthDb.workspaceAuthDbClient = client;
|
|
72
|
+
globalForWorkspaceAuthDb.workspaceAuthDb = db;
|
|
73
|
+
return db;
|
|
74
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { ACTIVE_PROJECT_ID_COOKIE, ACTIVE_PROJECT_SLUG_COOKIE, KOGNITIV_APPOINTMENTS_MANAGED_KEY_NAME, KOGNITIV_APPOINTMENTS_PRODUCT, KOGNITIV_VOICE_MANAGED_KEY_NAME, KOGNITIV_VOICE_PRODUCT, createWorkspaceProject, ensureManagedProjectApiKey, ensureOrganizationForClerk, getOwnedProject, getProductSetupState, hasManagedProjectApiKey, listOrganizationProjects, resolveActiveProject, resolveProductSetupWorkspace, slugifyWorkspaceValue, updateProductSetupState, type ProductSetupStateValue, } from "./workspace";
|
|
2
|
+
export { createRawApiKey, decryptManagedSecret, encryptManagedSecret, getApiKeyPrefix, hashApiKey, type EncryptedSecret, } from "./crypto";
|
|
3
|
+
export { getWorkspaceAuthDb, type WorkspaceAuthDb } from "./db";
|
|
4
|
+
export { apiKeys, managedProjectApiKeys, organizations, productSetupStates, projects, } from "./schema";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.projects = exports.productSetupStates = exports.organizations = exports.managedProjectApiKeys = exports.apiKeys = exports.getWorkspaceAuthDb = exports.hashApiKey = exports.getApiKeyPrefix = exports.encryptManagedSecret = exports.decryptManagedSecret = exports.createRawApiKey = exports.updateProductSetupState = exports.slugifyWorkspaceValue = exports.resolveProductSetupWorkspace = exports.resolveActiveProject = exports.listOrganizationProjects = exports.hasManagedProjectApiKey = exports.getProductSetupState = exports.getOwnedProject = exports.ensureOrganizationForClerk = exports.ensureManagedProjectApiKey = exports.createWorkspaceProject = exports.KOGNITIV_VOICE_PRODUCT = exports.KOGNITIV_VOICE_MANAGED_KEY_NAME = exports.KOGNITIV_APPOINTMENTS_PRODUCT = exports.KOGNITIV_APPOINTMENTS_MANAGED_KEY_NAME = exports.ACTIVE_PROJECT_SLUG_COOKIE = exports.ACTIVE_PROJECT_ID_COOKIE = void 0;
|
|
4
|
+
var workspace_1 = require("./workspace");
|
|
5
|
+
Object.defineProperty(exports, "ACTIVE_PROJECT_ID_COOKIE", { enumerable: true, get: function () { return workspace_1.ACTIVE_PROJECT_ID_COOKIE; } });
|
|
6
|
+
Object.defineProperty(exports, "ACTIVE_PROJECT_SLUG_COOKIE", { enumerable: true, get: function () { return workspace_1.ACTIVE_PROJECT_SLUG_COOKIE; } });
|
|
7
|
+
Object.defineProperty(exports, "KOGNITIV_APPOINTMENTS_MANAGED_KEY_NAME", { enumerable: true, get: function () { return workspace_1.KOGNITIV_APPOINTMENTS_MANAGED_KEY_NAME; } });
|
|
8
|
+
Object.defineProperty(exports, "KOGNITIV_APPOINTMENTS_PRODUCT", { enumerable: true, get: function () { return workspace_1.KOGNITIV_APPOINTMENTS_PRODUCT; } });
|
|
9
|
+
Object.defineProperty(exports, "KOGNITIV_VOICE_MANAGED_KEY_NAME", { enumerable: true, get: function () { return workspace_1.KOGNITIV_VOICE_MANAGED_KEY_NAME; } });
|
|
10
|
+
Object.defineProperty(exports, "KOGNITIV_VOICE_PRODUCT", { enumerable: true, get: function () { return workspace_1.KOGNITIV_VOICE_PRODUCT; } });
|
|
11
|
+
Object.defineProperty(exports, "createWorkspaceProject", { enumerable: true, get: function () { return workspace_1.createWorkspaceProject; } });
|
|
12
|
+
Object.defineProperty(exports, "ensureManagedProjectApiKey", { enumerable: true, get: function () { return workspace_1.ensureManagedProjectApiKey; } });
|
|
13
|
+
Object.defineProperty(exports, "ensureOrganizationForClerk", { enumerable: true, get: function () { return workspace_1.ensureOrganizationForClerk; } });
|
|
14
|
+
Object.defineProperty(exports, "getOwnedProject", { enumerable: true, get: function () { return workspace_1.getOwnedProject; } });
|
|
15
|
+
Object.defineProperty(exports, "getProductSetupState", { enumerable: true, get: function () { return workspace_1.getProductSetupState; } });
|
|
16
|
+
Object.defineProperty(exports, "hasManagedProjectApiKey", { enumerable: true, get: function () { return workspace_1.hasManagedProjectApiKey; } });
|
|
17
|
+
Object.defineProperty(exports, "listOrganizationProjects", { enumerable: true, get: function () { return workspace_1.listOrganizationProjects; } });
|
|
18
|
+
Object.defineProperty(exports, "resolveActiveProject", { enumerable: true, get: function () { return workspace_1.resolveActiveProject; } });
|
|
19
|
+
Object.defineProperty(exports, "resolveProductSetupWorkspace", { enumerable: true, get: function () { return workspace_1.resolveProductSetupWorkspace; } });
|
|
20
|
+
Object.defineProperty(exports, "slugifyWorkspaceValue", { enumerable: true, get: function () { return workspace_1.slugifyWorkspaceValue; } });
|
|
21
|
+
Object.defineProperty(exports, "updateProductSetupState", { enumerable: true, get: function () { return workspace_1.updateProductSetupState; } });
|
|
22
|
+
var crypto_1 = require("./crypto");
|
|
23
|
+
Object.defineProperty(exports, "createRawApiKey", { enumerable: true, get: function () { return crypto_1.createRawApiKey; } });
|
|
24
|
+
Object.defineProperty(exports, "decryptManagedSecret", { enumerable: true, get: function () { return crypto_1.decryptManagedSecret; } });
|
|
25
|
+
Object.defineProperty(exports, "encryptManagedSecret", { enumerable: true, get: function () { return crypto_1.encryptManagedSecret; } });
|
|
26
|
+
Object.defineProperty(exports, "getApiKeyPrefix", { enumerable: true, get: function () { return crypto_1.getApiKeyPrefix; } });
|
|
27
|
+
Object.defineProperty(exports, "hashApiKey", { enumerable: true, get: function () { return crypto_1.hashApiKey; } });
|
|
28
|
+
var db_1 = require("./db");
|
|
29
|
+
Object.defineProperty(exports, "getWorkspaceAuthDb", { enumerable: true, get: function () { return db_1.getWorkspaceAuthDb; } });
|
|
30
|
+
var schema_1 = require("./schema");
|
|
31
|
+
Object.defineProperty(exports, "apiKeys", { enumerable: true, get: function () { return schema_1.apiKeys; } });
|
|
32
|
+
Object.defineProperty(exports, "managedProjectApiKeys", { enumerable: true, get: function () { return schema_1.managedProjectApiKeys; } });
|
|
33
|
+
Object.defineProperty(exports, "organizations", { enumerable: true, get: function () { return schema_1.organizations; } });
|
|
34
|
+
Object.defineProperty(exports, "productSetupStates", { enumerable: true, get: function () { return schema_1.productSetupStates; } });
|
|
35
|
+
Object.defineProperty(exports, "projects", { enumerable: true, get: function () { return schema_1.projects; } });
|