Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): Add API key DB table #8593

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- CreateEnum
CREATE TYPE "APIKeyPermission" AS ENUM ('EXECUTE_GRAPH', 'READ_GRAPH', 'EXECUTE_BLOCK', 'READ_BLOCK');

-- CreateEnum
CREATE TYPE "APIKeyStatus" AS ENUM ('ACTIVE', 'REVOKED', 'SUSPENDED');

-- CreateTable
CREATE TABLE "APIKey" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"prefix" TEXT NOT NULL,
"postfix" TEXT NOT NULL,
"key" TEXT NOT NULL,
"status" "APIKeyStatus" NOT NULL DEFAULT 'ACTIVE',
"permissions" "APIKeyPermission"[],
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"lastUsedAt" TIMESTAMP(3),
"revokedAt" TIMESTAMP(3),
"description" TEXT,
"userId" TEXT NOT NULL,

CONSTRAINT "APIKey_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "APIKey_key_key" ON "APIKey"("key");

-- CreateIndex
CREATE INDEX "APIKey_key_idx" ON "APIKey"("key");

-- CreateIndex
CREATE INDEX "APIKey_prefix_idx" ON "APIKey"("prefix");

-- CreateIndex
CREATE INDEX "APIKey_userId_idx" ON "APIKey"("userId");

-- CreateIndex
CREATE INDEX "APIKey_status_idx" ON "APIKey"("status");

-- CreateIndex
CREATE INDEX "APIKey_userId_status_idx" ON "APIKey"("userId", "status");

-- AddForeignKey
ALTER TABLE "APIKey" ADD CONSTRAINT "APIKey_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
40 changes: 40 additions & 0 deletions autogpt_platform/backend/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ model User {
AnalyticsDetails AnalyticsDetails[]
AnalyticsMetrics AnalyticsMetrics[]
UserBlockCredit UserBlockCredit[]
APIKeys APIKey[]

@@index([id])
@@index([email])
Expand Down Expand Up @@ -277,3 +278,42 @@ model UserBlockCredit {

@@id(name: "creditTransactionIdentifier", [transactionKey, userId])
}

enum APIKeyPermission {
aarushik93 marked this conversation as resolved.
Show resolved Hide resolved
EXECUTE_GRAPH // Can execute agent graphs
READ_GRAPH // Can get graph versions and details
EXECUTE_BLOCK // Can execute individual blocks
READ_BLOCK // Can get block information
}

model APIKey {
id String @id @default(uuid())
name String
prefix String // First 8 chars for identification
postfix String
key String @unique // Hashed key
aarushik93 marked this conversation as resolved.
Show resolved Hide resolved
status APIKeyStatus @default(ACTIVE)
permissions APIKeyPermission[]

createdAt DateTime @default(now())
lastUsedAt DateTime?
revokedAt DateTime?

description String?

// Relation to user
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@index([key])
@@index([prefix])
@@index([userId])
@@index([status])
@@index([userId, status])
}

enum APIKeyStatus {
ACTIVE
REVOKED
SUSPENDED
}
Loading