diff options
| author | Mohamed Bassem <me@mbassem.com> | 2025-07-10 21:22:54 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2025-07-10 22:03:30 +0000 |
| commit | 613137ff99442885c5fe679b2cc1172adfc5a283 (patch) | |
| tree | 97f2b940448357870090364c6f73b780d6f473d9 /packages/trpc/routers/apiKeys.ts | |
| parent | 333d1610fad10e70759545f223959503288a02c6 (diff) | |
| download | karakeep-613137ff99442885c5fe679b2cc1172adfc5a283.tar.zst | |
feat: Add API ratelimits
Diffstat (limited to 'packages/trpc/routers/apiKeys.ts')
| -rw-r--r-- | packages/trpc/routers/apiKeys.ts | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/packages/trpc/routers/apiKeys.ts b/packages/trpc/routers/apiKeys.ts index eb52189b..d4e01aa5 100644 --- a/packages/trpc/routers/apiKeys.ts +++ b/packages/trpc/routers/apiKeys.ts @@ -11,7 +11,12 @@ import { logAuthenticationError, validatePassword, } from "../auth"; -import { authedProcedure, publicProcedure, router } from "../index"; +import { + authedProcedure, + createRateLimitMiddleware, + publicProcedure, + router, +} from "../index"; const zApiKeySchema = z.object({ id: z.string(), @@ -70,6 +75,13 @@ export const apiKeysAppRouter = router({ // Exchange the username and password with an API key. // Homemade oAuth. This is used by the extension. exchange: publicProcedure + .use( + createRateLimitMiddleware({ + name: "apiKey.exchange", + windowMs: 15 * 60 * 1000, + maxRequests: 10, + }), + ) // 10 requests per 15 minutes .input( z.object({ keyName: z.string(), @@ -97,6 +109,13 @@ export const apiKeysAppRouter = router({ return await generateApiKey(input.keyName, user.id); }), validate: publicProcedure + .use( + createRateLimitMiddleware({ + name: "apiKey.validate", + windowMs: 60 * 1000, + maxRequests: 30, + }), + ) // 30 requests per minute .input(z.object({ apiKey: z.string() })) .output(z.object({ success: z.boolean() })) .mutation(async ({ input, ctx }) => { |
