Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:auth): delete pending 2FA cache key after activation

johaven264f83c31d1e
diff --git a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.ts b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.ts
index 07e388d8..fcc8d24d 100644
--- a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.ts
+++ b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.ts
@@ -44,7 +44,8 @@ describe(AuthProvider2FA.name, () => {
provide: Cache,
useValue: {
get: vi.fn(),
- set: vi.fn()
+ set: vi.fn(),
+ del: vi.fn().mockResolvedValue(true)
}
},
{
@@ -152,6 +153,7 @@ describe(AuthProvider2FA.name, () => {
twoFaSecret: 'encrypted-secret',
recoveryCodes: expect.any(Array)
})
+ expect(cache.del).toHaveBeenCalledWith(`auth-2fa-pending-user-${mockUser.id}`)
expect(notificationsManager.sendEmailNotification).toHaveBeenCalledWith([mockRequest.user], {
app: 'auth_2fa',
event: 'Two-factor authentication (2FA) on your account has been enabled',
diff --git a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts
index 7ce60d81..dadcfb7a 100644
--- a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts
+++ b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts
@@ -36,7 +36,8 @@ export class AuthProvider2FA {
async enableTwoFactor(body: TwoFaVerifyWithPasswordDto, req: FastifyAuthenticatedRequest): Promise<TwoFaEnableResult> {
// retrieve encrypted secret from cache
- const secret: string = await this.cache.get(this.getCacheKey(req.user.id))
+ const cacheKey = this.getCacheKey(req.user.id)
+ const secret: string = await this.cache.get(cacheKey)
if (!secret) {
throw new HttpException('The secret has expired', HttpStatus.BAD_REQUEST)
}
@@ -56,6 +57,7 @@ export class AuthProvider2FA {
twoFaSecret: secret,
recoveryCodes: recoveryCodes.map((code) => this.encryptSecret(code))
})
+ await this.cache.del(cacheKey)
this.sendEmailNotification(req, ACTION.ADD)
return { ...auth, recoveryCodes: recoveryCodes }
}