Skip to content

Commit

Permalink
Add endpoint to remove all profile data (#1748)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/0/1205662081733275/f
Tech Design URL:
CC: @jotaemepereira @Bunn @brianhall 

**Description**:
This PR adds an endpoint to the DBP UI middleware to remove all profile
data from the database

**Steps to test this PR**:
1. In the embedded debug web page use the buttons to add names and
addresses to the profile
2. Click "Set State" to save the data to the database
3. Click "Remove All Data"
4. Restart the app and click "Get Profile" in the debug web page.
Nothing should be returned.

<!--
Tagging instructions
If this PR isn't ready to be merged for whatever reason it should be
marked with the `DO NOT MERGE` label (particularly if it's a draft)
If it's pending Product Review/PFR, please add the `Pending Product
Review` label.

If at any point it isn't actively being worked on/ready for
review/otherwise moving forward (besides the above PR/PFR exception)
strongly consider closing it (or not opening it in the first place). If
you decide not to close it, make sure it's labelled to make it clear the
PRs state and comment with more information.
-->

---
###### Internal references:
[Pull Request Review
Checklist](https://app.asana.com/0/1202500774821704/1203764234894239/f)
[Software Engineering
Expectations](https://app.asana.com/0/59792373528535/199064865822552)
[Technical Design
Template](https://app.asana.com/0/59792373528535/184709971311943)
[Pull Request
Documentation](https://app.asana.com/0/1202500774821704/1204012835277482/f)
  • Loading branch information
SlayterDev authored Oct 11, 2023
1 parent 69dda22 commit 3867aa5
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ extension DataBrokerProtectionDataManager: InMemoryDataCacheDelegate {
guard let profile = profile else { return }
await saveProfile(profile)
}

public func removeAllData() {
database.deleteProfileData()
}
}

public protocol InMemoryDataCacheDelegate: AnyObject {
func flushCache(profile: DataBrokerProtectionProfile?) async
func removeAllData()
}

public final class InMemoryDataCache {
Expand Down Expand Up @@ -163,6 +168,11 @@ extension InMemoryDataCache: DBPUICommunicationDelegate {
return DBPUIUserProfile(names: names, birthYear: profile.birthYear, addresses: addresses)
}

func deleteProfileData() {
profile = emptyProfile
delegate?.removeAllData()
}

func addNameToCurrentUserProfile(_ name: DBPUIUserProfileName) -> Bool {
let profile = profile ?? emptyProfile

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Common
protocol DataBrokerProtectionRepository {
func save(_ profile: DataBrokerProtectionProfile) async
func fetchProfile() -> DataBrokerProtectionProfile?
func deleteProfileData()

func fetchChildBrokers(for parentBroker: String) -> [DataBroker]

Expand Down Expand Up @@ -80,6 +81,16 @@ final class DataBrokerProtectionDatabase: DataBrokerProtectionRepository {
}
}

public func deleteProfileData() {
do {
let vault = try DataBrokerProtectionSecureVaultFactory.makeVault(errorReporter: nil)
try vault.deleteProfileData()
} catch {
os_log("Database error: removeProfileData, error: %{public}@", log: .error, error.localizedDescription)
return
}
}

func fetchChildBrokers(for parentBroker: String) -> [DataBroker] {
do {
let vault = try DataBrokerProtectionSecureVaultFactory.makeVault(errorReporter: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protocol DataBrokerProtectionDatabaseProvider: SecureStorageDatabaseProvider {
func saveProfile(profile: DataBrokerProtectionProfile, mapperToDB: MapperToDB) throws -> Int64
func updateProfile(profile: DataBrokerProtectionProfile, mapperToDB: MapperToDB) throws -> Int64
func fetchProfile(with id: Int64) throws -> FullProfileDB?
func deleteProfileData() throws

func save(_ broker: BrokerDB) throws -> Int64
func update(_ broker: BrokerDB) throws
Expand Down Expand Up @@ -320,6 +321,33 @@ final class DefaultDataBrokerProtectionDatabaseProvider: GRDBSecureStorageDataba
}
}

func deleteProfileData() throws {
try db.write { db in
try ScanHistoryEventDB
.deleteAll(db)
try OptOutHistoryEventDB
.deleteAll(db)
try ExtractedProfileDB
.deleteAll(db)
try ScanDB
.deleteAll(db)
try OptOutDB
.deleteAll(db)
try BrokerDB
.deleteAll(db)
try ProfileQueryDB
.deleteAll(db)
try NameDB
.deleteAll(db)
try AddressDB
.deleteAll(db)
try PhoneDB
.deleteAll(db)
try ProfileDB
.deleteAll(db)
}
}

func save(_ broker: BrokerDB) throws -> Int64 {
try db.write { db in
try broker.insert(db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protocol DataBrokerProtectionSecureVault: SecureVault {
func save(profile: DataBrokerProtectionProfile) throws -> Int64
func update(profile: DataBrokerProtectionProfile) throws -> Int64
func fetchProfile(with id: Int64) throws -> DataBrokerProtectionProfile?
func deleteProfileData() throws

func save(broker: DataBroker) throws -> Int64
func update(_ broker: DataBroker, with id: Int64) throws
Expand Down Expand Up @@ -110,6 +111,10 @@ final class DefaultDataBrokerProtectionSecureVault<T: DataBrokerProtectionDataba
}
}

func deleteProfileData() throws {
try self.providers.database.deleteProfileData()
}

func save(broker: DataBroker) throws -> Int64 {
let mapper = MapperToDB(mechanism: l2Encrypt(data:))
return try self.providers.database.save(mapper.mapToDB(broker))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Common
protocol DBPUICommunicationDelegate: AnyObject {
func setState()
func getUserProfile() -> DBPUIUserProfile?
func deleteProfileData()
func addNameToCurrentUserProfile(_ name: DBPUIUserProfileName) -> Bool
func setNameAtIndexInCurrentUserProfile(_ payload: DBPUINameAtIndex) -> Bool
func removeNameAtIndexFromUserProfile(_ index: DBPUIIndex) -> Bool
Expand All @@ -39,6 +40,7 @@ enum DBPUIReceivedMethodName: String {
case handshake
case setState
case getCurrentUserProfile
case deleteUserProfileData
case addNameToCurrentUserProfile
case setNameAtIndexInCurrentUserProfile
case removeNameAtIndexFromCurrentUserProfile
Expand Down Expand Up @@ -76,6 +78,7 @@ struct DBPUICommunicationLayer: Subfeature {
case .handshake: return handshake
case .setState: return setState
case .getCurrentUserProfile: return getCurrentUserProfile
case .deleteUserProfileData: return deleteUserProfileData
case .addNameToCurrentUserProfile: return addNameToCurrentUserProfile
case .setNameAtIndexInCurrentUserProfile: return setNameAtIndexInCurrentUserProfile
case .removeNameAtIndexFromCurrentUserProfile: return removeNameAtIndexFromCurrentUserProfile
Expand Down Expand Up @@ -126,6 +129,11 @@ struct DBPUICommunicationLayer: Subfeature {
return profile
}

func deleteUserProfileData(params: Any, original: WKScriptMessage) async throws -> Encodable? {
delegate?.deleteProfileData()
return DBPUIStandardResponse(version: Constants.version, success: true)
}

func addNameToCurrentUserProfile(params: Any, original: WKScriptMessage) async throws -> Encodable? {
guard let data = try? JSONSerialization.data(withJSONObject: params),
let result = try? JSONDecoder().decode(DBPUIUserProfileName.self, from: data) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ final public class DataBrokerProtectionViewController: NSViewController {
<input type="button" value="Set State" onclick="handshake()">
<input type="button" value="Get Profile" onclick="getProfile()">
<input type="button" value="Start Scan" onclick="startScan()">
<input type="button" value="Remove All Data" onclick="removeAllData()">
</form>
<p id="output"></p>
Expand Down Expand Up @@ -102,6 +103,14 @@ final public class DataBrokerProtectionViewController: NSViewController {
})
}
function removeAllData() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "deleteUserProfileData"
})
}
function handshake() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ final class DataBrokerProtectionSecureVaultMock: DataBrokerProtectionSecureVault
profile
}

func deleteProfileData() throws {
return
}

func save(broker: DataBroker) throws -> Int64 {
wasBrokerSavedCalled = true
return 1
Expand Down Expand Up @@ -607,6 +611,7 @@ public class MockDataBrokerProtectionPixelsHandler: EventMapping<DataBrokerProte
final class MockDatabase: DataBrokerProtectionRepository {
var wasSaveProfileCalled = false
var wasFetchProfileCalled = false
var wasDeleteProfileDataCalled = false
var wasSaveOptOutOperationCalled = false
var wasBrokerProfileQueryDataCalled = false
var wasFetchAllBrokerProfileQueryDataCalled = false
Expand All @@ -632,6 +637,7 @@ final class MockDatabase: DataBrokerProtectionRepository {
lazy var callsList: [Bool] = [
wasSaveProfileCalled,
wasFetchProfileCalled,
wasDeleteProfileDataCalled,
wasSaveOptOutOperationCalled,
wasBrokerProfileQueryDataCalled,
wasFetchAllBrokerProfileQueryDataCalled,
Expand All @@ -656,6 +662,10 @@ final class MockDatabase: DataBrokerProtectionRepository {
return nil
}

func deleteProfileData() {
wasDeleteProfileDataCalled = true
}

func saveOptOutOperation(optOut: OptOutOperationData, extractedProfile: ExtractedProfile) throws {
wasSaveOptOutOperationCalled = true
}
Expand Down

0 comments on commit 3867aa5

Please sign in to comment.