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

Subscription oauth v2 #1033

Open
wants to merge 128 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
52c4e2f
Networking V2 implemented and tested
federicocappelli Sep 6, 2024
6deb71d
networking v2 refinements and unit tests
federicocappelli Sep 9, 2024
aecc91b
headers renamed
federicocappelli Sep 9, 2024
23800c2
mocks, lint
federicocappelli Sep 10, 2024
d029027
api configuration removed and data moved to api request, lint
federicocappelli Sep 10, 2024
ef4949c
query items changed and tests fixed
federicocappelli Sep 10, 2024
0dbc46e
added unit test
federicocappelli Sep 10, 2024
00bc2b2
Merge branch 'main' into fcappelli/networkingV2
federicocappelli Sep 10, 2024
0000972
privacy ref repo updated
federicocappelli Sep 10, 2024
309770b
pkg updated
federicocappelli Sep 10, 2024
d502223
comments
federicocappelli Sep 10, 2024
0ea4332
Documentation + comments and improvements
federicocappelli Sep 11, 2024
3101949
Doc
federicocappelli Sep 11, 2024
b20199d
Merge branch 'main' into fcappelli/networkingV2
federicocappelli Sep 11, 2024
a11b58b
v1 restore
federicocappelli Sep 11, 2024
46a7423
v1 restore
federicocappelli Sep 11, 2024
a7e2cae
tests fix
federicocappelli Sep 11, 2024
99f80f7
comment fix
federicocappelli Sep 11, 2024
d3ca645
auth client, service etc
federicocappelli Sep 11, 2024
193792f
authorise call + real tests
federicocappelli Sep 12, 2024
969514d
authorisation API done
federicocappelli Sep 13, 2024
bb8fb82
error handling fixed
federicocappelli Sep 13, 2024
6ea42ad
sendotp call
federicocappelli Sep 13, 2024
237c90e
renaming
federicocappelli Sep 16, 2024
ea3d225
Merge branch 'fcappelli/networkingV2' into fcappelli/subscription_oau…
federicocappelli Sep 16, 2024
48091f9
login api call
federicocappelli Sep 16, 2024
8c70535
more APIs
federicocappelli Sep 17, 2024
0b246d0
access token api requests
federicocappelli Sep 17, 2024
aa00362
optimisations
federicocappelli Sep 17, 2024
420f488
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Sep 18, 2024
1567199
JWT and JWKs
federicocappelli Sep 18, 2024
d7e7be2
new jwtpayload
federicocappelli Sep 19, 2024
552b281
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Sep 19, 2024
420be21
files ranamed
federicocappelli Sep 23, 2024
4b60204
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Sep 23, 2024
2d2425f
more oauthclient features
federicocappelli Sep 24, 2024
8bd8835
more account activation and editing
federicocappelli Sep 24, 2024
5b17d89
backup
federicocappelli Sep 27, 2024
8782e77
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 14, 2024
a300bb6
builds
federicocappelli Oct 16, 2024
3bc981d
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 16, 2024
18b64e4
auth and first purchase improved
federicocappelli Oct 21, 2024
0bbb394
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 21, 2024
085e85c
purchase flow fixed
federicocappelli Oct 22, 2024
9400b0c
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 22, 2024
aa8a036
api call param fix
federicocappelli Oct 23, 2024
2e2ada3
purchase fixed
federicocappelli Oct 23, 2024
1aac90f
loggers improved and restore fixed
federicocappelli Oct 24, 2024
69bcced
subscription refresh improved
federicocappelli Oct 25, 2024
3503e04
v1 to v2 auth migration
federicocappelli Oct 25, 2024
b778df6
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 25, 2024
b3873ea
lint
federicocappelli Oct 25, 2024
ec3409a
unit tests improvements
federicocappelli Oct 28, 2024
52ad921
review suggestions applied
federicocappelli Oct 29, 2024
6412ff9
more tests and logs
federicocappelli Oct 30, 2024
196b83e
vpn fixed
federicocappelli Oct 31, 2024
20e63ac
unit tests
federicocappelli Nov 1, 2024
095e7e1
signout as async
federicocappelli Nov 1, 2024
156814d
Merge branch 'main' of https://github.com/duckduckgo/BrowserServicesK…
federicocappelli Nov 4, 2024
e69f756
purchase fixes
federicocappelli Nov 4, 2024
f4e03fd
unit tests fixed + utilities
federicocappelli Nov 4, 2024
bbd9dac
unit tests
federicocappelli Nov 4, 2024
3b05a78
unit tests
federicocappelli Nov 4, 2024
edfdfbb
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 4, 2024
54182e2
subscripti token background refresh added
federicocappelli Nov 5, 2024
976f625
lint + tests
federicocappelli Nov 5, 2024
4bdca2a
lint
federicocappelli Nov 5, 2024
133b245
warnings and concurrency issue fixed
federicocappelli Nov 5, 2024
7a1edcc
tests fix
federicocappelli Nov 5, 2024
3161a3f
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 6, 2024
b01c07d
lint
federicocappelli Nov 6, 2024
92a2154
build import fixed
federicocappelli Nov 6, 2024
da30fc9
cleanup and errors improvements
federicocappelli Nov 6, 2024
b8aad9b
unit tests impr
federicocappelli Nov 7, 2024
a5c6054
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 7, 2024
99ac675
subscription positive integration test
federicocappelli Nov 8, 2024
aab9795
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 14, 2024
b7d4d04
DI improved
federicocappelli Nov 19, 2024
13547dc
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 19, 2024
4f17767
DI done
federicocappelli Nov 20, 2024
571e083
token v1/v2 exchange fixed, token providing improved
federicocappelli Nov 22, 2024
53fcbbb
tokencontainer improvements and data extraction
federicocappelli Nov 22, 2024
7e17373
token container improvements and vpn token propagation
federicocappelli Nov 25, 2024
4a044a8
keychian type moved
federicocappelli Nov 26, 2024
744f3f3
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 26, 2024
9691ad2
it works!
federicocappelli Nov 26, 2024
9570d21
fixing unit tests
federicocappelli Nov 27, 2024
a27984e
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 27, 2024
7c74ea4
subscription manager mock fixed
federicocappelli Nov 28, 2024
43b151f
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 28, 2024
fe8b105
lint cleanup and tests fixed
federicocappelli Nov 28, 2024
4e4605c
unit tests green
federicocappelli Nov 28, 2024
02e7926
lint
federicocappelli Nov 28, 2024
c3c0143
error handling, moks and tests improved
federicocappelli Nov 29, 2024
71155a2
unit tests improved
federicocappelli Nov 29, 2024
1f1b48c
unit tests parallelism disabled
federicocappelli Nov 29, 2024
97ddc9c
Merge branch 'main' of https://github.com/duckduckgo/BrowserServicesK…
federicocappelli Nov 29, 2024
97e986e
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 2, 2024
38dd352
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 2, 2024
f2b8ca9
lint
federicocappelli Dec 2, 2024
c1ce80d
merge issue fixed
federicocappelli Dec 2, 2024
a265567
work-ish
federicocappelli Dec 5, 2024
c5aa475
logs
federicocappelli Dec 5, 2024
bb6f502
get token optimisations
federicocappelli Dec 5, 2024
ebef4d5
asd
federicocappelli Dec 5, 2024
7bbbedf
reverting minor changes
federicocappelli Dec 5, 2024
8e23ae9
Merge branch 'main' into fcappelli/vpn_error_2
federicocappelli Dec 5, 2024
63e5886
subscription purchase notification fixed
federicocappelli Dec 6, 2024
a4b2232
lint and unit tests fix
federicocappelli Dec 6, 2024
2a2fb68
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 6, 2024
dbf72ed
quite tests output
federicocappelli Dec 6, 2024
27f95f1
Merge branch 'main' of https://github.com/duckduckgo/BrowserServicesK…
federicocappelli Dec 6, 2024
79bf3ae
unit tests fixed
federicocappelli Dec 6, 2024
07e8b56
lint
federicocappelli Dec 6, 2024
f85e3a3
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 12, 2024
975eb88
backward compatibility typealias
federicocappelli Dec 12, 2024
2cd4bab
small cleanups
federicocappelli Dec 12, 2024
43774ad
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 12, 2024
30e74c8
extension moved from macos to bsk
federicocappelli Dec 12, 2024
57fc80b
code comments + sub restore improvements
federicocappelli Dec 12, 2024
50d4d64
mock fixed
federicocappelli Dec 12, 2024
b586366
Update pr.yml
federicocappelli Dec 12, 2024
1292c9d
code cleanup
federicocappelli Dec 12, 2024
f12498a
Merge branch 'fcappelli/subscription_oauth_api_v2' of https://github.…
federicocappelli Dec 12, 2024
a942eea
stripe success integration test
federicocappelli Dec 12, 2024
3ce186f
code cleanup
federicocappelli Dec 12, 2024
77e4a93
date extension improved and unit tests added
federicocappelli Dec 13, 2024
8fec94f
lint
federicocappelli Dec 13, 2024
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
Prev Previous commit
Next Next commit
code cleanup
federicocappelli committed Dec 12, 2024
commit 1292c9df1402d0b9eb93320a6d5209bc9d375aa9
1 change: 1 addition & 0 deletions Sources/Common/KeychainType.swift
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@

