unkeyed / unkey

Open source API management platform
https://go.unkey.com
Other
4.25k stars 505 forks source link

Create the /v1/keys.whoami endpoint #2140

Closed chronark closed 1 month ago

chronark commented 1 month ago

Preliminary Checks

Is your feature request related to a problem? Please describe.

Sometimes you need to know the current config and limits of a key, but using the /v1/keys.getKey endpoint requires a keyId, which you don't always have available.

Describe the solution

Create a new POST /v1/keys.whoami endpoint in /apps/api/src/routes as well as a testcase for it. Then register the handler in /apps/api/src/worker.ts.

I suggest copying another handler file and modifying, so you can reuse the basic structure of the openapi validation.

The endpoint must receive the following payload in a POST request:

{
  key: string
}

and be authenticated via a rootKey in the Authorization header. The root key must have the api.${apiId}.read_key or api.*.read_key permissions.

It must return the following json payload:

{
    id: string,
    name?: string,
    remaining?: number,
    identity?: {
      id: string,
      externalId: string,
    },
    meta?: unknown,
    createdAt: number,
    enabled: boolean,
    environment?: string,
}

General flow of the handler:

  1. parse the key from the request
  2. hash it using sha256, there's an internal library for it
  3. look up the key in the db by querying for its hash, include its api and identity relations
  4. do a root key auth check, and include the above mentioned permissions
  5. return 404 if not found or deleted
  6. return structured json as mentioned above

Describe alternatives you have considered (if any)

No response

Additional context

No response

linear[bot] commented 1 month ago

ENG-1344 Create the /v1/keys.whoami endpoint

harshsbhat commented 1 month ago

Is this discussed and ready to work? I would like to take this one if its not core team.

chronark commented 1 month ago

no, it's not ready yet, I will remove the needs approval badge once it is

syedbarimanjan commented 1 month ago

/assign

oss-gg[bot] commented 1 month ago

Assigned to @syedbarimanjan! Please open a draft PR linking this issue within 48h ⚠️ If we can't detect a PR from you linking this issue in 48h, you'll be unassigned automatically 🕹️ Excited to have you ship this 🚀

harshsbhat commented 1 month ago

Hey @syedbarimanjan I had already solved 20-25% of this issue but was waiting as it had Need approval tag. Just incase if you decide to drop this issue let me know. I would love to restart working on this. Also if you have any issues I can help with let me know 😊. Good luck!

syedbarimanjan commented 1 month ago

Hey @syedbarimanjan I had already solved 20-25% of this issue but was waiting as it had Need approval tag. Just incase if you decide to drop this issue let me know. I would love to restart working on this. Also if you have any issues I can help with let me know 😊. Good luck!

Hey you can keep working on it i have removed my assignment.