Skip to content

Commit

Permalink
Use subscription package for DBP access_token (#2765)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/72649045549333/1207193091966580/f

**Description**:
Set up subscription package for DBP access_token. 
This PR also gets rid of the old way of saving the token when the
sign-in notification was triggered because we don't need it anymore.
  • Loading branch information
Bunn authored May 17, 2024
1 parent 213a9a4 commit 5cf0022
Show file tree
Hide file tree
Showing 30 changed files with 622 additions and 120 deletions.
14 changes: 14 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@
31E163BA293A56F400963C10 /* BrokenSiteReportingReferenceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31E163B9293A56F400963C10 /* BrokenSiteReportingReferenceTests.swift */; };
31E163BD293A579E00963C10 /* PrivacyReferenceTestHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31E163BC293A579E00963C10 /* PrivacyReferenceTestHelper.swift */; };
31E163C0293A581900963C10 /* privacy-reference-tests in Resources */ = {isa = PBXBuildFile; fileRef = 31E163BF293A581900963C10 /* privacy-reference-tests */; };
31ECDA0E2BED317300AE679F /* BundleExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6106B9D26A565DA0013B453 /* BundleExtension.swift */; };
31ECDA0F2BED317300AE679F /* BundleExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6106B9D26A565DA0013B453 /* BundleExtension.swift */; };
31ECDA112BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31ECDA102BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift */; };
31ECDA122BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31ECDA102BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift */; };
31ECDA132BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31ECDA102BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift */; };
31ECDA142BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 31ECDA102BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift */; };
31EF1E802B63FFA800E6DB17 /* DBPHomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3192EC872A4DCF21001E97A5 /* DBPHomeViewController.swift */; };
31EF1E812B63FFB800E6DB17 /* DataBrokerProtectionManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3139A1512AA4B3C000969C7D /* DataBrokerProtectionManager.swift */; };
31EF1E822B63FFC200E6DB17 /* DataBrokerProtectionLoginItemScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B6D98662ADEB4B000CD35FE /* DataBrokerProtectionLoginItemScheduler.swift */; };
Expand Down Expand Up @@ -2907,6 +2913,7 @@
31E163B9293A56F400963C10 /* BrokenSiteReportingReferenceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrokenSiteReportingReferenceTests.swift; sourceTree = "<group>"; };
31E163BC293A579E00963C10 /* PrivacyReferenceTestHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyReferenceTestHelper.swift; sourceTree = "<group>"; };
31E163BF293A581900963C10 /* privacy-reference-tests */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = "privacy-reference-tests"; path = "Submodules/privacy-reference-tests"; sourceTree = SOURCE_ROOT; };
31ECDA102BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataBrokerAuthenticationManagerBuilder.swift; sourceTree = "<group>"; };
31F28C4C28C8EEC500119F70 /* YoutubePlayerUserScript.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YoutubePlayerUserScript.swift; sourceTree = "<group>"; };
31F28C4E28C8EEC500119F70 /* YoutubeOverlayUserScript.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = YoutubeOverlayUserScript.swift; sourceTree = "<group>"; };
31F28C5228C8EECA00119F70 /* DuckURLSchemeHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DuckURLSchemeHandler.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -6322,6 +6329,7 @@
7B0099782B65013800FE7C31 /* BrowserWindowManager.swift */,
9D9AE92B2AAB84FF0026E7DC /* DBPMocks.swift */,
9D9AE9172AAA3B450026E7DC /* UserText.swift */,
31ECDA102BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift */,
9D9AE9162AAA3B450026E7DC /* Assets.xcassets */,
9D9AE91A2AAA3B450026E7DC /* DuckDuckGoDBPBackgroundAgent.entitlements */,
9D9AE9192AAA3B450026E7DC /* DuckDuckGoDBPBackgroundAgentAppStore.entitlements */,
Expand Down Expand Up @@ -10276,6 +10284,7 @@
B602E8172A1E2570006D261F /* URL+NetworkProtection.swift in Sources */,
3706FCA1293F65D500E42796 /* AdjacentItemEnumerator.swift in Sources */,
9F9C49FA2BC7BC970099738D /* BookmarkAllTabsDialogView.swift in Sources */,
31ECDA122BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */,
3706FCA2293F65D500E42796 /* ChromiumKeychainPrompt.swift in Sources */,
3707C71E294B5D2900682A9F /* URLRequestExtension.swift in Sources */,
3706FCA3293F65D500E42796 /* WKProcessPool+GeolocationProvider.swift in Sources */,
Expand Down Expand Up @@ -10842,6 +10851,8 @@
9D9AE91D2AAA3B450026E7DC /* DuckDuckGoDBPBackgroundAgentAppDelegate.swift in Sources */,
9D9AE9212AAA3B450026E7DC /* UserText.swift in Sources */,
7BD01C192AD8319C0088B32E /* IPCServiceManager.swift in Sources */,
31ECDA132BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */,
31ECDA0E2BED317300AE679F /* BundleExtension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -10856,6 +10867,8 @@
9D9AE91E2AAA3B450026E7DC /* DuckDuckGoDBPBackgroundAgentAppDelegate.swift in Sources */,
9D9AE9222AAA3B450026E7DC /* UserText.swift in Sources */,
315A023D2B64216B00BFA577 /* IPCServiceManager.swift in Sources */,
31ECDA142BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */,
31ECDA0F2BED317300AE679F /* BundleExtension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -11156,6 +11169,7 @@
B60293E62BA19ECD0033186B /* NetPPopoverManagerMock.swift in Sources */,
B6B3E0E12657EA7A0040E0A2 /* NSScreenExtension.swift in Sources */,
B65E6BA026D9F10600095F96 /* NSBezierPathExtension.swift in Sources */,
31ECDA112BED339600AE679F /* DataBrokerAuthenticationManagerBuilder.swift in Sources */,
4B4D60E02A0C875F00BCD287 /* Bundle+VPN.swift in Sources */,
AA6820E425502F19005ED0D5 /* WebsiteDataStore.swift in Sources */,
9F64346B2BECA38B00D2D8A0 /* SubscriptionAttributionPixelHandler.swift in Sources */,
Expand Down
23 changes: 11 additions & 12 deletions DuckDuckGo/DBP/DBPHomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ final class DBPHomeViewController: NSViewController {
override func viewDidAppear() {
super.viewDidAppear()

if shouldAskForInviteCode() {
presentInviteCodeFlow()
if !dataBrokerProtectionManager.isUserAuthenticated() {
assertionFailure("This UI should never be presented if the user is not authenticated")
closeUI()
}
}

Expand All @@ -118,9 +119,7 @@ final class DBPHomeViewController: NSViewController {
}

private func setupUI() {
if !shouldAskForInviteCode() {
setupUIWithCurrentStatus()
}
setupUIWithCurrentStatus()
}

private func setupObserver() {
Expand Down Expand Up @@ -163,10 +162,6 @@ final class DBPHomeViewController: NSViewController {
}
}

private func shouldAskForInviteCode() -> Bool {
prerequisiteVerifier.checkStatus() == .valid && dataBrokerProtectionManager.shouldAskForInviteCode()
}

private func displayDBPUI() {
replaceChildController(dataBrokerProtectionViewController)
}
Expand All @@ -185,6 +180,12 @@ final class DBPHomeViewController: NSViewController {
NotificationCenter.default.removeObserver(observer)
}
}

private func closeUI() {
presentedWindowController?.window?.close()
presentedWindowController = nil
NotificationCenter.default.post(name: .dbpDidClose, object: nil)
}
}

