Skip to content

Commit

Permalink
Allow using a production subscription with staging VPN environment (#…
Browse files Browse the repository at this point in the history
…3109)

Task/Issue URL: https://app.asana.com/0/1193060753475688/1208066081988374/f
Tech Design URL:
CC:

Description:

This PR allows internal users to swap to the staging VPN environment with a production subscription.
  • Loading branch information
samsymons authored Aug 28, 2024
1 parent da7daf0 commit be5f2c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ extension NetworkProtectionDeviceManager {
}
}

extension NetworkProtectionCodeRedemptionCoordinator {
convenience init() {
let settings = Application.appDelegate.vpnSettings
self.init(environment: settings.selectedEnvironment,
tokenStore: NetworkProtectionKeychainTokenStore(),
errorEvents: .networkProtectionAppDebugEvents,
isSubscriptionEnabled: DefaultSubscriptionFeatureAvailability().isFeatureAvailable)
}
}

extension NetworkProtectionKeychainTokenStore {
convenience init() {
self.init(isSubscriptionEnabled: DefaultSubscriptionFeatureAvailability().isFeatureAvailable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,11 @@ final class NetworkProtectionDebugMenu: NSMenu {

private func populateNetworkProtectionEnvironmentListMenuItems() {
environmentMenu.items = [
NSMenuItem(title: "⚠️ The environment can be set in the Subscription > Environment menu", action: nil, target: nil),
NSMenuItem(title: "Production", action: nil, target: nil, keyEquivalent: ""),
NSMenuItem(title: "Staging", action: nil, target: nil, keyEquivalent: ""),
NSMenuItem(title: "⚠️ A staging subscription can be used for the staging VPN environment, a production subscription can be used for both", action: nil, target: nil),
NSMenuItem(title: "⚠️ Please restart the browser after changing environment", action: nil, target: nil),
NSMenuItem.separator(),
NSMenuItem(title: "Production", action: #selector(setSelectedEnvironment(_:)), target: self, keyEquivalent: ""),
NSMenuItem(title: "Staging", action: #selector(setSelectedEnvironment(_:)), target: self, keyEquivalent: ""),
]
}

Expand Down Expand Up @@ -407,10 +409,10 @@ final class NetworkProtectionDebugMenu: NSMenu {

private func updateEnvironmentMenu() {
let selectedEnvironment = settings.selectedEnvironment
guard environmentMenu.items.count == 3 else { return }
guard environmentMenu.items.count == 5 else { return }

environmentMenu.items[1].state = selectedEnvironment == .production ? .on: .off
environmentMenu.items[2].state = selectedEnvironment == .staging ? .on: .off
environmentMenu.items[3].state = selectedEnvironment == .production ? .on : .off
environmentMenu.items[4].state = selectedEnvironment == .staging ? .on : .off
}

private func updatePreferredServerMenu() {
Expand Down Expand Up @@ -481,6 +483,28 @@ final class NetworkProtectionDebugMenu: NSMenu {
@objc private func toggleExcludeDDGBrowser() {
transparentProxySettings.toggleExclusion(for: ddgBrowserAppIdentifier)
}

// MARK: Environment

@objc func setSelectedEnvironment(_ menuItem: NSMenuItem) {
let title = menuItem.title
let selectedEnvironment: VPNSettings.SelectedEnvironment

if title == "Staging" {
selectedEnvironment = .staging
} else {
selectedEnvironment = .production
}

settings.selectedEnvironment = selectedEnvironment

Task {
_ = try await NetworkProtectionDeviceManager.create().refreshServerList()
try? await populateNetworkProtectionServerListMenuItems()

settings.selectedServer = .automatic
}
}
}

#if DEBUG
Expand Down
4 changes: 3 additions & 1 deletion DuckDuckGo/Subscription/VPNSettings+Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public extension VPNSettings {
func alignTo(subscriptionEnvironment: SubscriptionEnvironment) {
switch subscriptionEnvironment.serviceEnvironment {
case .production:
self.selectedEnvironment = .production
// Do nothing for a production subscription, as it can be used for both VPN environments.
break
case .staging:
// If using a staging subscription, force the staging VPN environment as it is not compatible with anything else.
self.selectedEnvironment = .staging
}
}
Expand Down

0 comments on commit be5f2c8

Please sign in to comment.