import Foundation

/// A convenience enum to unify the logic for selecting the right keychain through the query attributes.
public enum KeychainType {
case dataProtection(_ accessGroup: AccessGroup)
/// Uses the system keychain.
Original file line number Diff line number Diff line change
@@ -53,28 +53,28 @@ public actor NetworkProtectionEntitlementMonitor {
// MARK: - Start/Stop monitoring

public func start(entitlementCheck: @escaping () async -> Swift.Result<Bool, Error>, callback: @escaping (Result) async -> Void) {
Logger.networkProtectionEntitlement.log("Starting entitlement monitor")
Logger.networkProtectionEntitlement.log("⚫️ Starting entitlement monitor")

task = Task.periodic(interval: Self.monitoringInterval) {
let result = await entitlementCheck()
switch result {
case .success(let hasEntitlement):
if hasEntitlement {
Logger.networkProtectionEntitlement.log("Valid entitlement")
Logger.networkProtectionEntitlement.log("⚫️ Valid entitlement")
await callback(.validEntitlement)
} else {
Logger.networkProtectionEntitlement.log("Invalid entitlement")
Logger.networkProtectionEntitlement.log("⚫️ Invalid entitlement")
await callback(.invalidEntitlement)
}
case .failure(let error):
Logger.networkProtectionEntitlement.error("Error retrieving entitlement: \(error.localizedDescription, privacy: .public)")
Logger.networkProtectionEntitlement.error("⚫️ Error retrieving entitlement: \(error.localizedDescription, privacy: .public)")
await callback(.error(error))
}
}
}

public func stop() {
Logger.networkProtectionEntitlement.log("Stopping entitlement monitor")
Logger.networkProtectionEntitlement.log("⚫️ Stopping entitlement monitor")

task?.cancel() // Just making extra sure in case it's detached
task = nil
38 changes: 19 additions & 19 deletions Sources/NetworkProtection/Networking/NetworkProtectionClient.swift
Original file line number Diff line number Diff line change
@@ -90,25 +90,25 @@ public enum NetworkProtectionClientError: CustomNSError, NetworkProtectionErrorC
}
}

// public var errorDescription: String? {
// switch self {
// case .failedToFetchLocationList: return "Failed to fetch location list"
// case .failedToParseLocationListResponse: return "Failed to parse location list response"
// case .failedToFetchServerList: return "Failed to fetch server list"
// case .failedToParseServerListResponse: return "Failed to parse server list response"
// case .failedToEncodeRegisterKeyRequest: return "Failed to encode register key request"
// case .failedToFetchServerStatus(let error):
// return "Failed to fetch server status: \(error)"
// case .failedToParseServerStatusResponse(let error):
// return "Failed to parse server status response: \(error)"
// case .failedToFetchRegisteredServers(let error):
// return "Failed to fetch registered servers: \(error)"
// case .failedToParseRegisteredServersResponse(let error):
// return "Failed to parse registered servers response: \(error)"
// case .invalidAuthToken: return "Invalid auth token"
// case .accessDenied: return "Access denied"
// }
// }
public var errorDescription: String? {
switch self {
case .failedToFetchLocationList: return "Failed to fetch location list"
case .failedToParseLocationListResponse: return "Failed to parse location list response"
case .failedToFetchServerList: return "Failed to fetch server list"
case .failedToParseServerListResponse: return "Failed to parse server list response"
case .failedToEncodeRegisterKeyRequest: return "Failed to encode register key request"
case .failedToFetchServerStatus(let error):
return "Failed to fetch server status: \(error)"
case .failedToParseServerStatusResponse(let error):
return "Failed to parse server status response: \(error)"
case .failedToFetchRegisteredServers(let error):
return "Failed to fetch registered servers: \(error)"
case .failedToParseRegisteredServersResponse(let error):
return "Failed to parse registered servers response: \(error)"
case .invalidAuthToken: return "Invalid auth token"
case .accessDenied: return "Access denied"
}
}
}

