From 40a1774a81662ce75800c622d8d1cbec005b2043 Mon Sep 17 00:00:00 2001 From: Thomas Belin Date: Wed, 17 Jan 2024 10:48:17 +0100 Subject: [PATCH] refactor: streamline softLock logic (#16536) --- src/script/E2EIdentity/DelayTimer/delay.ts | 23 ++++------- .../E2EIdentity/E2EIdentityEnrollment.ts | 28 ++++++++------ src/script/hooks/useAppSoftLock.ts | 38 ++++++------------- src/script/hooks/useDeviceIdentities.ts | 4 +- 4 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/script/E2EIdentity/DelayTimer/delay.ts b/src/script/E2EIdentity/DelayTimer/delay.ts index bad85231f69..bb366f37875 100644 --- a/src/script/E2EIdentity/DelayTimer/delay.ts +++ b/src/script/E2EIdentity/DelayTimer/delay.ts @@ -18,7 +18,7 @@ */ import {EnrollmentConfig} from '../E2EIdentityEnrollment'; -import {MLSStatuses, getActiveWireIdentity} from '../E2EIdentityVerification'; +import {MLSStatuses, WireIdentity} from '../E2EIdentityVerification'; /* eslint-disable no-magic-numbers */ @@ -54,22 +54,13 @@ export function getDelayTime(gracePeriodInMs: number): number { return 0; } -export async function shouldEnableSoftLock(enrollmentConfig: EnrollmentConfig): Promise { - const identity = await getActiveWireIdentity(); +export function shouldEnableSoftLock(enrollmentConfig: EnrollmentConfig, identity?: WireIdentity): boolean { + if (!enrollmentConfig.timer.isSnoozeTimeAvailable() || enrollmentConfig.isFreshMLSSelfClient) { + // The user has used up the entire grace period or has a fresh new client, he now needs to enroll + return true; + } if (!identity?.certificate) { return false; } - const isSnoozeTimeAvailable = !!enrollmentConfig?.timer.isSnoozeTimeAvailable(); - const isFreshMLSSelfClient = !!enrollmentConfig?.isFreshMLSSelfClient; - const certificateExpired = identity.status === MLSStatuses.EXPIRED; - const certificateRevoked = identity.status === MLSStatuses.REVOKED; - const certificateValidWithNoGracePeriod = identity.status === MLSStatuses.VALID && !isSnoozeTimeAvailable; - - return ( - isFreshMLSSelfClient || - !isSnoozeTimeAvailable || - certificateValidWithNoGracePeriod || - certificateExpired || - certificateRevoked - ); + return [MLSStatuses.EXPIRED, MLSStatuses.REVOKED].includes(identity.status); } diff --git a/src/script/E2EIdentity/E2EIdentityEnrollment.ts b/src/script/E2EIdentity/E2EIdentityEnrollment.ts index 9f97e2d70d8..1ec429edde3 100644 --- a/src/script/E2EIdentity/E2EIdentityEnrollment.ts +++ b/src/script/E2EIdentity/E2EIdentityEnrollment.ts @@ -64,7 +64,9 @@ interface E2EIHandlerParams { isFreshMLSSelfClient?: boolean; } -type Events = {enrollmentSuccessful: void; identityUpdate: {enrollmentConfig: EnrollmentConfig}}; +type Events = { + identityUpdated: {enrollmentConfig: EnrollmentConfig; identity: WireIdentity}; +}; export type EnrollmentConfig = { timer: DelayTimerService; @@ -146,10 +148,10 @@ export class E2EIHandler extends TypedEventEmitter { this.showE2EINotificationMessage(); return new Promise(resolve => { const handleSuccess = () => { - this.off('enrollmentSuccessful', handleSuccess); + this.off('identityUpdated', handleSuccess); resolve(); }; - this.on('enrollmentSuccessful', handleSuccess); + this.on('identityUpdated', handleSuccess); }); } @@ -168,8 +170,7 @@ export class E2EIHandler extends TypedEventEmitter { // Check if an enrollment is already in progress if (this.coreE2EIService.isEnrollmentInProgress()) { - await this.enroll(); - return; + return this.enroll(); } const renewalTimeMS = this.calculateRenewalTime(timeRemainingMS, historyTimeMS, this.config!.gracePeriodInMs); @@ -179,8 +180,7 @@ export class E2EIHandler extends TypedEventEmitter { // Check if it's time to renew the certificate if (currentTime >= renewalPromptTime) { await this.renewCertificate(); - - E2EIHandler.instance!.emit('identityUpdate', {enrollmentConfig: this.config!}); + this.emit('identityUpdated', {enrollmentConfig: this.config!, identity}); } } @@ -339,10 +339,14 @@ export class E2EIHandler extends TypedEventEmitter { PrimaryModal.show(modalType, modalOptions); } - private showSuccessMessage(isCertificateRenewal = false): void { + private async showSuccessMessage(isCertificateRenewal = false) { if (this.currentStep !== E2EIHandlerStep.SUCCESS) { return; } + const identity = await getActiveWireIdentity(); + if (!identity) { + return; + } const {modalOptions, modalType} = getModalOptions({ type: ModalType.SUCCESS, @@ -351,7 +355,7 @@ export class E2EIHandler extends TypedEventEmitter { extraParams: { isRenewal: isCertificateRenewal, }, - primaryActionFn: () => this.emit('enrollmentSuccessful'), + primaryActionFn: () => this.emit('identityUpdated', {enrollmentConfig: this.config!, identity}), secondaryActionFn: () => { amplify.publish(WebAppEvents.PREFERENCES.MANAGE_DEVICES); }, @@ -371,7 +375,7 @@ export class E2EIHandler extends TypedEventEmitter { // Clear the e2e identity progress this.coreE2EIService.clearAllProgress(); - const isSoftLockEnabled = await shouldEnableSoftLock(this.config!); + const isSoftLockEnabled = shouldEnableSoftLock(this.config!); const {modalOptions, modalType} = getModalOptions({ type: ModalType.ERROR, @@ -415,11 +419,11 @@ export class E2EIHandler extends TypedEventEmitter { private async showModal(modalType: ModalType = ModalType.ENROLL, hideSecondary = false): Promise { // Check if config is defined and timer is available - const isSoftLockEnabled = await shouldEnableSoftLock(this.config!); + const isSoftLockEnabled = shouldEnableSoftLock(this.config!); // Show the modal with the provided modal type const {modalOptions, modalType: determinedModalType} = getModalOptions({ - hideSecondary: !isSoftLockEnabled || hideSecondary, + hideSecondary: isSoftLockEnabled || hideSecondary, primaryActionFn: () => { if (modalType === ModalType.SNOOZE_REMINDER) { return undefined; diff --git a/src/script/hooks/useAppSoftLock.ts b/src/script/hooks/useAppSoftLock.ts index bcf2e494fa5..6274a50bf86 100644 --- a/src/script/hooks/useAppSoftLock.ts +++ b/src/script/hooks/useAppSoftLock.ts @@ -20,7 +20,7 @@ import {useEffect, useState} from 'react'; import {CallingRepository} from '../calling/CallingRepository'; -import {E2EIHandler, EnrollmentConfig, isE2EIEnabled, isFreshMLSSelfClient} from '../E2EIdentity'; +import {E2EIHandler, EnrollmentConfig, isE2EIEnabled, WireIdentity} from '../E2EIdentity'; import {shouldEnableSoftLock} from '../E2EIdentity/DelayTimer/delay'; import {NotificationRepository} from '../notification/NotificationRepository'; @@ -37,14 +37,14 @@ export function useAppSoftLock(callingRepository: CallingRepository, notificatio notificationRepository.setSoftLock(isLocked); }; - const checkIfIsFreshMLSSelfClient = async () => { - const initializedIsFreshMLSSelfClient = await isFreshMLSSelfClient(); - - setAppSoftLock(initializedIsFreshMLSSelfClient); - }; - - const handleSoftLockActivation = async ({enrollmentConfig}: {enrollmentConfig: EnrollmentConfig}) => { - const isSoftLockEnabled = await shouldEnableSoftLock(enrollmentConfig); + const handleSoftLockActivation = ({ + enrollmentConfig, + identity, + }: { + enrollmentConfig: EnrollmentConfig; + identity: WireIdentity; + }) => { + const isSoftLockEnabled = shouldEnableSoftLock(enrollmentConfig, identity); setAppSoftLock(isSoftLockEnabled); }; @@ -53,27 +53,11 @@ export function useAppSoftLock(callingRepository: CallingRepository, notificatio return () => {}; } - E2EIHandler.getInstance().on('identityUpdate', handleSoftLockActivation); + E2EIHandler.getInstance().on('identityUpdated', handleSoftLockActivation); return () => { - E2EIHandler.getInstance().off('identityUpdate', handleSoftLockActivation); + E2EIHandler.getInstance().off('identityUpdated', handleSoftLockActivation); }; }, [e2eiEnabled]); - useEffect(() => { - if (e2eiEnabled) { - void checkIfIsFreshMLSSelfClient(); - } - }, [e2eiEnabled]); - - useEffect(() => { - if (!freshMLSSelfClient) { - return () => {}; - } - E2EIHandler.getInstance().on('enrollmentSuccessful', checkIfIsFreshMLSSelfClient); - return () => { - E2EIHandler.getInstance().off('enrollmentSuccessful', checkIfIsFreshMLSSelfClient); - }; - }, [freshMLSSelfClient]); - return {isFreshMLSSelfClient: freshMLSSelfClient, softLockLoaded: e2eiEnabled ? softLockLoaded : true}; } diff --git a/src/script/hooks/useDeviceIdentities.ts b/src/script/hooks/useDeviceIdentities.ts index efdf6582fc3..2b0459acdb3 100644 --- a/src/script/hooks/useDeviceIdentities.ts +++ b/src/script/hooks/useDeviceIdentities.ts @@ -42,9 +42,9 @@ export const useUserIdentity = (userId: QualifiedId, groupId?: string, updateAft if (!updateAfterEnrollment) { return () => {}; } - E2EIHandler.getInstance().on('enrollmentSuccessful', refreshDeviceIdentities); + E2EIHandler.getInstance().on('identityUpdated', refreshDeviceIdentities); return () => { - E2EIHandler.getInstance().off('enrollmentSuccessful', refreshDeviceIdentities); + E2EIHandler.getInstance().off('identityUpdated', refreshDeviceIdentities); }; }, [refreshDeviceIdentities, updateAfterEnrollment]);