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

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 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
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
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
name: Unit Tests

runs-on: macos-14-xlarge
timeout-minutes: 20
timeout-minutes: 15

outputs:
commit_author: ${{ steps.fetch_commit_author.outputs.commit_author }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Logger+Subscription.swift
// Logger+Pixel.swift
// 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 @@ -235,7 +235,7 @@
}

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 @@ -265,8 +265,8 @@
allowedQueryReservedCharacters: allowedQueryReservedCharacters,
headers: headers)
let request = APIRequest(configuration: configuration, urlSession: .session(useMainThreadCallbackQueue: true))
request.fetch { _, error in

Check warning on line 268 in Core/Pixel.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

'fetch(completion:)' is deprecated: Please use 'APIService' instead.

Check warning on line 268 in Core/Pixel.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'fetch(completion:)' is deprecated: Please use 'APIService' instead.

Check warning on line 268 in Core/Pixel.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'fetch(completion:)' is deprecated: Please use 'APIService' instead.
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 @@ -730,6 +730,7 @@ extension Pixel {
case privacyProSubscriptionCookieRefreshedWithAccessToken
case privacyProSubscriptionCookieRefreshedWithEmptyValue
case privacyProSubscriptionCookieFailedToSetSubscriptionCookie
case privacyProDeadTokenDetected

// MARK: Pixel Experiment
case pixelExperimentEnrollment
Expand Down Expand Up @@ -1572,6 +1573,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
135 changes: 74 additions & 61 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" : "948420e704ea4d9412a4fc3e2c2ab0d5ea5fe5d7",
"version" : "209.1.0"
"branch" : "fcappelli/subscription_oauth_api_v2",
"revision" : "aab97954107399096a9bbe64b6aff8eb49bc648b"
}
},
{
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" : "06dc63c6d8da54ee11ceb268cde1fa68161afc96",
"version" : "3.9.1"
}
},
{
"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
30 changes: 16 additions & 14 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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 @@ -91,6 +91,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 @@ -101,10 +102,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 @@ -378,6 +375,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 @@ -612,7 +612,7 @@ import os.log
}
}

AppDependencyProvider.shared.subscriptionManager.refreshCachedSubscriptionAndEntitlements { isSubscriptionActive in
AppDependencyProvider.shared.subscriptionManager.refreshCachedSubscription { isSubscriptionActive in
if isSubscriptionActive {
DailyPixel.fire(pixel: .privacyProSubscriptionActive)
}
Expand All @@ -634,7 +634,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 @@ -742,6 +743,7 @@ import os.log
suspendSync()
syncDataProviders.bookmarksAdapter.cancelFaviconsFetching(application)
privacyProDataReporter.saveApplicationLastSessionEnded()
tokenBackgroundRefreshTask?.scheduleTask()
}

private func suspendSync() {
Expand Down Expand Up @@ -1031,7 +1033,8 @@ import os.log
return
}

if case .success(true) = await accountManager.hasEntitlement(forProductName: .networkProtection, cachePolicy: .returnCacheDataDontLoad) {
let entitlements = AppDependencyProvider.shared.subscriptionManager.entitlements
if entitlements.contains(.networkProtection) {
let items = [
UIApplicationShortcutItem(type: ShortcutKey.openVPNSettings,
localizedTitle: UserText.netPOpenVPNQuickAction,
Expand Down Expand Up @@ -1109,12 +1112,11 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
}

func presentNetworkProtectionStatusSettingsModal() {
Task {
if case .success(let hasEntitlements) = await accountManager.hasEntitlement(forProductName: .networkProtection), hasEntitlements {
(window?.rootViewController as? MainViewController)?.segueToVPN()
} else {
(window?.rootViewController as? MainViewController)?.segueToPrivacyPro()
}
let entitlements = AppDependencyProvider.shared.subscriptionManager.entitlements
if entitlements.contains(.networkProtection) {
(window?.rootViewController as? MainViewController)?.segueToVPN()
} else {
(window?.rootViewController as? MainViewController)?.segueToPrivacyPro()
}
}

Expand Down
Loading
Loading