From 06a82eead994681793b941adaf5d4dad057ad622 Mon Sep 17 00:00:00 2001 From: Alistair Brown Date: Tue, 24 May 2022 12:23:11 +0100 Subject: [PATCH] Add support for email protection logout (#98) * Add support for email protection logout * PR Feedback * Bump autofill to v4.5.0 --- Package.resolved | 9 +++++ Package.swift | 2 +- .../Autofill/AutofillUserScript+Email.swift | 21 ++++++++++ .../Autofill/AutofillUserScript.swift | 40 ++++++++++--------- .../Email/EmailManager.swift | 4 ++ .../AutofillEmailUserScriptTests.swift | 5 +++ 6 files changed, 62 insertions(+), 19 deletions(-) diff --git a/Package.resolved b/Package.resolved index a50004666..b5bf73e7e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,6 +1,15 @@ { "object": { "pins": [ + { + "package": "Autofill", + "repositoryURL": "https://github.com/duckduckgo/duckduckgo-autofill.git", + "state": { + "branch": null, + "revision": "a4bff09eb0f3b14005c3960c590ec4efb9c5a23c", + "version": "4.5.0" + } + }, { "package": "GRDB", "repositoryURL": "https://github.com/duckduckgo/GRDB.swift.git", diff --git a/Package.swift b/Package.swift index 13445297b..e36b81086 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( .library(name: "BrowserServicesKit", targets: ["BrowserServicesKit"]) ], dependencies: [ - .package(name: "Autofill", url: "https://github.com/duckduckgo/duckduckgo-autofill.git", .exact("4.4.0")), + .package(name: "Autofill", url: "https://github.com/duckduckgo/duckduckgo-autofill.git", .exact("4.5.0")), .package(name: "GRDB", url: "https://github.com/duckduckgo/GRDB.swift.git", .exact("1.1.0")), .package(url: "https://github.com/duckduckgo/TrackerRadarKit", .exact("1.0.3")), .package(name: "Punycode", url: "https://github.com/gumob/PunycodeSwift.git", .exact("2.1.0")) diff --git a/Sources/BrowserServicesKit/Autofill/AutofillUserScript+Email.swift b/Sources/BrowserServicesKit/Autofill/AutofillUserScript+Email.swift index 4f4ed72df..fef4b937c 100644 --- a/Sources/BrowserServicesKit/Autofill/AutofillUserScript+Email.swift +++ b/Sources/BrowserServicesKit/Autofill/AutofillUserScript+Email.swift @@ -29,6 +29,7 @@ public protocol AutofillEmailDelegate: AnyObject { func autofillUserScript(_: AutofillUserScript, didRequestStoreToken token: String, username: String, cohort: String?) func autofillUserScriptDidRequestUsernameAndAlias(_ : AutofillUserScript, completionHandler: @escaping UsernameAndAliasCompletion) func autofillUserScriptDidRequestUserData(_ : AutofillUserScript, completionHandler: @escaping UserDataCompletion) + func autofillUserScriptDidRequestSignOut(_ : AutofillUserScript) func autofillUserScriptDidRequestSignedInStatus(_: AutofillUserScript) -> Bool } @@ -52,6 +53,11 @@ extension AutofillUserScript { replyHandler(nil) } + func emailRemoveToken(_ message: AutofillMessage, _ replyHandler: MessageReplyHandler) { + emailDelegate?.autofillUserScriptDidRequestSignOut(self) + replyHandler(nil) + } + func emailGetAlias(_ message: AutofillMessage, _ replyHandler: @escaping MessageReplyHandler) { guard let dict = message.messageBody as? [String: Any], let requiresUserPermission = dict["requiresUserPermission"] as? Bool, @@ -120,4 +126,19 @@ extension AutofillUserScript { } } + private struct DeviceEmailCapabilities: Encodable { + public let addUserData: Bool + public let getUserData: Bool + public let removeUserData: Bool + } + + func emailGetDeviceCapabilities(_ message: AutofillMessage, _ replyHandler: @escaping MessageReplyHandler) { + let capabilities = DeviceEmailCapabilities(addUserData: true, getUserData: true, removeUserData: true) + if let json = try? JSONEncoder().encode(capabilities), let jsonString = String(data: json, encoding: .utf8) { + replyHandler(jsonString) + } else { + replyHandler(nil) + } + } + } diff --git a/Sources/BrowserServicesKit/Autofill/AutofillUserScript.swift b/Sources/BrowserServicesKit/Autofill/AutofillUserScript.swift index 0a209c018..ead285af9 100644 --- a/Sources/BrowserServicesKit/Autofill/AutofillUserScript.swift +++ b/Sources/BrowserServicesKit/Autofill/AutofillUserScript.swift @@ -27,8 +27,10 @@ public class AutofillUserScript: NSObject, UserScript { internal enum MessageName: String, CaseIterable { case emailHandlerStoreToken + case emailHandlerRemoveToken case emailHandlerGetAlias case emailHandlerGetUserData + case emailHandlerGetCapabilities case emailHandlerRefreshAlias case emailHandlerGetAddresses @@ -88,24 +90,26 @@ public class AutofillUserScript: NSObject, UserScript { os_log("AutofillUserScript: received '%{public}s'", log: .userScripts, type: .debug, messageName) switch message { - case .emailHandlerStoreToken: return emailStoreToken - case .emailHandlerGetAlias: return emailGetAlias - case .emailHandlerGetUserData: return emailGetUserData - case .emailHandlerRefreshAlias: return emailRefreshAlias - case .emailHandlerGetAddresses: return emailGetAddresses - case .emailHandlerCheckAppSignedInStatus: return emailCheckSignedInStatus - - case .pmHandlerGetAutofillInitData: return pmGetAutoFillInitData - - case .pmHandlerStoreData: return pmStoreData - case .pmHandlerGetAccounts: return pmGetAccounts - case .pmHandlerGetAutofillCredentials: return pmGetAutofillCredentials - case .pmHandlerGetIdentity: return pmGetIdentity - case .pmHandlerGetCreditCard: return pmGetCreditCard - - case .pmHandlerOpenManageCreditCards: return pmOpenManageCreditCards - case .pmHandlerOpenManageIdentities: return pmOpenManageIdentities - case .pmHandlerOpenManagePasswords: return pmOpenManagePasswords + case .emailHandlerStoreToken: return emailStoreToken + case .emailHandlerRemoveToken: return emailRemoveToken + case .emailHandlerGetAlias: return emailGetAlias + case .emailHandlerGetUserData: return emailGetUserData + case .emailHandlerGetCapabilities: return emailGetDeviceCapabilities + case .emailHandlerRefreshAlias: return emailRefreshAlias + case .emailHandlerGetAddresses: return emailGetAddresses + case .emailHandlerCheckAppSignedInStatus: return emailCheckSignedInStatus + + case .pmHandlerGetAutofillInitData: return pmGetAutoFillInitData + + case .pmHandlerStoreData: return pmStoreData + case .pmHandlerGetAccounts: return pmGetAccounts + case .pmHandlerGetAutofillCredentials: return pmGetAutofillCredentials + case .pmHandlerGetIdentity: return pmGetIdentity + case .pmHandlerGetCreditCard: return pmGetCreditCard + + case .pmHandlerOpenManageCreditCards: return pmOpenManageCreditCards + case .pmHandlerOpenManageIdentities: return pmOpenManageIdentities + case .pmHandlerOpenManagePasswords: return pmOpenManagePasswords } } diff --git a/Sources/BrowserServicesKit/Email/EmailManager.swift b/Sources/BrowserServicesKit/Email/EmailManager.swift index 617b0dd47..d47fd19f1 100644 --- a/Sources/BrowserServicesKit/Email/EmailManager.swift +++ b/Sources/BrowserServicesKit/Email/EmailManager.swift @@ -252,6 +252,10 @@ extension EmailManager: AutofillEmailDelegate { completionHandler(self.username, alias, self.token, nil) } } + + public func autofillUserScriptDidRequestSignOut(_: AutofillUserScript) { + self.signOut() + } public func autofillUserScript(_: AutofillUserScript, didRequestAliasAndRequiresUserPermission requiresUserPermission: Bool, diff --git a/Tests/BrowserServicesKitTests/Autofill/AutofillEmailUserScriptTests.swift b/Tests/BrowserServicesKitTests/Autofill/AutofillEmailUserScriptTests.swift index 7c2be92f9..60f3d2be7 100644 --- a/Tests/BrowserServicesKitTests/Autofill/AutofillEmailUserScriptTests.swift +++ b/Tests/BrowserServicesKitTests/Autofill/AutofillEmailUserScriptTests.swift @@ -268,6 +268,7 @@ class MockAutofillMessage: AutofillMessage { class MockAutofillEmailDelegate: AutofillEmailDelegate { var signedInCallback: (() -> Void)? + var signedOutCallback: (() -> Void)? var requestAliasCallback: (() -> Void)? var requestStoreTokenCallback: ((String, String, String?) -> Void)? var refreshAliasCallback: (() -> Void)? @@ -304,6 +305,10 @@ class MockAutofillEmailDelegate: AutofillEmailDelegate { requestUserDataCallback?() completionHandler("username", "alias", "token", nil) } + + func autofillUserScriptDidRequestSignOut(_: AutofillUserScript) { + signedOutCallback?() + } } class MockWebView: WKWebView {