-
Notifications
You must be signed in to change notification settings - Fork 6
/
certificateDetails.js
48 lines (41 loc) · 1.17 KB
/
certificateDetails.js
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
const forge = require('node-forge');
const {
extractSignature,
getMessageFromSignature,
preparePDF,
} = require('./helpers');
const mapEntityAtrributes = (attrs) => attrs.reduce((agg, { name, value }) => {
if (!name) return agg;
agg[name] = value;
return agg;
}, {});
const extractSingleCertificateDetails = (cert) => {
const { issuer, subject, validity } = cert;
return {
issuedBy: mapEntityAtrributes(issuer.attributes),
issuedTo: mapEntityAtrributes(subject.attributes),
validityPeriod: validity,
pemCertificate: forge.pki.certificateToPem(cert),
};
};
const extractCertificatesDetails = (certs) => certs
.map(extractSingleCertificateDetails)
.map((cert, i) => {
if (i) return cert;
return {
clientCertificate: true,
...cert,
};
});
const getCertificatesInfoFromPDF = (pdf) => {
const pdfBuffer = preparePDF(pdf);
const { signatureStr } = extractSignature(pdfBuffer);
return signatureStr.map(signature => {
const { certificates } = getMessageFromSignature(signature);
return extractCertificatesDetails(certificates);
});
};
module.exports = {
extractCertificatesDetails,
getCertificatesInfoFromPDF,
};