2021-10-04 21:31:36 +00:00
|
|
|
import { helper } from '@ember/component/helper';
|
|
|
|
import { pki } from 'node-forge';
|
|
|
|
|
|
|
|
export function parsePkiCert([model]) {
|
|
|
|
// model has to be the responseJSON from PKI serializer
|
|
|
|
if (!model.certificate) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-23 18:51:02 +00:00
|
|
|
let cert;
|
|
|
|
// node-forge cannot parse EC (elliptical curve) certs
|
|
|
|
// set canParse to false if unable to convert a Forge cert from PEM
|
|
|
|
try {
|
|
|
|
cert = pki.certificateFromPem(model.certificate);
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
can_parse: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const commonName = cert?.subject.getField('CN') ? cert.subject.getField('CN').value : null;
|
|
|
|
const expiryDate = cert?.validity.notAfter;
|
|
|
|
const issueDate = cert?.validity.notBefore;
|
2021-10-04 21:31:36 +00:00
|
|
|
return {
|
2021-11-23 18:51:02 +00:00
|
|
|
can_parse: true,
|
2021-10-04 21:31:36 +00:00
|
|
|
common_name: commonName,
|
|
|
|
expiry_date: expiryDate,
|
2021-11-23 18:51:02 +00:00
|
|
|
issue_date: issueDate,
|
2021-10-04 21:31:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default helper(parsePkiCert);
|