From 5ccd255e9c61d56c94a2dc99e4dbcfbab97f4fce Mon Sep 17 00:00:00 2001 From: Julian Locke Date: Sun, 10 Mar 2024 13:51:36 -0400 Subject: [PATCH 1/3] Don't assume that custom transport clients are authed. Allow custom transport clients on re-auth --- .../Handwritten/DropboxClientsManager.swift | 56 ++++++++++--------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift b/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift index a0897cf2..355fb0f1 100644 --- a/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift +++ b/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift @@ -56,15 +56,18 @@ public class DropboxClientsManager { ) DropboxOAuthManager.sharedOAuthManager = oAuthManager - setUpAuthorizedClient( - transportClient: transportClient, - backgroundTransportClient: backgroundTransportClient, - sessionConfiguration: nil, - backgroundSessionConfiguration: nil, - oAuthManager: oAuthManager, - oauthSetupIntent: oauthSetupIntent, - requestsToReconnect: requestsToReconnect - ) + if let token = token(for: oauthSetupIntent.userKind, using: oAuthManager) { + setUpAuthorizedClient( + token: token, + transportClient: transportClient, + backgroundTransportClient: backgroundTransportClient, + sessionConfiguration: nil, + backgroundSessionConfiguration: nil, + oAuthManager: oAuthManager, + oauthSetupIntent: oauthSetupIntent, + requestsToReconnect: requestsToReconnect + ) + } checkAccessibilityMigrationOneTime(oauthManager: oAuthManager) } @@ -83,15 +86,18 @@ public class DropboxClientsManager { ) DropboxOAuthManager.sharedOAuthManager = oAuthManager - setUpAuthorizedClient( - transportClient: nil, - backgroundTransportClient: nil, - sessionConfiguration: sessionConfiguration, - backgroundSessionConfiguration: backgroundSessionConfiguration, - oAuthManager: oAuthManager, - oauthSetupIntent: oauthSetupIntent, - requestsToReconnect: requestsToReconnect - ) + if let token = token(for: oauthSetupIntent.userKind, using: oAuthManager) { + setUpAuthorizedClient( + token: token, + transportClient: nil, + backgroundTransportClient: nil, + sessionConfiguration: sessionConfiguration, + backgroundSessionConfiguration: backgroundSessionConfiguration, + oAuthManager: oAuthManager, + oauthSetupIntent: oauthSetupIntent, + requestsToReconnect: requestsToReconnect + ) + } checkAccessibilityMigrationOneTime(oauthManager: oAuthManager) } @@ -106,6 +112,7 @@ public class DropboxClientsManager { } private static func setUpAuthorizedClient( + token: DropboxAccessToken, transportClient: DropboxTransportClient?, backgroundTransportClient: DropboxTransportClient?, sessionConfiguration: NetworkSessionConfiguration?, @@ -114,8 +121,6 @@ public class DropboxClientsManager { oauthSetupIntent: OAuthSetupContext, requestsToReconnect: RequestsToReconnect? ) { - let token = token(for: oauthSetupIntent.userKind, using: oAuthManager) - if oauthSetupIntent.isTeam { setupAuthorizedTeamClient(token, transportClient: transportClient, sessionConfiguration: sessionConfiguration) } else { @@ -136,11 +141,11 @@ public class DropboxClientsManager { } } - public static func reauthorizeClient(_ tokenUid: String, sessionConfiguration: NetworkSessionConfiguration? = nil) { + public static func reauthorizeClient(_ tokenUid: String, sessionConfiguration: NetworkSessionConfiguration? = nil, transportClient: DropboxTransportClient? = nil) { precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method") if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) { - setupAuthorizedClient(token, transportClient: nil, sessionConfiguration: sessionConfiguration) + setupAuthorizedClient(token, transportClient: transportClient, sessionConfiguration: sessionConfiguration) } checkAccessibilityMigrationOneTime(oauthManager: DropboxOAuthManager.sharedOAuthManager) } @@ -148,21 +153,22 @@ public class DropboxClientsManager { public static func reauthorizeBackgroundClient( _ tokenUid: String, sessionConfiguration: NetworkSessionConfiguration? = nil, + transportClient: DropboxTransportClient? = nil, requestsToReconnect: @escaping RequestsToReconnect ) { precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method") if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) { - setupAuthorizedBackgroundClient(token, transportClient: nil, sessionConfiguration: sessionConfiguration, requestsToReconnect: requestsToReconnect) + setupAuthorizedBackgroundClient(token, transportClient: transportClient, sessionConfiguration: sessionConfiguration, requestsToReconnect: requestsToReconnect) } checkAccessibilityMigrationOneTime(oauthManager: DropboxOAuthManager.sharedOAuthManager) } - public static func reauthorizeTeamClient(_ tokenUid: String, sessionConfiguration: NetworkSessionConfiguration? = nil) { + public static func reauthorizeTeamClient(_ tokenUid: String, sessionConfiguration: NetworkSessionConfiguration? = nil, transportClient: DropboxTransportClient? = nil) { precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method") if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) { - setupAuthorizedTeamClient(token, transportClient: nil, sessionConfiguration: sessionConfiguration) + setupAuthorizedTeamClient(token, transportClient: transportClient, sessionConfiguration: sessionConfiguration) } checkAccessibilityMigrationOneTime(oauthManager: DropboxOAuthManager.sharedOAuthManager) } From b2cec676c3ac6df4ab41687f8e1d438da95c728a Mon Sep 17 00:00:00 2001 From: Julian Locke Date: Mon, 11 Mar 2024 11:21:00 -0400 Subject: [PATCH 2/3] Add id-only flavor of handle redirect url --- .../Handwritten/DropboxClientsManager.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift b/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift index 355fb0f1..32de416f 100644 --- a/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift +++ b/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift @@ -361,6 +361,35 @@ public class DropboxClientsManager { }) } + /// Handle a redirect and automatically initialize the client and save the token. + /// + /// - parameters: + /// - url: The URL to attempt to handle. + /// - backgroundSessionIdentifier: The URLSession identifier to use for the background client + /// - sharedContainerIdentifier: The URLSessionConfiguration shared container identifier to use for the background client + /// - completion: The callback closure to receive auth result. + /// - returns: Whether the redirect URL can be handled. + /// + @discardableResult + public static func handleRedirectURL( + _ url: URL, + backgroundSessionIdentifier: String, + sharedContainerIdentifier: String? = nil, + completion: @escaping DropboxOAuthCompletion + ) -> Bool { + let backgroundNetworkSessionConfiguration = NetworkSessionConfiguration.background( + withIdentifier: backgroundSessionIdentifier, + sharedContainerIdentifier: sharedContainerIdentifier + ) + return handleRedirectURL( + url, + includeBackgroundClient: true, + backgroundSessionConfiguration: backgroundNetworkSessionConfiguration, + completion: completion + ) + } + + /// Handle a redirect and automatically initialize the client and save the token. /// /// - parameters: From 10e56324135dd9e1cca9754f01182d9fe0461e15 Mon Sep 17 00:00:00 2001 From: Julian Locke Date: Fri, 15 Mar 2024 12:10:30 -0400 Subject: [PATCH 3/3] Address PR feedback --- .../Shared/Handwritten/DropboxClientsManager.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift b/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift index 32de416f..4e158160 100644 --- a/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift +++ b/Source/SwiftyDropbox/Shared/Handwritten/DropboxClientsManager.swift @@ -141,7 +141,7 @@ public class DropboxClientsManager { } } - public static func reauthorizeClient(_ tokenUid: String, sessionConfiguration: NetworkSessionConfiguration? = nil, transportClient: DropboxTransportClient? = nil) { + public static func reauthorizeClient(_ tokenUid: String, transportClient: DropboxTransportClient? = nil, sessionConfiguration: NetworkSessionConfiguration? = nil) { precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method") if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) { @@ -152,8 +152,8 @@ public class DropboxClientsManager { public static func reauthorizeBackgroundClient( _ tokenUid: String, - sessionConfiguration: NetworkSessionConfiguration? = nil, transportClient: DropboxTransportClient? = nil, + sessionConfiguration: NetworkSessionConfiguration? = nil, requestsToReconnect: @escaping RequestsToReconnect ) { precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method") @@ -164,7 +164,7 @@ public class DropboxClientsManager { checkAccessibilityMigrationOneTime(oauthManager: DropboxOAuthManager.sharedOAuthManager) } - public static func reauthorizeTeamClient(_ tokenUid: String, sessionConfiguration: NetworkSessionConfiguration? = nil, transportClient: DropboxTransportClient? = nil) { + public static func reauthorizeTeamClient(_ tokenUid: String, transportClient: DropboxTransportClient? = nil, sessionConfiguration: NetworkSessionConfiguration? = nil) { precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method") if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) {