From 34eb835a0b38ec069ae5b353f332c9e8c8f49d9c Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Fri, 6 Jan 2023 10:27:27 +0200 Subject: [PATCH] Storage location change (#404) * Remove LegacyRestorationToken and handling * Fixes #389 - Migrate and store session data in Application Support instead of Caches --- ElementX/Sources/Other/Extensions/URL.swift | 33 ++++++++++++++++--- .../Keychain/KeychainController.swift | 11 ------- .../UserSession/RestorationToken.swift | 28 ---------------- changelog.d/389.bugfix | 1 + 4 files changed, 30 insertions(+), 43 deletions(-) create mode 100644 changelog.d/389.bugfix diff --git a/ElementX/Sources/Other/Extensions/URL.swift b/ElementX/Sources/Other/Extensions/URL.swift index c7a5c1a03..b7ee85529 100644 --- a/ElementX/Sources/Other/Extensions/URL.swift +++ b/ElementX/Sources/Other/Extensions/URL.swift @@ -35,12 +35,17 @@ extension URL { /// The base directory where all session data is stored. static var sessionsBaseDirectory: URL { - let url = cacheBaseDirectory - .appendingPathComponent("Sessions", isDirectory: true) + let cacheSessionsURL = cacheBaseDirectory.appendingPathComponent("Sessions", isDirectory: true) + let applicationSupportSessionsURL = applicationSupportBaseDirectory.appendingPathComponent("Sessions", isDirectory: true) + + #warning("Migration from caches to application support. Remove this in a couple of releases.") + if FileManager.default.directoryExists(at: cacheSessionsURL) { + try? FileManager.default.moveItem(at: cacheSessionsURL, to: applicationSupportSessionsURL) + } - try? FileManager.default.createDirectoryIfNeeded(at: url) + try? FileManager.default.createDirectoryIfNeeded(at: applicationSupportSessionsURL) - return url + return applicationSupportSessionsURL } /// The base directory where all cache is stored. @@ -53,4 +58,24 @@ extension URL { return url } + + /// The base directory where all application support data is stored. + static var applicationSupportBaseDirectory: URL { + var url = appGroupContainerDirectory + .appendingPathComponent("Library", isDirectory: true) + .appendingPathComponent("Application Support", isDirectory: true) + .appendingPathComponent(InfoPlistReader.target.baseBundleIdentifier, isDirectory: true) + + try? FileManager.default.createDirectoryIfNeeded(at: url) + + do { + var resourceValues = URLResourceValues() + resourceValues.isExcludedFromBackup = true + try url.setResourceValues(resourceValues) + } catch { + MXLog.error("Failed excluding Application Support from backups") + } + + return url + } } diff --git a/ElementX/Sources/Services/Keychain/KeychainController.swift b/ElementX/Sources/Services/Keychain/KeychainController.swift index 56aa3c713..6daf50ef1 100644 --- a/ElementX/Sources/Services/Keychain/KeychainController.swift +++ b/ElementX/Sources/Services/Keychain/KeychainController.swift @@ -50,17 +50,6 @@ class KeychainController: KeychainControllerProtocol { return nil } - // Handle the previous restoration token format as we don't want users to have to log in again - // It will automatically be updated to the new version after login - if let legacyRestorationToken = try? JSONDecoder().decode(LegacyRestorationToken.self, from: tokenData) { - return .init(session: .init(accessToken: legacyRestorationToken.session.accessToken, - refreshToken: nil, - userId: legacyRestorationToken.session.userId, - deviceId: legacyRestorationToken.session.deviceId, - homeserverUrl: legacyRestorationToken.homeURL, - isSoftLogout: legacyRestorationToken.isSoftLogout ?? false)) - } - return try JSONDecoder().decode(RestorationToken.self, from: tokenData) } catch { MXLog.error("Failed retrieving user restore token") diff --git a/ElementX/Sources/Services/UserSession/RestorationToken.swift b/ElementX/Sources/Services/UserSession/RestorationToken.swift index 0cfbcc51e..92c2c10cd 100644 --- a/ElementX/Sources/Services/UserSession/RestorationToken.swift +++ b/ElementX/Sources/Services/UserSession/RestorationToken.swift @@ -46,31 +46,3 @@ extension MatrixRustSDK.Session: Codable { case accessToken, refreshToken, userId, deviceId, homeserverUrl, isSoftLogout } } - -#warning("Remove this in a couple of releases - sceriu 03.11.2022") -struct LegacyRestorationToken: Decodable { - let isGuest: Bool? - let isSoftLogout: Bool? - let homeURL: String - let session: Session - - enum CodingKeys: String, CodingKey { - case isGuest = "is_guest" - case isSoftLogout = "is_soft_logout" - case homeURL = "homeurl" - case session - } - - struct Session: Decodable { - let accessToken: String - let userId: String - let deviceId: String - - // swiftlint:disable:next nesting - enum CodingKeys: String, CodingKey { - case accessToken = "access_token" - case userId = "user_id" - case deviceId = "device_id" - } - } -} diff --git a/changelog.d/389.bugfix b/changelog.d/389.bugfix new file mode 100644 index 000000000..b7befbc73 --- /dev/null +++ b/changelog.d/389.bugfix @@ -0,0 +1 @@ +Migrate and store session data in Application Support instead of Caches \ No newline at end of file