mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 23:47:06 +00:00
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import ms from "ms";
|
|
|
|
import { TCertificateTemplates } from "@app/db/schemas";
|
|
import { BadRequestError } from "@app/lib/errors";
|
|
|
|
export const validateCertificateDetailsAgainstTemplate = (
|
|
cert: {
|
|
commonName: string;
|
|
notBeforeDate: Date;
|
|
notAfterDate: Date;
|
|
altNames: string[];
|
|
},
|
|
template: TCertificateTemplates
|
|
) => {
|
|
const commonNameRegex = new RegExp(template.commonName);
|
|
if (!commonNameRegex.test(cert.commonName)) {
|
|
throw new BadRequestError({
|
|
message: "Invalid common name based on template policy"
|
|
});
|
|
}
|
|
|
|
if (cert.notAfterDate.getTime() - cert.notBeforeDate.getTime() > ms(template.ttl)) {
|
|
throw new BadRequestError({
|
|
message: "Invalid validity date based on template policy"
|
|
});
|
|
}
|
|
|
|
const subjectAlternativeNameRegex = new RegExp(template.subjectAlternativeName);
|
|
cert.altNames.forEach((altName) => {
|
|
if (!subjectAlternativeNameRegex.test(altName)) {
|
|
throw new BadRequestError({
|
|
message: "Invalid subject alternative name based on template policy"
|
|
});
|
|
}
|
|
});
|
|
};
|