-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase ratio of complete form saves (#1124)
Please review the release process for BrowserServicesKit [here](https://app.asana.com/0/1200194497630846/1200837094583426). **Required**: Task/Issue URL: https://app.asana.com/0/0/1206048666874235/f iOS PR: duckduckgo/iOS#3698 macOS PR: duckduckgo/macos-browser#3649 What kind of version bump will this require?: Major **Description**: Increase ratio of complete credentials saves (i.e. that include the username) by checking for recently filled emails/usernames when a form save event is incomplete. Approach is to have a new form save events for username/email-only forms that stays in memory for 3 minutes. If a password-only form save is detected for the same domain, we can complete the form with the previous partial data. This would also cover mobile where we don't have identities autofill – and in general in all cases where the user inputs data manually. **Steps to test this PR**: https://app.asana.com/0/1202926619870900/1208866651723703/f **OS Testing**: * [ ] iOS 14 * [ ] iOS 15 * [ ] iOS 16 * [ ] macOS 10.15 * [ ] macOS 11 * [ ] macOS 12 --- ###### Internal references: [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943)
- Loading branch information
Showing
9 changed files
with
224 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Sources/BrowserServicesKit/SecureVault/PartialFormSaveManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// PartialFormSaveManager.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import Common | ||
|
||
final class PartialFormSaveManager { | ||
typealias WebsiteAccount = SecureVaultModels.WebsiteAccount | ||
|
||
private static var accounts: [String: WebsiteAccount] = .init() | ||
|
||
private let tld: TLD | ||
|
||
init(tld: TLD) { | ||
self.tld = tld | ||
} | ||
|
||
func partialAccount(forDomain domain: String) -> WebsiteAccount? { | ||
guard let tldPlus1 = tld.eTLDplus1(domain) else { | ||
return nil | ||
} | ||
guard let account = Self.accounts[tldPlus1] else { | ||
return nil | ||
} | ||
|
||
guard account.lastUpdated.isLessThan(minutesAgo: 3) else { | ||
Self.accounts.removeValue(forKey: domain) | ||
return nil | ||
} | ||
|
||
return account | ||
} | ||
|
||
func store(partialAccount: WebsiteAccount, for domain: String) { | ||
guard let tldPlus1 = tld.eTLDplus1(domain) else { | ||
return | ||
} | ||
Self.accounts[tldPlus1] = partialAccount | ||
} | ||
|
||
func removePartialAccount(for domain: String) { | ||
guard let tldPlus1 = tld.eTLDplus1(domain) else { | ||
return | ||
} | ||
Self.accounts.removeValue(forKey: tldPlus1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ class SecureVaultManagerTests: XCTestCase { | |
|
||
self.testVault = DefaultAutofillSecureVault(providers: providers) | ||
self.secureVaultManagerDelegate = MockSecureVaultManagerDelegate() | ||
self.manager = SecureVaultManager(vault: self.testVault) | ||
self.manager = SecureVaultManager(vault: self.testVault, shouldAllowPartialFormSaves: true) | ||
self.manager.delegate = secureVaultManagerDelegate | ||
} | ||
|
||
|
@@ -854,6 +854,94 @@ class SecureVaultManagerTests: XCTestCase { | |
|
||
} | ||
|
||
func testWhenFormSubmittedWithNilUsername_afterPartialSaveWithUsername_storesAndPromptsWithFullCredentials() { | ||
let partialCredentials = AutofillUserScript.IncomingCredentials(username: "[email protected]", password: nil, autogenerated: false) | ||
let partialData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: partialCredentials, creditCard: nil, trigger: .partialSave) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "fill.dev", data: partialData) | ||
|
||
var incomingCredentials = AutofillUserScript.IncomingCredentials(username: nil, password: "m4nu4lP4sswOrd", autogenerated: false) | ||
var incomingData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: incomingCredentials, creditCard: nil, trigger: .formSubmission) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "fill.dev", data: incomingData) | ||
|
||
// Check stored | ||
incomingCredentials = AutofillUserScript.IncomingCredentials(username: "[email protected]", password: "m4nu4lP4sswOrd", autogenerated: false) | ||
incomingData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: incomingCredentials, creditCard: nil, trigger: .formSubmission) | ||
let entries = try? manager.existingEntries(for: "fill.dev", autofillData: incomingData) | ||
XCTAssertEqual(entries?.credentials?.account.username, "[email protected]") | ||
XCTAssertEqual(entries?.credentials?.password, Data("m4nu4lP4sswOrd".utf8)) | ||
|
||
// Check prompted | ||
XCTAssertNotNil(secureVaultManagerDelegate.promptedAutofillData) | ||
XCTAssertEqual(secureVaultManagerDelegate.promptedAutofillData?.credentials?.account.username, "[email protected]") | ||
XCTAssertEqual(secureVaultManagerDelegate.promptedAutofillData?.credentials?.password, Data("m4nu4lP4sswOrd".utf8)) | ||
} | ||
|
||
func testWhenFormSubmittedWithNilUsername_afterPartialSaveWithUsername_domainsDifferent_onlyStoresTheFormSubmission() { | ||
let partialCredentials = AutofillUserScript.IncomingCredentials(username: "[email protected]", password: nil, autogenerated: false) | ||
let partialData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: partialCredentials, creditCard: nil, trigger: .partialSave) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "dill.fev", data: partialData) | ||
|
||
var incomingCredentials = AutofillUserScript.IncomingCredentials(username: nil, password: "m4nu4lP4sswOrd", autogenerated: false) | ||
var incomingData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: incomingCredentials, creditCard: nil, trigger: .formSubmission) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "fill.dev", data: incomingData) | ||
|
||
// Check stored | ||
incomingCredentials = AutofillUserScript.IncomingCredentials(username: nil, password: "m4nu4lP4sswOrd", autogenerated: false) | ||
incomingData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: incomingCredentials, creditCard: nil, trigger: .formSubmission) | ||
let entries = try? manager.existingEntries(for: "fill.dev", autofillData: incomingData) | ||
XCTAssertNotEqual(entries?.credentials?.account.username, "[email protected]") | ||
XCTAssertEqual(entries?.credentials?.password, Data("m4nu4lP4sswOrd".utf8)) | ||
|
||
// Check prompted | ||
XCTAssertNotNil(secureVaultManagerDelegate.promptedAutofillData) | ||
XCTAssertNotEqual(secureVaultManagerDelegate.promptedAutofillData?.credentials?.account.username, "[email protected]") | ||
XCTAssertEqual(secureVaultManagerDelegate.promptedAutofillData?.credentials?.password, Data("m4nu4lP4sswOrd".utf8)) | ||
} | ||
|
||
func testWhenFormSubmittedWithNilUsername_afterPartialSaveWithUsername_updatesExistingUsernameOnlyStoredData() { | ||
// Check initial stored | ||
let initialCredentials = SecureVaultModels.WebsiteCredentials(account: .init(username: "[email protected]", domain: "fill.dev"), password: nil) | ||
guard let theID = try? testVault.storeWebsiteCredentials(initialCredentials) else { | ||
XCTFail("Couldn't store initial credentials") | ||
return | ||
} | ||
|
||
let partialCredentials = AutofillUserScript.IncomingCredentials(username: "[email protected]", password: nil, autogenerated: false) | ||
let partialData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: partialCredentials, creditCard: nil, trigger: .partialSave) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "fill.dev", data: partialData) | ||
|
||
let incomingCredentials = AutofillUserScript.IncomingCredentials(username: nil, password: "m4nu4lP4sswOrd", autogenerated: false) | ||
let incomingData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: incomingCredentials, creditCard: nil, trigger: .formSubmission) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "fill.dev", data: incomingData) | ||
|
||
// Check prompted | ||
XCTAssertNotNil(secureVaultManagerDelegate.promptedAutofillData) | ||
// Prompting with an account with ID will result in an update | ||
XCTAssertEqual(secureVaultManagerDelegate.promptedAutofillData?.credentials?.account.id, String(theID)) | ||
} | ||
|
||
func testWhenFormSubmittedWithNilUsername_afterPartialSaveWithUsername_updatesExistingPasswordOnlyStoredData() { | ||
// Check initial stored | ||
let initialCredentials = SecureVaultModels.WebsiteCredentials(account: .init(username: nil, domain: "fill.dev"), password: "m4nu4lP4sswOrd".data(using: .utf8)) | ||
guard let theID = try? testVault.storeWebsiteCredentials(initialCredentials) else { | ||
XCTFail("Couldn't store initial credentials") | ||
return | ||
} | ||
|
||
let partialCredentials = AutofillUserScript.IncomingCredentials(username: "[email protected]", password: nil, autogenerated: false) | ||
let partialData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: partialCredentials, creditCard: nil, trigger: .partialSave) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "fill.dev", data: partialData) | ||
|
||
let incomingCredentials = AutofillUserScript.IncomingCredentials(username: nil, password: "m4nu4lP4sswOrd", autogenerated: false) | ||
let incomingData = AutofillUserScript.DetectedAutofillData(identity: nil, credentials: incomingCredentials, creditCard: nil, trigger: .formSubmission) | ||
manager.autofillUserScript(mockAutofillUserScript, didRequestStoreDataForDomain: "fill.dev", data: incomingData) | ||
|
||
// Check prompted | ||
XCTAssertNotNil(secureVaultManagerDelegate.promptedAutofillData) | ||
// Prompting with an account with ID will result in an update | ||
XCTAssertEqual(secureVaultManagerDelegate.promptedAutofillData?.credentials?.account.id, String(theID)) | ||
} | ||
|
||
// MARK: - Test Utilities | ||
|
||
private func identity(id: Int64? = nil, name: (String, String, String), addressStreet: String?) -> SecureVaultModels.Identity { | ||
|