struct RegisterKeyRequestBody: Encodable {
3 changes: 0 additions & 3 deletions Sources/Networking/OAuth/README.md

This file was deleted.

1 change: 0 additions & 1 deletion Sources/Networking/OAuth/SessionDelegate.swift
Original file line number Diff line number Diff line change
@@ -23,7 +23,6 @@ public final class SessionDelegate: NSObject, URLSessionTaskDelegate {

/// Disable automatic redirection, in our specific OAuth implementation we manage the redirection, not the user
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {
// Logger.networking.debug("Stopping OAuth API redirection: \(response)")
return nil
}
}
1 change: 0 additions & 1 deletion Sources/Networking/v2/APIResponseV2.swift
Original file line number Diff line number Diff line change
@@ -35,7 +35,6 @@ public extension APIResponseV2 {
/// - Parameter decoder: A custom JSONDecoder, if not provided the default JSONDecoder() is used
/// - Returns: An instance of a Decodable model of the type inferred, throws an error if the body is empty or the decoding fails
func decodeBody<T: Decodable>(decoder: JSONDecoder = JSONDecoder()) throws -> T {
// decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .millisecondsSince1970

guard let data = self.data else {
63 changes: 0 additions & 63 deletions Sources/Subscription/API/SubscriptionRequest.swift
Original file line number Diff line number Diff line change
@@ -81,66 +81,3 @@ struct SubscriptionRequest {
return SubscriptionRequest(apiRequest: request)
}
}
/*
extension SubscriptionRequest {

// MARK: - Response body errors

enum Error: LocalizedError, Equatable {
case APIError(code: BodyErrorCode)
case missingResponseValue(String)

public var errorDescription: String? {
switch self {
case .APIError(let code):
"Subscription API responded with error \(code.rawValue) - \(code.description)"
case .missingResponseValue(let value):
"The API response is missing \(value)"
}
}

public static func == (lhs: SubscriptionRequest.Error, rhs: SubscriptionRequest.Error) -> Bool {
switch (lhs, rhs) {
case (.APIError(let lhsCode), .APIError(let rhsCode)):
return lhsCode == rhsCode
case (.missingResponseValue(let lhsValue), .missingResponseValue(let rhsValue)):
return lhsValue == rhsValue
default:
return lhs == rhs
}
}
}

struct BodyError: Decodable {
let error: BodyErrorCode
}

public enum BodyErrorCode: String, Decodable {
case noSubscriptionFound = "No subscription found"

public var description: String {
switch self {
case .noSubscriptionFound:
return self.rawValue
}
}
}

func extractBodyError(from response: APIResponseV2) -> BodyError? {
do {
let bodyError: SubscriptionRequest.BodyError = try response.decodeBody()
return bodyError
} catch {
return nil
}
}

func throwError(forResponse response: APIResponseV2) throws {
if let extractBodyError = extractBodyError(from: response) {
throw SubscriptionRequest.Error.APIError(code: extractBodyError.error)
} else {
throw SubscriptionRequest.Error.missingResponseValue("Body error")
}
}
}
*/
Original file line number Diff line number Diff line change
@@ -31,15 +31,13 @@ public final class SubscriptionTokenKeychainStorageV2: TokenStoring {

public var tokenContainer: TokenContainer? {
get {
// Logger.subscriptionKeychain.debug("get TokenContainer")
guard let data = try? retrieveData(forField: .tokens) else {
Logger.subscriptionKeychain.debug("TokenContainer not found")
return nil
}
return CodableHelper.decode(jsonData: data)
}
set {
// Logger.subscriptionKeychain.debug("set TokenContainer")
do {
guard let newValue else {
Logger.subscriptionKeychain.debug("remove TokenContainer")