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

await resetSession and add log statements. #6942

Closed
Closed
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
1 change: 1 addition & 0 deletions src/api/worker/facades/KeyLoaderFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class KeyLoaderFacade {

async loadSymUserGroupKey(userGroupKeyVersion: number): Promise<AesKey> {
// 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())
}

Expand Down
4 changes: 2 additions & 2 deletions src/api/worker/facades/LoginFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class LoginFacade {
}

async resetSession(): Promise<void> {
this.eventBusClient.close(CloseEventBusOption.Terminate)
await this.eventBusClient.close(CloseEventBusOption.Terminate)
await this.deInitCache()
this.userFacade.reset()
}
Expand Down Expand Up @@ -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
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/api/worker/facades/UserFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -43,13 +44,15 @@ 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")
}
this.user = user
}

unlockUserGroupKey(userPassphraseKey: AesKey) {
console.log("UserFacade: unlockUserGroupKey")
if (this.user == null) {
throw new ProgrammingError("Invalid state: no user")
}
Expand All @@ -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.")
}
Expand Down Expand Up @@ -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")
}
}
Expand Down Expand Up @@ -158,14 +163,15 @@ 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 {
return this.leaderStatus.leaderStatus
}

reset() {
console.log("UserFacade: reset user")
this.user = null
this.accessToken = null
this.currentUserGroupKey = null
Expand Down
27 changes: 23 additions & 4 deletions src/api/worker/facades/lazy/ConfigurationDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,28 @@ export class ConfigurationDatabase {
readonly db: LazyLoaded<ConfigDb>

constructor(
userFacade: UserFacade,
readonly userFacade: UserFacade,
dbLoadFn: (arg0: User, arg1: KeyLoaderFacade) => Promise<ConfigDb> = (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<void> {
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)
return addAddressToImageList(db, encryptedAddress, rule)
}

async getExternalImageRule(address: string): Promise<ExternalImageRule> {
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)
Expand All @@ -83,6 +86,8 @@ export class ConfigurationDatabase {
}

async loadConfigDb(user: User, keyLoaderFacade: KeyLoaderFacade): Promise<ConfigDb> {
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) {
Expand All @@ -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,
Expand All @@ -119,16 +126,26 @@ 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
}
}
}
}

async delete(userId: Id): Promise<void> {
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 {
Expand All @@ -146,6 +163,7 @@ async function loadDataWithGivenVersion(
userGroupKeyVersion: number,
transaction: DbTransaction,
): Promise<EncryptionMetadata | null> {
console.log("ConfigurationDatabase: loadDataWithGivenVersion")
const userGroupKey = await keyLoaderFacade.loadSymUserGroupKey(userGroupKeyVersion)

const encDbKey = await transaction.get(ConfigurationMetaDataOS, Metadata.userEncDbKey)
Expand All @@ -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)
}

Expand Down