Skip to content

Commit

Permalink
fix: show enrollment modal for existing clients after e2ei activation…
Browse files Browse the repository at this point in the history
… [WPB-9816] (#17422)

* fix: show modal after first activation

* chore: please pipeline, run tests

* fix: make it possible to snooze for existing clients after failed enrolment
  • Loading branch information
aweiss-dev authored May 22, 2024
1 parent 8500525 commit 72c8c63
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/script/E2EIdentity/E2EIdentityEnrollment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
} else {
// If we have an enrollment in progress but we are not coming back from an idp redirect, we need to clear the progress and start over
await this.coreE2EIService.clearAllProgress();
await this.startEnrollment(ModalType.ENROLL, false);
await this.startEnrollment(ModalType.ENROLL, !isFreshClient);
}
} else if (isFreshClient) {
// When the user logs in to a new device in an environment that has e2ei enabled, they should be forced to enroll
Expand All @@ -174,18 +174,24 @@ export class E2EIHandler extends TypedEventEmitter<Events> {
* @returns the delay under which the next enrollment/renewal modal will be prompted
*/
public async startTimers() {
// We store the first time the user was prompted with the enrollment modal
const storedE2eActivatedAt = this.enrollmentStore.get.e2eiActivatedAt();
const e2eActivatedAt = storedE2eActivatedAt || Date.now();
this.enrollmentStore.store.e2eiActivatedAt(e2eActivatedAt);
// Get the time when the user was first prompted with the enrollment modal
let storedE2eActivatedAt = this.enrollmentStore.get.e2eiActivatedAt();
// Check if the user has never been prompted with the enrollment modal, default store value is 0
const isFirstActivation = storedE2eActivatedAt === 0;
// If the user has never been prompted with the enrollment modal, we store the current time as the first activation time
if (isFirstActivation) {
storedE2eActivatedAt = Date.now();
this.enrollmentStore.store.e2eiActivatedAt(storedE2eActivatedAt);
}

const timerKey = 'enrollmentTimer';
const identity = await getActiveWireIdentity();

const {firingDate: computedFiringDate, isSnoozable} = getEnrollmentTimer(
identity,
e2eActivatedAt,
storedE2eActivatedAt,
this.config.gracePeriodInMs,
isFirstActivation,
);

const task = async () => {
Expand Down
16 changes: 16 additions & 0 deletions src/script/E2EIdentity/EnrollmentTimer/EnrollmentTimer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ describe('e2ei delays', () => {
jest.setSystemTime(1709050878009);
});

it('should return an immediate delay at feature activation', () => {
const delay = getEnrollmentTimer(
{
x509Identity: {
certificate: ' ',
notAfter: (Date.now() + TimeInMillis.MINUTE * 10) / 1000,
},
} as any,
Date.now(),
TimeInMillis.DAY * 30,
true,
);

expect(delay).toEqual({firingDate: Date.now(), isSnoozable: true});
});

it('should return an immediate delay if the identity is expired', () => {
const delay = getEnrollmentTimer({status: MLSStatuses.EXPIRED} as any, Date.now(), gracePeriod);

Expand Down
6 changes: 6 additions & 0 deletions src/script/E2EIdentity/EnrollmentTimer/EnrollmentTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function getEnrollmentTimer(
identity: WireIdentity | undefined,
e2eiActivatedAt: number,
teamGracePeriodDuration: number,
isFirstActivation: boolean = false,
) {
if (identity?.status === MLSStatuses.EXPIRED) {
return {isSnoozable: false, firingDate: Date.now()};
Expand All @@ -99,6 +100,11 @@ export function getEnrollmentTimer(
const deadline = getGracePeriod(identity, e2eiActivatedAt, teamGracePeriodDuration);
const nextTick = getNextTick(deadline);

// For the first activation, we want to trigger the timer immediately
if (isFirstActivation) {
return {isSnoozable: nextTick > 0, firingDate: Date.now()};
}

// When logging in to a old device that doesn't have an identity yet, we trigger an enrollment timer
return {isSnoozable: nextTick > 0, firingDate: Date.now() + nextTick};
}

0 comments on commit 72c8c63

Please sign in to comment.