-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: api keys support for CLI/backend/auth (#1387)
* feat: api keys support for CLI/backend/auth * fix(backend): test execution fix * fix: changed api key id to uuid * chore: Add changeset --------- Co-authored-by: Mohamad Mohebifar <[email protected]>
- Loading branch information
1 parent
563df0e
commit b0dae1b
Showing
23 changed files
with
707 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"codemod": minor | ||
--- | ||
|
||
Add API keys functionality |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ CLERK_SECRET_KEY= | |
CLERK_JWT_KEY= | ||
CLERK_PUBLISH_KEY= | ||
CLI_TOKEN_TEMPLATE= | ||
|
||
UNKEY_ROOT_KEY= | ||
UNKEY_API_ID= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
type CreateAPIKeyResponse, | ||
createAPIKeyRequestSchema, | ||
} from "@codemod-com/api-types"; | ||
import type { UserDataPopulatedRequest } from "@codemod-com/auth"; | ||
import { prisma } from "@codemod-com/database"; | ||
import type { RouteHandler } from "fastify"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { parse } from "valibot"; | ||
import { createApiKey } from "../services/UnkeyService.js"; | ||
|
||
export const createAPIKeyHandler: RouteHandler<{ | ||
Reply: CreateAPIKeyResponse; | ||
}> = async (request: UserDataPopulatedRequest) => { | ||
const user = request.user!; | ||
|
||
const uuid = uuidv4(); | ||
|
||
const apiKey = await createApiKey({ | ||
externalId: user.id, | ||
apiKeyData: parse(createAPIKeyRequestSchema, request.body), | ||
uuid, | ||
}); | ||
|
||
await prisma.apiKey.create({ | ||
data: { | ||
externalId: user.id, | ||
uuid, | ||
keyId: apiKey.keyId, | ||
}, | ||
}); | ||
|
||
const reply: CreateAPIKeyResponse = { key: apiKey.key, uuid }; | ||
return reply; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
type DeleteAPIKeysResponse, | ||
deleteAPIKeysRequestSchema, | ||
} from "@codemod-com/api-types"; | ||
import type { UserDataPopulatedRequest } from "@codemod-com/auth"; | ||
import { prisma } from "@codemod-com/database"; | ||
import type { RouteHandler } from "fastify"; | ||
import { parse } from "valibot"; | ||
import { deleteApiKeys, listApiKeys } from "../services/UnkeyService.js"; | ||
|
||
export const deleteAPIKeysHandler: RouteHandler<{ | ||
Reply: DeleteAPIKeysResponse; | ||
}> = async (request: UserDataPopulatedRequest) => { | ||
const user = request.user!; | ||
|
||
const { uuid } = parse(deleteAPIKeysRequestSchema, request.params); | ||
|
||
const keysToDelete = await prisma.apiKey.findMany({ | ||
where: { externalId: user.id, uuid }, | ||
}); | ||
|
||
const keysInfo = await listApiKeys({ externalId: user.id }); | ||
|
||
await deleteApiKeys({ keyIds: keysToDelete.map((key) => key.keyId) }); | ||
|
||
await prisma.apiKey.deleteMany({ | ||
where: { | ||
externalId: user.id, | ||
keyId: { in: keysToDelete.map((key) => key.keyId) }, | ||
}, | ||
}); | ||
|
||
const reply: DeleteAPIKeysResponse = { | ||
keys: keysToDelete | ||
.map(({ uuid }) => keysInfo.keys.find((k) => k.meta?.uuid === uuid)) | ||
.filter((key) => !!key) | ||
.map(({ start, name }) => ({ start, name })), | ||
}; | ||
return reply; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { ListAPIKeysResponse } from "@codemod-com/api-types"; | ||
import type { UserDataPopulatedRequest } from "@codemod-com/auth"; | ||
import type { RouteHandler } from "fastify"; | ||
import { listApiKeys } from "../services/UnkeyService.js"; | ||
|
||
export const listAPIKeysHandler: RouteHandler<{ | ||
Reply: ListAPIKeysResponse; | ||
}> = async (request: UserDataPopulatedRequest) => { | ||
const user = request.user!; | ||
|
||
const apiKeys = await listApiKeys({ externalId: user.id }).then( | ||
({ keys }) => keys, | ||
); | ||
|
||
const reply: ListAPIKeysResponse = { | ||
keys: apiKeys.map(({ start, name, createdAt, expires, meta }) => ({ | ||
start, | ||
name, | ||
createdAt, | ||
expiresAt: expires, | ||
uuid: meta?.uuid as string | undefined, | ||
})), | ||
}; | ||
return reply; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { CreateAPIKeyRequest } from "@codemod-com/api-types"; | ||
import { Unkey } from "@unkey/api"; | ||
import { memoize } from "lodash-es"; | ||
|
||
const UNKEY_API_ID = process.env.UNKEY_API_ID as string; | ||
const UNKEY_ROOT_KEY = process.env.UNKEY_ROOT_KEY as string; | ||
|
||
const getUnkey = memoize(() => new Unkey({ rootKey: UNKEY_ROOT_KEY })); | ||
|
||
export const createApiKey = async ({ | ||
apiKeyData, | ||
externalId, | ||
uuid, | ||
}: { apiKeyData: CreateAPIKeyRequest; externalId: string; uuid: string }) => { | ||
const response = await getUnkey().keys.create({ | ||
apiId: UNKEY_API_ID, | ||
prefix: "codemod.com", | ||
externalId, | ||
name: apiKeyData.name, | ||
expires: apiKeyData.expiresAt | ||
? Date.parse(apiKeyData.expiresAt) | ||
: undefined, | ||
meta: { | ||
uuid, | ||
}, | ||
}); | ||
|
||
if (response.error) { | ||
throw new Error(response.error.message); | ||
} | ||
|
||
return response.result; | ||
}; | ||
|
||
export const listApiKeys = async ({ externalId }: { externalId: string }) => { | ||
const response = await getUnkey().apis.listKeys({ | ||
apiId: UNKEY_API_ID, | ||
externalId, | ||
}); | ||
|
||
if (response.error) { | ||
throw new Error(response.error.message); | ||
} | ||
|
||
return response.result; | ||
}; | ||
|
||
export const deleteApiKeys = async ({ keyIds }: { keyIds: string[] }) => { | ||
await Promise.all( | ||
keyIds.map(async (keyId) => | ||
getUnkey().keys.delete({ | ||
keyId, | ||
}), | ||
), | ||
); | ||
}; |
Oops, something went wrong.