Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 365x 5x 5x 5x 5x 5x 15x 5x 4x 4x 12x 4x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 4x 2x 12x 12x 4x 5x 5x 5x 5x 5x 5x | import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { pdfkitAddPlaceholder } from "@signpdf/placeholder-pdfkit010";
import forge from "node-forge";
import PDFDocument from "pdfkit";
import * as testData from "./test.data.js";
const {
attrs: { rootCaAttrs, intermediateCaAttrs, clientAttrs },
} = testData;
/**
* Creates a Buffer containing a PDF.
* Returns a Promise that is resolved with the resulting Buffer of the PDFDocument.
*
* @param {Uint8Array} [params]
* @returns {Promise<Buffer>}
*/
const createPDF = async (params) =>
new Promise((resolve) => {
const requestParams = {
placeholder: {},
text: "node-signpdf",
addSignaturePlaceholder: true,
pages: 1,
...params,
};
const pdf = new PDFDocument({
autoFirstPage: false,
size: "A4",
layout: "portrait",
bufferPages: true,
});
// pdf.info.CreationDate = new Date();
Iif (requestParams.pages < 1) {
requestParams.pages = 1;
}
// Add some content to the page(s)
for (let i = 0; i < requestParams.pages; i += 1) {
pdf
.addPage()
.fillColor("#333")
.fontSize(25)
.moveDown()
.text(requestParams.text)
.save();
}
// Collect the ouput PDF
// and, when done, resolve with it stored in a Buffer
/**
* @type {Uint8Array[]}
*/
const pdfChunks = [];
pdf.on("data", (data) => {
pdfChunks.push(data);
});
pdf.on("end", () => {
resolve(Buffer.concat(pdfChunks));
});
Eif (requestParams.addSignaturePlaceholder) {
// Externally (to PDFKit) add the signature placeholder.
const refs = pdfkitAddPlaceholder({
contactInfo: "test",
name: "test",
location: "test",
pdf,
pdfBuffer: Buffer.from(
// @ts-expect-error IDK
[pdf],
),
reason: "I am the author",
...requestParams.placeholder,
});
// Externally end the streams of the created objects.
// PDFKit doesn't know much about them, so it won't .end() them.
for (const ref of Object.values(refs)) {
ref.end();
}
}
// Also end the PDFDocument stream.
// See pdf.on('end'... on how it is then converted to Buffer.
pdf.end();
});
const __dirname = dirname(fileURLToPath(import.meta.url));
const getResourceAbsolutePath = (resourceRelativePath = "") =>
join(__dirname, "../../test-resources", resourceRelativePath);
/**
* @typedef Cert
* @property {forge.pki.CertificateField[]} subjectAttrs
* @property {forge.pki.CertificateField[]} issuerAttrs
* @property {string} serialNumber
* @property {boolean} [selfSignedCertificate]
* @property {forge.pki.rsa.PrivateKey} [privateKey]
* @property {string} password
* @property {Date} notBefore
* @property {Date} notAfter
*/
/**
* @param {Cert} params
*/
const createCertificate = ({
subjectAttrs,
issuerAttrs,
serialNumber,
selfSignedCertificate,
privateKey,
password,
notBefore,
notAfter,
}) => {
const keys = forge.pki.rsa.generateKeyPair(2048);
const cert = forge.pki.createCertificate();
cert.publicKey = keys.publicKey;
cert.serialNumber = serialNumber;
cert.validity.notBefore = notBefore;
cert.validity.notAfter = notAfter;
cert.setSubject(subjectAttrs);
cert.setIssuer(issuerAttrs);
/**
* @type {forge.pki.rsa.PrivateKey}
*/
// @ts-expect-error not-null
const signingKey = selfSignedCertificate ? keys.privateKey : privateKey;
cert.sign(signingKey);
const pkcs12Asn1 = forge.pkcs12.toPkcs12Asn1(
keys.privateKey,
[cert],
password,
{
generateLocalKeyId: true,
friendlyName: "test",
},
);
return {
pkcs12Cert: forge.asn1.toDer(pkcs12Asn1).getBytes(),
publicKey: keys.publicKey,
privateKey: keys.privateKey,
pemCertificate: forge.pki.certificateToPem(cert),
};
};
/**
* @typedef Attr
* @property {string} [name]
* @property {unknown} value
* @property {string} [shortName]
*/
/**
* @param {readonly Attr[]} attrs
*/
const parseAttrs = (attrs) =>
attrs.reduce((agg, ele) => {
const key =
ele.name ||
(ele.shortName === "ST" && "stateOrProvinceName") ||
ele.shortName === "OU" ||
"organizationalUnitName";
// biome-ignore lint/performance/noAccumulatingSpread: <explanation>
return { ...agg, [`${key}`]: ele.value };
}, {});
/**
* @typedef Validity
* @property {forge.pki.rsa.PrivateKey} [privateKey]
* @property {Date} notBefore
* @property {Date} notAfter
*/
/**
* @param {{clientValidity: Validity; intermediateValidity: Validity; rootValidity: Validity}} params
*/
const createCertificateChain = ({
clientValidity,
intermediateValidity,
rootValidity,
}) => {
const {
pki: { certificateFromPem },
} = forge;
const passphrase = "password";
const { pemCertificate: rootPemCertificate, privateKey: rootPrivateKey } =
createCertificate({
subjectAttrs: rootCaAttrs,
issuerAttrs: rootCaAttrs,
serialNumber: "01",
selfSignedCertificate: true,
password: passphrase,
...rootValidity,
});
const {
pemCertificate: intermediatePemCertificate,
privateKey: intermediatePrivateKey,
} = createCertificate({
subjectAttrs: intermediateCaAttrs,
issuerAttrs: rootCaAttrs,
serialNumber: "02",
password: passphrase,
...intermediateValidity,
privateKey: rootPrivateKey,
});
const { pemCertificate: clientPemCertificate } = createCertificate({
subjectAttrs: clientAttrs,
issuerAttrs: intermediateCaAttrs,
serialNumber: "03",
password: passphrase,
...clientValidity,
privateKey: intermediatePrivateKey,
});
return [
certificateFromPem(intermediatePemCertificate),
certificateFromPem(clientPemCertificate),
certificateFromPem(rootPemCertificate),
];
};
export {
createPDF,
getResourceAbsolutePath,
createCertificate,
parseAttrs,
createCertificateChain,
};
|