( req: MonkeyRequest<undefined, AddApeKeyRequest>, )
| 34 | } |
| 35 | |
| 36 | export 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 | |
| 74 | export async function editApeKey( |
| 75 | req: MonkeyRequest<undefined, EditApeKeyRequest, ApeKeyParams>, |
nothing calls this directly
no test coverage detected