feat: v2 rate limiting (#16882)

* refactor: pass redis storage to throttler guard

* chore: upgrade throttler to latest

* feat: ApiKey RateLimit table

* chore: upgrade redis storage throttler

* feat: rate limit by api key

* refactor: on delete api key cascade rate limit

* fix: permissions guard work with oauth credentials

* chore: set rate limit in env

* tests: throttler

* feat: include rate limit name in response

* fix: correctly handle multiple rate limits

* chore: remove unused import

* delete migrations

* chore: prisma migration

* doc

* dummy

* fix: permissions guard unit test

* refactor: remove route specific @Throttles

* fix: permissions guard
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2024-10-01 12:01:56 +00:00
1 parent 9d3be8de03
commit 4b6a389212
15 files changed
+747 -80

No files matched your search

@@ -0,0 +1,19 @@
-- CreateTable
CREATE TABLE "RateLimit" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"apiKeyId" TEXT NOT NULL,
"ttl" INTEGER NOT NULL,
"limit" INTEGER NOT NULL,
"blockDuration" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "RateLimit_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "RateLimit_apiKeyId_idx" ON "RateLimit"("apiKeyId");
-- AddForeignKey
ALTER TABLE "RateLimit" ADD CONSTRAINT "RateLimit_apiKeyId_fkey" FOREIGN KEY ("apiKeyId") REFERENCES "ApiKey"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+22 -6
View File
@@ -803,22 +803,38 @@ model Impersonations {
}
model ApiKey {
id String @id @unique @default(cuid())
id String @id @unique @default(cuid())
userId Int
teamId Int?
note String?
createdAt DateTime @default(now())
createdAt DateTime @default(now())
expiresAt DateTime?
lastUsedAt DateTime?
hashedKey String @unique()
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
app App? @relation(fields: [appId], references: [slug], onDelete: Cascade)
hashedKey String @unique()
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
app App? @relation(fields: [appId], references: [slug], onDelete: Cascade)
appId String?
rateLimits RateLimit[]
@@index([userId])
}
model RateLimit {
id String @id @default(uuid())
name String
apiKeyId String
ttl Int
limit Int
blockDuration Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
apiKey ApiKey @relation(fields: [apiKeyId], references: [id], onDelete: Cascade)
@@index([apiKeyId])
}
model HashedLink {
id Int @id @default(autoincrement())
link String @unique()