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

Use subs token for fetching locations #724

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions Sources/NetworkProtection/AppLaunching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum AppLaunchCommand: Codable {
case stopVPN
case enableOnDemand
case moveAppToApplications
case showPrivacyPro
}

public protocol AppLaunching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public actor NetworkProtectionEntitlementMonitor {

// MARK: - Init & deinit

init() {
public init() {
os_log("[+] %{public}@", log: .networkProtectionMemoryLog, type: .debug, String(describing: self))
}

Expand Down
28 changes: 25 additions & 3 deletions Sources/NetworkProtection/Networking/NetworkProtectionClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {

func getLocations(authToken: String) async -> Result<[NetworkProtectionLocation], NetworkProtectionClientError> {
var request = URLRequest(url: locationsURL)
request.setValue("bearer \(authToken)", forHTTPHeaderField: "Authorization")
let adaptedToken = adaptTokenIfNeeded(authToken)
request.setValue("bearer \(adaptedToken)", forHTTPHeaderField: "Authorization")
let downloadedData: Data

do {
Expand All @@ -209,7 +210,8 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {

func getServers(authToken: String) async -> Result<[NetworkProtectionServer], NetworkProtectionClientError> {
var request = URLRequest(url: serversURL)
request.setValue("bearer \(authToken)", forHTTPHeaderField: "Authorization")
let adaptedToken = adaptTokenIfNeeded(authToken)
request.setValue("bearer \(adaptedToken)", forHTTPHeaderField: "Authorization")
let downloadedData: Data

do {
Expand Down Expand Up @@ -245,7 +247,8 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {
}

var request = URLRequest(url: registerKeyURL)
request.setValue("bearer \(authToken)", forHTTPHeaderField: "Authorization")
let adaptedToken = adaptTokenIfNeeded(authToken)
request.setValue("bearer \(adaptedToken)", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = requestBodyData
Expand Down Expand Up @@ -342,7 +345,26 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {
return .failure(NetworkProtectionClientError.failedToParseRedeemResponse(error))
}
}
}

fileprivate extension NetworkProtectionBackendClient {
private static var authTokenPrefix: String { "ddg:" }

func adaptTokenIfNeeded(_ accessToken: String) -> String {
if isSubscriptionEnabled && !Self.isSubscriptionAccessToken(accessToken) {
return Self.makeToken(from: accessToken)
} else {
return accessToken
}
}

private static func makeToken(from subscriptionAccessToken: String) -> String {
"\(authTokenPrefix)\(subscriptionAccessToken)"
}

private static func isSubscriptionAccessToken(_ token: String) -> Bool {
token.hasPrefix(authTokenPrefix)
}
}

extension URL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public enum NetworkProtectionNotification: String {
case showConnectedNotification
case showIssuesNotResolvedNotification
case showVPNSupersededNotification
case showExpiredEntitlementNotification
case showTestNotification

// Server Selection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public protocol NetworkProtectionLocationListRepository {
final public class NetworkProtectionLocationListCompositeRepository: NetworkProtectionLocationListRepository {
@MainActor private static var locationList: [NetworkProtectionLocation] = []
private let client: NetworkProtectionClient
private let tokenStore: NetworkProtectionTokenStore
private let fetchToken: () throws -> String?
private let errorEvents: EventMapping<NetworkProtectionError>
private let isSubscriptionEnabled: Bool

Expand All @@ -36,18 +36,30 @@ final public class NetworkProtectionLocationListCompositeRepository: NetworkProt
isSubscriptionEnabled: Bool) {
self.init(
client: NetworkProtectionBackendClient(environment: environment, isSubscriptionEnabled: isSubscriptionEnabled),
tokenStore: tokenStore,
fetchToken: tokenStore.fetchToken,
errorEvents: errorEvents,
isSubscriptionEnabled: isSubscriptionEnabled
)
}

convenience public init(environment: VPNSettings.SelectedEnvironment,
fetchToken: @escaping () throws -> String?,
errorEvents: EventMapping<NetworkProtectionError>,
isSubscriptionEnabled: Bool) {
self.init(
client: NetworkProtectionBackendClient(environment: environment, isSubscriptionEnabled: isSubscriptionEnabled),
fetchToken: fetchToken,
errorEvents: errorEvents,
isSubscriptionEnabled: isSubscriptionEnabled
)
}

init(client: NetworkProtectionClient,
tokenStore: NetworkProtectionTokenStore,
fetchToken: @escaping () throws -> String?,
errorEvents: EventMapping<NetworkProtectionError>,
isSubscriptionEnabled: Bool) {
self.client = client
self.tokenStore = tokenStore
self.fetchToken = fetchToken
self.errorEvents = errorEvents
self.isSubscriptionEnabled = isSubscriptionEnabled
}
Expand All @@ -58,7 +70,7 @@ final public class NetworkProtectionLocationListCompositeRepository: NetworkProt
return Self.locationList
}
do {
guard let authToken = try tokenStore.fetchToken() else {
guard let authToken = try fetchToken() else {
throw NetworkProtectionError.noAuthTokenFound
}
Self.locationList = try await client.getLocations(authToken: authToken).get()
Expand Down