From a1b40bfc47b3dc849ed775e63d9bfcaed445a57b Mon Sep 17 00:00:00 2001 From: Tobias Hagemann Date: Thu, 12 Sep 2024 10:39:46 +0200 Subject: [PATCH 1/3] Fixed SwiftLint warnings --- .../Crypto/VaultFormat6/VaultFormat6ShortenedNameCache.swift | 2 +- .../Crypto/VaultFormat7/VaultFormat7ShortenedNameCache.swift | 2 +- Sources/CryptomatorCloudAccess/Logging/HTTPDebugLogger.swift | 2 -- Tests/CryptomatorCloudAccessTests/Crypto/CryptorMock.swift | 2 +- Tests/CryptomatorCloudAccessTests/Crypto/VaultConfigTests.swift | 2 +- 5 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Sources/CryptomatorCloudAccess/Crypto/VaultFormat6/VaultFormat6ShortenedNameCache.swift b/Sources/CryptomatorCloudAccess/Crypto/VaultFormat6/VaultFormat6ShortenedNameCache.swift index f90486f..66544b5 100644 --- a/Sources/CryptomatorCloudAccess/Crypto/VaultFormat6/VaultFormat6ShortenedNameCache.swift +++ b/Sources/CryptomatorCloudAccess/Crypto/VaultFormat6/VaultFormat6ShortenedNameCache.swift @@ -121,7 +121,7 @@ class VaultFormat6ShortenedNameCache { return Promise(originalName) } else { return loadLngFile(shortenedName).then { data -> String in - let originalName = String(decoding: data, as: UTF8.self) + let originalName = String(data: data, encoding: .utf8)! try? self.addToCache(shortenedName, originalName: originalName) return originalName } diff --git a/Sources/CryptomatorCloudAccess/Crypto/VaultFormat7/VaultFormat7ShortenedNameCache.swift b/Sources/CryptomatorCloudAccess/Crypto/VaultFormat7/VaultFormat7ShortenedNameCache.swift index c4ce566..a0ce300 100644 --- a/Sources/CryptomatorCloudAccess/Crypto/VaultFormat7/VaultFormat7ShortenedNameCache.swift +++ b/Sources/CryptomatorCloudAccess/Crypto/VaultFormat7/VaultFormat7ShortenedNameCache.swift @@ -181,7 +181,7 @@ class VaultFormat7ShortenedNameCache { return Promise(originalName) } else { return loadNameC9S(c9sDirPath).then { data -> String in - let originalName = String(decoding: data, as: UTF8.self) + let originalName = String(data: data, encoding: .utf8)! try? self.addToCache(shortenedName, originalName: originalName) return originalName } diff --git a/Sources/CryptomatorCloudAccess/Logging/HTTPDebugLogger.swift b/Sources/CryptomatorCloudAccess/Logging/HTTPDebugLogger.swift index 5d375da..e7f60d3 100644 --- a/Sources/CryptomatorCloudAccess/Logging/HTTPDebugLogger.swift +++ b/Sources/CryptomatorCloudAccess/Logging/HTTPDebugLogger.swift @@ -42,7 +42,6 @@ enum HTTPDebugLogger { guard let bodyData = request.httpBody else { return .none } - // swiftlint:disable:next non_optional_string_data_conversion if let body = String(data: bodyData, encoding: .utf8) { if body.isEmpty { return .empty @@ -103,7 +102,6 @@ enum HTTPDebugLogger { if let contentEncodingHeaderField = response.value(forHTTPHeaderField: "Content-Encoding"), contentEncodingHeaderField.caseInsensitiveCompare("identity") != .orderedSame { return .encoded } - // swiftlint:disable:next non_optional_string_data_conversion if let body = String(data: bodyData, encoding: .utf8) { if body.isEmpty { return .empty diff --git a/Tests/CryptomatorCloudAccessTests/Crypto/CryptorMock.swift b/Tests/CryptomatorCloudAccessTests/Crypto/CryptorMock.swift index 3d8b888..88704f1 100644 --- a/Tests/CryptomatorCloudAccessTests/Crypto/CryptorMock.swift +++ b/Tests/CryptomatorCloudAccessTests/Crypto/CryptorMock.swift @@ -55,7 +55,7 @@ public class CryptorMock: Cryptor { } override public func encryptDirId(_ dirId: Data) throws -> String { - if let dirId = dirIds[String(decoding: dirId, as: UTF8.self)] { + if let dirId = dirIds[String(data: dirId, encoding: .utf8)!] { return dirId } else { return "99ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" diff --git a/Tests/CryptomatorCloudAccessTests/Crypto/VaultConfigTests.swift b/Tests/CryptomatorCloudAccessTests/Crypto/VaultConfigTests.swift index 2ba2624..82a4671 100644 --- a/Tests/CryptomatorCloudAccessTests/Crypto/VaultConfigTests.swift +++ b/Tests/CryptomatorCloudAccessTests/Crypto/VaultConfigTests.swift @@ -86,7 +86,7 @@ class VaultConfigTests: XCTestCase { let vaultConfig = VaultConfig(id: "ABB9F673-F3E8-41A7-A43B-D29F5DA65068", format: 8, cipherCombo: .sivCtrMac, shorteningThreshold: 220) let rawKey = [UInt8](repeating: 0x55, count: 64) let token = try vaultConfig.toToken(keyId: "masterkeyfile:masterkey.cryptomator", rawKey: rawKey) - let tokenComponents = String(decoding: token, as: UTF8.self).split(separator: ".") + let tokenComponents = String(data: token, encoding: .utf8)!.split(separator: ".") // check header let header: [String: String] = try decodeTokenComponent(String(tokenComponents[0])) XCTAssertEqual(["typ": "JWT", "alg": "HS256", "kid": "masterkeyfile:masterkey.cryptomator"], header) From aedecd161ac80909d821d120d0a1d03439f9145c Mon Sep 17 00:00:00 2001 From: Tobias Hagemann Date: Thu, 12 Sep 2024 12:03:21 +0200 Subject: [PATCH 2/3] Fixed file upload in OneDrive --- .../project.pbxproj | 4 ++++ ...SClientFactory+UnauthenticatedClient.swift | 20 +++++++++++++++++++ .../OneDrive/OneDriveCloudProvider.swift | 6 +++++- 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Sources/CryptomatorCloudAccess/OneDrive/MSClientFactory+UnauthenticatedClient.swift diff --git a/CryptomatorCloudAccess.xcodeproj/project.pbxproj b/CryptomatorCloudAccess.xcodeproj/project.pbxproj index ff3fe7d..0b0d5b7 100644 --- a/CryptomatorCloudAccess.xcodeproj/project.pbxproj +++ b/CryptomatorCloudAccess.xcodeproj/project.pbxproj @@ -153,6 +153,7 @@ 748BD4CC24B4D3820001CA8C /* Date+RFC822.swift in Sources */ = {isa = PBXBuildFile; fileRef = 748BD4CB24B4D3820001CA8C /* Date+RFC822.swift */; }; 7494505F24BC5C3300149816 /* PropfindResponseParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7494505E24BC5C3300149816 /* PropfindResponseParserTests.swift */; }; 7494B06D249CEAC500430610 /* VaultFormat7ShorteningProviderDecoratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7494B06C249CEAC500430610 /* VaultFormat7ShorteningProviderDecoratorTests.swift */; }; + 749D91152C92EFA600FAD1A4 /* MSClientFactory+UnauthenticatedClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749D91142C92EFA600FAD1A4 /* MSClientFactory+UnauthenticatedClient.swift */; }; 749F59A52BD1211500AB299A /* PCloudSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749F59A42BD1211500AB299A /* PCloudSetup.swift */; }; 74B321E42923F979008E7C01 /* HTTPDebugLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74B321E32923F979008E7C01 /* HTTPDebugLogger.swift */; }; 74C0FB2729B209B6008EF811 /* S3Authenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74C0FB2629B209B6008EF811 /* S3Authenticator.swift */; }; @@ -357,6 +358,7 @@ 748BD4CB24B4D3820001CA8C /* Date+RFC822.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Date+RFC822.swift"; sourceTree = ""; }; 7494505E24BC5C3300149816 /* PropfindResponseParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropfindResponseParserTests.swift; sourceTree = ""; }; 7494B06C249CEAC500430610 /* VaultFormat7ShorteningProviderDecoratorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VaultFormat7ShorteningProviderDecoratorTests.swift; sourceTree = ""; }; + 749D91142C92EFA600FAD1A4 /* MSClientFactory+UnauthenticatedClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MSClientFactory+UnauthenticatedClient.swift"; sourceTree = ""; }; 749F59A42BD1211500AB299A /* PCloudSetup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PCloudSetup.swift; sourceTree = ""; }; 74B321E32923F979008E7C01 /* HTTPDebugLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPDebugLogger.swift; sourceTree = ""; }; 74C0FB2629B209B6008EF811 /* S3Authenticator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = S3Authenticator.swift; sourceTree = ""; }; @@ -530,6 +532,7 @@ 4A1A11692629A53300DAF62F /* OneDrive */ = { isa = PBXGroup; children = ( + 749D91142C92EFA600FAD1A4 /* MSClientFactory+UnauthenticatedClient.swift */, 4AD55338263ABA4200126046 /* MSGraphDriveItem+CloudItemType.swift */, 4A3896CD2632D65B00BA3E88 /* OneDriveAuthenticationProvider.swift */, 4A1A11782629ACD500DAF62F /* OneDriveAuthenticator.swift */, @@ -1332,6 +1335,7 @@ 9ECEC0AF246ED1FB00151299 /* DirectoryIdCache.swift in Sources */, 4A77390D286DB5A00006B3C3 /* AWSS3TransferUtility+ForegroundSession.swift in Sources */, 745949E225F22F4D00B7B28C /* VaultFormat8ProviderDecorator.swift in Sources */, + 749D91152C92EFA600FAD1A4 /* MSClientFactory+UnauthenticatedClient.swift in Sources */, 745949E625F2326D00B7B28C /* VaultFormat8ShorteningProviderDecorator.swift in Sources */, 4A628EB8252F610C0097DE94 /* URLSession+Promises.swift in Sources */, 4A567B692615D64E002C4D82 /* GoogleDriveSetup.swift in Sources */, diff --git a/Sources/CryptomatorCloudAccess/OneDrive/MSClientFactory+UnauthenticatedClient.swift b/Sources/CryptomatorCloudAccess/OneDrive/MSClientFactory+UnauthenticatedClient.swift new file mode 100644 index 0000000..2474138 --- /dev/null +++ b/Sources/CryptomatorCloudAccess/OneDrive/MSClientFactory+UnauthenticatedClient.swift @@ -0,0 +1,20 @@ +// +// MSClientFactory+UnauthenticatedClient.swift +// CryptomatorCloudAccess +// +// Created by Tobias Hagemann on 12.09.24. +// Copyright © 2024 Skymatic GmbH. All rights reserved. +// + +import MSGraphClientSDK + +extension MSClientFactory { + static func createUnauthenticatedHTTPClient(with urlSessionConfiguration: URLSessionConfiguration) -> MSHTTPClient { + let redirectHandler = MSMiddlewareFactory.createMiddleware(.redirect) as MSGraphMiddleware + let retryHandler = MSMiddlewareFactory.createMiddleware(.retry) as MSGraphMiddleware + let sessionManager = MSURLSessionManager(sessionConfiguration: urlSessionConfiguration) + redirectHandler.setNext(retryHandler) + retryHandler.setNext(sessionManager) + return MSClientFactory.createHTTPClient(with: redirectHandler) + } +} diff --git a/Sources/CryptomatorCloudAccess/OneDrive/OneDriveCloudProvider.swift b/Sources/CryptomatorCloudAccess/OneDrive/OneDriveCloudProvider.swift index a2499fd..11bffed 100644 --- a/Sources/CryptomatorCloudAccess/OneDrive/OneDriveCloudProvider.swift +++ b/Sources/CryptomatorCloudAccess/OneDrive/OneDriveCloudProvider.swift @@ -15,12 +15,14 @@ public class OneDriveCloudProvider: CloudProvider { private static let maxUploadFileChunkLength = 16 * 320 * 1024 // 5MiB private let client: MSHTTPClient + private let unauthenticatedClient: MSHTTPClient private let identifierCache: OneDriveIdentifierCache private let tmpDirURL: URL private let maxPageSize: Int init(credential: OneDriveCredential, maxPageSize: Int = .max, urlSessionConfiguration: URLSessionConfiguration) throws { self.client = MSClientFactory.createHTTPClient(with: credential.authProvider, andSessionConfiguration: urlSessionConfiguration) + self.unauthenticatedClient = MSClientFactory.createUnauthenticatedHTTPClient(with: urlSessionConfiguration) self.identifierCache = try OneDriveIdentifierCache() self.tmpDirURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true) self.maxPageSize = min(max(1, maxPageSize), 1000) @@ -483,7 +485,9 @@ public class OneDriveCloudProvider: CloudProvider { private func executeMSURLSessionUploadTask(with request: NSMutableURLRequest, localURL: URL) -> Promise<(Data, HTTPURLResponse)> { HTTPDebugLogger.logRequest(request as URLRequest) return Promise<(Data, HTTPURLResponse)> { fulfill, reject in - let task = MSURLSessionUploadTask(request: request, fromFile: localURL, client: self.client) { data, response, error in + // We have to use an unauthenticated client, because the PUT request shouldn't contain an Authorization header. + // https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0#remarks + let task = MSURLSessionUploadTask(request: request, fromFile: localURL, client: self.unauthenticatedClient) { data, response, error in if let response = response { HTTPDebugLogger.logResponse(response, with: data, or: nil) } From 3356312611d9801b607fa55896333b5993aabfe4 Mon Sep 17 00:00:00 2001 From: Tobias Hagemann Date: Thu, 12 Sep 2024 12:07:02 +0200 Subject: [PATCH 3/3] Updated dependencies --- CryptomatorCloudAccess.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CryptomatorCloudAccess.xcodeproj/project.pbxproj b/CryptomatorCloudAccess.xcodeproj/project.pbxproj index 0b0d5b7..cf1314d 100644 --- a/CryptomatorCloudAccess.xcodeproj/project.pbxproj +++ b/CryptomatorCloudAccess.xcodeproj/project.pbxproj @@ -1893,7 +1893,7 @@ repositoryURL = "https://github.com/box/box-swift-sdk-gen.git"; requirement = { kind = upToNextMinorVersion; - minimumVersion = 0.3.0; + minimumVersion = 0.5.0; }; }; /* End XCRemoteSwiftPackageReference section */ diff --git a/CryptomatorCloudAccess.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/CryptomatorCloudAccess.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index e883bfa..baadeef 100644 --- a/CryptomatorCloudAccess.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/CryptomatorCloudAccess.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -33,8 +33,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/box/box-swift-sdk-gen.git", "state" : { - "revision" : "7b2e3963a4f9d7c608ba9176fa3c32d260599906", - "version" : "0.3.1" + "revision" : "a6b5793ab4053f9b55b840245ad07c9e3fdae344", + "version" : "0.5.0" } }, { @@ -141,8 +141,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pCloud/pcloud-sdk-swift.git", "state" : { - "revision" : "6da4ca6bb4e7068145d9325988e29862d26300ba", - "version" : "3.2.0" + "revision" : "ad1a7d8b3a59f12185d7bc89ff7a1b8c087ed0c0", + "version" : "3.2.2" } }, {