Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🔥 Remove personal api keys (#2114)

Luke Vellae755885eddf0
diff --git a/packages/database/prisma/migrations/20260107155716_remove_user_api_keys/migration.sql b/packages/database/prisma/migrations/20260107155716_remove_user_api_keys/migration.sql
new file mode 100644
index 000000000..a7fc6bad2
--- /dev/null
+++ b/packages/database/prisma/migrations/20260107155716_remove_user_api_keys/migration.sql
@@ -0,0 +1,11 @@
+/*
+ Warnings:
+
+ - You are about to drop the `api_keys` table. If the table is not empty, all the data it contains will be lost.
+
+*/
+-- DropForeignKey
+ALTER TABLE "api_keys" DROP CONSTRAINT "api_keys_user_id_fkey";
+
+-- DropTable
+DROP TABLE "api_keys";
diff --git a/packages/database/prisma/models/user.prisma b/packages/database/prisma/models/user.prisma
index 95d03ebb6..8c061dc72 100644
--- a/packages/database/prisma/models/user.prisma
+++ b/packages/database/prisma/models/user.prisma
@@ -64,7 +64,6 @@ model User {
participants Participant[]
paymentMethods PaymentMethod[]
spaceMemberInvites SpaceMemberInvite[]
- apiKeys ApiKey[]
subscriptions Subscription[] @relation("UserToSubscription")
spaces Space[] @relation("UserSpaces")
@@ -87,29 +86,6 @@ model User {
@@map("users")
}
-model ApiKey {
- id String @id @default(cuid())
- userId String @map("user_id")
- name String
- prefix String @unique
- hashedKey String @map("hashed_key") @db.Text
-
- lastUsedAt DateTime? @map("last_used_at")
- expiresAt DateTime? @map("expires_at")
- revokedAt DateTime? @map("revoked_at")
-
- createdAt DateTime @default(now()) @map("created_at")
- updatedAt DateTime @updatedAt @map("updated_at")
-
- user User @relation(fields: [userId], references: [id], onDelete: Cascade)
-
- @@unique([userId, name], name: "user_api_key_name_unique")
- @@index([userId], name: "api_key_user_idx")
- @@index([expiresAt], name: "api_key_expires_idx")
- @@index([revokedAt], name: "api_key_revoked_idx")
- @@map("api_keys")
-}
-
model VerificationToken {
identifier String @db.Citext
token String @unique
diff --git a/packages/database/prisma/scripts/create-api-key.ts b/packages/database/prisma/scripts/create-api-key.ts
deleted file mode 100644
index b101043be..000000000
--- a/packages/database/prisma/scripts/create-api-key.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import { PrismaClient } from "@prisma/client";
-import crypto from "node:crypto";
-
-const prisma = new PrismaClient();
-
-type Args = {
- userId?: string;
- userEmail?: string;
- name?: string;
- expiresAt?: string;
-};
-
-const parseArgs = (argv: string[]) => {
- return argv.reduce<Args>((acc, arg) => {
- const [key, value] = arg.split("=");
- if (!key?.startsWith("--")) {
- return acc;
- }
- const normalizedKey = key.slice(2);
- const normalizedValue = value?.trim() || undefined;
- if (!normalizedValue) {
- return acc;
- }
- if (normalizedKey === "user-id") {
- return { ...acc, userId: normalizedValue };
- }
- if (normalizedKey === "user-email") {
- return { ...acc, userEmail: normalizedValue };
- }
- if (normalizedKey === "name") {
- return { ...acc, name: normalizedValue };
- }
- if (normalizedKey === "expires-at") {
- return { ...acc, expiresAt: normalizedValue };
- }
- return acc;
- }, {});
-};
-
-const randomToken = (bytes: number) => crypto.randomBytes(bytes).toString("base64url");
-
-const sha256Hex = (value: string) =>
- crypto.createHash("sha256").update(value).digest("hex");
-
-async function main() {
- const args = parseArgs(process.argv.slice(2));
-
- if (!args.name) {
- throw new Error("Missing required arg: --name");
- }
-
- if (!args.userId && !args.userEmail) {
- throw new Error("Missing required arg: --user-id or --user-email");
- }
-
- const user = args.userId
- ? await prisma.user.findUnique({ where: { id: args.userId }, select: { id: true } })
- : await prisma.user.findUnique({
- where: { email: args.userEmail },
- select: { id: true },
- });
-
- if (!user) {
- throw new Error("User not found");
- }
-
- const prefix = randomToken(6);
- const secret = randomToken(24);
- const apiKey = `rly_${prefix}_${secret}`;
-
- const salt = randomToken(12);
- const hashedKey = `sha256$${salt}$${sha256Hex(`${salt}:${apiKey}`)}`;
-
- const expiresAt = args.expiresAt ? new Date(args.expiresAt) : undefined;
- if (expiresAt && Number.isNaN(expiresAt.getTime())) {
- throw new Error("Invalid --expires-at value (expected ISO date string)");
- }
-
- await prisma.apiKey.create({
- data: {
- userId: user.id,
- name: args.name,
- prefix,
- hashedKey,
- expiresAt,
- },
- select: {
- id: true,
- },
- });
-
- process.stdout.write(`${apiKey}\n`);
-}
-
-main()
- .catch((error) => {
- process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
- process.exitCode = 1;
- })
- .finally(async () => {
- await prisma.$disconnect();
- });
-
-