UNPKG

@peculiar/fortify-webcomponents

Version:

Web-components for creating CSR or Certificate and viewing certificates list using Fortify

524 lines (523 loc) 21.9 kB
/*! * © Peculiar Ventures https://peculiarventures.com/ - BSD 3-Clause License */ /** * @license * Copyright (c) Peculiar Ventures, LLC. * * This source code is licensed under the BSD 3-Clause license found in the * LICENSE file in the root directory of this source tree. */ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _FortifyEnrollment_fortify; import { Host, h, } from "@stencil/core"; import { FortifyAPI, EName, } from "@peculiar/fortify-client-core"; import { downloadFromBuffer } from "@peculiar/certificates-viewer"; import { Convert } from "pvtsutils"; import { Name } from "@peculiar/x509"; import { l10n, } from "../../utils/l10n"; import { FortifyNotDetected } from "../fortify-not-detected"; import { FortifyNotSupported } from "../fortify-not-supported"; import { FortifyProvidersEmpty } from "../fortify-providers-empty"; import { FortifyConnecting } from "../fortify-connecting"; import { FortifyChallengeNotApproved } from "../fortify-challenge-not-approved"; import { FortifyAuthorization } from "../fortify-authorization"; import { FortifyStatus } from "../fortify-status"; import { FortifyLayout } from "../fortify-layout"; import { defaultFormPolicyConfig } from "./policyConfig"; import { EnrollmentRejected } from "./enrollment-rejected"; import { EnrollmentResolved } from "./enrollment-resolved"; import { EnrollmentForm } from "./enrollment-form"; export class FortifyEnrollment { constructor() { _FortifyEnrollment_fortify.set(this, void 0); /** * A link to download the application when a connection to Fortify is not found. */ this.downloadAppLink = 'https://fortifyapp.com'; /** * A link to redirect the user to the help page * if there are any questions about the operation of the application. */ this.helpPageLink = 'https://fortifyapp.com/help'; /** * If `true`, the component footer will be hidden. */ this.hideFooter = false; /** * Object that contains filters for user certificates and providers. */ this.filters = {}; this.providers = []; this.isFetching = { connectionDetect: 'pending' }; this.debugEvents = []; this.handleDocumentKeydown = (event) => { /** * Reset Fortify filters manually using `Shift+R` shortcut. */ if (event.shiftKey && event.code === 'KeyR') { __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").options.filters = {}; this.tryGetData(); } /** * Download Fortify debug events like a json file. */ if (event.shiftKey && event.code === 'KeyD') { downloadFromBuffer(Convert.FromString(JSON.stringify(this.debugEvents, undefined, 2)), `debug-fortify-wc.${Date.now()}`, 'json', 'text/json'); } }; this.handleClickCancel = () => { this.creationCancel.emit(); }; this.handleClickEnrollRerun = () => { this.deleteFetching('enroll'); this.enrollResult = undefined; this.tryGetData(); }; this.handleClickClose = () => { this.creationClose.emit(); }; this.handleClickTryAgain = async () => { const login = await this.tryLogin(); if (!login) { return; } await this.tryGetData(); }; this.handleSubmit = async (data) => { this.setFetching('enroll', 'pending'); try { const name = new Name(data.subjectName).toJSON(); let commonName = ''; let result; name.forEach((n) => { if (!commonName && n[EName.CommonName] && n[EName.CommonName].length) { commonName = n[EName.CommonName][0]; } }); if (data.useSelfSignedCertificate) { result = await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").createX509(data.providerId, data); } else { result = await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").createPKCS10(data.providerId, data); } this.enrollResult = { pem: result.pem, name: commonName, type: data.useSelfSignedCertificate ? 'x509' : 'pkcs10', }; this.setFetching('enroll', 'resolved'); this.creationSuccess.emit({ providerId: data.providerId, raw: result.der, privateKey: result.privateKey, publicKey: result.publicKey, socketProvider: __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").server, }); } catch (error) { this.setFetching('enroll', 'rejected'); this.creationFail.emit(error); } }; } componentWillLoad() { l10n.setLocale(this.language); this.formPolicy = this.formPolicy || defaultFormPolicyConfig(); } componentDidLoad() { __classPrivateFieldSet(this, _FortifyEnrollment_fortify, new FortifyAPI({ onDebug: (event) => { this.debugEvents.push(event); }, onClose: this.handleCloseConnection.bind(this), onProvidersAdded: this.tryGetData.bind(this), onProvidersRemoved: this.tryGetData.bind(this), filters: this.filters, }), "f"); this.start(); document.addEventListener('keydown', this.handleDocumentKeydown); } disconnectedCallback() { __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").finish(); document.removeEventListener('keydown', this.handleDocumentKeydown); } handleCloseConnection() { this.providers = []; this.setFetching('connectionDetect', 'rejected'); } async start() { this.setFetching('connectionDetect', 'pending'); if (!__classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").isConnectionSupported()) { this.setFetching('connectionSupport', 'rejected'); return; } if (!await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").isConnectionDetected()) { this.setFetching('connectionDetect', 'rejected'); } await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").isConnectionDetectedAuto(); this.setFetching('connectionDetect', 'pending'); await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").connect(); const login = await this.tryLogin(); if (!login) { return; } await this.tryGetData(); } async tryGetData() { this.setFetching('providers', 'pending'); this.providers = []; try { this.providers = await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").getProviders(); this.setFetching('providers', 'resolved'); } catch (_a) { this.setFetching('providers', 'rejected'); } } async tryLogin() { this.challenge = undefined; this.setFetching('connectionApprove', 'pending'); try { const challenge = await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").challenge(); if (challenge) { this.challenge = challenge; await __classPrivateFieldGet(this, _FortifyEnrollment_fortify, "f").login(); } this.setFetching('connectionApprove', 'resolved'); } catch (_a) { this.setFetching('connectionApprove', 'rejected'); return undefined; } return true; } setFetching(name, value) { this.isFetching = Object.assign(Object.assign({}, this.isFetching), { [name]: value }); } deleteFetching(name) { this.isFetching = Object.assign(Object.assign({}, this.isFetching), { [name]: undefined }); } static renderConnecting() { return (h(FortifyConnecting, null)); } static renderFortifyAuthorization(pin) { return (h(FortifyAuthorization, { pin: pin })); } renderFortifyNotDetected() { return (h(FortifyNotDetected, { downloadAppLink: this.downloadAppLink })); } renderFortifyNotSupported() { return (h(FortifyNotSupported, { onCancel: this.handleClickCancel })); } renderChallengeNotApproved() { return (h(FortifyChallengeNotApproved, { onCancel: this.handleClickCancel, onTryAgain: this.handleClickTryAgain })); } renderProvidersEmpty() { return (h(FortifyProvidersEmpty, { helpPageLink: this.helpPageLink, onCancel: this.handleClickCancel })); } renderEnrollForm() { return (h(EnrollmentForm, { providers: this.providers, formPolicy: this.formPolicy, onSubmit: this.handleSubmit, onCancel: this.handleClickCancel })); } renderEnrollResolved() { return (h(EnrollmentResolved, { type: this.enrollResult.type, pem: this.enrollResult.pem, name: this.enrollResult.name, onClose: this.handleClickClose })); } renderEnrollRejected() { return (h(EnrollmentRejected, { onCancel: this.handleClickCancel, onAccept: this.handleClickEnrollRerun })); } static renderUnresolvedStatus() { return (h(FortifyStatus, { title: "Someting went wrong for status", description: "ERROR" })); } render() { let content = null; switch (true) { case this.isFetching.connectionSupport === 'rejected': content = this.renderFortifyNotSupported(); break; case this.isFetching.connectionDetect === 'rejected': content = this.renderFortifyNotDetected(); break; case this.isFetching.connectionApprove === 'rejected': content = this.renderChallengeNotApproved(); break; case this.isFetching.connectionApprove === 'pending' && !!this.challenge: content = FortifyEnrollment.renderFortifyAuthorization(this.challenge); break; case this.isFetching.enroll === 'pending': content = FortifyEnrollment.renderConnecting(); break; case this.isFetching.enroll === 'rejected': content = this.renderEnrollRejected(); break; case this.isFetching.enroll === 'resolved': content = this.renderEnrollResolved(); break; case this.isFetching.providers === 'resolved' && !this.providers.length: content = this.renderProvidersEmpty(); break; case this.isFetching.providers === 'resolved': content = this.renderEnrollForm(); break; case this.isFetching.connectionDetect === 'pending': case this.isFetching.connectionSupport === 'pending': case this.isFetching.connectionApprove === 'pending': case this.isFetching.providers === 'pending': content = FortifyEnrollment.renderConnecting(); break; default: content = FortifyEnrollment.renderUnresolvedStatus(); } return (h(Host, { key: 'd71cf13afa2fa731a09f14513b4bf58b01cca18a' }, h(FortifyLayout, { key: 'cb84828fa3fa91017f63b4c4c61147177d6bab97', hideFooter: this.hideFooter }, content))); } static get is() { return "peculiar-fortify-enrollment"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["fortify-enrollment.scss"] }; } static get styleUrls() { return { "$": ["fortify-enrollment.css"] }; } static get properties() { return { "language": { "type": "string", "mutable": false, "complexType": { "original": "TSupportedMessagesType", "resolved": "\"de\" | \"el\" | \"en\" | \"es\" | \"fr\" | \"he\" | \"it\" | \"ja\" | \"nl\" | \"pl\" | \"pt\" | \"tr\" | \"uk\"", "references": { "TSupportedMessagesType": { "location": "import", "path": "../../utils/l10n", "id": "src/utils/l10n.ts::TSupportedMessagesType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Component language." }, "getter": false, "setter": false, "reflect": true, "attribute": "language" }, "downloadAppLink": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "A link to download the application when a connection to Fortify is not found." }, "getter": false, "setter": false, "reflect": true, "attribute": "download-app-link", "defaultValue": "'https://fortifyapp.com'" }, "helpPageLink": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "A link to redirect the user to the help page\nif there are any questions about the operation of the application." }, "getter": false, "setter": false, "reflect": true, "attribute": "help-page-link", "defaultValue": "'https://fortifyapp.com/help'" }, "hideFooter": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "If `true`, the component footer will be hidden." }, "getter": false, "setter": false, "reflect": true, "attribute": "hide-footer", "defaultValue": "false" }, "filters": { "type": "unknown", "mutable": false, "complexType": { "original": "Pick<IFilters, 'onlySmartcards' | 'providerNameMatch' | 'providerATRMatch'>", "resolved": "{ onlySmartcards?: boolean; providerNameMatch?: string | RegExp; providerATRMatch?: string | RegExp; }", "references": { "Pick": { "location": "global", "id": "global::Pick" }, "IFilters": { "location": "import", "path": "@peculiar/fortify-client-core", "id": "../client-core/dist/types/index.d.ts::IFilters" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Object that contains filters for user certificates and providers." }, "getter": false, "setter": false, "defaultValue": "{}" }, "formPolicy": { "type": "unknown", "mutable": false, "complexType": { "original": "IFormPolicyConfig", "resolved": "IFormPolicyConfig", "references": { "IFormPolicyConfig": { "location": "import", "path": "./policyConfig", "id": "src/components/fortify-enrollment/policyConfig.ts::IFormPolicyConfig" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Object that contains configurations for enrollment form." }, "getter": false, "setter": false } }; } static get states() { return { "providers": {}, "challenge": {}, "isFetching": {}, "enrollResult": {} }; } static get events() { return [{ "method": "creationCancel", "name": "creationCancel", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Fires when the user has canceled the creation flow." }, "complexType": { "original": "void", "resolved": "void", "references": {} } }, { "method": "creationClose", "name": "creationClose", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Fires when the user clicks on the \"Close\" button after\nsuccessfully creating a certificate or certificate request." }, "complexType": { "original": "void", "resolved": "void", "references": {} } }, { "method": "creationSuccess", "name": "creationSuccess", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Fires when the user has successfully created a certificate or certificate request." }, "complexType": { "original": "ICreationSuccessEvent", "resolved": "ICreationSuccessEvent", "references": { "ICreationSuccessEvent": { "location": "local", "path": "/home/runner/work/fortify-webcomponents/fortify-webcomponents/packages/webcomponents/src/components/fortify-enrollment/fortify-enrollment.tsx", "id": "src/components/fortify-enrollment/fortify-enrollment.tsx::ICreationSuccessEvent" } } } }, { "method": "creationFail", "name": "creationFail", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "Fires when the user failed to generate a certificate or certificate request." }, "complexType": { "original": "Error", "resolved": "Error", "references": { "Error": { "location": "global", "id": "global::Error" } } } }]; } } _FortifyEnrollment_fortify = new WeakMap(); //# sourceMappingURL=fortify-enrollment.js.map