MCPcopy Create free account
hub / github.com/monkeytypegame/monkeytype / generateApeKey

Function generateApeKey

backend/src/api/controllers/ape-key.ts:36–72  ·  view source on GitHub ↗
(
  req: MonkeyRequest<undefined, AddApeKeyRequest>,
)

Source from the content-addressed store, hash-verified

34}
35
36export async function generateApeKey(
37 req: MonkeyRequest<undefined, AddApeKeyRequest>,
38): Promise<AddApeKeyResponse> {
39 const { name, enabled } = req.body;
40 const { uid } = req.ctx.decodedToken;
41 const { maxKeysPerUser, apeKeyBytes, apeKeySaltRounds } =
42 req.ctx.configuration.apeKeys;
43
44 const currentNumberOfApeKeys = await ApeKeysDAL.countApeKeysForUser(uid);
45
46 if (currentNumberOfApeKeys >= maxKeysPerUser) {
47 throw new MonkeyError(409, "Maximum number of ApeKeys have been generated");
48 }
49
50 const apiKey = randomBytes(apeKeyBytes).toString("base64url");
51 const saltyHash = await hash(apiKey, apeKeySaltRounds);
52
53 const apeKey: ApeKeysDAL.DBApeKey = {
54 _id: new ObjectId(),
55 name,
56 enabled,
57 uid,
58 hash: saltyHash,
59 createdOn: Date.now(),
60 modifiedOn: Date.now(),
61 lastUsedOn: -1,
62 useCount: 0,
63 };
64
65 const apeKeyId = await ApeKeysDAL.addApeKey(apeKey);
66
67 return new MonkeyResponse("ApeKey generated", {
68 apeKey: base64UrlEncode(`${apeKeyId}.${apiKey}`),
69 apeKeyId,
70 apeKeyDetails: cleanApeKey(apeKey),
71 });
72}
73
74export async function editApeKey(
75 req: MonkeyRequest<undefined, EditApeKeyRequest, ApeKeyParams>,

Callers

nothing calls this directly

Calls 4

base64UrlEncodeFunction · 0.90
hashFunction · 0.85
cleanApeKeyFunction · 0.85
toStringMethod · 0.80

Tested by

no test coverage detected