This commit is contained in:
Frank
2025-10-03 23:48:29 -04:00
parent 477586835a
commit a11a608760
9 changed files with 1488 additions and 32 deletions

View File

@@ -19,7 +19,6 @@ export namespace Key {
}
export const create = fn(z.object({ name: z.string().min(1).max(255) }), async (input) => {
const workspaceID = Actor.workspace()
const { name } = input
// Generate secret key: sk- + 64 random characters (upper, lower, numbers)
@@ -31,45 +30,32 @@ export namespace Key {
secretKey += chars[array[i] % chars.length]
}
const keyID = Identifier.create("key")
const user = Actor.assert("user")
await Database.use((tx) =>
tx.insert(KeyTable).values({
id: keyID,
workspaceID,
actor: Actor.use(),
workspaceID: Actor.workspace(),
actor: user,
userID: user.properties.userID,
name,
key: secretKey,
timeUsed: null,
}),
).catch((e: any) => {
if (e.message.match(/Duplicate entry '.*' for key 'key.name'/))
throw new Error("A key with this name already exists. Please choose a different name.")
throw e
})
)
return keyID
})
export const remove = fn(z.object({ id: z.string() }), async (input) => {
const workspace = Actor.workspace()
await Database.transaction(async (tx) => {
const row = await tx
.select({
name: KeyTable.name,
})
.from(KeyTable)
.where(and(eq(KeyTable.id, input.id), eq(KeyTable.workspaceID, workspace)))
.then((rows) => rows[0])
if (!row) return
await tx
await Database.transaction((tx) =>
tx
.update(KeyTable)
.set({
timeDeleted: sql`now()`,
oldName: row.name,
name: input.id, // Use the key ID as the name
})
.where(and(eq(KeyTable.id, input.id), eq(KeyTable.workspaceID, workspace)))
})
.where(and(eq(KeyTable.id, input.id), eq(KeyTable.workspaceID, workspace))),
)
})
}

View File

@@ -1,5 +1,5 @@
import { mysqlTable, varchar, uniqueIndex, json } from "drizzle-orm/mysql-core"
import { timestamps, utc, workspaceColumns } from "../drizzle/types"
import { timestamps, ulid, utc, workspaceColumns } from "../drizzle/types"
import { workspaceIndexes } from "./workspace.sql"
import { Actor } from "../actor"
@@ -12,11 +12,8 @@ export const KeyTable = mysqlTable(
name: varchar("name", { length: 255 }).notNull(),
oldName: varchar("old_name", { length: 255 }),
key: varchar("key", { length: 255 }).notNull(),
userID: ulid("user_id"),
timeUsed: utc("time_used"),
},
(table) => [
...workspaceIndexes(table),
uniqueIndex("global_key").on(table.key),
uniqueIndex("name").on(table.workspaceID, table.name),
],
(table) => [...workspaceIndexes(table), uniqueIndex("global_key").on(table.key)],
)

View File

@@ -151,9 +151,10 @@ export namespace User {
await Promise.all(
invitations.map((invite) =>
Actor.provide(
"system",
"user",
{
workspaceID: invite.workspaceID,
userID: invite.id,
},
() => Key.create({ name: "Default API Key" }),
),

View File

@@ -12,13 +12,14 @@ export namespace Workspace {
export const create = fn(z.void(), async () => {
const account = Actor.assert("account")
const workspaceID = Identifier.create("workspace")
const userID = Identifier.create("user")
await Database.transaction(async (tx) => {
await tx.insert(WorkspaceTable).values({
id: workspaceID,
})
await tx.insert(UserTable).values({
workspaceID,
id: Identifier.create("user"),
id: userID,
accountID: account.properties.accountID,
name: "",
role: "admin",
@@ -30,9 +31,10 @@ export namespace Workspace {
})
})
await Actor.provide(
"system",
"user",
{
workspaceID,
userID,
},
() => Key.create({ name: "Default API Key" }),
)