store certs in db

This commit is contained in:
Huskydog9988
2025-03-14 10:53:37 -04:00
parent 98c8258127
commit 61764e81b8
3 changed files with 78 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import path from "path";
import fs from "fs";
import { CertificateBundle } from "./ca";
import prisma from "../db/database";
export type CertificateStore = {
store(name: string, data: CertificateBundle): Promise<void>;
@ -33,3 +34,63 @@ export const fsCertificateStore = (base: string) => {
};
return store;
};
export const dbCertificateStore = () => {
const store: CertificateStore = {
async store(name: string, data: CertificateBundle) {
await prisma.certificate.upsert({
where: {
id: name,
},
create: {
id: name,
privateKey: data.priv,
certificate: data.cert,
},
update: {
privateKey: data.priv,
certificate: data.cert,
},
});
},
async fetch(name: string) {
const result = await prisma.certificate.findUnique({
where: {
id: name,
},
select: {
privateKey: true,
certificate: true,
},
});
if (result === null) return undefined;
return {
priv: result.privateKey,
cert: result.certificate,
};
},
async blacklistCertificate(name: string) {
await prisma.certificate.update({
where: {
id: name,
},
data: {
blacklisted: true,
},
});
},
async checkBlacklistCertificate(name: string): Promise<boolean> {
const result = await prisma.certificate.findUnique({
where: {
id: name,
},
select: {
blacklisted: true,
},
});
if (result === null) return false;
return result.blacklisted;
},
};
return store;
};