@peculiar/fortify-webcomponents
Version:
Web-components for creating CSR or Certificate and viewing certificates list using Fortify
223 lines (215 loc) • 18.6 kB
JavaScript
/*!
* © Peculiar Ventures https://peculiarventures.com/ - BSD 3-Clause License
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const index = require('./index-39af7d9f.js');
const certification_request = require('./certification_request-89396ee6.js');
const x509_certificate = require('./x509_certificate-197d7aaf.js');
const x509_crl = require('./x509_crl-d7366252.js');
const object_identifiers = require('./object_identifiers-d4a25c28.js');
const button = require('./button-528dbba8.js');
const typography = require('./typography-526424d5.js');
require('./utils-10d69476.js');
/*!
* © Peculiar Ventures https://peculiarventures.com/ - MIT License
*/
/**
* @license
* Copyright (c) Peculiar Ventures, LLC.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Read file as Binary string
*
* @example
* ```js
* const file = new File(['file'], 'file.pdf', { type: 'text/plain' });
* readAsBinaryString(file)
* .then(result => console.log('Readed success', result))
* .catch(err => console.log('An error occured when reading file', err));
* ```
*/
function readAsBinaryString(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve({
value: reader.result,
fileName: file.name,
fileSize: file.size,
sourceMime: file.type,
});
reader.onerror = () => reject(reader.error);
reader.readAsBinaryString(file);
});
}
/*!
* © Peculiar Ventures https://peculiarventures.com/ - MIT License
*/
class X509Certificates extends Array {
constructor(raw) {
super();
const rawItems = certification_request.PemConverter.isPem(raw)
? certification_request.PemConverter.decode(raw).map((value) => certification_request.Convert_1.ToBase64Url(value))
: raw.split(',');
if (rawItems.length < 2) {
throw new Error('Unable to parse string. The array of elements is less than 2');
}
// eslint-disable-next-line no-restricted-syntax
for (const item of rawItems) {
this.push(new x509_certificate.X509Certificate(item));
}
}
get commonName() {
return Array.from(this)
.map((o) => o.commonName)
.join('_');
}
get raw() {
const signedData = new certification_request.SignedData();
signedData.version = 1;
signedData.encapContentInfo.eContentType = object_identifiers.id_data;
signedData.encapContentInfo.eContent = new certification_request.EncapsulatedContent({
single: new certification_request.OctetString(),
});
signedData.certificates = new certification_request.CertificateSet(Array.from(this).map((o) => new certification_request.CertificateChoices({
certificate: certification_request.AsnConvert.parse(o.raw, certification_request.Certificate),
})));
const cms = new certification_request.ContentInfo({
contentType: object_identifiers.id_signedData,
content: certification_request.AsnConvert.serialize(signedData),
});
return certification_request.AsnConvert.serialize(cms);
}
toString(format = 'pem') {
switch (format) {
case 'pem':
return Array.from(this)
.map((o) => o.toString('pem'))
.join('\n');
case 'base64url':
return Array.from(this)
.map((o) => o.toString('base64url'))
.join(',');
default:
return Array.from(this)
.map((o) => o.toString('base64'))
.join(',');
}
}
downloadAsPEM(name) {
certification_request.Download.cert.asPEM(this.toString('pem'), name || this.commonName);
}
downloadAsDER(name) {
certification_request.Download.cert.asDER(this.raw, name || this.commonName);
}
}
const certificateDecoderCss = "*{-webkit-box-sizing:border-box;box-sizing:border-box;margin:0;padding:0;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;-moz-text-size-adjust:none;-ms-text-size-adjust:none;text-size-adjust:none;-webkit-text-size-adjust:none;-webkit-tap-highlight-color:transparent;font-family:var(--pv-font-family, inherit)}.t-h1{font-weight:var(--pv-text-h1-weight);font-size:var(--pv-text-h1-size);line-height:var(--pv-text-h1-height);letter-spacing:var(--pv-text-h1-spacing)}.t-h2{font-weight:var(--pv-text-h2-weight);font-size:var(--pv-text-h2-size);line-height:var(--pv-text-h2-height);letter-spacing:var(--pv-text-h2-spacing)}.t-h3{font-weight:var(--pv-text-h3-weight);font-size:var(--pv-text-h3-size);line-height:var(--pv-text-h3-height);letter-spacing:var(--pv-text-h3-spacing)}.t-h4{font-weight:var(--pv-text-h4-weight);font-size:var(--pv-text-h4-size);line-height:var(--pv-text-h4-height);letter-spacing:var(--pv-text-h4-spacing)}.t-h5{font-weight:var(--pv-text-h5-weight);font-size:var(--pv-text-h5-size);line-height:var(--pv-text-h5-height);letter-spacing:var(--pv-text-h5-spacing)}.t-s1{font-weight:var(--pv-text-s1-weight);font-size:var(--pv-text-s1-size);line-height:var(--pv-text-s1-height);letter-spacing:var(--pv-text-s1-spacing)}.t-s2{font-weight:var(--pv-text-s2-weight);font-size:var(--pv-text-s2-size);line-height:var(--pv-text-s2-height);letter-spacing:var(--pv-text-s2-spacing)}.t-b1{font-weight:var(--pv-text-b1-weight);font-size:var(--pv-text-b1-size);line-height:var(--pv-text-b1-height);letter-spacing:var(--pv-text-b1-spacing)}.t-b2{font-weight:var(--pv-text-b2-weight);font-size:var(--pv-text-b2-size);line-height:var(--pv-text-b2-height);letter-spacing:var(--pv-text-b2-spacing)}.t-b3{font-weight:var(--pv-text-b3-weight);font-size:var(--pv-text-b3-size);line-height:var(--pv-text-b3-height);letter-spacing:var(--pv-text-b3-spacing)}.t-btn1{font-weight:var(--pv-text-btn1-weight);font-size:var(--pv-text-btn1-size);line-height:var(--pv-text-btn1-height);letter-spacing:var(--pv-text-btn1-spacing)}.t-btn2{font-weight:var(--pv-text-btn2-weight);font-size:var(--pv-text-btn2-size);line-height:var(--pv-text-btn2-height);letter-spacing:var(--pv-text-btn2-spacing)}.t-c1{font-weight:var(--pv-text-c1-weight);font-size:var(--pv-text-c1-size);line-height:var(--pv-text-c1-height);letter-spacing:var(--pv-text-c1-spacing)}.t-c2{font-weight:var(--pv-text-c2-weight);font-size:var(--pv-text-c2-size);line-height:var(--pv-text-c2-height);letter-spacing:var(--pv-text-c2-spacing)}.c-primary-tint-5{--pv-color-base:var(--pv-color-primary-tint-5)}.c-primary-tint-4{--pv-color-base:var(--pv-color-primary-tint-4)}.c-primary-tint-3{--pv-color-base:var(--pv-color-primary-tint-3)}.c-primary-tint-2{--pv-color-base:var(--pv-color-primary-tint-2)}.c-primary-tint-1{--pv-color-base:var(--pv-color-primary-tint-1)}.c-primary{--pv-color-base:var(--pv-color-primary)}.c-primary-shade-1{--pv-color-base:var(--pv-color-primary-shade-1)}.c-primary-shade-2{--pv-color-base:var(--pv-color-primary-shade-2)}.c-primary-shade-3{--pv-color-base:var(--pv-color-primary-shade-3)}.c-primary-shade-4{--pv-color-base:var(--pv-color-primary-shade-4)}.c-primary-shade-5{--pv-color-base:var(--pv-color-primary-shade-5)}.c-primary-contrast{--pv-color-base:var(--pv-color-primary-contrast)}.c-secondary-tint-5{--pv-color-base:var(--pv-color-secondary-tint-5)}.c-secondary-tint-4{--pv-color-base:var(--pv-color-secondary-tint-4)}.c-secondary-tint-3{--pv-color-base:var(--pv-color-secondary-tint-3)}.c-secondary-tint-2{--pv-color-base:var(--pv-color-secondary-tint-2)}.c-secondary-tint-1{--pv-color-base:var(--pv-color-secondary-tint-1)}.c-secondary{--pv-color-base:var(--pv-color-secondary)}.c-secondary-shade-1{--pv-color-base:var(--pv-color-secondary-shade-1)}.c-secondary-shade-2{--pv-color-base:var(--pv-color-secondary-shade-2)}.c-secondary-shade-3{--pv-color-base:var(--pv-color-secondary-shade-3)}.c-secondary-shade-4{--pv-color-base:var(--pv-color-secondary-shade-4)}.c-secondary-shade-5{--pv-color-base:var(--pv-color-secondary-shade-5)}.c-secondary-contrast{--pv-color-base:var(--pv-color-secondary-contrast)}.c-wrong-tint-5{--pv-color-base:var(--pv-color-wrong-tint-5)}.c-wrong-tint-4{--pv-color-base:var(--pv-color-wrong-tint-4)}.c-wrong-tint-3{--pv-color-base:var(--pv-color-wrong-tint-3)}.c-wrong-tint-2{--pv-color-base:var(--pv-color-wrong-tint-2)}.c-wrong-tint-1{--pv-color-base:var(--pv-color-wrong-tint-1)}.c-wrong{--pv-color-base:var(--pv-color-wrong)}.c-wrong-shade-1{--pv-color-base:var(--pv-color-wrong-shade-1)}.c-wrong-shade-2{--pv-color-base:var(--pv-color-wrong-shade-2)}.c-wrong-shade-3{--pv-color-base:var(--pv-color-wrong-shade-3)}.c-wrong-shade-4{--pv-color-base:var(--pv-color-wrong-shade-4)}.c-wrong-shade-5{--pv-color-base:var(--pv-color-wrong-shade-5)}.c-wrong-contrast{--pv-color-base:var(--pv-color-wrong-contrast)}.c-attention-tint-5{--pv-color-base:var(--pv-color-attention-tint-5)}.c-attention-tint-4{--pv-color-base:var(--pv-color-attention-tint-4)}.c-attention-tint-3{--pv-color-base:var(--pv-color-attention-tint-3)}.c-attention-tint-2{--pv-color-base:var(--pv-color-attention-tint-2)}.c-attention-tint-1{--pv-color-base:var(--pv-color-attention-tint-1)}.c-attention{--pv-color-base:var(--pv-color-attention)}.c-attention-shade-1{--pv-color-base:var(--pv-color-attention-shade-1)}.c-attention-shade-2{--pv-color-base:var(--pv-color-attention-shade-2)}.c-attention-shade-3{--pv-color-base:var(--pv-color-attention-shade-3)}.c-attention-shade-4{--pv-color-base:var(--pv-color-attention-shade-4)}.c-attention-shade-5{--pv-color-base:var(--pv-color-attention-shade-5)}.c-success-tint-5{--pv-color-base:var(--pv-color-success-tint-5)}.c-success-tint-4{--pv-color-base:var(--pv-color-success-tint-4)}.c-success-tint-3{--pv-color-base:var(--pv-color-success-tint-3)}.c-success-tint-2{--pv-color-base:var(--pv-color-success-tint-2)}.c-success-tint-1{--pv-color-base:var(--pv-color-success-tint-1)}.c-success{--pv-color-base:var(--pv-color-success)}.c-success-shade-1{--pv-color-base:var(--pv-color-success-shade-1)}.c-success-shade-2{--pv-color-base:var(--pv-color-success-shade-2)}.c-success-shade-3{--pv-color-base:var(--pv-color-success-shade-3)}.c-success-shade-4{--pv-color-base:var(--pv-color-success-shade-4)}.c-success-shade-5{--pv-color-base:var(--pv-color-success-shade-5)}.c-black{--pv-color-base:var(--pv-color-black)}.c-gray-10{--pv-color-base:var(--pv-color-gray-10)}.c-gray-9{--pv-color-base:var(--pv-color-gray-9)}.c-gray-8{--pv-color-base:var(--pv-color-gray-8)}.c-gray-7{--pv-color-base:var(--pv-color-gray-7)}.c-gray-6{--pv-color-base:var(--pv-color-gray-6)}.c-gray-5{--pv-color-base:var(--pv-color-gray-5)}.c-gray-4{--pv-color-base:var(--pv-color-gray-4)}.c-gray-3{--pv-color-base:var(--pv-color-gray-3)}.c-gray-2{--pv-color-base:var(--pv-color-gray-2)}.c-gray-1{--pv-color-base:var(--pv-color-gray-1)}.c-white{--pv-color-base:var(--pv-color-white)}.c-extra-1{--pv-color-base:var(--pv-color-extra-1)}.c-extra-2{--pv-color-base:var(--pv-color-extra-2)}.typography{color:var(--pv-color-base)}.button{display:-ms-inline-flexbox;display:inline-flex;cursor:pointer;background:transparent;border:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;text-decoration:none;outline:none;font-family:inherit;border-radius:4px;height:var(--pv-size-base-6);min-width:var(--pv-size-base-6);padding:0 var(--pv-size-base-2);-webkit-transition:background-color 200ms ease 0s;transition:background-color 200ms ease 0s;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;gap:var(--pv-size-base-2);font-size:0}.button.m_no_padding{padding:0}.button:hover{background-color:var(--pv-color-gray-3)}.button:focus{background-color:var(--pv-color-gray-4)}.button:active{background-color:var(--pv-color-gray-5)}:host{display:block;width:100%}.textarea{min-height:300px;width:100%;border-radius:4px;border:1px solid var(--pv-color-gray-5);padding:14px;font-family:monospace;resize:vertical}.controls{margin-top:var(--pv-size-base-2);display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;gap:var(--pv-size-base-2);margin-bottom:var(--pv-size-base-12)}.control_row{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;gap:var(--pv-size-base)}";
const PeculiarCertificateDecoderStyle0 = certificateDecoderCss;
const CertificateDecoder = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.successParse = index.createEvent(this, "successParse", 7);
this.clearCertificate = index.createEvent(this, "clearCertificate", 7);
this.handleClickDecode = () => {
const { value } = this.inputPaste;
if (value) {
this.decode(value);
}
};
this.handleClickClear = () => {
this.clearValue();
};
this.handleChangeInputFile = async (event) => {
const element = event.target;
if (element.files) {
const file = await readAsBinaryString(element.files[0]);
if (typeof file.value === 'string') {
this.decode(file.value);
}
element.value = '';
}
};
this.handleChangeExample = (event) => {
if (event.target.value) {
this.decode(event.target.value);
}
else {
this.clearValue();
}
};
this.handleDropFile = async (event) => {
event.stopPropagation();
event.preventDefault();
const element = event.dataTransfer;
if (element.files) {
const file = await readAsBinaryString(element.files[0]);
if (typeof file.value === 'string') {
this.decode(file.value);
}
}
};
this.certificateExamples = undefined;
this.certificateToDecode = undefined;
this.certificateDecoded = undefined;
}
componentDidLoad() {
if (this.certificateToDecode) {
/**
* Prevent Stencil warning about re-render
*/
setTimeout(() => this.decode(this.certificateToDecode), 100);
}
}
clearValue() {
this.inputPaste.value = '';
this.certificateDecoded = null;
this.clearCertificate.emit();
}
setValue(value) {
this.certificateDecoded = value;
this.inputPaste.value = value.toString('pem');
this.successParse.emit(value.toString('base64url'));
}
decode(value) {
new Promise((resolve) => {
resolve(new X509Certificates(value));
})
.catch(() => new x509_certificate.X509Certificate(value))
.catch(() => new x509_crl.X509AttributeCertificate(value))
.catch(() => new x509_crl.Pkcs10CertificateRequest(value))
.catch(() => new x509_crl.X509Crl(value))
.then((res) => this.setValue(res))
.catch((error) => {
console.log(error);
alert(`Error decoding certificate:\n"${value}"\n\nPlease try to use Certificate/AttributeCertificate/CertificateRequest/CRL.`);
});
}
renderCertificate() {
if (this.certificateDecoded instanceof X509Certificates) {
return (index.h("peculiar-certificate-chain-viewer", { certificates: this.certificateDecoded, download: true }));
}
if (this.certificateDecoded instanceof x509_certificate.X509Certificate) {
return (index.h("peculiar-certificate-viewer", { certificate: this.certificateDecoded, download: true }));
}
if (this.certificateDecoded instanceof x509_crl.X509AttributeCertificate) {
return (index.h("peculiar-attribute-certificate-viewer", { certificate: this.certificateDecoded, download: true }));
}
if (this.certificateDecoded instanceof x509_crl.Pkcs10CertificateRequest) {
return (index.h("peculiar-csr-viewer", { certificate: this.certificateDecoded, download: true }));
}
if (this.certificateDecoded instanceof x509_crl.X509Crl) {
return (index.h("peculiar-crl-viewer", { certificate: this.certificateDecoded, download: true }));
}
return null;
}
render() {
var _a;
return (index.h(index.Host, { key: '466ae73c347d305938c8b5f7a9ca0fcc8ec000eb' }, index.h("textarea", { key: '00d3552d847823c1e4e10162c5ac22de48062c47', placeholder: "Certificate DER or PEM", class: "textarea t-b2 c-black", ref: (el) => { this.inputPaste = el; }, onDrop: this.handleDropFile }), index.h("div", { key: '2b45a534e3db2d4c879efcc849f439d494ad5adc', class: "controls" }, index.h("div", { key: 'c78db2ec219c7417c10c8aa4b99e72e1fc16499d', class: "control_row" }, index.h(typography.Typography, { key: '9e72c34b567870083b68fdaf082aae28f8899b24', variant: "b3", color: "secondary-tint-2" }, "Drag or load file:"), index.h("input", { key: '7069e5db2f0de85126b4fdcace19e181ab407a16', type: "file", accept: "application/pkix-cert,application/x-x509-ca-cert,application/x-x509-user-cert,application/pkcs10,application/pkix-crl,.csr,.req,.crl", onChange: this.handleChangeInputFile, value: "" })), ((_a = this.certificateExamples) === null || _a === void 0 ? void 0 : _a.length) && (index.h("div", { key: 'e184a0864d8432e1f0aa26788e58f3c336abc598', class: "control_row" }, index.h(typography.Typography, { key: '19e3213a14ef0b24bb110df7a4e943bf1f2a8ebf', variant: "b3", color: "secondary-tint-2" }, "Load examples:"), index.h("select", { key: '79082619d5d4bc82cf90afddf58ec58a5ae232b7', onChange: this.handleChangeExample }, index.h("option", { key: 'add23975d376914239389b621bd956bcd4fb52eb', value: "" }, "None"), this.certificateExamples.map((example) => (index.h("option", { value: example.value }, example.title)))))), index.h("div", { key: '876ccefd3125618d5c1e7c83e277349fa78da802', class: "control_row" }, index.h(button.Button, { key: 'bb890810c0895ba3fdcc22695e72cd8c2cf9645d', onClick: this.handleClickDecode }, "Decode"), index.h(button.Button, { key: 'dadc94b3e9e2e7211d993d99c127abfc8e593545', onClick: this.handleClickClear }, "Clear"))), this.renderCertificate()));
}
};
CertificateDecoder.style = PeculiarCertificateDecoderStyle0;
exports.peculiar_certificate_decoder = CertificateDecoder;
//# sourceMappingURL=peculiar-certificate-decoder.cjs.entry.js.map