From 4b78b452cf320709ed31dc3af2e6f0aaac046945 Mon Sep 17 00:00:00 2001 From: bed Date: Wed, 15 May 2024 15:55:55 +0200 Subject: [PATCH] await resetSession and add log statements. --- src/api/worker/facades/KeyLoaderFacade.ts | 1 + src/api/worker/facades/LoginFacade.ts | 4 +-- src/api/worker/facades/UserFacade.ts | 8 +++++- .../facades/lazy/ConfigurationDatabase.ts | 27 ++++++++++++++++--- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/api/worker/facades/KeyLoaderFacade.ts b/src/api/worker/facades/KeyLoaderFacade.ts index 13f7e9a3e8a3..bdcaa2ab9612 100644 --- a/src/api/worker/facades/KeyLoaderFacade.ts +++ b/src/api/worker/facades/KeyLoaderFacade.ts @@ -57,6 +57,7 @@ export class KeyLoaderFacade { async loadSymUserGroupKey(userGroupKeyVersion: number): Promise { // we provide the current user group key to break a possibly infinite recursion + console.log("KeyLoaderFacade: loadSymUserGroupKey") return this.loadSymGroupKey(this.userFacade.getUserGroupId(), userGroupKeyVersion, this.userFacade.getCurrentUserGroupKey()) } diff --git a/src/api/worker/facades/LoginFacade.ts b/src/api/worker/facades/LoginFacade.ts index b175e0cce085..ebb993f769c3 100644 --- a/src/api/worker/facades/LoginFacade.ts +++ b/src/api/worker/facades/LoginFacade.ts @@ -201,7 +201,7 @@ export class LoginFacade { } async resetSession(): Promise { - this.eventBusClient.close(CloseEventBusOption.Terminate) + await this.eventBusClient.close(CloseEventBusOption.Terminate) await this.deInitCache() this.userFacade.reset() } @@ -729,7 +729,7 @@ export class LoginFacade { this.loginListener.onFullLoginSuccess(sessionType, cacheInfo) return { user, accessToken, userGroupInfo } } catch (e) { - this.resetSession() + await this.resetSession() throw e } } diff --git a/src/api/worker/facades/UserFacade.ts b/src/api/worker/facades/UserFacade.ts index bbb6e3eb937c..02a0a998e5fc 100644 --- a/src/api/worker/facades/UserFacade.ts +++ b/src/api/worker/facades/UserFacade.ts @@ -35,6 +35,7 @@ export class UserFacade implements AuthDataProvider { // 2. User is set // 3. UserGroupKey is unlocked setAccessToken(accessToken: string | null) { + console.log("UserFacade: set accesSToken") this.accessToken = accessToken } @@ -43,6 +44,7 @@ export class UserFacade implements AuthDataProvider { } setUser(user: User) { + console.log("UserFacade: set user") if (this.accessToken == null) { throw new ProgrammingError("invalid state: no access token") } @@ -50,6 +52,7 @@ export class UserFacade implements AuthDataProvider { } unlockUserGroupKey(userPassphraseKey: AesKey) { + console.log("UserFacade: unlockUserGroupKey") if (this.user == null) { throw new ProgrammingError("Invalid state: no user") } @@ -61,6 +64,7 @@ export class UserFacade implements AuthDataProvider { } updateUser(user: User) { + console.log("UserFacade: updateUser", user) if (this.user == null) { throw new ProgrammingError("Update user is called without logging in. This function is not for you.") } @@ -99,6 +103,7 @@ export class UserFacade implements AuthDataProvider { if (this.isPartiallyLoggedIn()) { throw new LoginIncompleteError("userGroupKey not available") } else { + console.log("UserFacade - getCurrentUserGroupKey - ProgrammingError", new Error()) throw new ProgrammingError("Invalid state: userGroupKey is not available") } } @@ -158,7 +163,7 @@ export class UserFacade implements AuthDataProvider { setLeaderStatus(status: WebsocketLeaderStatus) { this.leaderStatus = status - console.log("New leader status set:", status.leaderStatus) + console.log("UserFacade: New leader status set:", status.leaderStatus) } isLeader(): boolean { @@ -166,6 +171,7 @@ export class UserFacade implements AuthDataProvider { } reset() { + console.log("UserFacade: reset user") this.user = null this.accessToken = null this.currentUserGroupKey = null diff --git a/src/api/worker/facades/lazy/ConfigurationDatabase.ts b/src/api/worker/facades/lazy/ConfigurationDatabase.ts index f6d71c2d7b84..a71704437557 100644 --- a/src/api/worker/facades/lazy/ConfigurationDatabase.ts +++ b/src/api/worker/facades/lazy/ConfigurationDatabase.ts @@ -43,18 +43,20 @@ export class ConfigurationDatabase { readonly db: LazyLoaded constructor( - userFacade: UserFacade, + readonly userFacade: UserFacade, dbLoadFn: (arg0: User, arg1: KeyLoaderFacade) => Promise = (user: User, keyLoaderFacade: KeyLoaderFacade) => this.loadConfigDb(user, keyLoaderFacade), readonly keyLoaderFacade: KeyLoaderFacade, ) { this.db = new LazyLoaded(() => { + console.log("ConfigurationDatabase - LazyLoaded") const user = assertNotNull(userFacade.getLoggedInUser()) return dbLoadFn(user, keyLoaderFacade) }) } async addExternalImageRule(address: string, rule: ExternalImageRule): Promise { + console.log("ConfigurationDatabase - addExternalImageRule - db.getAsync") const { db, metaData } = await this.db.getAsync() if (!db.indexingSupported) return const encryptedAddress = await encryptItem(address, metaData.key, metaData.iv) @@ -62,6 +64,7 @@ export class ConfigurationDatabase { } async getExternalImageRule(address: string): Promise { + console.log("ConfigurationDatabase - getExternalImageRule - db.getAsync") const { db, metaData } = await this.db.getAsync() if (!db.indexingSupported) return ExternalImageRule.None const encryptedAddress = await encryptItem(address, metaData.key, metaData.iv) @@ -83,6 +86,8 @@ export class ConfigurationDatabase { } async loadConfigDb(user: User, keyLoaderFacade: KeyLoaderFacade): Promise { + console.log("ConfigurationDatabase - loadConfigDb") + const timeBefore = Date.now() const id = this.getDbId(user._id) const db = new DbFacade(VERSION, async (event, db, dbFacade) => { if (event.oldVersion === 0) { @@ -107,6 +112,8 @@ export class ConfigurationDatabase { } }) const metaData = (await loadEncryptionMetadata(db, id, keyLoaderFacade)) || (await initializeDb(db, id, keyLoaderFacade)) + const timeAfter = Date.now() + console.log("ConfigurationDatabase - loadConfigDb - done after: " + (timeAfter - timeBefore) + "ms") return { db, metaData, @@ -119,9 +126,18 @@ export class ConfigurationDatabase { if (!(event.operation === OperationType.UPDATE && isSameTypeRefByAttr(UserTypeRef, event.application, event.type))) { continue } - const configDb = await this.db.getAsync() - if (configDb.db.isSameDbId(this.getDbId(event.instanceId))) { - return updateEncryptionMetadata(configDb.db, this.keyLoaderFacade, ConfigurationMetaDataOS) + console.log("ConfigurationDatabase - onEntityEventsReceived - db.getAsync") + try { + const configDb = await this.db.getAsync() + if (configDb.db.isSameDbId(this.getDbId(event.instanceId))) { + await updateEncryptionMetadata(configDb.db, this.keyLoaderFacade, ConfigurationMetaDataOS) + } + } catch (e) { + if (!this.userFacade.isFullyLoggedIn()) { + console.log("user is not logged in anymore, ignore error") + } else { + throw e + } } } } @@ -129,6 +145,7 @@ export class ConfigurationDatabase { async delete(userId: Id): Promise { const dbId = this.getDbId(userId) if (this.db.isLoadedOrLoading()) { + console.log("ConfigurationDatabase - delete - db.getAsync") const { db } = await this.db.getAsync() await db.deleteDatabase(dbId) } else { @@ -146,6 +163,7 @@ async function loadDataWithGivenVersion( userGroupKeyVersion: number, transaction: DbTransaction, ): Promise { + console.log("ConfigurationDatabase: loadDataWithGivenVersion") const userGroupKey = await keyLoaderFacade.loadSymUserGroupKey(userGroupKeyVersion) const encDbKey = await transaction.get(ConfigurationMetaDataOS, Metadata.userEncDbKey) @@ -172,6 +190,7 @@ export async function loadEncryptionMetadata(db: DbFacade, id: string, keyLoader await db.open(id) const transaction = await db.createTransaction(true, [ConfigurationMetaDataOS]) const userGroupKeyVersion = await getMetaDataGroupKeyVersion(transaction, ConfigurationMetaDataOS) + return await loadDataWithGivenVersion(keyLoaderFacade, userGroupKeyVersion, transaction) }