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 #3480

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
cc16889
builds
federicocappelli Oct 16, 2024
8b07ea5
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 16, 2024
79faa6b
auth and first purchase improved
federicocappelli Oct 21, 2024
44e00ee
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 21, 2024
c6c2bef
purchase flow fixed
federicocappelli Oct 22, 2024
c640a91
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 22, 2024
018751a
purchase fixed
federicocappelli Oct 23, 2024
de81314
token fixes
federicocappelli Oct 23, 2024
b4c661e
loggers improved and restore fixed
federicocappelli Oct 24, 2024
d123038
subscription refresh improved
federicocappelli Oct 25, 2024
9d90ccf
v1 to v2 auth migration
federicocappelli Oct 25, 2024
457922c
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Oct 25, 2024
60d4012
lint ad restore commented code
federicocappelli Oct 25, 2024
07e2cc0
review suggestions applied
federicocappelli Oct 29, 2024
bd5bb1b
tests and logs
federicocappelli Oct 30, 2024
e92d0db
vpn fixed
federicocappelli Oct 31, 2024
1406315
debug menu improved
federicocappelli Oct 31, 2024
eebbc48
unit tests and lint
federicocappelli Oct 31, 2024
8ea3254
unit tests and improvements
federicocappelli Nov 1, 2024
c78a9c2
signout as async
federicocappelli Nov 1, 2024
c021b95
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 4, 2024
0f426a1
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 4, 2024
f06370b
lint
federicocappelli Nov 5, 2024
5f69e54
BSK points to branch now
federicocappelli Nov 5, 2024
929e12d
bsk updated
federicocappelli Nov 5, 2024
386fd70
Merge branch 'main' of https://github.com/duckduckgo/iOS into fcappel…
federicocappelli Nov 6, 2024
b018cce
bsk local
federicocappelli Nov 7, 2024
00e55c5
Merge branch 'main' of https://github.com/duckduckgo/iOS into fcappel…
federicocappelli Nov 7, 2024
a955a78
BSK
federicocappelli Nov 7, 2024
b67e5d4
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 7, 2024
47ac372
BSK update
federicocappelli Nov 8, 2024
80be4fe
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 14, 2024
bd51e3f
DI improved
federicocappelli Nov 19, 2024
7327d2d
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 19, 2024
67d5d50
token v1/v2 exchange fixed, token providing improved
federicocappelli Nov 22, 2024
d0c8344
updates after mac integration
federicocappelli Nov 27, 2024
60bad87
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 27, 2024
170ed9e
merge
federicocappelli Nov 27, 2024
32e5de6
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 27, 2024
87455ba
local bsk
federicocappelli Nov 27, 2024
0191b02
project fix
federicocappelli Nov 27, 2024
bd5cb7c
unit tests builds
federicocappelli Nov 28, 2024
f229b65
main merge
federicocappelli Nov 29, 2024
e4fbf00
BSK > branch
federicocappelli Nov 29, 2024
f8eabd5
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Nov 29, 2024
a745b49
packages
federicocappelli Nov 29, 2024
b3482bd
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 2, 2024
464e598
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 2, 2024
ba4cbad
Merge branch 'main' into fcappelli/subscription_oauth_api_v2
federicocappelli Dec 12, 2024
edf1bfa
updates from mac and unit tests fixed
federicocappelli Dec 12, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Logger+Subscription.swift
// Logger+Pixel.swift
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the same file, GH is confused

// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
Expand All @@ -20,9 +20,6 @@
import Foundation
import os.log

