Ape keys dal (#2925) bruception

* Convert Ape-keys DAO to DAL

* Fix this reference
This commit is contained in:
Bruce Berrios 2022-05-03 18:13:48 -04:00 committed by GitHub
parent 89f160f664
commit 12a63b1cb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 79 deletions

View file

@ -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<MonkeyResponse> {
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");
}

View file

@ -15,81 +15,82 @@ function getApeKeyFilter(
};
}
class ApeKeysDAO {
static async getApeKeys(uid: string): Promise<MonkeyTypes.ApeKey[]> {
return await db
.collection<MonkeyTypes.ApeKey>(COLLECTION_NAME)
.find({ uid })
.toArray();
}
export async function getApeKeys(uid: string): Promise<MonkeyTypes.ApeKey[]> {
return await db
.collection<MonkeyTypes.ApeKey>(COLLECTION_NAME)
.find({ uid })
.toArray();
}
static async getApeKey(keyId: string): Promise<MonkeyTypes.ApeKey | null> {
return await db
.collection<MonkeyTypes.ApeKey>(COLLECTION_NAME)
.findOne({ _id: new ObjectId(keyId) });
}
export async function getApeKey(
keyId: string
): Promise<MonkeyTypes.ApeKey | null> {
return await db
.collection<MonkeyTypes.ApeKey>(COLLECTION_NAME)
.findOne({ _id: new ObjectId(keyId) });
}
static async countApeKeysForUser(uid: string): Promise<number> {
const apeKeys = await this.getApeKeys(uid);
return _.size(apeKeys);
}
export async function countApeKeysForUser(uid: string): Promise<number> {
const apeKeys = await getApeKeys(uid);
return _.size(apeKeys);
}
static async addApeKey(apeKey: MonkeyTypes.ApeKey): Promise<string> {
const insertionResult = await db
.collection<MonkeyTypes.ApeKey>(COLLECTION_NAME)
.insertOne(apeKey);
return insertionResult.insertedId.toHexString();
}
export async function addApeKey(apeKey: MonkeyTypes.ApeKey): Promise<string> {
const insertionResult = await db
.collection<MonkeyTypes.ApeKey>(COLLECTION_NAME)
.insertOne(apeKey);
return insertionResult.insertedId.toHexString();
}
private static async updateApeKey(
uid: string,
keyId: string,
updates: MatchKeysAndValues<MonkeyTypes.ApeKey>
): Promise<void> {
const updateResult = await db
.collection<MonkeyTypes.ApeKey>(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<MonkeyTypes.ApeKey>
): Promise<void> {
const updateResult = await db
.collection<MonkeyTypes.ApeKey>(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<void> {
const apeKeyUpdates = {
name,
enabled,
modifiedOn: Date.now(),
};
await this.updateApeKey(uid, keyId, apeKeyUpdates);
}
static async updateLastUsedOn(uid: string, keyId: string): Promise<void> {
const apeKeyUpdates = {
lastUsedOn: Date.now(),
};
await this.updateApeKey(uid, keyId, apeKeyUpdates);
}
static async deleteApeKey(uid: string, keyId: string): Promise<void> {
const deletionResult = await db
.collection<MonkeyTypes.ApeKey>(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<void> {
const apeKeyUpdates = {
name,
enabled,
modifiedOn: Date.now(),
};
await updateApeKey(uid, keyId, apeKeyUpdates);
}
export async function updateLastUsedOn(
uid: string,
keyId: string
): Promise<void> {
const apeKeyUpdates = {
lastUsedOn: Date.now(),
};
await updateApeKey(uid, keyId, apeKeyUpdates);
}
export async function deleteApeKey(uid: string, keyId: string): Promise<void> {
const deletionResult = await db
.collection<MonkeyTypes.ApeKey>(COLLECTION_NAME)
.deleteOne(getApeKeyFilter(uid, keyId));
if (deletionResult.deletedCount === 0) {
throw new MonkeyError(404, "ApeKey not found");
}
}

View file

@ -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",