Skip to content

Commit

Permalink
refactor: streamline softLock logic (#16536)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc authored Jan 17, 2024
1 parent f02fa35 commit 40a1774
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 57 deletions.
23 changes: 7 additions & 16 deletions src/script/E2EIdentity/DelayTimer/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import {EnrollmentConfig} from '../E2EIdentityEnrollment';
import {MLSStatuses, getActiveWireIdentity} from '../E2EIdentityVerification';
import {MLSStatuses, WireIdentity} from '../E2EIdentityVerification';

/* eslint-disable no-magic-numbers */

Expand Down Expand Up @@ -54,22 +54,13 @@ export function getDelayTime(gracePeriodInMs: number): number {
return 0;
}

export async function shouldEnableSoftLock(enrollmentConfig: EnrollmentConfig): Promise<boolean> {
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);
}
28 changes: 16 additions & 12 deletions src/script/E2EIdentity/E2EIdentityEnrollment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,10 +148,10 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
this.showE2EINotificationMessage();
return new Promise<void>(resolve => {
const handleSuccess = () => {
this.off('enrollmentSuccessful', handleSuccess);
this.off('identityUpdated', handleSuccess);
resolve();
};
this.on('enrollmentSuccessful', handleSuccess);
this.on('identityUpdated', handleSuccess);
});
}

Expand All @@ -168,8 +170,7 @@ export class E2EIHandler extends TypedEventEmitter<Events> {

// 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);
Expand All @@ -179,8 +180,7 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
// 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});
}
}

Expand Down Expand Up @@ -339,10 +339,14 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
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,
Expand All @@ -351,7 +355,7 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
extraParams: {
isRenewal: isCertificateRenewal,
},
primaryActionFn: () => this.emit('enrollmentSuccessful'),
primaryActionFn: () => this.emit('identityUpdated', {enrollmentConfig: this.config!, identity}),
secondaryActionFn: () => {
amplify.publish(WebAppEvents.PREFERENCES.MANAGE_DEVICES);
},
Expand All @@ -371,7 +375,7 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
// 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,
Expand Down Expand Up @@ -415,11 +419,11 @@ export class E2EIHandler extends TypedEventEmitter<Events> {

private async showModal(modalType: ModalType = ModalType.ENROLL, hideSecondary = false): Promise<void> {
// 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;
Expand Down
38 changes: 11 additions & 27 deletions src/script/hooks/useAppSoftLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
};

Expand All @@ -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};
}
4 changes: 2 additions & 2 deletions src/script/hooks/useDeviceIdentities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down

0 comments on commit 40a1774

Please sign in to comment.