stack/packages/stack-shared/src/interface/crud/internal-api-keys.ts
Moritz Schneider 592d259dde
Api Keys (#590)
<!--

Make sure you've read the CONTRIBUTING.md guidelines:
https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md

-->

<img width="1510" alt="image"
src="https://github.com/user-attachments/assets/99619123-6be5-4788-aebe-5fc2a9a36245"
/>

<img width="1510" alt="image"
src="https://github.com/user-attachments/assets/660677bf-f19a-4673-94c8-59ac50eb6ae5"
/>

<img width="1510" alt="image"
src="https://github.com/user-attachments/assets/11ae63c4-5813-4fd8-aa01-fa580d2103be"
/>


<!-- ELLIPSIS_HIDDEN -->


----

> [!IMPORTANT]
> Introduces API key management for users and teams, integrating with
existing project configurations and permissions, and adds comprehensive
tests and examples.
> 
>   - **API Key Management**:
> - Introduces `ProjectApiKey` model in `schema.prisma` for managing API
keys.
> - Adds `createApiKeyHandlers` in `handlers.tsx` to handle API key CRUD
operations.
>     - Implements API key creation, revocation, and validation logic.
>   - **Permissions and Configurations**:
> - Adds `allowUserApiKeys` and `allowTeamApiKeys` to `ProjectConfig` in
`schema.prisma`.
> - Updates `TeamSystemPermission` enum to include `MANAGE_API_KEYS`.
> - Ensures API key operations respect project configurations and
user/team permissions.
>   - **Testing and Examples**:
> - Adds extensive tests in `api-keys.test.ts` to cover various API key
scenarios.
>     - Updates example projects to demonstrate API key usage.
>   - **Miscellaneous**:
>     - Refactors existing code to integrate API key functionalities.
> - Updates documentation and type definitions to reflect new API key
features.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=stack-auth%2Fstack-auth&utm_source=github&utm_medium=referral)<sup>
for 96f60c57f0. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->

---------

Co-authored-by: Konsti Wohlwend <n2d4xc@gmail.com>
2025-04-04 13:03:10 -07:00

70 lines
2.2 KiB
TypeScript

import { CrudTypeOf, createCrud } from "../../crud";
import { yupBoolean, yupMixed, yupNumber, yupObject, yupString } from "../../schema-fields";
const baseInternalApiKeysReadSchema = yupObject({
id: yupString().defined(),
description: yupString().defined(),
expires_at_millis: yupNumber().defined(),
manually_revoked_at_millis: yupNumber().optional(),
created_at_millis: yupNumber().defined(),
});
// Used for the result of the create endpoint
export const internalApiKeysCreateInputSchema = yupObject({
description: yupString().defined(),
expires_at_millis: yupNumber().defined(),
has_publishable_client_key: yupBoolean().defined(),
has_secret_server_key: yupBoolean().defined(),
has_super_secret_admin_key: yupBoolean().defined(),
});
export const internalApiKeysCreateOutputSchema = baseInternalApiKeysReadSchema.concat(yupObject({
publishable_client_key: yupString().optional(),
secret_server_key: yupString().optional(),
super_secret_admin_key: yupString().optional(),
}).defined());
// Used for list, read and update endpoints after the initial creation
export const internalApiKeysCrudAdminObfuscatedReadSchema = baseInternalApiKeysReadSchema.concat(yupObject({
publishable_client_key: yupObject({
last_four: yupString().defined(),
}).optional(),
secret_server_key: yupObject({
last_four: yupString().defined(),
}).optional(),
super_secret_admin_key: yupObject({
last_four: yupString().defined(),
}).optional(),
}));
export const internalApiKeysCrudAdminUpdateSchema = yupObject({
description: yupString().optional(),
revoked: yupBoolean().oneOf([true]).optional(),
}).defined();
export const internalApiKeysCrudAdminDeleteSchema = yupMixed();
export const internalApiKeysCrud = createCrud({
adminReadSchema: internalApiKeysCrudAdminObfuscatedReadSchema,
adminUpdateSchema: internalApiKeysCrudAdminUpdateSchema,
adminDeleteSchema: internalApiKeysCrudAdminDeleteSchema,
docs: {
adminList: {
hidden: true,
},
adminRead: {
hidden: true,
},
adminCreate: {
hidden: true,
},
adminUpdate: {
hidden: true,
},
adminDelete: {
hidden: true,
},
},
});
export type InternalApiKeysCrud = CrudTypeOf<typeof internalApiKeysCrud>;