Skip to content

Commit

Permalink
Update VPN metadata to include entitlement (#3002)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
samsymons authored Jun 27, 2024
1 parent f9f08bb commit 9ce5afe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
3 changes: 1 addition & 2 deletions DuckDuckGo/Feedback/VPNFeedbackFormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 11 additions & 17 deletions DuckDuckGo/Feedback/VPNMetadataCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions DuckDuckGo/NetworkProtectionDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 9ce5afe

Please sign in to comment.