extract common func

This commit is contained in:
Fang-Pen Lin
2025-11-13 09:21:31 -08:00
parent d559d48e7e
commit 490087dd90
@@ -317,13 +317,16 @@ export const AcmeCertificateAuthorityFns = ({
return cas.map(castDbEntryToAcmeCertificateAuthority); return cas.map(castDbEntryToAcmeCertificateAuthority);
}; };
const orderSubscriberCertificate = async (subscriberId: string) => { const orderCertificate = async ({
const subscriber = await pkiSubscriberDAL.findById(subscriberId); caId,
if (!subscriber.caId) { commonName,
throw new BadRequestError({ message: "Subscriber does not have a CA" }); altNames
} }: {
caId: string;
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(subscriber.caId); commonName: string;
altNames?: string[];
}): Promise<string> => {
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
if (!ca.externalCa || ca.externalCa.type !== CaType.ACME) { if (!ca.externalCa || ca.externalCa.type !== CaType.ACME) {
throw new BadRequestError({ message: "CA is not an ACME CA" }); throw new BadRequestError({ message: "CA is not an ACME CA" });
} }
@@ -401,8 +404,8 @@ export const AcmeCertificateAuthorityFns = ({
const [, certificateCsr] = await acme.crypto.createCsr( const [, certificateCsr] = await acme.crypto.createCsr(
{ {
altNames: subscriber.subjectAlternativeNames, altNames,
commonName: subscriber.commonName commonName
}, },
skLeaf skLeaf
); );
@@ -535,10 +538,21 @@ export const AcmeCertificateAuthorityFns = ({
await triggerAutoSyncForSubscriber(subscriber.id, { pkiSyncDAL, pkiSyncQueue }); await triggerAutoSyncForSubscriber(subscriber.id, { pkiSyncDAL, pkiSyncQueue });
}; };
const orderCertificateForAcmeProfile = async (profileId: string, commonName: string): Promise<string> => {
const profile = await certificateProfileDAL.findByIdWithConfigs(profileId);
if (!profile) {
throw new NotFoundError({ message: "Certificate profile not found" });
}
if (profile.enrollmentType !== EnrollmentType.ACME) {
throw new NotFoundError({ message: "Certificate profile is not configured for ACME enrollment" });
}
};
return { return {
createCertificateAuthority, createCertificateAuthority,
updateCertificateAuthority, updateCertificateAuthority,
listCertificateAuthorities, listCertificateAuthorities,
orderSubscriberCertificate orderSubscriberCertificate,
orderCertificateForAcmeProfile
}; };
}; };