Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refresh user after kdf migration #7888

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
bedhub marked this conversation as resolved.
Show resolved Hide resolved
// 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