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

VPN: Cleanup authorize call #763

Merged
merged 3 commits into from
Apr 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public protocol NetworkProtectionCodeRedeeming {
/// Redeems an invite code with the Network Protection backend and stores the resulting auth token
func redeem(_ code: String) async throws

/// Exchanges an access token for an auth token, and stores the resulting auth token
@available(*, deprecated, message: "[NetP Subscription] Use subscription access token instead")
func exchange(accessToken: String) async throws

}

/// Coordinates calls to the backend and oAuth token storage
Expand Down Expand Up @@ -59,7 +55,7 @@ public final class NetworkProtectionCodeRedemptionCoordinator: NetworkProtection
}

public func redeem(_ code: String) async throws {
let result = await networkClient.authenticate(withMethod: .inviteCode(code))
let result = await networkClient.redeem(inviteCode: code)
switch result {
case .success(let token):
try tokenStore.store(token)
Expand All @@ -75,16 +71,4 @@ public final class NetworkProtectionCodeRedemptionCoordinator: NetworkProtection
}
}

public func exchange(accessToken code: String) async throws {
let result = await networkClient.authenticate(withMethod: .subscription(code))
switch result {
case .success(let token):
try tokenStore.store(token)

case .failure(let error):
errorEvents.fire(error.networkProtectionError)
throw error
}
}

}
27 changes: 2 additions & 25 deletions Sources/NetworkProtection/Networking/NetworkProtectionClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@

import Foundation

public enum NetworkProtectionAuthenticationMethod {
case inviteCode(String)
case subscription(String)
}

protocol NetworkProtectionClient {
func authenticate(withMethod method: NetworkProtectionAuthenticationMethod) async -> Result<String, NetworkProtectionClientError>
func redeem(inviteCode: String) async -> Result<String, NetworkProtectionClientError>
func getLocations(authToken: String) async -> Result<[NetworkProtectionLocation], NetworkProtectionClientError>
func getServers(authToken: String) async -> Result<[NetworkProtectionServer], NetworkProtectionClientError>
func register(authToken: String,
Expand Down Expand Up @@ -161,10 +156,6 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {
endpointURL.appending("/redeem")
}

var authorizeURL: URL {
endpointURL.appending("/authorize")
}

private let decoder: JSONDecoder = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withFullDate, .withFullTime, .withFractionalSeconds]
Expand Down Expand Up @@ -338,15 +329,6 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {
}
}

public func authenticate(withMethod method: NetworkProtectionAuthenticationMethod) async -> Result<String, NetworkProtectionClientError> {
switch method {
case .inviteCode(let code):
return await redeem(inviteCode: code)
case .subscription(let accessToken):
return await exchange(accessToken: accessToken)
}
}

public enum AuthTokenError: CustomNSError {
case noResponse
case unexpectedStatus(status: Int)
Expand All @@ -362,16 +344,11 @@ final class NetworkProtectionBackendClient: NetworkProtectionClient {
}
}

private func redeem(inviteCode: String) async -> Result<String, NetworkProtectionClientError> {
public func redeem(inviteCode: String) async -> Result<String, NetworkProtectionClientError> {
let requestBody = RedeemInviteCodeRequestBody(code: inviteCode)
return await retrieveAuthToken(requestBody: requestBody, endpoint: redeemURL)
}

private func exchange(accessToken: String) async -> Result<String, NetworkProtectionClientError> {
let requestBody = ExchangeAccessTokenRequestBody(token: accessToken)
return await retrieveAuthToken(requestBody: requestBody, endpoint: authorizeURL)
}

private func retrieveAuthToken<RequestBody: Encodable>(
requestBody: RequestBody,
endpoint: URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,11 @@ public final class MockNetworkProtectionClient: NetworkProtectionClient {
self.stubRegister = stubRegister
}

public func authenticate(
withMethod method: NetworkProtection.NetworkProtectionAuthenticationMethod
public func redeem(
inviteCode: String
) async -> Result<String, NetworkProtection.NetworkProtectionClientError> {
switch method {
case .inviteCode(let inviteCode):
spyRedeemInviteCode = inviteCode
return stubRedeem
case .subscription(let accessToken):
spyRedeemAccessToken = accessToken
return stubRedeem
}
spyRedeemInviteCode = inviteCode
return stubRedeem
}

public var spyGetServersAuthToken: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ final class NetworkProtectionClientTests: XCTestCase {
MockURLProtocol.stubs[client.redeemURL] = (response: HTTPURLResponse(url: client.redeemURL, statusCode: 200)!,
.success(successData))

let result = await client.authenticate(withMethod: .inviteCode("DH76F8S"))
let result = await client.redeem(inviteCode: "DH76F8S")

XCTAssertEqual(try? result.get(), token)
}
Expand All @@ -97,7 +97,7 @@ final class NetworkProtectionClientTests: XCTestCase {
MockURLProtocol.stubs[client.redeemURL] = (response: HTTPURLResponse(url: client.redeemURL, statusCode: 400)!,
.success(emptyData))

let result = await client.authenticate(withMethod: .inviteCode("DH76F8S"))
let result = await client.redeem(inviteCode: "DH76F8S")

guard case .failure(let error) = result, case .invalidInviteCode = error else {
XCTFail("Expected an invalidInviteCode error to be thrown")
Expand All @@ -112,7 +112,7 @@ final class NetworkProtectionClientTests: XCTestCase {
MockURLProtocol.stubs[client.redeemURL] = (response: HTTPURLResponse(url: client.redeemURL, statusCode: code)!,
.success(emptyData))

let result = await client.authenticate(withMethod: .inviteCode("DH76F8S"))
let result = await client.redeem(inviteCode: "DH76F8S")

guard case .failure(let error) = result, case .failedToRedeemInviteCode = error else {
XCTFail("Expected a failedToRedeemInviteCode error to be thrown")
Expand All @@ -126,7 +126,7 @@ final class NetworkProtectionClientTests: XCTestCase {
MockURLProtocol.stubs[client.redeemURL] = (response: HTTPURLResponse(url: client.redeemURL, statusCode: 200)!,
.success(undecodableData))

let result = await client.authenticate(withMethod: .inviteCode("DH76F8S"))
let result = await client.redeem(inviteCode: "DH76F8S")

guard case .failure(let error) = result, case .failedToParseRedeemResponse = error else {
XCTFail("Expected a failedToRedeemInviteCode error to be thrown")
Expand Down
Loading