From 99a6627a4874eaad97de7bcac09bef602b85f208 Mon Sep 17 00:00:00 2001 From: Roma Sosnovsky Date: Mon, 4 Oct 2021 15:36:49 +0300 Subject: [PATCH 1/2] issue #587 delete in-memory passphrases on logout --- .../DataManager/Local Storage/LocalStorage.swift | 6 +++++- .../Key Services/InMemoryPassPhraseStorage.swift | 16 ++++++++++------ .../InMemoryPassPhraseStorageTest.swift | 6 ++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/FlowCrypt/Functionality/DataManager/Local Storage/LocalStorage.swift b/FlowCrypt/Functionality/DataManager/Local Storage/LocalStorage.swift index 0a7ea359e..c4f6f543c 100644 --- a/FlowCrypt/Functionality/DataManager/Local Storage/LocalStorage.swift +++ b/FlowCrypt/Functionality/DataManager/Local Storage/LocalStorage.swift @@ -23,9 +23,12 @@ struct LocalStorage: LocalStorageType { } let storage: UserDefaults + let passPhraseStorage: PassPhraseStorageType & LogOutHandler - init(storage: UserDefaults = .standard) { + init(storage: UserDefaults = .standard, + passPhraseStorage: PassPhraseStorageType & LogOutHandler = InMemoryPassPhraseStorage()) { self.storage = storage + self.passPhraseStorage = passPhraseStorage } } @@ -40,6 +43,7 @@ extension LocalStorage { extension LocalStorage: LogOutHandler { func logOutUser(email: String) throws { + try passPhraseStorage.logOutUser(email: email) // For now we store only trash folder path in user defaults // see no reason to add logic for removing data for a concrete user cleanup() diff --git a/FlowCrypt/Functionality/Services/Key Services/InMemoryPassPhraseStorage.swift b/FlowCrypt/Functionality/Services/Key Services/InMemoryPassPhraseStorage.swift index 13e5d2c2b..e1f056474 100644 --- a/FlowCrypt/Functionality/Services/Key Services/InMemoryPassPhraseStorage.swift +++ b/FlowCrypt/Functionality/Services/Key Services/InMemoryPassPhraseStorage.swift @@ -35,7 +35,7 @@ final class InMemoryPassPhraseStorage: PassPhraseStorageType { } func remove(passPhrase: PassPhrase) { - passPhraseProvider.removePassPhrases(with: passPhrase) + passPhraseProvider.remove(passPhrases: [passPhrase]) } func getPassPhrases() -> [PassPhrase] { @@ -65,12 +65,18 @@ final class InMemoryPassPhraseStorage: PassPhraseStorageType { } } +extension InMemoryPassPhraseStorage: LogOutHandler { + func logOutUser(email: String) throws { + passPhraseProvider.remove(passPhrases: passPhraseProvider.passPhrases) + } +} + // MARK: - Convenience protocol InMemoryPassPhraseProviderType { var passPhrases: Set { get } func save(passPhrase: PassPhrase) - func removePassPhrases(with objects: PassPhrase) + func remove(passPhrases: Set) } /// - Warning: - should be shared instance @@ -86,9 +92,7 @@ final class InMemoryPassPhraseProvider: InMemoryPassPhraseProviderType { passPhrases.insert(passPhrase) } - func removePassPhrases(with objects: PassPhrase) { - if passPhrases.contains(objects) { - passPhrases.remove(objects) - } + func remove(passPhrases passPhrasesToDelete: Set) { + passPhrasesToDelete.forEach { passPhrases.remove($0) } } } diff --git a/FlowCryptAppTests/Functionality/Services/PassPhraseStorageTests/InMemoryPassPhraseStorageTest.swift b/FlowCryptAppTests/Functionality/Services/PassPhraseStorageTests/InMemoryPassPhraseStorageTest.swift index 7f034f1a7..bdbd13466 100644 --- a/FlowCryptAppTests/Functionality/Services/PassPhraseStorageTests/InMemoryPassPhraseStorageTest.swift +++ b/FlowCryptAppTests/Functionality/Services/PassPhraseStorageTests/InMemoryPassPhraseStorageTest.swift @@ -74,9 +74,7 @@ class InMemoryPassPhraseProviderMock: InMemoryPassPhraseProviderType { passPhrases.insert(passPhrase) } - func removePassPhrases(with objects: PassPhrase) { - if passPhrases.contains(objects) { - passPhrases.remove(objects) - } + func remove(passPhrases passPhrasesToDelete: Set) { + passPhrasesToDelete.forEach { passPhrases.remove($0) } } } From 9fee8aae7f740678a2a881f65d2c1947eb6a71ca Mon Sep 17 00:00:00 2001 From: Roma Sosnovsky Date: Mon, 4 Oct 2021 15:49:43 +0300 Subject: [PATCH 2/2] issue #587 update LocalStorageTests --- FlowCryptAppTests/LocalStorageTests.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/FlowCryptAppTests/LocalStorageTests.swift b/FlowCryptAppTests/LocalStorageTests.swift index 0f4586ae4..600b0e20d 100644 --- a/FlowCryptAppTests/LocalStorageTests.swift +++ b/FlowCryptAppTests/LocalStorageTests.swift @@ -14,6 +14,9 @@ class LocalStorageTests: XCTestCase { override func setUp() { sut = LocalStorage() + + let passPhrase = PassPhrase(value: "123", fingerprints: ["123"], date: nil) + sut.passPhraseStorage.save(passPhrase: passPhrase) } var trashKey: String { @@ -27,8 +30,12 @@ class LocalStorageTests: XCTestCase { } func testLogOutForUser() throws { + XCTAssertFalse(sut.passPhraseStorage.getPassPhrases().isEmpty) + let user = "anton@gmail.com" try sut.logOutUser(email: user) + XCTAssertNil(UserDefaults.standard.string(forKey: trashKey)) + XCTAssertTrue(sut.passPhraseStorage.getPassPhrases().isEmpty) } }