From 33f5941e589e1cd89fa556bb5e8c1bc374ba38f0 Mon Sep 17 00:00:00 2001 From: Artem Vasilev Date: Thu, 23 Jun 2022 11:57:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D0=BE=D0=B1=D1=8A?= =?UTF-8?q?=D0=B5=D0=B4=D0=B5=D0=BD=D1=91=D0=BD=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20=D1=81=D0=B5=D1=80=D1=82?= =?UTF-8?q?=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=82=D0=BE=D0=B2=20=D0=B8=D0=B7?= =?UTF-8?q?=20=D0=BB=D0=B8=D1=87=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=B8=20=D0=B8?= =?UTF-8?q?=D0=B7=20=D0=B7=D0=B0=D0=BA=D1=80=D1=8B=D1=82=D0=BE=D0=B3=D0=BE?= =?UTF-8?q?=20=D0=BA=D0=BB=D1=8E=D1=87=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/getAllCertificates.ts | 62 ++++++++++++++++++++++++++++++++ src/api/getCertificates.ts | 66 +++++++++++++++++++++++++++++++++++ src/api/index.ts | 2 ++ 3 files changed, 130 insertions(+) create mode 100755 src/api/getAllCertificates.ts create mode 100755 src/api/getCertificates.ts diff --git a/src/api/getAllCertificates.ts b/src/api/getAllCertificates.ts new file mode 100755 index 0000000..ecce935 --- /dev/null +++ b/src/api/getAllCertificates.ts @@ -0,0 +1,62 @@ +import {Certificate} from './certificate'; +import {_afterPluginsLoaded} from '../helpers/_afterPluginsLoaded'; +import {getAllUserCertificates} from './getAllUserCertificates'; +import {getAllContainerCertificates} from './getAllContainerCertificates'; + +let certificatesCache: Certificate[]; +/** + * Возвращает список сертификатов, доступных пользователю из пользовательского хранилища и закрытых ключей, не установленных в системе, без фильтрации по дате и наличию приватного ключа + * + * @param resetCache = false - позволяет сбросить кэш ранее полученных сертификатов + * @returns список сертификатов + */ +export const getAllCertificates = _afterPluginsLoaded( + async (resetCache: boolean = false): Promise => { + if (!resetCache && certificatesCache) { + return certificatesCache; + } + + let availableCertificates: Certificate[]; + + try { + availableCertificates = await getAllUserCertificates(resetCache); + } catch (error) { + console.error(error); + + availableCertificates = []; + } + + try { + const containerAllCertificates: Certificate[] = await getAllContainerCertificates(resetCache); + + if (!availableCertificates) { + availableCertificates = containerAllCertificates; + } else { + let containerAllCertificatesCount = containerAllCertificates.length - 1; + let foundAvailableCertificate; + + while (containerAllCertificatesCount) { + foundAvailableCertificate = availableCertificates.find( + (cert) => cert.thumbprint === containerAllCertificates[containerAllCertificatesCount].thumbprint + ); + + if (!foundAvailableCertificate) { + availableCertificates.push(containerAllCertificates[containerAllCertificatesCount]); + } + + containerAllCertificatesCount--; + } + } + } catch (error) { + console.error(error); + } + + if (!availableCertificates) { + throw new Error('Нет доступных сертификатов'); + } + + certificatesCache = availableCertificates; + + return certificatesCache; + }, +); diff --git a/src/api/getCertificates.ts b/src/api/getCertificates.ts new file mode 100755 index 0000000..3bcdfc2 --- /dev/null +++ b/src/api/getCertificates.ts @@ -0,0 +1,66 @@ +import {Certificate} from './certificate'; +import {_afterPluginsLoaded} from '../helpers/_afterPluginsLoaded'; +import {getUserCertificates} from './getUserCertificates'; +import {getContainerCertificates} from "./getContainerCertificates"; +import {getAllUserCertificates} from "./getAllUserCertificates"; +import {getAllContainerCertificates} from "./getAllContainerCertificates"; + + +let certificatesCache: Certificate[]; +/** + * Возвращает список сертификатов, доступных пользователю из пользовательского хранилища и закрытых ключей, не установленных в системе + * + * @param resetCache = false - позволяет сбросить кэш ранее полученных сертификатов + * @returns список сертификатов + */ +export const getCertificates = _afterPluginsLoaded( + async (resetCache: boolean = false): Promise => { + + if (!resetCache && certificatesCache) { + return certificatesCache; + } + + let availableCertificates: Certificate[]; + + try { + availableCertificates = await getUserCertificates(resetCache); + } catch (error) { + console.error(error); + + availableCertificates = []; + } + + try { + const containerCertificates: Certificate[] = await getContainerCertificates(resetCache); + + if (!availableCertificates) { + availableCertificates = containerCertificates; + } else { + let containerCertificatesCount = containerCertificates.length - 1; + let foundAvailableCertificate; + + while (containerCertificatesCount) { + foundAvailableCertificate = availableCertificates.find( + (cert) => cert.thumbprint === containerCertificates[containerCertificatesCount].thumbprint + ); + + if (!foundAvailableCertificate) { + availableCertificates.push(containerCertificates[containerCertificatesCount]); + } + + containerCertificatesCount--; + } + } + } catch (error) { + console.error(error); + } + + if (!availableCertificates) { + throw new Error('Нет доступных сертификатов'); + } + + certificatesCache = availableCertificates; + + return certificatesCache; + }, +); diff --git a/src/api/index.ts b/src/api/index.ts index 98ba640..d5bb4f8 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,6 +3,8 @@ export * from './getUserCertificates'; export * from './getAllUserCertificates'; export * from './getContainerCertificates'; export * from './getAllContainerCertificates'; +export * from './getCertificates'; +export * from './getAllCertificates'; export * from './getSystemInfo'; export * from './isValidSystemSetup'; export * from './createXMLSignature';