Skip to content

Commit

Permalink
Add support for email protection logout (#98)
Browse files Browse the repository at this point in the history
* Add support for email protection logout

* PR Feedback

* Bump autofill to v4.5.0
  • Loading branch information
alistairjcbrown authored May 24, 2022
1 parent 7365786 commit 06a82ee
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
21 changes: 21 additions & 0 deletions Sources/BrowserServicesKit/Autofill/AutofillUserScript+Email.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
}

}
40 changes: 22 additions & 18 deletions Sources/BrowserServicesKit/Autofill/AutofillUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
4 changes: 4 additions & 0 deletions Sources/BrowserServicesKit/Email/EmailManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down Expand Up @@ -304,6 +305,10 @@ class MockAutofillEmailDelegate: AutofillEmailDelegate {
requestUserDataCallback?()
completionHandler("username", "alias", "token", nil)
}

func autofillUserScriptDidRequestSignOut(_: AutofillUserScript) {
signedOutCallback?()
}
}

class MockWebView: WKWebView {
Expand Down

0 comments on commit 06a82ee

Please sign in to comment.