From f712970fa91ab990c9ce959bf0889f68bfad8658 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 26 May 2022 15:23:11 +0200 Subject: refactor: move certificate trust to main process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is no need to synchronize the list of trusted certificates to the renderer process, so we can get away with storing them in the transient state of the Profile store. Signed-off-by: Kristóf Marussy --- packages/main/src/stores/Profile.ts | 43 ++++++++++++++++++++++++++----------- packages/main/src/stores/Service.ts | 5 +++++ 2 files changed, 36 insertions(+), 12 deletions(-) (limited to 'packages/main') diff --git a/packages/main/src/stores/Profile.ts b/packages/main/src/stores/Profile.ts index b4343a0..73f4f0b 100644 --- a/packages/main/src/stores/Profile.ts +++ b/packages/main/src/stores/Profile.ts @@ -18,21 +18,40 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { Certificate, Profile as ProfileBase } from '@sophie/shared'; -import { clone, getSnapshot, Instance } from 'mobx-state-tree'; +import { + type Certificate, + type CertificateSnapshotIn, + Profile as ProfileBase, +} from '@sophie/shared'; +import { getSnapshot, type Instance } from 'mobx-state-tree'; import type ProfileConfig from './config/ProfileConfig.js'; -const Profile = ProfileBase.views((self) => ({ - get config(): ProfileConfig { - const { id, settings } = self; - return { ...getSnapshot(settings), id }; - }, -})).actions((self) => ({ - temporarilyTrustCertificate(certificate: Certificate): void { - self.temporarilyTrustedCertificates.push(clone(certificate)); - }, -})); +const Profile = ProfileBase.volatile( + (): { + temporarilyTrustedCertificates: string[]; + } => ({ + temporarilyTrustedCertificates: [], + }), +) + .views((self) => ({ + get config(): ProfileConfig { + const { id, settings } = self; + return { ...getSnapshot(settings), id }; + }, + isCertificateTemporarilyTrusted( + certificate: CertificateSnapshotIn, + ): boolean { + return self.temporarilyTrustedCertificates.includes( + certificate.fingerprint, + ); + }, + })) + .actions((self) => ({ + temporarilyTrustCertificate(certificate: Certificate): void { + self.temporarilyTrustedCertificates.push(certificate.fingerprint); + }, + })); /* eslint-disable-next-line @typescript-eslint/no-redeclare -- diff --git a/packages/main/src/stores/Service.ts b/packages/main/src/stores/Service.ts index 3b7d0b2..d062fe1 100644 --- a/packages/main/src/stores/Service.ts +++ b/packages/main/src/stores/Service.ts @@ -67,6 +67,11 @@ const Service = defineServiceModel(ServiceSettings) get shouldBeLoaded(): boolean { return !self.crashed; }, + isCertificateTemporarilyTrusted( + certificate: CertificateSnapshotIn, + ): boolean { + return self.settings.profile.isCertificateTemporarilyTrusted(certificate); + }, })) .views((self) => ({ get shouldBeVisible(): boolean { -- cgit v1.2.3-54-g00ecf