Add MVP version

This commit is contained in:
KMKoushik
2024-03-24 17:43:56 +11:00
parent 9032efa9b2
commit bbc64b5392
49 changed files with 3249 additions and 298 deletions

View File

@@ -0,0 +1,59 @@
import { ApiPermission } from "@prisma/client";
import { db } from "../db";
import { randomBytes } from "crypto";
import { hashToken } from "../auth";
export async function addApiKey({
name,
permission,
teamId,
}: {
name: string;
permission: ApiPermission;
teamId: number;
}) {
try {
const token = `us_${randomBytes(20).toString("hex")}`;
const hashedToken = hashToken(token);
await db.apiKey.create({
data: {
name,
permission: permission,
teamId,
tokenHash: hashedToken,
partialToken: `${token.slice(0, 8)}...${token.slice(-5)}`,
},
});
return token;
} catch (error) {
console.error("Error adding API key:", error);
throw error;
}
}
export async function retrieveApiKey(token: string) {
const hashedToken = hashToken(token);
try {
const apiKey = await db.apiKey.findUnique({
where: {
tokenHash: hashedToken,
},
select: {
id: true,
name: true,
permission: true,
teamId: true,
partialToken: true,
},
});
if (!apiKey) {
throw new Error("API Key not found");
}
return apiKey;
} catch (error) {
console.error("Error retrieving API key:", error);
throw error;
}
}