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: Who Am I route #2159

Merged
merged 19 commits into from
Oct 7, 2024
Merged

feat: Who Am I route #2159

merged 19 commits into from
Oct 7, 2024

Conversation

harshsbhat
Copy link
Contributor

@harshsbhat harshsbhat commented Oct 3, 2024

What does this PR do?

Creates a new API route called Who AM I for keys.

Fixes #2140

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

  • Test A
  • Test B

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • New Features

    • Introduced a new API route at /v1/keys.whoAmI for retrieving key information using a POST request.
    • The route supports bearer authentication and returns detailed key information in JSON format.
    • New documentation available for the /v1/keys.whoAmI endpoint, detailing its usage and purpose.
  • Bug Fixes

    • Enhanced error handling for scenarios where keys are not found or database queries encounter issues.
  • Tests

    • Added integration tests to validate the behavior of the /v1/keys.whoAmI endpoint for non-existent keys and permissions based on user roles.
    • Implemented tests to ensure correct responses are returned for valid key requests and identity associations.

Copy link

changeset-bot bot commented Oct 3, 2024

🦋 Changeset detected

Latest commit: 02e91fc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
api Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Oct 3, 2024

@harshsbhat is attempting to deploy a commit to the Unkey Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Oct 3, 2024

Caution

Review failed

The pull request is closed.

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce two new API endpoints: /v1/keys.whoAmI in the v1_keys_getWhoAmI.ts file for retrieving key information via a query parameter, and /v1/keys.whoAmI in the v1_keys_whoAmI.ts file for retrieving key information via a POST request with a JSON body. Both endpoints require bearer authentication and include error handling and authorization checks. The function registerV1KeysWhoAmi is registered in the worker.ts file to handle requests for these new endpoints.

Changes

File Path Change Summary
apps/api/src/routes/v1_keys_getWhoAmI.ts Introduced new API route for /v1/keys.whoAmI (GET), including request handling and response structure. Added exported methods and types.
apps/api/src/routes/v1_keys_whoAmI.ts Introduced new API route for /v1/keys.whoAmI (POST), including request handling and response structure. Added exported methods and types.
apps/api/src/worker.ts Added import and registration of the registerV1KeysWhoAmI function to handle requests for the new endpoints.

Assessment against linked issues

