Skip to content

Commit

Permalink
refresh user after kdf migration
Browse files Browse the repository at this point in the history
fix the issue we had when trying to see the recover code after kdfMigration to argon 2.
tutadb#1894
  • Loading branch information
bedhub committed Nov 6, 2024
1 parent abff358 commit 7f8bfdf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ export async function initLocator(worker: CalendarWorkerImpl, browserData: Brows
async (error: Error) => {
await worker.sendError(error)
},
locator.cacheManagement,
)

locator.userManagement = lazyMemoized(async () => {
Expand Down
8 changes: 8 additions & 0 deletions src/common/api/worker/facades/LoginFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DeferredObject,
Hex,
hexToUint8Array,
lazyAsync,
neverNull,
ofClass,
uint8ArrayToBase64,
Expand Down Expand Up @@ -96,6 +97,7 @@ import { Argon2idFacade } from "./Argon2idFacade.js"
import { CredentialType } from "../../../misc/credentials/CredentialType.js"
import { KeyRotationFacade } from "./KeyRotationFacade.js"
import { encryptString } from "../crypto/CryptoWrapper.js"
import { CacheManagementFacade } from "./lazy/CacheManagementFacade.js"

assertWorkerOrNode()

Expand Down Expand Up @@ -198,6 +200,7 @@ export class LoginFacade {
private readonly argon2idFacade: Argon2idFacade,
private readonly noncachingEntityClient: EntityClient,
private readonly sendError: (error: Error) => Promise<void>,
private readonly cacheManagementFacade: lazyAsync<CacheManagementFacade>,
) {}

init(eventBusClient: EventBusClient) {
Expand Down Expand Up @@ -333,6 +336,11 @@ export class LoginFacade {
})
console.log("Migrate KDF from:", user.kdfVersion, "to", targetKdfType)
await this.serviceExecutor.post(ChangeKdfService, changeKdfPostIn)
// We reload the user because we experienced a race condition
// were we do not process the User update after doing the argon2 migration from the web client.´
// In order do not rework the entity processing and its initialization for new clients we
// replace the cached instances after doing the migration
await (await this.cacheManagementFacade()).reloadUser()
this.userFacade.setUserGroupKeyDistributionKey(newUserPassphraseKey)
}

Expand Down
1 change: 1 addition & 0 deletions src/mail-app/workerUtils/worker/WorkerLocator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ export async function initLocator(worker: WorkerImpl, browserData: BrowserData)
async (error: Error) => {
await worker.sendError(error)
},
locator.cacheManagement,
)

locator.search = lazyMemoized(async () => {
Expand Down
5 changes: 5 additions & 0 deletions test/tests/api/worker/facades/LoginFacadeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { createTestEntity } from "../../../TestUtils.js"
import { KeyRotationFacade } from "../../../../../src/common/api/worker/facades/KeyRotationFacade.js"
import { CredentialType } from "../../../../../src/common/misc/credentials/CredentialType.js"
import { encryptString } from "../../../../../src/common/api/worker/crypto/CryptoWrapper.js"
import { CacheManagementFacade } from "../../../../../src/common/api/worker/facades/lazy/CacheManagementFacade.js"

const { anything, argThat } = matchers

Expand Down Expand Up @@ -97,6 +98,7 @@ o.spec("LoginFacadeTest", function () {
let blobAccessTokenFacade: BlobAccessTokenFacade
let databaseKeyFactoryMock: DatabaseKeyFactory
let argon2idFacade: Argon2idFacade
let cacheManagmentFacadeMock: CacheManagementFacade

const timeRangeDays = 42
const login = "born.slippy@tuta.io"
Expand Down Expand Up @@ -139,6 +141,7 @@ o.spec("LoginFacadeTest", function () {
databaseKeyFactoryMock = object()
argon2idFacade = object()
when(argon2idFacade.generateKeyFromPassphrase(anything(), anything())).thenResolve(PASSWORD_KEY)
cacheManagmentFacadeMock = object()

facade = new LoginFacade(
restClientMock,
Expand All @@ -156,6 +159,7 @@ o.spec("LoginFacadeTest", function () {
argon2idFacade,
entityClientMock,
async (error: Error) => {},
async () => cacheManagmentFacadeMock,
)

eventBusClientMock = instance(EventBusClient)
Expand Down Expand Up @@ -802,6 +806,7 @@ o.spec("LoginFacadeTest", function () {
}),
),
)
verify(cacheManagmentFacadeMock.reloadUser())
})
o.afterEach(() => {
Const.EXECUTE_KDF_MIGRATION = false
Expand Down

0 comments on commit 7f8bfdf

Please sign in to comment.