open-vault/ui/app/helpers/parse-pki-cert.js
claire bontempo e8c9affee1
UI/Fix node-forge EC error (#13238)
* add catch for node-forge error handling

* update comment

* adds changelog

* alphabetize attrs and add canParse attr

* show alert banner if unable to parse metadata

* add test to check info banner renders
2021-11-23 13:51:02 -05:00

31 lines
844 B
JavaScript

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;
}
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;
return {
can_parse: true,
common_name: commonName,
expiry_date: expiryDate,
issue_date: issueDate,
};
}
export default helper(parsePkiCert);