Skip to content

Api Keys#590

Merged
bazumo merged 55 commits intodevfrom
api.keys
Apr 4, 2025
Merged

Api Keys#590
bazumo merged 55 commits intodevfrom
api.keys

Conversation

@bazumo
Copy link
Copy Markdown
Contributor

@bazumo bazumo commented Mar 27, 2025

image image image

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.

This description was created by Ellipsis for 96f60c5. It will automatically update as commits are pushed.

bazumo and others added 18 commits March 18, 2025 15:18
<!--

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

-->

<!-- ELLIPSIS_HIDDEN -->

----

> [!IMPORTANT]
> Refactor and enhance API key management with new types, utility
functions, and schema validation across the codebase.
> 
>   - **API Key Management**:
> - Introduced `ApiKey` type with `user` and `team` variants in
`api-keys/index.ts`.
> - Added `apiKeyCreationOptionsToCrud` and `apiKeyUpdateOptionsToCrud`
functions for CRUD operations.
> - Updated `client-app-impl.ts` and `server-app-impl.ts` to use new API
key types and functions.
>   - **Schema and Validation**:
> - Added `yupValidate` function in `schema-fields.ts` for enhanced
validation.
> - Introduced `IfAndOnlyIf` type in `types.ts` for conditional type
handling.
>   - **Utility Functions**:
> - Added `toHexString`, `getBase32CharacterFromIndex`, and
`getBase32IndexFromCharacter` in `bytes.tsx`.
> - Updated `sha512` function in `hashes.tsx` to return `Uint8Array`.
> - Introduced `filterUndefinedOrNull` in `objects.tsx` for filtering
null and undefined values.
>   - **Code Refactoring**:
> - Refactored API key handling in `client-app-impl.ts` and
`server-app-impl.ts` to use new utility functions and types.
> - Removed deprecated functions and streamlined API key operations
across the codebase.
> 
> <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 2d1c66a. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->
@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 27, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
stack-backend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 7:03pm
stack-dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 7:03pm
stack-demo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 7:03pm

@recurseml
Copy link
Copy Markdown

recurseml Bot commented Mar 27, 2025

⚠️ Only 50 files will be analyzed due to processing limits.

Comment thread apps/backend/src/route-handlers/smart-route-handler.tsx
Comment thread apps/dashboard/src/components/data-table/api-key-table.tsx
Comment thread apps/e2e/tests/backend/endpoints/api/v1/internal-metrics.test.ts
Comment thread apps/e2e/tests/backend/endpoints/api/v1/teams.test.ts
Comment thread apps/e2e/tests/snapshot-serializer.ts Outdated
@recurseml
Copy link
Copy Markdown

recurseml Bot commented Mar 27, 2025

😱 Found 7 issues. Time to roll up your sleeves! 😱

🗒️ View all ignored comments in this repo
  • The constraint 'TokenStoreType extends string' is too restrictive. It should likely be 'TokenStoreType extends string | object' to match the condition check in line 113 where TokenStoreType is checked against {}
  • Return type mismatch - the interface declares useUsers() returning ServerUser[] but the Team interface that this extends declares useUsers() returning TeamUser[]
  • There is a syntax error in the super constructor call due to the ellipsis operator used incorrectly. Objects aren't being merged correctly. This syntax usage can lead to runtime errors when trying to pass the merged object to 'super()'. Verify that the intended alterations to the object occur before or outside of the super() call if needed.
  • Throwing an error when no active span is found is too aggressive. The log function should gracefully fallback to console.log or another logging mechanism when there's no active span, since not all execution contexts will have an active span. This makes the code less resilient and could break functionality in non-traced environments.

📚 Relevant Docs

  • Function sets backendContext with a new configuration but doesn't pass 'defaultProjectKeys'. Since defaultProjectKeys is required in the type definition and cannot be updated (throws error if tried to set), this will cause a type error.
  • The schema is using array syntax for pick() which is incorrect for Yup schemas. The pick() method in Yup expects individual arguments, not an array. Should be changed to: emailConfigSchema.pick('type', 'host', 'port', 'username', 'sender_name', 'sender_email')

📚 Relevant Docs

  • Creating a refresh token with current timestamp as expiration means it expires immediately. Should set a future date for token expiration.
  • The 'tools' object is initialized as an empty object, even though 'tools' is presumably expected to contain tool definitions. This could cause the server capabilities to lack necessary tool configurations, thus potentially impacting functionalities that depend on certain tool setups.

📚 Relevant Docs

  • 'STACK_SECRET_SERVER_KEY' is potentially being included in every request header without checking its existence again here. Although it's checked during initialization, this could lead to security issues as it's exposed in all communications where the header is logged or captured.

📚 Relevant Docs

  • When adding 'use client' directive at the beginning, it doesn't check if file.text already contains the 'use client' directive. This could lead to duplicate 'use client' directives if the file already has one.

📚 Relevant Docs

Copy link
Copy Markdown
Contributor

@fomalhautb fomalhautb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did finish reviewing everything, but left some comments now

Comment thread apps/backend/src/app/api/latest/(api-keys)/handlers.tsx
Comment thread apps/backend/src/app/api/latest/(api-keys)/handlers.tsx
Comment thread apps/backend/src/app/api/latest/(api-keys)/handlers.tsx
Copy link
Copy Markdown
Contributor

@fomalhautb fomalhautb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add e2e tests for client library?

Comment thread packages/template/src/lib/stack-app/apps/implementations/server-app-impl.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread packages/template/src/lib/stack-app/apps/implementations/server-app-impl.ts Outdated
Comment thread apps/e2e/tests/snapshot-serializer.ts
@bazumo
Copy link
Copy Markdown
Contributor Author

bazumo commented Apr 3, 2025

@fomalhautb can you think of any tests that are missing?

Comment thread apps/backend/prisma/schema.prisma Outdated
Comment thread apps/backend/prisma/schema.prisma Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread apps/e2e/tests/backend/endpoints/api/v1/api-keys.test.ts Outdated
Comment thread packages/stack-shared/src/known-errors.tsx Outdated
Comment thread packages/stack-shared/src/utils/api-keys.tsx Outdated
Copy link
Copy Markdown
Contributor

@N2D4 N2D4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like about this PR that 2/3s of its LOC are tests

@fomalhautb waiting for your review too, but after fixing the comments here LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants