Skip to content

Commit

Permalink
Fixed unauthorized error handling, fixed Box integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobihagemann committed Aug 21, 2024
1 parent 423e085 commit b0b45c1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CryptomatorCloudAccess.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
748A42B824AA231D00DEB6D0 /* WebDAVAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 748A42B724AA231D00DEB6D0 /* WebDAVAuthenticator.swift */; };
748A42BA24AA34F300DEB6D0 /* WebDAVCredential.swift in Sources */ = {isa = PBXBuildFile; fileRef = 748A42B924AA34F300DEB6D0 /* WebDAVCredential.swift */; };
748A42C024AB424500DEB6D0 /* WebDAVClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 748A42BF24AB424500DEB6D0 /* WebDAVClient.swift */; };
748BAD5B2C76146B008534E1 /* BoxAuthenticationMock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 748BAD5A2C76146B008534E1 /* BoxAuthenticationMock.swift */; };
748BD4CA24B4B1D50001CA8C /* PropfindResponseParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 748BD4C924B4B1D50001CA8C /* PropfindResponseParser.swift */; };
748BD4CC24B4D3820001CA8C /* Date+RFC822.swift in Sources */ = {isa = PBXBuildFile; fileRef = 748BD4CB24B4D3820001CA8C /* Date+RFC822.swift */; };
7494505F24BC5C3300149816 /* PropfindResponseParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7494505E24BC5C3300149816 /* PropfindResponseParserTests.swift */; };
Expand Down Expand Up @@ -351,6 +352,7 @@
748A42B724AA231D00DEB6D0 /* WebDAVAuthenticator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebDAVAuthenticator.swift; sourceTree = "<group>"; };
748A42B924AA34F300DEB6D0 /* WebDAVCredential.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebDAVCredential.swift; sourceTree = "<group>"; };
748A42BF24AB424500DEB6D0 /* WebDAVClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebDAVClient.swift; sourceTree = "<group>"; };
748BAD5A2C76146B008534E1 /* BoxAuthenticationMock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoxAuthenticationMock.swift; sourceTree = "<group>"; };
748BD4C924B4B1D50001CA8C /* PropfindResponseParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropfindResponseParser.swift; sourceTree = "<group>"; };
748BD4CB24B4D3820001CA8C /* Date+RFC822.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+RFC822.swift"; sourceTree = "<group>"; };
7494505E24BC5C3300149816 /* PropfindResponseParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropfindResponseParserTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1014,6 +1016,7 @@
B3D513942BA9A37A00DE0D36 /* Box */ = {
isa = PBXGroup;
children = (
748BAD5A2C76146B008534E1 /* BoxAuthenticationMock.swift */,
B3D513952BA9A3BB00DE0D36 /* BoxCloudProviderIntegrationTests.swift */,
B3408AC72BCD32CA005271D2 /* BoxCredentialMock.swift */,
);
Expand Down Expand Up @@ -1417,6 +1420,7 @@
4ACA64072615FF9800D19304 /* WebDAVCloudProviderIntegrationTests.swift in Sources */,
4ACA63E72615FF6400D19304 /* VaultFormat7WebDAVIntegrationTests.swift in Sources */,
7470C54D2656A44600E361B8 /* MSALPublicClientApplicationStub.swift in Sources */,
748BAD5B2C76146B008534E1 /* BoxAuthenticationMock.swift in Sources */,
4ACA63A92615FE5C00D19304 /* IntegrationTestError.swift in Sources */,
4ACA63B12615FE8000D19304 /* CloudAccessIntegrationTestWithAuthentication.swift in Sources */,
4ACA63E92615FF6400D19304 /* VaultFormat7LocalFileSystemIntegrationTests.swift in Sources */,
Expand Down
54 changes: 23 additions & 31 deletions Sources/CryptomatorCloudAccess/Box/BoxCloudProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

import BoxSdkGen
import CryptoKit
import Foundation
import Promises

Expand Down Expand Up @@ -142,11 +141,9 @@ public class BoxCloudProvider: CloudProvider {
CloudAccessDDLogDebug("BoxCloudProvider: fetchFileMetadata(for: \(item.identifier)) received file: \(file)")
let cloudItemMetadata = convertToCloudItemMetadata(file, at: item.cloudPath)
pendingPromise.fulfill(cloudItemMetadata)
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: fetchFileMetadata(for: \(item.identifier)) error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -162,11 +159,9 @@ public class BoxCloudProvider: CloudProvider {
CloudAccessDDLogDebug("BoxCloudProvider: fetchFolderMetadata(for: \(item.identifier)) received folder: \(folder)")
let cloudItemMetadata = convertToCloudItemMetadata(folder, at: item.cloudPath)
pendingPromise.fulfill(cloudItemMetadata)
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: fetchFolderMetadata(for: \(item.identifier)) error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -188,7 +183,7 @@ public class BoxCloudProvider: CloudProvider {
pendingPromise.reject(CloudProviderError.pageTokenInvalid)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: fetchItemList(for: \(folderItem.identifier), pageToken: \(pageToken ?? "nil")) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -207,7 +202,7 @@ public class BoxCloudProvider: CloudProvider {
pendingPromise.fulfill(())
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: downloadFile(for: \(item.identifier), to: \(localURL)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand Down Expand Up @@ -269,7 +264,7 @@ public class BoxCloudProvider: CloudProvider {
pendingPromise.fulfill(cloudItemMetadata)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: uploadSmallNewFile(for: \(parentItem.identifier), to: \(cloudPath)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -295,7 +290,7 @@ public class BoxCloudProvider: CloudProvider {
pendingPromise.fulfill(cloudItemMetadata)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: uploadSmallExistingFile(for: \(existingItem.identifier), to: \(cloudPath)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -311,7 +306,7 @@ public class BoxCloudProvider: CloudProvider {
pendingPromise.fulfill(cloudItemMetadata)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: uploadLargeNewFile(for: \(parentItem.identifier), to: \(cloudPath), fileSize: \(fileSize)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -327,7 +322,7 @@ public class BoxCloudProvider: CloudProvider {
pendingPromise.fulfill(cloudItemMetadata)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: uploadLargeExistingFile(for: \(existingItem.identifier), to: \(cloudPath), fileSize: \(fileSize)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand Down Expand Up @@ -364,11 +359,9 @@ public class BoxCloudProvider: CloudProvider {
let item = BoxItem(cloudPath: cloudPath, folder: folder)
try self.identifierCache.addOrUpdate(item)
pendingPromise.fulfill(())
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: createFolder(for: \(parentItem.identifier), with: \(name)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -386,13 +379,11 @@ public class BoxCloudProvider: CloudProvider {
CloudAccessDDLogDebug("BoxCloudProvider: deleteFile(for: \(item.identifier)) succeeded")
try self.identifierCache.invalidate(item)
pendingPromise.fulfill(())
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch let error as BoxAPIError where error.responseInfo.statusCode == 404 {
pendingPromise.reject(CloudProviderError.itemNotFound)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: deleteFile(for: \(item.identifier)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -411,13 +402,11 @@ public class BoxCloudProvider: CloudProvider {
CloudAccessDDLogDebug("BoxCloudProvider: deleteFolder(for: \(item.identifier)) succeeded")
try self.identifierCache.invalidate(item)
pendingPromise.fulfill(())
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch let error as BoxAPIError where error.responseInfo.statusCode == 404 {
pendingPromise.reject(CloudProviderError.itemNotFound)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: deleteFolder(for: \(item.identifier)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -438,11 +427,9 @@ public class BoxCloudProvider: CloudProvider {
let targetItem = BoxItem(cloudPath: targetCloudPath, file: file)
try self.identifierCache.addOrUpdate(targetItem)
pendingPromise.fulfill(())
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: moveFile(from: \(sourceItem.identifier), toParent: \(targetParentItem.identifier), targetCloudPath: \(targetCloudPath.path)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand All @@ -463,11 +450,9 @@ public class BoxCloudProvider: CloudProvider {
let newItem = BoxItem(cloudPath: targetCloudPath, folder: folder)
try self.identifierCache.addOrUpdate(newItem)
pendingPromise.fulfill(())
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: moveFolder(from: \(sourceItem.identifier), toParent: \(targetParentItem.identifier), targetCloudPath: \(targetCloudPath.path)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand Down Expand Up @@ -525,11 +510,9 @@ public class BoxCloudProvider: CloudProvider {
do {
let foundItem = try await findBoxItem(in: parentItem, withName: name, marker: nil)
pendingPromise.fulfill(foundItem)
} catch let error as BoxSDKError where error.message.contains("Developer token has expired") {
pendingPromise.reject(CloudProviderError.unauthorized)
} catch {
CloudAccessDDLogDebug("BoxCloudProvider: getBoxItem(for: \(name), withParentItem: \(parentItem.identifier)) failed with error: \(error.localizedDescription)")
pendingPromise.reject(error)
pendingPromise.reject(convertStandardError(error))
}
}
return pendingPromise
Expand Down Expand Up @@ -601,4 +584,13 @@ public class BoxCloudProvider: CloudProvider {
}
return CloudItemList(items: items, nextPageToken: folderItems.nextMarker)
}

private func convertStandardError(_ error: Error) -> Error {
switch error {
case let error as BoxAPIError where error.responseInfo.statusCode == 401:
return CloudProviderError.unauthorized
default:
return error
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// BoxAuthenticationMock.swift
// CryptomatorCloudAccessIntegrationTests
//
// Created by Tobias Hagemann on 21.08.24.
// Copyright © 2024 Skymatic GmbH. All rights reserved.
//

import BoxSdkGen
import Foundation

class BoxAuthenticationMock: Authentication {
func retrieveToken(networkSession: NetworkSession?) async throws -> AccessToken {
return AccessToken()
}

func refreshToken(networkSession: NetworkSession?) async throws -> AccessToken {
return AccessToken()
}

func retrieveAuthorizationHeader(networkSession: NetworkSession?) async throws -> String {
return ""
}

func revokeToken(networkSession: NetworkSession?) async throws {
// do nothing
}

func downscopeToken(scopes: [String], resource: String?, sharedLink: String?, networkSession: BoxSdkGen.NetworkSession?) async throws -> AccessToken {
return AccessToken()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class BoxCloudProviderIntegrationTests: CloudAccessIntegrationTestWithAuthentica
}

override func deauthenticate() -> Promise<Void> {
return credential.deauthenticate()
let invalidCredential = BoxInvalidCredentialMock()
// swiftlint:disable:next force_try
provider = try! BoxCloudProvider(credential: invalidCredential)
return Promise(())
}

override func createLimitedCloudProvider() throws -> CloudProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
// Copyright © 2024 Skymatic GmbH. All rights reserved.
//

import BoxSdkGen
import Foundation
import Promises
@testable import BoxSdkGen
#if canImport(CryptomatorCloudAccessCore)
@testable import CryptomatorCloudAccessCore
#else
Expand All @@ -24,3 +23,12 @@ class BoxCredentialMock: BoxCredential {
client = BoxClient(auth: auth)
}
}

class BoxInvalidCredentialMock: BoxCredential {
init() {
BoxSetup.constants = BoxSetup(clientId: IntegrationTestSecrets.boxClientId, clientSecret: IntegrationTestSecrets.boxClientSecret, sharedContainerIdentifier: "")
super.init(tokenStorage: InMemoryTokenStorage())
auth = BoxAuthenticationMock()
client = BoxClient(auth: auth)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Foundation
import ObjectiveDropboxOfficial
import Promises
#if canImport(CryptomatorCloudAccessCore)
@testable import CryptomatorCloudAccessCore
#else
Expand Down

0 comments on commit b0b45c1

Please sign in to comment.