Skip to content

Commit

Permalink
DBP: Integrate subscription account authentication to DBP (#1995)
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaemepereira authored Dec 21, 2023
1 parent 6389927 commit 187b319
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 3 deletions.
6 changes: 6 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,8 @@
B6FA893D269C423100588ECD /* PrivacyDashboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B6FA893C269C423100588ECD /* PrivacyDashboard.storyboard */; };
B6FA893F269C424500588ECD /* PrivacyDashboardViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6FA893E269C424500588ECD /* PrivacyDashboardViewController.swift */; };
B6FA8941269C425400588ECD /* PrivacyDashboardPopover.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6FA8940269C425400588ECD /* PrivacyDashboardPopover.swift */; };
BB5789722B2CA70F0009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5789712B2CA70F0009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift */; };
BB5789732B2CC0300009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5789712B2CA70F0009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift */; };
BBDFDC5A2B2B8A0900F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBDFDC592B2B8A0900F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift */; };
BBDFDC5C2B2B8D7000F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBDFDC592B2B8A0900F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift */; };
BBDFDC5D2B2B8E2100F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBDFDC592B2B8A0900F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift */; };
Expand Down Expand Up @@ -4222,6 +4224,7 @@
B6FA893C269C423100588ECD /* PrivacyDashboard.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = PrivacyDashboard.storyboard; sourceTree = "<group>"; };
B6FA893E269C424500588ECD /* PrivacyDashboardViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyDashboardViewController.swift; sourceTree = "<group>"; };
B6FA8940269C425400588ECD /* PrivacyDashboardPopover.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyDashboardPopover.swift; sourceTree = "<group>"; };
BB5789712B2CA70F0009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataBrokerProtectionSubscriptionEventHandler.swift; sourceTree = "<group>"; };
BBDFDC592B2B8A0900F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataBrokerProtectionExternalWaitlistPixels.swift; sourceTree = "<group>"; };
CB24F70B29A3D9CB006DCC58 /* AppConfigurationURLProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfigurationURLProvider.swift; sourceTree = "<group>"; };
CB6BCDF827C6BEFF00CC76DC /* PrivacyFeatures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyFeatures.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4673,6 +4676,7 @@
3199C6F82AF94F5B002A7BA1 /* DataBrokerProtectionFeatureDisabler.swift */,
3199C6FC2AF97367002A7BA1 /* DataBrokerProtectionAppEvents.swift */,
BBDFDC592B2B8A0900F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift */,
BB5789712B2CA70F0009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift */,
);
path = DBP;
sourceTree = "<group>";
Expand Down Expand Up @@ -10907,6 +10911,7 @@
4B957B8F2AC7AE700062CA31 /* Assertions.swift in Sources */,
4B957B902AC7AE700062CA31 /* BookmarkViewModel.swift in Sources */,
4B957B912AC7AE700062CA31 /* DaxSpeech.swift in Sources */,
BB5789732B2CC0300009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift in Sources */,
4B957B922AC7AE700062CA31 /* DuckPlayerSchemeHandler.swift in Sources */,
4B957B932AC7AE700062CA31 /* FirePopoverViewModel.swift in Sources */,
4B957B942AC7AE700062CA31 /* BWCommand.swift in Sources */,
Expand Down Expand Up @@ -11473,6 +11478,7 @@
4B9DB04A2A983B24000927DB /* NotificationService.swift in Sources */,
3775912D29AAC72700E26367 /* SyncPreferences.swift in Sources */,
1DB9618329F67F6200CF5568 /* FaviconNullStore.swift in Sources */,
BB5789722B2CA70F0009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift in Sources */,
B693954F26F04BEB0015B914 /* PaddedImageButton.swift in Sources */,
4BA1A6B8258B081600F6F690 /* EncryptionKeyStoring.swift in Sources */,
B65783E725F8AAFB00D8DB33 /* String+Punycode.swift in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions DuckDuckGo/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel
private var emailCancellables = Set<AnyCancellable>()
let bookmarksManager = LocalBookmarkManager.shared

#if DBP && SUBSCRIPTION
private let dataBrokerProtectionSubscriptionEventHandler = DataBrokerProtectionSubscriptionEventHandler()
#endif

private var didFinishLaunching = false

#if SPARKLE
Expand Down Expand Up @@ -243,6 +247,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel
UNUserNotificationCenter.current().delegate = self
#endif

#if DBP && SUBSCRIPTION
dataBrokerProtectionSubscriptionEventHandler.registerForSubscriptionAccountManagerEvents()
#endif

#if DBP
DataBrokerProtectionAppEvents().applicationDidFinishLaunching()
#endif
Expand Down
55 changes: 55 additions & 0 deletions DuckDuckGo/DBP/DataBrokerProtectionSubscriptionEventHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// DataBrokerProtectionSubscriptionEventHandler.swift
//
// Copyright © 2023 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.
//
#if DBP && SUBSCRIPTION

import Foundation
import Account
import DataBrokerProtection

final class DataBrokerProtectionSubscriptionEventHandler {

private let accountManager: Account.AccountManaging
private let authRepository: AuthenticationRepository

init(accountManager: Account.AccountManaging = Account.AccountManager(),
authRepository: AuthenticationRepository = KeychainAuthenticationData()) {
self.accountManager = accountManager
self.authRepository = authRepository
}

func registerForSubscriptionAccountManagerEvents() {
NotificationCenter.default.addObserver(self, selector: #selector(handleAccountDidSignIn), name: .accountDidSignIn, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleAccountDidSignOut), name: .accountDidSignOut, object: nil)
}

@objc private func handleAccountDidSignIn() {
guard let token = accountManager.token else {
Pixel.fire(.dataBrokerProtectionErrorWhenFetchingSubscriptionAuthTokenAfterSignIn)
assertionFailure("[DBP Subscription] AccountManager signed in but token could not be retrieved")
return
}

authRepository.save(accessToken: token)
}

@objc private func handleAccountDidSignOut() {
// Going to be defined here: https://app.asana.com/0/1204006570077678/1206206961074916/f
}
}

#endif
6 changes: 5 additions & 1 deletion DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ final class MoreOptionsMenu: NSMenu {

#if DBP
@objc func openDataBrokerProtection(_ sender: NSMenuItem) {
if !DefaultDataBrokerProtectionFeatureVisibility.bypassWaitlist && DataBrokerProtectionWaitlistViewControllerPresenter.shouldPresentWaitlist() {
#if SUBSCRIPTION
actionDelegate?.optionsButtonMenuRequestedDataBrokerProtection(self)
#else
if !DefaultDataBrokerProtectionFeatureVisibility.bypassWaitlist && DataBrokerProtectionWaitlistViewControllerPresenter.shouldPresentWaitlist() {
DataBrokerProtectionWaitlistViewControllerPresenter.show()
} else {
actionDelegate?.optionsButtonMenuRequestedDataBrokerProtection(self)
}
#endif
}
#endif // DBP

Expand Down
5 changes: 5 additions & 0 deletions DuckDuckGo/Statistics/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ extension Pixel {
case dataBrokerProtectionWaitlistTermsAndConditionsDisplayed
case dataBrokerProtectionWaitlistTermsAndConditionsAccepted

// DataBrokerProtection Other
case dataBrokerProtectionErrorWhenFetchingSubscriptionAuthTokenAfterSignIn

// 28-day Home Button
case homeButtonHidden
case homeButtonLeft
Expand Down Expand Up @@ -538,6 +541,8 @@ extension Pixel.Event {
return "m_mac_dbp_imp_terms"
case .dataBrokerProtectionWaitlistTermsAndConditionsAccepted:
return "m_mac_dbp_ev_terms_accepted"
case .dataBrokerProtectionErrorWhenFetchingSubscriptionAuthTokenAfterSignIn:
return "m_mac_dbp_error_when_fetching_subscription_auth_token_after_sign_in"

// 28-day Home Button
case .homeButtonHidden:
Expand Down
1 change: 1 addition & 0 deletions DuckDuckGo/Statistics/PixelParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ extension Pixel.Event {
.dataBrokerProtectionWaitlistCardUITapped,
.dataBrokerProtectionWaitlistTermsAndConditionsDisplayed,
.dataBrokerProtectionWaitlistTermsAndConditionsAccepted,
.dataBrokerProtectionErrorWhenFetchingSubscriptionAuthTokenAfterSignIn,
.homeButtonLeft,
.homeButtonRight,
.homeButtonHidden:
Expand Down
8 changes: 7 additions & 1 deletion LocalPackages/Account/Sources/Account/AccountManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public protocol AccountManagerKeychainAccessDelegate: AnyObject {
func accountManagerKeychainAccessFailed(accessType: AccountKeychainAccessType, error: AccountKeychainAccessError)
}

public class AccountManager {
public protocol AccountManaging {

var token: String? { get }

}

public class AccountManager: AccountManaging {

private let storage: AccountStorage
public weak var delegate: AccountManagerKeychainAccessDelegate?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ let nonSandboxedExtraInputFiles: Set<InputFile> = [
.init("VPNMetadataCollector.swift", .source),
.init("VPNFeedbackCategory.swift", .source),
.init("VPNFeedbackSender.swift", .source),
.init("DuckDuckGoDBPBackgroundAgent.app", .unknown)
.init("DuckDuckGoDBPBackgroundAgent.app", .unknown),
.init("DataBrokerProtectionSubscriptionEventHandler.swift", .source)
]

/**
Expand Down

0 comments on commit 187b319

Please sign in to comment.