Objective Addressed Explanation
Create the /v1/keys.whoami endpoint (#2140)
Include authentication and response structure
Implement error handling and authorization checks

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

github-actions bot commented Oct 3, 2024

Thank you for following the naming conventions for pull request titles! 🙏

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (3)
apps/api/src/worker.ts (1)

12-12: Overall impact: Positive addition with a minor suggestion.

The introduction of the "Who Am I" route is well-integrated into the existing codebase structure. It enhances the API's capabilities without disrupting the current functionality, aligning perfectly with the PR objectives.

To improve code readability and maintainability, consider grouping related route registrations. You could add a comment above the key-related routes to clearly demarcate this section.

Consider adding a comment above the key-related routes:

 // misc
 registerV1Liveness(app);
 
+// Key-related routes
 // keys
 registerV1KeysGetKey(app);
 registerV1KeysWhoAmi(app);
 registerV1KeysDeleteKey(app);
 // ... (other key-related routes)

Also applies to: 71-71

apps/api/src/routes/v1_keys_getWhoAmI.ts (2)

88-88: Correct function name for consistency and readability

The function is named registerV1KeysWhoAmi, but the correct spelling should be registerV1KeysWhoAmI to match the operation ID and route path. This improves consistency and readability.

Apply this diff to fix the function name:

(Line 88):

-export const registerV1KeysWhoAmi = (app: App) =>
+export const registerV1KeysWhoAmI = (app: App) =>

152-155: Reuse parsed meta data to avoid redundant parsing

The key.meta is being parsed twice. First in lines 152-155 and again in line 168. Consider reusing the meta variable to avoid redundant parsing operations.

Apply this diff:

(Line 168):

-        meta: key.meta ? JSON.parse(key.meta) : undefined,
+        meta: meta,

Also applies to: 168-168

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b93bfa8 and 0da4d7b.

📒 Files selected for processing (2)
  • apps/api/src/routes/v1_keys_getWhoAmI.ts (1 hunks)
  • apps/api/src/worker.ts (2 hunks)
🔇 Additional comments (2)
apps/api/src/worker.ts (2)

12-12: LGTM: Import statement for new route handler.

The import statement for registerV1KeysWhoAmi is consistent with the existing pattern and naming convention used for other route handlers. This aligns well with the PR objective of introducing a new "Who Am I" route for keys.


71-71: LGTM: Route registration for "Who Am I" functionality.

The registerV1KeysWhoAmi(app) call is correctly placed within the keys section of route registrations, maintaining consistency with the existing codebase structure. This addition aligns with the PR objective of creating a new "Who Am I" route for keys.

To ensure the implementation meets the requirements outlined in the PR objectives, let's verify the route handler:

apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
@chronark chronark self-assigned this Oct 3, 2024
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_getWhoAmI.ts Outdated Show resolved Hide resolved
@chronark
Copy link
Collaborator

chronark commented Oct 3, 2024

Please add a tests

@harshsbhat
Copy link
Contributor Author

I will get working on this again tomorrow. Had to do some college work today.

@harshsbhat harshsbhat changed the title wip: Who Am I route feat: Who Am I route Oct 4, 2024
Copy link

vercel bot commented Oct 4, 2024

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

Name Status Preview Comments Updated (UTC)
dashboard ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 7, 2024 11:42am
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 7, 2024 11:42am
play ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 7, 2024 11:42am
unkey-engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 7, 2024 11:42am
workflows ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 7, 2024 11:42am
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 7, 2024 11:42am

@harshsbhat
Copy link
Contributor Author

I have resolved the requested changes. I will add the tests in sometime.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (1)
apps/api/src/routes/v1_keys_whoAmI.ts (1)

145-150: Consider returning 'FORBIDDEN' instead of 'NOT_FOUND' for unauthorized access

When key.workspaceId does not match auth.authorizedWorkspaceId, an error with code 'NOT_FOUND' is thrown. While this hides resource existence, returning 'FORBIDDEN' may be more appropriate to indicate insufficient permissions.

Evaluate the security policy and adjust the error code if necessary to accurately reflect the authorization failure.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0da4d7b and 971abe2.

📒 Files selected for processing (2)
  • apps/api/src/routes/v1_keys_whoAmI.ts (1 hunks)
  • apps/api/src/worker.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/src/worker.ts
🧰 Additional context used
📓 Learnings (1)
apps/api/src/routes/v1_keys_whoAmI.ts (1)
Learnt from: chronark
PR: unkeyed/unkey#2159
File: apps/api/src/routes/v1_keys_getWhoAmI.ts:12-13
Timestamp: 2024-10-03T09:51:43.832Z
Learning: When implementing endpoints that require a key, avoid sending the key as part of the URL. Use POST methods with the key in the request body instead, as URLs get logged and could leak sensitive information.

apps/api/src/routes/v1_keys_whoAmI.ts Show resolved Hide resolved
apps/api/src/routes/v1_keys_whoAmI.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_whoAmI.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (3)
apps/api/src/routes/v1_keys_whoAmI.error.test.ts (1)

8-34: LGTM: Well-structured test case with comprehensive assertions.

The test case effectively covers the scenario where the queried key does not exist. It follows a clear setup-action-assertion pattern and uses the IntegrationHarness for consistent testing. The assertions check both the status code and the detailed structure of the error response, which is a good practice.

Consider adding a comment explaining the purpose of apiId (line 10) as it's created but not directly used in the test. This would improve code clarity for future maintainers.

apps/api/src/routes/v1_keys_whoAmI.security.test.ts (2)

84-84: Enhance type safety for roles mapping

When roles are functions that depend on apiId, consider adding explicit type annotations or using more descriptive variable names to improve code readability and maintainability.


98-98: Simplify assertion error message for clarity

Including the entire response object in the assertion error message can make the test output verbose and harder to read. Simplify the assertion to make test failures easier to diagnose.

Update the assertion as follows:

-expect(res.status, `expected 200, received: ${JSON.stringify(res, null, 2)}`).toEqual(200);
+expect(res.status).toEqual(200);

If additional information is needed upon failure, consider logging the response body separately.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 971abe2 and ef054c3.

📒 Files selected for processing (4)
  • apps/api/src/routes/v1_keys_whoAmI.error.test.ts (1 hunks)
  • apps/api/src/routes/v1_keys_whoAmI.security.test.ts (1 hunks)
  • apps/api/src/routes/v1_keys_whoAmI.ts (1 hunks)
  • apps/api/src/worker.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/src/worker.ts
🧰 Additional context used
📓 Learnings (1)
apps/api/src/routes/v1_keys_whoAmI.ts (1)
Learnt from: chronark
PR: unkeyed/unkey#2159
File: apps/api/src/routes/v1_keys_getWhoAmI.ts:12-13
Timestamp: 2024-10-03T09:51:43.832Z
Learning: When implementing endpoints that require a key, avoid sending the key as part of the URL. Use POST methods with the key in the request body instead, as URLs get logged and could leak sensitive information.
🔇 Additional comments (7)
apps/api/src/routes/v1_keys_whoAmI.error.test.ts (1)

1-6: LGTM: Imports are well-organized and relevant.

The imports are appropriately structured, including necessary dependencies and types for the test case. The use of named imports enhances code readability.

apps/api/src/routes/v1_keys_whoAmI.ts (6)

1-89: Well-structured route definition with proper security considerations.

The route definition is comprehensive and follows best practices:

  • Uses POST method, aligning with the recommendation to avoid sending keys in URLs.
  • Implements bearer authentication for security.
  • Utilizes Zod for request and response schema validation, enhancing type safety and documentation.

91-97: Clear and consistent type exports.

The exported types leverage the Zod schemas, ensuring consistency between runtime validation and TypeScript types. This approach enhances type safety and improves developer experience when working with this API route.


99-103: Solid initial setup for the route handler.

The route handler function is well-structured:

  • It correctly extracts the validated key from the request body.
  • Computes the hash of the key, which is a good security practice for key handling.

104-153: Efficient key retrieval and robust authorization logic.

The implementation demonstrates several good practices:

  • Utilizes a cache with SWR strategy, reducing database load.
  • Performs a well-structured database query when necessary.
  • Implements proper authorization checks to ensure secure access to key information.

159-174: Well-structured response matching the defined schema.

The response construction is thorough and aligns with the Zod schema:

  • Includes all required fields from the key data.
  • Properly handles optional fields with null checks and fallbacks.
  • Utilizes the parsed metadata, maintaining consistency with the earlier parsing.

1-174: Excellent implementation of the "Who Am I" route, meeting PR objectives.

This implementation successfully addresses the requirements outlined in issue #2140:

  • Provides a secure POST endpoint for retrieving key information without requiring a keyId.
  • Implements proper authentication and authorization checks.
  • Returns a well-structured response with all required key details.

The code demonstrates several strengths:

  • Efficient caching strategy to reduce database load.
  • Strong type safety through consistent use of Zod schemas.
  • Adherence to security best practices, such as using POST for sensitive data and hashing keys.

Minor optimization opportunities have been identified and suggested in previous comments. Overall, this is a high-quality implementation that meets the project's standards and objectives.

apps/api/src/routes/v1_keys_whoAmI.error.test.ts Outdated Show resolved Hide resolved
apps/api/src/routes/v1_keys_whoAmI.security.test.ts Outdated Show resolved Hide resolved
@harshsbhat
Copy link
Contributor Author

@chronark should be good for review

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
apps/api/src/routes/v1_keys_whoAmI.happy.test.ts (3)

11-53: LGTM! Comprehensive test case with minor improvement needed.

The test case "returns 200" is well-structured and covers the essential aspects of the /v1/keys.whoAmI endpoint. It properly sets up the test environment, creates necessary data, makes the API call, and verifies the response.

There's a duplicate assertion for the 'name' field. Consider removing one of these lines:

  expect(res.body.name).toEqual(keySchema.name);
  expect(res.body.remaining).toEqual(keySchema.remaining);
- expect(res.body.name).toEqual(keySchema.name);
  expect(res.body.meta).toEqual(JSON.parse(keySchema.meta));

55-86: LGTM! Well-structured test for identity retrieval.

The test case "returns identity" effectively verifies the /v1/keys.whoAmI endpoint's ability to return identity information associated with a key. The setup, API call, and assertions are all appropriate for this specific test scenario.

Consider adding assertions for other key fields (similar to the first test) if it's important to verify that all key information is correctly returned along with the identity. This would ensure that the presence of identity doesn't affect other key data. For example:

expect(res.body.id).toBeDefined();
expect(res.body.name).toBeDefined();
expect(res.body.remaining).toBeDefined();
// ... other key field assertions

However, if the focus of this test is specifically on identity retrieval, the current assertions are sufficient.


1-86: Great job on the happy path tests! Consider adding error scenarios.

The test file provides comprehensive coverage for the happy path scenarios of the /v1/keys.whoAmI endpoint. Both tests are well-structured, using the IntegrationHarness effectively for setup and API calls. They cover the main functionality of retrieving key information and associated identity data.

To further improve the test suite, consider adding the following test cases:

  1. Error scenario: Invalid key

    • Test the API response when an invalid or non-existent key is provided.
    • Verify that the appropriate error status code and message are returned.
  2. Error scenario: Unauthorized access

    • Test the API response when the root key lacks the necessary permissions.
    • Verify that the appropriate error status code and message are returned.
  3. Edge case: Key with all optional fields

    • Create a key with all possible optional fields populated.
    • Verify that all fields are correctly returned in the response.

These additional tests would help ensure the robustness of the endpoint under various conditions.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between ef054c3 and e483d0e.

📒 Files selected for processing (2)
  • apps/api/src/routes/v1_keys_whoAmI.happy.test.ts (1 hunks)
  • apps/api/src/routes/v1_keys_whoAmI.security.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/src/routes/v1_keys_whoAmI.security.test.ts
🧰 Additional context used
📓 Learnings (1)
📓 Common learnings
Learnt from: chronark
PR: unkeyed/unkey#2159
File: apps/api/src/routes/v1_keys_getWhoAmI.ts:12-13
Timestamp: 2024-10-03T09:51:43.832Z
Learning: When implementing endpoints that require a key, avoid sending the key as part of the URL. Use POST methods with the key in the request body instead, as URLs get logged and could leak sensitive information.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Outside diff range and nitpick comments (3)
.changeset/late-colts-shout.md (1)

1-5: Approved with suggestions for improvement.

The changeset correctly identifies this as a minor API change, which is appropriate for adding a new route. However, the description could be more informative.

Consider expanding the description to include:

  1. The purpose of the new route (e.g., "to retrieve current key configuration and limits").
  2. Any notable features or requirements (e.g., "uses bearer authentication").
  3. Reference to related issue or PR numbers for traceability.

Here's a suggested revision:

---
"api": minor
---

Add /v1/keys.whoami route to retrieve current key configuration and limits
- Allows users to get key information without knowing the keyId
- Requires bearer authentication with a rootKey
- Implements POST method for secure key handling
- Related to issue #2140 and PR #2159

This provides more context and helps with future reference and changelog generation.

apps/docs/api-reference/keys/whoami.mdx (1)

13-14: Expand the endpoint description with more details.

While the current description provides a good rationale for the endpoint, it lacks crucial information about its usage and behavior.

Consider expanding the description to include:

  1. Specify that this is a POST request.
  2. Mention the authentication method (bearer token with rootKey).
  3. Describe the expected request body structure.
  4. Outline the response structure and possible status codes.

For example:

This POST endpoint offers an alternative to [`/v1/keys.getKey`](/api-reference/keys/get) when you don't have access to the `keyId`. 

Authentication is required using a bearer token with a valid rootKey.

The request body should be a JSON object containing the `key` field:

```json
{
  "key": "your_actual_key_here"
}

The response will include detailed information about the key, such as its ID, name, remaining usage, and more. Refer to the OpenAPI specification below for the full response structure.

Possible status codes:

  • 200: Successful operation
  • 401: Unauthorized
  • 404: Key not found

This additional information will help users understand how to use the endpoint correctly.

</blockquote></details>
<details>
<summary>apps/docs/mint.json (1)</summary><blockquote>

`237-238`: **LGTM! Consider adding a comma for consistency.**

The addition of the "whoami" page to the "Keys" group is appropriate and aligns with the PR's objective of introducing a new "Who Am I" route. This change enhances the documentation structure by including the new endpoint.



For consistency with the JSON format used throughout the file, consider adding a trailing comma after the new line. Here's the suggested change:

```diff
 "api-reference/keys/set-roles",
-"api-reference/keys/whoami"
+"api-reference/keys/whoami",
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e483d0e and f8e6f2d.

📒 Files selected for processing (8)
  • .changeset/late-colts-shout.md (1 hunks)
  • apps/api/src/routes/v1_keys_whoAmI.error.test.ts (1 hunks)
  • apps/api/src/routes/v1_keys_whoAmI.happy.test.ts (1 hunks)
  • apps/api/src/routes/v1_keys_whoAmI.security.test.ts (1 hunks)
  • apps/api/src/routes/v1_keys_whoAmI.ts (1 hunks)
  • apps/api/src/worker.ts (2 hunks)
  • apps/docs/api-reference/keys/whoami.mdx (1 hunks)
  • apps/docs/mint.json (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • apps/api/src/routes/v1_keys_whoAmI.error.test.ts
  • apps/api/src/routes/v1_keys_whoAmI.happy.test.ts
  • apps/api/src/routes/v1_keys_whoAmI.security.test.ts
  • apps/api/src/worker.ts
🧰 Additional context used
📓 Learnings (2)
📓 Common learnings
Learnt from: chronark
PR: unkeyed/unkey#2159
File: apps/api/src/routes/v1_keys_getWhoAmI.ts:12-13
Timestamp: 2024-10-03T09:51:43.832Z
Learning: When implementing endpoints that require a key, avoid sending the key as part of the URL. Use POST methods with the key in the request body instead, as URLs get logged and could leak sensitive information.
apps/api/src/routes/v1_keys_whoAmI.ts (1)
Learnt from: chronark
PR: unkeyed/unkey#2159
File: apps/api/src/routes/v1_keys_getWhoAmI.ts:12-13
Timestamp: 2024-10-03T09:51:43.832Z
Learning: When implementing endpoints that require a key, avoid sending the key as part of the URL. Use POST methods with the key in the request body instead, as URLs get logged and could leak sensitive information.
🔇 Additional comments (6)
apps/api/src/routes/v1_keys_whoAmI.ts (6)

1-89: Well-structured OpenAPI route definition with comprehensive schemas.

The route definition follows OpenAPI standards and includes proper documentation. The request and response schemas are well-defined using Zod, ensuring proper validation and type safety. The comprehensive response schema covers all necessary fields for the key information.


91-97: Good practice: Exporting inferred types for route, request, and response.

Exporting these types enhances type safety and promotes reusability across the codebase. The types are correctly inferred from the Zod schemas, ensuring consistency between the OpenAPI definition and the TypeScript types.


99-103: LGTM: Proper route registration and key handling.

The registerV1KeysWhoAmI function correctly registers the route with the app. The extraction of the key from the validated request body and the use of SHA-256 for hashing demonstrate good security practices.


104-147: Efficient key retrieval with proper authorization checks.

The implementation demonstrates good practices:

  • Using cache with a database fallback optimizes performance.
  • Proper authorization checks ensure secure access to key information.
  • Comprehensive error handling covers various scenarios.

These aspects contribute to a robust and secure implementation of the "Who Am I" functionality.


159-173: Well-structured response construction aligned with the schema.

The response object is correctly constructed, aligning with the defined response schema. The use of optional chaining and nullish coalescing operators for handling potentially undefined values is a good practice, ensuring a consistent response structure.


1-174: Summary: Successful implementation of the "Who Am I" route for keys

This implementation successfully addresses the requirements outlined in issue #2140 and aligns well with the PR objectives. Key points:

  1. The route allows retrieval of key information without requiring a keyId.
  2. The use of a POST method with the key in the request body enhances security by avoiding key exposure in URLs.
  3. The response structure matches the specified requirements, providing comprehensive key information.
  4. The implementation follows good practices for OpenAPI route definition, type safety, and error handling.

Overall, this is a solid implementation that enhances the API functionality as intended.

apps/docs/api-reference/keys/whoami.mdx Outdated Show resolved Hide resolved
apps/docs/api-reference/keys/whoami.mdx Show resolved Hide resolved
apps/api/src/routes/v1_keys_whoAmI.ts Show resolved Hide resolved
apps/api/src/routes/v1_keys_whoAmI.ts Show resolved Hide resolved
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@chronark chronark merged commit 09d36ad into unkeyed:main Oct 7, 2024
18 of 24 checks passed
Copy link

oss-gg bot commented Oct 7, 2024

Awarding harshsbhat: 300 points 🕹️ Well done! Check out your new contribution on oss.gg/harshsbhat

@harshsbhat
Copy link
Contributor Author

Enjoyed every minute of writing this new route. Thanks @chronark for giving an opportunity for developers like me ( with no experience ) to actually see my code in the production. I would love to update the docs for this too

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

Successfully merging this pull request may close these issues.

Create the /v1/keys.whoami endpoint
4 participants