diff --git a/backend/api/controllers/ape-key.ts b/backend/api/controllers/ape-key.ts index 1e8c0ae14..d620adbe7 100644 --- a/backend/api/controllers/ape-key.ts +++ b/backend/api/controllers/ape-key.ts @@ -1,7 +1,7 @@ import _ from "lodash"; import { randomBytes } from "crypto"; import { hash } from "bcrypt"; -import ApeKeysDAO from "../../dao/ape-keys"; +import * as ApeKeysDAL from "../../dao/ape-keys"; import MonkeyError from "../../utils/error"; import { MonkeyResponse } from "../../utils/monkey-response"; import { base64UrlEncode } from "../../utils/misc"; @@ -16,7 +16,7 @@ export async function getApeKeys( ): Promise { const { uid } = req.ctx.decodedToken; - const apeKeys = await ApeKeysDAO.getApeKeys(uid); + const apeKeys = await ApeKeysDAL.getApeKeys(uid); const cleanedKeys = _(apeKeys).keyBy("_id").mapValues(cleanApeKey).value(); return new MonkeyResponse("ApeKeys retrieved", cleanedKeys); @@ -30,7 +30,7 @@ export async function generateApeKey( const { maxKeysPerUser, apeKeyBytes, apeKeySaltRounds } = req.ctx.configuration.apeKeys; - const currentNumberOfApeKeys = await ApeKeysDAO.countApeKeysForUser(uid); + const currentNumberOfApeKeys = await ApeKeysDAL.countApeKeysForUser(uid); if (currentNumberOfApeKeys >= maxKeysPerUser) { throw new MonkeyError(409, "Maximum number of ApeKeys have been generated"); @@ -51,7 +51,7 @@ export async function generateApeKey( useCount: 0, }; - const apeKeyId = await ApeKeysDAO.addApeKey(apeKey); + const apeKeyId = await ApeKeysDAL.addApeKey(apeKey); return new MonkeyResponse("ApeKey generated", { apeKey: base64UrlEncode(`${apeKeyId}.${apiKey}`), @@ -67,7 +67,7 @@ export async function editApeKey( const { name, enabled } = req.body; const { uid } = req.ctx.decodedToken; - await ApeKeysDAO.editApeKey(uid, apeKeyId, name, enabled); + await ApeKeysDAL.editApeKey(uid, apeKeyId, name, enabled); return new MonkeyResponse("ApeKey updated"); } @@ -78,7 +78,7 @@ export async function deleteApeKey( const { apeKeyId } = req.params; const { uid } = req.ctx.decodedToken; - await ApeKeysDAO.deleteApeKey(uid, apeKeyId); + await ApeKeysDAL.deleteApeKey(uid, apeKeyId); return new MonkeyResponse("ApeKey deleted"); } diff --git a/backend/dao/ape-keys.ts b/backend/dao/ape-keys.ts index 27c2cc4aa..72dc8fd14 100644 --- a/backend/dao/ape-keys.ts +++ b/backend/dao/ape-keys.ts @@ -15,81 +15,82 @@ function getApeKeyFilter( }; } -class ApeKeysDAO { - static async getApeKeys(uid: string): Promise { - return await db - .collection(COLLECTION_NAME) - .find({ uid }) - .toArray(); - } +export async function getApeKeys(uid: string): Promise { + return await db + .collection(COLLECTION_NAME) + .find({ uid }) + .toArray(); +} - static async getApeKey(keyId: string): Promise { - return await db - .collection(COLLECTION_NAME) - .findOne({ _id: new ObjectId(keyId) }); - } +export async function getApeKey( + keyId: string +): Promise { + return await db + .collection(COLLECTION_NAME) + .findOne({ _id: new ObjectId(keyId) }); +} - static async countApeKeysForUser(uid: string): Promise { - const apeKeys = await this.getApeKeys(uid); - return _.size(apeKeys); - } +export async function countApeKeysForUser(uid: string): Promise { + const apeKeys = await getApeKeys(uid); + return _.size(apeKeys); +} - static async addApeKey(apeKey: MonkeyTypes.ApeKey): Promise { - const insertionResult = await db - .collection(COLLECTION_NAME) - .insertOne(apeKey); - return insertionResult.insertedId.toHexString(); - } +export async function addApeKey(apeKey: MonkeyTypes.ApeKey): Promise { + const insertionResult = await db + .collection(COLLECTION_NAME) + .insertOne(apeKey); + return insertionResult.insertedId.toHexString(); +} - private static async updateApeKey( - uid: string, - keyId: string, - updates: MatchKeysAndValues - ): Promise { - const updateResult = await db - .collection(COLLECTION_NAME) - .updateOne(getApeKeyFilter(uid, keyId), { - $inc: { useCount: _.has(updates, "lastUsedOn") ? 1 : 0 }, - $set: _.pickBy(updates, (value) => !_.isNil(value)), - }); +async function updateApeKey( + uid: string, + keyId: string, + updates: MatchKeysAndValues +): Promise { + const updateResult = await db + .collection(COLLECTION_NAME) + .updateOne(getApeKeyFilter(uid, keyId), { + $inc: { useCount: _.has(updates, "lastUsedOn") ? 1 : 0 }, + $set: _.pickBy(updates, (value) => !_.isNil(value)), + }); - if (updateResult.modifiedCount === 0) { - throw new MonkeyError(404, "ApeKey not found"); - } - } - - static async editApeKey( - uid: string, - keyId: string, - name: string, - enabled: boolean - ): Promise { - const apeKeyUpdates = { - name, - enabled, - modifiedOn: Date.now(), - }; - - await this.updateApeKey(uid, keyId, apeKeyUpdates); - } - - static async updateLastUsedOn(uid: string, keyId: string): Promise { - const apeKeyUpdates = { - lastUsedOn: Date.now(), - }; - - await this.updateApeKey(uid, keyId, apeKeyUpdates); - } - - static async deleteApeKey(uid: string, keyId: string): Promise { - const deletionResult = await db - .collection(COLLECTION_NAME) - .deleteOne(getApeKeyFilter(uid, keyId)); - - if (deletionResult.deletedCount === 0) { - throw new MonkeyError(404, "ApeKey not found"); - } + if (updateResult.modifiedCount === 0) { + throw new MonkeyError(404, "ApeKey not found"); } } -export default ApeKeysDAO; +export async function editApeKey( + uid: string, + keyId: string, + name: string, + enabled: boolean +): Promise { + const apeKeyUpdates = { + name, + enabled, + modifiedOn: Date.now(), + }; + + await updateApeKey(uid, keyId, apeKeyUpdates); +} + +export async function updateLastUsedOn( + uid: string, + keyId: string +): Promise { + const apeKeyUpdates = { + lastUsedOn: Date.now(), + }; + + await updateApeKey(uid, keyId, apeKeyUpdates); +} + +export async function deleteApeKey(uid: string, keyId: string): Promise { + const deletionResult = await db + .collection(COLLECTION_NAME) + .deleteOne(getApeKeyFilter(uid, keyId)); + + if (deletionResult.deletedCount === 0) { + throw new MonkeyError(404, "ApeKey not found"); + } +} diff --git a/backend/middlewares/auth.ts b/backend/middlewares/auth.ts index 7cfc0c6cc..901ccce56 100644 --- a/backend/middlewares/auth.ts +++ b/backend/middlewares/auth.ts @@ -1,5 +1,5 @@ import { compare } from "bcrypt"; -import ApeKeysDAO from "../dao/ape-keys"; +import { getApeKey, updateLastUsedOn } from "../dao/ape-keys"; import MonkeyError from "../utils/error"; import { verifyIdToken } from "../utils/auth"; import { base64UrlDecode } from "../utils/misc"; @@ -161,7 +161,7 @@ async function authenticateWithApeKey( const decodedKey = base64UrlDecode(key); const [keyId, apeKey] = decodedKey.split("."); - const targetApeKey = await ApeKeysDAO.getApeKey(keyId); + const targetApeKey = await getApeKey(keyId); if (!targetApeKey) { throw new MonkeyError(404, "ApeKey not found"); } @@ -177,7 +177,7 @@ async function authenticateWithApeKey( throw new MonkeyError(code, message); } - await ApeKeysDAO.updateLastUsedOn(targetApeKey.uid, keyId); + await updateLastUsedOn(targetApeKey.uid, keyId); return { type: "ApeKey",