From d24214c674c2341ee34a1dd227cc76d2188f6c46 Mon Sep 17 00:00:00 2001 From: Doug <6060466+pixlwave@users.noreply.github.com> Date: Thu, 12 Sep 2024 10:24:05 +0100 Subject: [PATCH] Make sure we don't reuse an old NSEUserSession after logging out and back in. (#3273) --- NSE/Sources/NotificationServiceExtension.swift | 4 +++- NSE/Sources/Other/NSEUserSession.swift | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/NSE/Sources/NotificationServiceExtension.swift b/NSE/Sources/NotificationServiceExtension.swift index ce8f537aa..ae119e96d 100644 --- a/NSE/Sources/NotificationServiceExtension.swift +++ b/NSE/Sources/NotificationServiceExtension.swift @@ -72,7 +72,9 @@ class NotificationServiceExtension: UNNotificationServiceExtension { MXLog.info("\(tag) Payload came: \(request.content.userInfo)") Self.serialQueue.sync { - if Self.userSession == nil { + // If the session directories have changed, the user has logged out and back in (even if they entered the same user ID). + // We can't do this comparison with the access token of the existing session here due to token refresh when using OIDC. + if Self.userSession == nil || Self.userSession?.sessionDirectories != credentials.restorationToken.sessionDirectories { // This function might be run concurrently and from different processes // It's imperative that we create **at most** one UserSession/Client per process Task.synchronous { [appHooks] in diff --git a/NSE/Sources/Other/NSEUserSession.swift b/NSE/Sources/Other/NSEUserSession.swift index 64627b0bb..39a74d2d3 100644 --- a/NSE/Sources/Other/NSEUserSession.swift +++ b/NSE/Sources/Other/NSEUserSession.swift @@ -9,6 +9,8 @@ import Foundation import MatrixRustSDK final class NSEUserSession { + let sessionDirectories: SessionDirectories + private let baseClient: Client private let notificationClient: NotificationClient private let userID: String @@ -18,6 +20,8 @@ final class NSEUserSession { private let delegateHandle: TaskHandle? init(credentials: KeychainCredentials, clientSessionDelegate: ClientSessionDelegate, appHooks: AppHooks) async throws { + sessionDirectories = credentials.restorationToken.sessionDirectories + userID = credentials.userID if credentials.restorationToken.passphrase != nil { MXLog.info("Restoring client with encrypted store.")