Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBP: Integrate subscription account authentication to DBP #1995

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2922,6 +2922,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 @@ -4209,6 +4211,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 @@ -4651,6 +4654,7 @@
3199C6F82AF94F5B002A7BA1 /* DataBrokerProtectionFeatureDisabler.swift */,
3199C6FC2AF97367002A7BA1 /* DataBrokerProtectionAppEvents.swift */,
BBDFDC592B2B8A0900F62D90 /* DataBrokerProtectionExternalWaitlistPixels.swift */,
BB5789712B2CA70F0009DFE2 /* DataBrokerProtectionSubscriptionEventHandler.swift */,
);
path = DBP;
sourceTree = "<group>";
Expand Down Expand Up @@ -10867,6 +10871,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 @@ -11433,6 +11438,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 @@ -73,6 +73,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel
private var emailCancellables = Set<AnyCancellable>()
let bookmarksManager = LocalBookmarkManager.shared

#if DBP && SUBSCRIPTION
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I've been adding some of these checks myself as a part of the remote messaging change. Once we enable these features in the App Store target, we should be good to remove the flags.

private let dataBrokerProtectionSubscriptionEventHandler = DataBrokerProtectionSubscriptionEventHandler()
#endif

private var didFinishLaunching = false

#if SPARKLE
Expand Down Expand Up @@ -242,6 +246,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 {
samsymons marked this conversation as resolved.
Show resolved Hide resolved

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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samsymons I know this is unlikely, but I wonder if I should add a safeguard somewhere else for this to happen.

For example, fetching the token again before using it (if no token is present). What are your thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case we're good since we're reacting to a sign in event. But, we can talk with Michał about it to be sure – I am of the opinion that we should only need to access the token, and the underlying subscription logic should ensure that we always get the latest.

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 @@ -539,6 +542,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
Loading