extension Logger {

static var subscription: Logger = {
Logger(subsystem: Bundle.main.bundleIdentifier ?? "DuckDuckGo", category: "SubscriptionPro")
}()
public extension Logger {
static var pixels = { Logger(subsystem: "Pixels", category: "") }()
}
2 changes: 1 addition & 1 deletion Core/PersistentPixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public final class PersistentPixel: PersistentPixelFiring {
var additionalParameters = additionalParameters
additionalParameters[PixelParameters.originalPixelTimestamp] = dateString

Logger.general.debug("Firing persistent pixel named \(pixel.name)")
Logger.pixels.debug("Firing persistent pixel named \(pixel.name)")

pixelFiring.fire(pixel: pixel,
error: error,
Expand Down
4 changes: 2 additions & 2 deletions Core/Pixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public class Pixel {
}

guard !isDryRun else {
Logger.general.debug("Pixel fired \(pixelName.replacingOccurrences(of: "_", with: "."), privacy: .public) \(params.count > 0 ? "\(params)" : "", privacy: .public)")
Logger.pixels.debug("Pixel fired \(pixelName.replacingOccurrences(of: "_", with: "."), privacy: .public) \(params.count > 0 ? "\(params)" : "", privacy: .public)")
// simulate server response time for Dry Run mode
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
onComplete(nil)
Expand Down Expand Up @@ -273,7 +273,7 @@ public class Pixel {
headers: headers)
let request = APIRequest(configuration: configuration, urlSession: .session(useMainThreadCallbackQueue: true))
request.fetch { _, error in
Logger.general.debug("Pixel fired \(pixelName, privacy: .public) \(params, privacy: .public)")
Logger.pixels.debug("Pixel fired \(pixelName, privacy: .public) \(params, privacy: .public)")
onComplete(error)
}
}
Expand Down
2 changes: 2 additions & 0 deletions Core/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ extension Pixel {
case privacyProSubscriptionCookieRefreshedWithAccessToken
case privacyProSubscriptionCookieRefreshedWithEmptyValue
case privacyProSubscriptionCookieFailedToSetSubscriptionCookie
case privacyProDeadTokenDetected

// MARK: Pixel Experiment
case pixelExperimentEnrollment
Expand Down Expand Up @@ -1712,6 +1713,7 @@ extension Pixel.Event {
case .privacyProSubscriptionCookieRefreshedWithAccessToken: return "m_privacy-pro_subscription-cookie-refreshed_with_access_token"
case .privacyProSubscriptionCookieRefreshedWithEmptyValue: return "m_privacy-pro_subscription-cookie-refreshed_with_empty_value"
case .privacyProSubscriptionCookieFailedToSetSubscriptionCookie: return "m_privacy-pro_subscription-cookie-failed_to_set_subscription_cookie"
case .privacyProDeadTokenDetected: return "m_privacy-pro_dead_token_detected"

// MARK: Pixel Experiment
case .pixelExperimentEnrollment: return "pixel_experiment_enrollment"
Expand Down
42 changes: 17 additions & 25 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/DuckDuckGo/BrowserServicesKit",
"state" : {
"revision" : "e8654e1a51bd20fa8fa234a4155a9aec37411ed1",
"version" : "221.1.0"
"branch" : "fcappelli/subscription_oauth_api_v2",
"revision" : "50d4d64cba660ee779d987585b6c866d5b710057"
}
},
{
Expand Down Expand Up @@ -90,6 +90,15 @@
"version" : "2.0.0"
}
},
{
"identity" : "jwt-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/jwt-kit.git",
"state" : {
"revision" : "c2595b9ad7f512d7f334830b4df1fed6e917946a",
"version" : "4.13.4"
}
},
{
"identity" : "kingfisher",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -144,6 +153,24 @@
"version" : "1.4.0"
}
},
{
"identity" : "swift-asn1",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-asn1.git",
"state" : {
"revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6",
"version" : "1.3.0"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "ff0f781cf7c6a22d52957e50b104f5768b50c779",
"version" : "3.10.0"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/xcshareddata/xcschemes/Core.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
wasCreatedForAppExtension = "YES"
version = "2.0">
<BuildAction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
wasCreatedForAppExtension = "YES"
version = "2.0">
<BuildAction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
wasCreatedForAppExtension = "YES"
version = "2.0">
<BuildAction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
wasCreatedForAppExtension = "YES"
version = "2.0">
<BuildAction
Expand Down
36 changes: 23 additions & 13 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import os.log
@MainActor
private lazy var vpnWorkaround: VPNRedditSessionWorkaround = {
return VPNRedditSessionWorkaround(
accountManager: AppDependencyProvider.shared.accountManager,
subscriptionManager: AppDependencyProvider.shared.subscriptionManager,
tunnelController: AppDependencyProvider.shared.networkProtectionTunnelController
)
}()
Expand Down Expand Up @@ -93,6 +93,7 @@ import os.log
private var subscriptionCookieManager: SubscriptionCookieManaging!
private var subscriptionCookieManagerFeatureFlagCancellable: AnyCancellable?
var privacyProDataReporter: PrivacyProDataReporting!
private var tokenBackgroundRefreshTask: TokenBackgroundRefreshTask?

// MARK: - Feature specific app event handlers

Expand All @@ -103,10 +104,6 @@ import os.log
@UserDefaultsWrapper(key: .privacyConfigCustomURL, defaultValue: nil)
private var privacyConfigCustomURL: String?

var accountManager: AccountManager {
AppDependencyProvider.shared.accountManager
}

@UserDefaultsWrapper(key: .didCrashDuringCrashHandlersSetUp, defaultValue: false)
private var didCrashDuringCrashHandlersSetUp: Bool

Expand Down Expand Up @@ -320,7 +317,11 @@ import os.log

let url = URL.pixelUrl(forPixelNamed: pixelName)
let apiHeaders = APIRequestV2.HeadersV2(additionalHeaders: headers)
let request = APIRequestV2(url: url, method: .get, queryItems: parameters, headers: apiHeaders)
guard let request = APIRequestV2(url: url, method: .get, queryItems: parameters, headers: apiHeaders) else {
onComplete(false, nil)
return
}

Task {
do {
_ = try await DefaultAPIService().fetch(request: request)
Expand Down Expand Up @@ -454,6 +455,9 @@ import os.log
// Having both in `didBecomeActive` can sometimes cause the exception when running on a physical device, so registration happens here.
AppConfigurationFetch.registerBackgroundRefreshTaskHandler()

tokenBackgroundRefreshTask = TokenBackgroundRefreshTask(subscriptionManager: AppDependencyProvider.shared.subscriptionManager)
tokenBackgroundRefreshTask?.registerBackgroundRefreshTaskHandler()

UNUserNotificationCenter.current().delegate = self

window?.windowScene?.screenshotService?.delegate = self
Expand Down Expand Up @@ -714,7 +718,7 @@ import os.log
}
}

AppDependencyProvider.shared.subscriptionManager.refreshCachedSubscriptionAndEntitlements { isSubscriptionActive in
AppDependencyProvider.shared.subscriptionManager.refreshCachedSubscription { isSubscriptionActive in
if isSubscriptionActive {
DailyPixel.fire(pixel: .privacyProSubscriptionActive)
}
Expand All @@ -736,7 +740,8 @@ import os.log

private func stopAndRemoveVPNIfNotAuthenticated() async {
// Only remove the VPN if the user is not authenticated, and it's installed:
guard !accountManager.isUserAuthenticated, await AppDependencyProvider.shared.networkProtectionTunnelController.isInstalled else {
guard !AppDependencyProvider.shared.subscriptionManager.isUserAuthenticated,
await AppDependencyProvider.shared.networkProtectionTunnelController.isInstalled else {
return
}

Expand Down Expand Up @@ -846,6 +851,7 @@ import os.log
suspendSync()
syncDataProviders.bookmarksAdapter.cancelFaviconsFetching(application)
privacyProDataReporter.saveApplicationLastSessionEnded()
tokenBackgroundRefreshTask?.scheduleTask()
resetAppStartTime()
}

Expand Down Expand Up @@ -1150,7 +1156,7 @@ import os.log
return
}

if case .success(true) = await accountManager.hasEntitlement(forProductName: .networkProtection, cachePolicy: .returnCacheDataDontLoad) {
if await AppDependencyProvider.shared.subscriptionManager.isFeatureActive(.networkProtection) {
let items = [
UIApplicationShortcutItem(type: ShortcutKey.openVPNSettings,
localizedTitle: UserText.netPOpenVPNQuickAction,
Expand Down Expand Up @@ -1226,13 +1232,17 @@ extension AppDelegate: UNUserNotificationCenterDelegate {

completionHandler()
}

func presentNetworkProtectionStatusSettingsModal() {
Task {
if case .success(let hasEntitlements) = await accountManager.hasEntitlement(forProductName: .networkProtection), hasEntitlements {
(window?.rootViewController as? MainViewController)?.segueToVPN()
if await AppDependencyProvider.shared.subscriptionManager.isFeatureActive(.networkProtection) {
Task { @MainActor in
(window?.rootViewController as? MainViewController)?.segueToVPN()
}
} else {
(window?.rootViewController as? MainViewController)?.segueToPrivacyPro()
Task { @MainActor in
(window?.rootViewController as? MainViewController)?.segueToPrivacyPro()
}
}
}
}
Expand Down
Loading