extension DBPHomeViewController: DataBrokerProtectionInviteDialogsViewModelDelegate {
Expand All @@ -195,9 +196,7 @@ extension DBPHomeViewController: DataBrokerProtectionInviteDialogsViewModelDeleg
}

func dataBrokerProtectionInviteDialogsViewModelDidCancel(_ viewModel: DataBrokerProtectionInviteDialogsViewModel) {
presentedWindowController?.window?.close()
presentedWindowController = nil
NotificationCenter.default.post(name: .dbpDidClose, object: nil)
closeUI()
}
}

Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/DBP/DataBrokerProtectionDebugMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
}

@objc private func runCustomJSON() {
let viewController = DataBrokerRunCustomJSONViewController()
let authenticationManager = DataBrokerAuthenticationManagerBuilder.buildAuthenticationManager()
let viewController = DataBrokerRunCustomJSONViewController(authenticationManager: authenticationManager)
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 500, height: 400),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered,
Expand Down
12 changes: 4 additions & 8 deletions DuckDuckGo/DBP/DataBrokerProtectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public final class DataBrokerProtectionManager {
static let shared = DataBrokerProtectionManager()

private let pixelHandler: EventMapping<DataBrokerProtectionPixels> = DataBrokerProtectionPixelsHandler()
private let authenticationRepository: AuthenticationRepository = KeychainAuthenticationData()
private let authenticationService: DataBrokerProtectionAuthenticationService = AuthenticationService()
private let redeemUseCase: DataBrokerProtectionRedeemUseCase
private let authenticationManager: DataBrokerProtectionAuthenticationManaging
private let fakeBrokerFlag: DataBrokerDebugFlag = DataBrokerDebugFlagFakeBroker()
private let dataBrokerProtectionWaitlistDataSource: WaitlistActivationDateStore = DefaultWaitlistActivationDateStore(source: .dbp)

Expand All @@ -56,13 +54,11 @@ public final class DataBrokerProtectionManager {
}()

private init() {
self.redeemUseCase = RedeemUseCase(authenticationService: authenticationService,
authenticationRepository: authenticationRepository)

self.authenticationManager = DataBrokerAuthenticationManagerBuilder.buildAuthenticationManager()
}

public func shouldAskForInviteCode() -> Bool {
redeemUseCase.shouldAskForInviteCode()
public func isUserAuthenticated() -> Bool {
authenticationManager.isUserAuthenticated
}

// MARK: - Debugging Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,16 @@ import DataBrokerProtection
import PixelKit

final class DataBrokerProtectionSubscriptionEventHandler {

private let accountManager: AccountManaging
private let authRepository: AuthenticationRepository
private let featureDisabler: DataBrokerProtectionFeatureDisabling

init(accountManager: AccountManaging = AccountManager(subscriptionAppGroup: Bundle.main.appGroup(bundle: .subs)),
authRepository: AuthenticationRepository = KeychainAuthenticationData(),
featureDisabler: DataBrokerProtectionFeatureDisabling = DataBrokerProtectionFeatureDisabler()) {
self.accountManager = accountManager
self.authRepository = authRepository
init(featureDisabler: DataBrokerProtectionFeatureDisabling = DataBrokerProtectionFeatureDisabler()) {
self.featureDisabler = featureDisabler
}

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.accessToken else {
PixelKit.fire(GeneralPixel.dataBrokerProtectionErrorWhenFetchingSubscriptionAuthTokenAfterSignIn)
assertionFailure("[DBP Subscription] AccountManager signed in but token could not be retrieved")
return
}

authRepository.save(accessToken: token)
}

@objc private func handleAccountDidSignOut() {
featureDisabler.disableAndDelete()
}
Expand Down
5 changes: 0 additions & 5 deletions DuckDuckGo/Statistics/GeneralPixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ enum GeneralPixel: PixelKitEventV2 {
case dataBrokerResetLoginItemDaily
case dataBrokerDisableAndDeleteDaily

// DataBrokerProtection Other
case dataBrokerProtectionErrorWhenFetchingSubscriptionAuthTokenAfterSignIn

// Default Browser
case defaultRequestedFromHomepage
case defaultRequestedFromHomepageSetupView
Expand Down Expand Up @@ -507,8 +504,6 @@ enum GeneralPixel: PixelKitEventV2 {
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"
case .dataBrokerProtectionRemoteMessageDisplayed(let messageID):
return "m_mac_dbp_remote_message_displayed_\(messageID)"
case .dataBrokerProtectionRemoteMessageDismissed(let messageID):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// DataBrokerAuthenticationManagerBuilder.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 DataBrokerProtection
import Subscription

final public class DataBrokerAuthenticationManagerBuilder {
static func buildAuthenticationManager(redeemUseCase: RedeemUseCase = RedeemUseCase()) -> DataBrokerProtectionAuthenticationManager {
let accountManager = AccountManager(subscriptionAppGroup: Bundle.main.appGroup(bundle: .subs))
let subscriptionManager = DataBrokerProtectionSubscriptionManager(accountManager: accountManager,
environmentManager: DataBrokerProtectionSubscriptionPurchaseEnvironmentManager())
return DataBrokerProtectionAuthenticationManager(redeemUseCase: redeemUseCase,
subscriptionManager: subscriptionManager)

}
}

extension AccountManager: DataBrokerProtectionAccountManaging {
public func hasEntitlement(for cachePolicy: CachePolicy) async -> Result<Bool, any Error> {
await hasEntitlement(for: .dataBrokerProtection, cachePolicy: .reloadIgnoringLocalCacheData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Common
import BrowserServicesKit
import DataBrokerProtection
import PixelKit
import Subscription

public final class DataBrokerProtectionBackgroundManager {

Expand All @@ -30,7 +31,7 @@ public final class DataBrokerProtectionBackgroundManager {

private let authenticationRepository: AuthenticationRepository = KeychainAuthenticationData()
private let authenticationService: DataBrokerProtectionAuthenticationService = AuthenticationService()
private let redeemUseCase: DataBrokerProtectionRedeemUseCase
private let authenticationManager: DataBrokerProtectionAuthenticationManaging
private let fakeBrokerFlag: DataBrokerDebugFlag = DataBrokerDebugFlagFakeBroker()

private lazy var ipcServiceManager = IPCServiceManager(scheduler: scheduler, pixelHandler: pixelHandler)
Expand Down Expand Up @@ -65,13 +66,15 @@ public final class DataBrokerProtectionBackgroundManager {
dataManager: dataManager,
notificationCenter: NotificationCenter.default,
pixelHandler: pixelHandler,
redeemUseCase: redeemUseCase,
authenticationManager: authenticationManager,
userNotificationService: userNotificationService)
}()

private init() {
self.redeemUseCase = RedeemUseCase(authenticationService: authenticationService,
authenticationRepository: authenticationRepository)
let redeemUseCase = RedeemUseCase(authenticationService: authenticationService,
authenticationRepository: authenticationRepository)
self.authenticationManager = DataBrokerAuthenticationManagerBuilder.buildAuthenticationManager(redeemUseCase: redeemUseCase)

_ = ipcServiceManager
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<key>keychain-access-groups</key>
<array>
<string>$(DBP_APP_GROUP)</string>
<string>$(SUBSCRIPTION_APP_GROUP)</string>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<key>keychain-access-groups</key>
<array>
<string>$(DBP_APP_GROUP)</string>
<string>$(SUBSCRIPTION_APP_GROUP)</string>
</array>
</dict>
</plist>
18 changes: 9 additions & 9 deletions DuckDuckGoDBPBackgroundAgent/Info-AppStore.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DBP_APP_GROUP</key>
<string>$(DBP_APP_GROUP)</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
<key>DBP_APP_GROUP</key>
<string>$(DBP_APP_GROUP)</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
<key>SUBSCRIPTION_APP_GROUP</key>
<string>$(SUBSCRIPTION_APP_GROUP)</string>
</dict>
</plist>
18 changes: 9 additions & 9 deletions DuckDuckGoDBPBackgroundAgent/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DBP_APP_GROUP</key>
<string>$(DBP_APP_GROUP)</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
<key>DBP_APP_GROUP</key>
<string>$(DBP_APP_GROUP)</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
<key>SUBSCRIPTION_APP_GROUP</key>
<string>$(SUBSCRIPTION_APP_GROUP)</string>
</dict>
</plist>
Loading

0 comments on commit 5cf0022

Please sign in to comment.