feat(ca): generate a server certificate for mtls APIs

This commit is contained in:
DecDuck
2024-11-06 11:38:32 +11:00
parent b9ae26cb27
commit 9c4b6f35bb

View File

@ -22,13 +22,22 @@ export class CertificateAuthority {
static async new(store: CertificateStore) { static async new(store: CertificateStore) {
const root = await store.fetch("ca"); const root = await store.fetch("ca");
let ca;
if (root === undefined) { if (root === undefined) {
const [cert, priv] = droplet.generateRootCa(); const [cert, priv] = droplet.generateRootCa();
const bundle: CertificateBundle = { priv, cert }; const bundle: CertificateBundle = { priv, cert };
await store.store("ca", bundle); await store.store("ca", bundle);
return new CertificateAuthority(store, bundle); ca = new CertificateAuthority(store, bundle);
} else {
ca = new CertificateAuthority(store, root);
} }
return new CertificateAuthority(store, root);
const serverCertificate = await ca.fetchClientCertificate("server");
if (!serverCertificate) {
await ca.generateClientCertificate("server", "Drop Server");
}
return ca;
} }
async generateClientCertificate(clientId: string, clientName: string) { async generateClientCertificate(clientId: string, clientName: string) {
@ -39,7 +48,7 @@ export class CertificateAuthority {
clientId, clientId,
clientName, clientName,
caCertificate.cert, caCertificate.cert,
caCertificate.priv, caCertificate.priv
); );
const certBundle: CertificateBundle = { const certBundle: CertificateBundle = {
priv, priv,
@ -53,8 +62,9 @@ export class CertificateAuthority {
} }
async fetchClientCertificate(clientId: string) { async fetchClientCertificate(clientId: string) {
const isBlacklist = const isBlacklist = await this.certificateStore.checkBlacklistCertificate(
await this.certificateStore.checkBlacklistCertificate(clientId); clientId
);
if (isBlacklist) return undefined; if (isBlacklist) return undefined;
return await this.certificateStore.fetch(`client:${clientId}`); return await this.certificateStore.fetch(`client:${clientId}`);
} }