From 9ce5afedd54aaf96e348e17e9e8c7396da4b9c94 Mon Sep 17 00:00:00 2001 From: Sam Symons Date: Thu, 27 Jun 2024 15:32:01 +1200 Subject: [PATCH] Update VPN metadata to include entitlement (#3002) Task/Issue URL: https://app.asana.com/0/414235014887631/1207672846964315/f Tech Design URL: CC: Description: This PR updates the metadata collector to include entitlement status. --- DuckDuckGo/Feedback/VPNFeedbackFormView.swift | 3 +- .../Feedback/VPNMetadataCollector.swift | 28 ++++++++----------- ...NetworkProtectionDebugViewController.swift | 3 +- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/DuckDuckGo/Feedback/VPNFeedbackFormView.swift b/DuckDuckGo/Feedback/VPNFeedbackFormView.swift index fd116f15e6..b74eca8b17 100644 --- a/DuckDuckGo/Feedback/VPNFeedbackFormView.swift +++ b/DuckDuckGo/Feedback/VPNFeedbackFormView.swift @@ -25,8 +25,7 @@ import NetworkProtection @available(iOS 15.0, *) struct VPNFeedbackFormCategoryView: View { @Environment(\.dismiss) private var dismiss - let collector = DefaultVPNMetadataCollector(statusObserver: AppDependencyProvider.shared.connectionObserver, - tokenStore: AppDependencyProvider.shared.networkProtectionKeychainTokenStore) + let collector = DefaultVPNMetadataCollector(statusObserver: AppDependencyProvider.shared.connectionObserver) var body: some View { VStack { diff --git a/DuckDuckGo/Feedback/VPNMetadataCollector.swift b/DuckDuckGo/Feedback/VPNMetadataCollector.swift index b439d4c0f9..8345ec44b5 100644 --- a/DuckDuckGo/Feedback/VPNMetadataCollector.swift +++ b/DuckDuckGo/Feedback/VPNMetadataCollector.swift @@ -65,8 +65,8 @@ struct VPNMetadata: Encodable { } struct PrivacyProInfo: Encodable { - let hasToken: Bool - let subscriptionActive: Bool + let hasPrivacyProAccount: Bool + let hasVPNEntitlement: Bool } struct LastDisconnectError: Encodable { @@ -114,18 +114,18 @@ protocol VPNMetadataCollector { final class DefaultVPNMetadataCollector: VPNMetadataCollector { private let statusObserver: ConnectionStatusObserver private let serverInfoObserver: ConnectionServerInfoObserver - private let tokenStore: NetworkProtectionTokenStore + private let accountManager: AccountManager private let settings: VPNSettings private let defaults: UserDefaults init(statusObserver: ConnectionStatusObserver, serverInfoObserver: ConnectionServerInfoObserver = ConnectionServerInfoObserverThroughSession(), - tokenStore: NetworkProtectionTokenStore, + accountManager: AccountManager = AppDependencyProvider.shared.subscriptionManager.accountManager, settings: VPNSettings = .init(defaults: .networkProtectionGroupDefaults), defaults: UserDefaults = .networkProtectionGroupDefaults) { self.statusObserver = statusObserver self.serverInfoObserver = serverInfoObserver - self.tokenStore = tokenStore + self.accountManager = accountManager self.settings = settings self.defaults = defaults } @@ -136,7 +136,7 @@ final class DefaultVPNMetadataCollector: VPNMetadataCollector { let networkInfoMetadata = await collectNetworkInformation() let vpnState = await collectVPNState() let vpnSettingsState = collectVPNSettingsState() - let privacyProInfo = collectPrivacyProInfo() + let privacyProInfo = await collectPrivacyProInfo() return VPNMetadata( appInfo: appInfoMetadata, @@ -253,20 +253,14 @@ final class DefaultVPNMetadataCollector: VPNMetadataCollector { ) } - func collectPrivacyProInfo() -> VPNMetadata.PrivacyProInfo { - var hasToken: Bool { - guard let token = try? tokenStore.fetchToken(), - !token.hasPrefix(NetworkProtectionKeychainTokenStore.authTokenPrefix) else { - return false - } - return true - } - + func collectPrivacyProInfo() async -> VPNMetadata.PrivacyProInfo { + let hasVPNEntitlement = (try? await accountManager.hasEntitlement(forProductName: .networkProtection).get()) ?? false return .init( - hasToken: hasToken, - subscriptionActive: AppDependencyProvider.shared.subscriptionManager.accountManager.isUserAuthenticated + hasPrivacyProAccount: accountManager.isUserAuthenticated, + hasVPNEntitlement: hasVPNEntitlement ) } + } private extension NSError { diff --git a/DuckDuckGo/NetworkProtectionDebugViewController.swift b/DuckDuckGo/NetworkProtectionDebugViewController.swift index 27cdd8d7ac..472402a443 100644 --- a/DuckDuckGo/NetworkProtectionDebugViewController.swift +++ b/DuckDuckGo/NetworkProtectionDebugViewController.swift @@ -654,8 +654,7 @@ shouldShowVPNShortcut: \(vpnVisibility.shouldShowVPNShortcut() ? "YES" : "NO") @MainActor private func refreshMetadata() async { - let collector = DefaultVPNMetadataCollector(statusObserver: AppDependencyProvider.shared.connectionObserver, - tokenStore: AppDependencyProvider.shared.networkProtectionKeychainTokenStore) + let collector = DefaultVPNMetadataCollector(statusObserver: AppDependencyProvider.shared.connectionObserver) self.vpnMetadata = await collector.collectMetadata() self.tableView.reloadData() }