Skip to content

Commit

Permalink
Refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
tobihagemann committed Dec 31, 2024
1 parent 72a9fb0 commit b6fe6dd
Show file tree
Hide file tree
Showing 38 changed files with 415 additions and 333 deletions.
148 changes: 82 additions & 66 deletions CryptomatorCloudAccess.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
Identifier = "LocalFileSystemProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "PCloudCloudProviderIntegrationTests">
Expand All @@ -62,7 +62,7 @@
Identifier = "VaultFormat6LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6PCloudIntegrationTests">
Expand All @@ -83,7 +83,7 @@
Identifier = "VaultFormat7LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7PCloudIntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
Identifier = "LocalFileSystemProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "PCloudCloudProviderIntegrationTests">
Expand All @@ -62,7 +62,7 @@
Identifier = "VaultFormat6LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6PCloudIntegrationTests">
Expand All @@ -86,7 +86,7 @@
Identifier = "VaultFormat7LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7PCloudIntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
Identifier = "LocalFileSystemProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "PCloudCloudProviderIntegrationTests">
Expand All @@ -62,7 +62,7 @@
Identifier = "VaultFormat6LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6PCloudIntegrationTests">
Expand All @@ -83,7 +83,7 @@
Identifier = "VaultFormat7LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7PCloudIntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
Identifier = "LocalFileSystemProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "PCloudCloudProviderIntegrationTests">
Expand All @@ -62,7 +62,7 @@
Identifier = "VaultFormat6LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6PCloudIntegrationTests">
Expand All @@ -83,7 +83,7 @@
Identifier = "VaultFormat7LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7PCloudIntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
Identifier = "GoogleDriveCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "PCloudCloudProviderIntegrationTests">
Expand All @@ -62,7 +62,7 @@
Identifier = "VaultFormat6GoogleDriveIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6PCloudIntegrationTests">
Expand All @@ -83,7 +83,7 @@
Identifier = "VaultFormat7GoogleDriveIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7PCloudIntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
Identifier = "LocalFileSystemProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "S3CloudProviderIntegrationTests">
Expand All @@ -65,7 +65,7 @@
Identifier = "VaultFormat6LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6S3IntegrationTests">
Expand All @@ -86,7 +86,7 @@
Identifier = "VaultFormat7LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7S3IntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
Identifier = "LocalFileSystemProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "PCloudCloudProviderIntegrationTests">
Expand All @@ -65,7 +65,7 @@
Identifier = "VaultFormat6LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6PCloudIntegrationTests">
Expand All @@ -86,7 +86,7 @@
Identifier = "VaultFormat7LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7PCloudIntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
Identifier = "LocalFileSystemProviderIntegrationTests">
</Test>
<Test
Identifier = "OneDriveCloudProviderIntegrationTests">
Identifier = "MicrosoftGraphCloudProviderIntegrationTests">
</Test>
<Test
Identifier = "PCloudCloudProviderIntegrationTests">
Expand All @@ -68,7 +68,7 @@
Identifier = "VaultFormat6LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6OneDriveIntegrationTests">
Identifier = "VaultFormat6MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat6PCloudIntegrationTests">
Expand All @@ -89,7 +89,7 @@
Identifier = "VaultFormat7LocalFileSystemIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7OneDriveIntegrationTests">
Identifier = "VaultFormat7MicrosoftGraphIntegrationTests">
</Test>
<Test
Identifier = "VaultFormat7PCloudIntegrationTests">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MicrosoftGraphClientFactory+UnauthenticatedClient.swift
// MSClientFactory+UnauthenticatedClient.swift
// CryptomatorCloudAccess
//
// Created by Tobias Hagemann on 12.09.24.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// MicrosoftGraphDriveItem+CloudItemType.swift
// MSGraphDriveItem+CloudItemType.swift
// CryptomatorCloudAccess
//
// Created by Philipp Schmid on 29.04.21.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,32 @@ import CryptomatorCloudAccessCore
import MSAL
import Promises
import UIKit
import CryptomatorCommonCore

public enum MicrosoftGraphAuthenticatorError: Error {
case missingAccountIdentifier
}

public enum MicrosoftGraphAuthenticator {
public static func authenticate(from viewController: UIViewController, for providerType: CloudProviderType) -> Promise<MicrosoftGraphCredential> {
let scopes: [String]
let credentialType: MicrosoftGraphCredential.Type

switch providerType {
case .oneDrive:
scopes = OneDriveCredential.scopes
credentialType = OneDriveCredential.self
case .sharePoint:
scopes = SharePointCredential.scopes
credentialType = SharePointCredential.self
default:
return Promise(MicrosoftGraphAuthenticatorError.missingAccountIdentifier)
}

public class MicrosoftGraphAuthenticator {
public static func authenticate(from viewController: UIViewController, with scopes: [String]) -> Promise<MicrosoftGraphCredential> {
let webviewParameters = MSALWebviewParameters(authPresentationViewController: viewController)
let interactiveParameters = MSALInteractiveTokenParameters(scopes: scopes, webviewParameters: webviewParameters)
interactiveParameters.promptType = .login

return MicrosoftGraphSetup.constants.clientApplication.acquireToken(with: interactiveParameters).then { result -> MicrosoftGraphCredential in
guard let identifier = result.account.identifier else {
throw MicrosoftGraphAuthenticatorError.missingAccountIdentifier
}

return try credentialType.init(with: identifier)
return MicrosoftGraphCredential(identifier: identifier, scopes: scopes)
}
}
}

public static func authenticateForOneDrive(from viewController: UIViewController) -> Promise<MicrosoftGraphCredential> {
return authenticate(from: viewController, with: MicrosoftGraphScopes.oneDrive)
}

public static func authenticateForSharePoint(from viewController: UIViewController) -> Promise<MicrosoftGraphCredential> {
return authenticate(from: viewController, with: MicrosoftGraphScopes.sharePoint)
}
}

private extension MSALPublicClientApplication {
func acquireToken(with interactiveParameters: MSALInteractiveTokenParameters) -> Promise<MSALResult> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,31 @@ public class MicrosoftGraphCloudProvider: CloudProvider {

private let client: MSHTTPClient
private let unauthenticatedClient: MSHTTPClient
private let driveIdentifier: String?
private let identifierCache: MicrosoftGraphIdentifierCache
private let tmpDirURL: URL
private let maxPageSize: Int

init(credential: MicrosoftGraphCredential, maxPageSize: Int = .max, urlSessionConfiguration: URLSessionConfiguration, unauthenticatedURLSessionConfiguration: URLSessionConfiguration) throws {
init(credential: MicrosoftGraphCredential, driveIdentifier: String? = nil, maxPageSize: Int = .max, urlSessionConfiguration: URLSessionConfiguration, unauthenticatedURLSessionConfiguration: URLSessionConfiguration) throws {
self.client = MSClientFactory.createHTTPClient(with: credential.authProvider, andSessionConfiguration: urlSessionConfiguration)
self.unauthenticatedClient = MSClientFactory.createUnauthenticatedHTTPClient(with: unauthenticatedURLSessionConfiguration)
self.driveIdentifier = driveIdentifier
self.identifierCache = try MicrosoftGraphIdentifierCache()
self.tmpDirURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true)
self.maxPageSize = min(max(1, maxPageSize), 1000)
try FileManager.default.createDirectory(at: tmpDirURL, withIntermediateDirectories: true)
}

public convenience init(credential: MicrosoftGraphCredential, maxPageSize: Int = .max) throws {
try self.init(credential: credential, maxPageSize: maxPageSize, urlSessionConfiguration: .default, unauthenticatedURLSessionConfiguration: .default)
public convenience init(credential: MicrosoftGraphCredential, driveIdentifier: String? = nil, maxPageSize: Int = .max) throws {
try self.init(credential: credential, driveIdentifier: driveIdentifier, maxPageSize: maxPageSize, urlSessionConfiguration: .default, unauthenticatedURLSessionConfiguration: .default)
}

public static func withBackgroundSession(credential: MicrosoftGraphCredential, maxPageSize: Int = .max, sessionIdentifier: String) throws -> MicrosoftGraphCloudProvider {
public static func withBackgroundSession(credential: MicrosoftGraphCredential, driveIdentifier: String? = nil, maxPageSize: Int = .max, sessionIdentifier: String) throws -> MicrosoftGraphCloudProvider {
let configuration = URLSessionConfiguration.background(withIdentifier: sessionIdentifier)
configuration.sharedContainerIdentifier = MicrosoftGraphSetup.constants.sharedContainerIdentifier
let unauthenticatedConfiguration = URLSessionConfiguration.background(withIdentifier: "\(sessionIdentifier)_unauthenticated")
unauthenticatedConfiguration.sharedContainerIdentifier = MicrosoftGraphSetup.constants.sharedContainerIdentifier
return try MicrosoftGraphCloudProvider(credential: credential, maxPageSize: maxPageSize, urlSessionConfiguration: configuration, unauthenticatedURLSessionConfiguration: unauthenticatedConfiguration)
return try MicrosoftGraphCloudProvider(credential: credential, driveIdentifier: driveIdentifier, maxPageSize: maxPageSize, urlSessionConfiguration: configuration, unauthenticatedURLSessionConfiguration: unauthenticatedConfiguration)
}

deinit {
Expand Down Expand Up @@ -322,7 +324,7 @@ public class MicrosoftGraphCloudProvider: CloudProvider {
// MARK: - Requests

func requestURLString(for item: MicrosoftGraphItem) -> String {
if let driveIdentifier = item.driveIdentifier {
if let driveIdentifier = item.driveIdentifier ?? driveIdentifier {
return "\(MSGraphBaseURL)/drives/\(driveIdentifier)/items/\(item.identifier)"
} else {
return "\(MSGraphBaseURL)/me/drive/items/\(item.identifier)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ public enum MicrosoftGraphCredentialError: Error {

public class MicrosoftGraphCredential {
public let identifier: String
public class var scopes: [String] {
return ["https://graph.microsoft.com/Files.ReadWrite"]
}

public let scopes: [String]
let authProvider: MSAuthenticationProvider
private let clientApplication: MSALPublicClientApplication

public required init(with identifier: String) throws {
public convenience init(identifier: String, scopes: [String]) {
let authProvider = MicrosoftGraphAuthenticationProvider(identifier: identifier, clientApplication: MicrosoftGraphSetup.constants.clientApplication, scopes: scopes)
self.init(identifier: identifier, scopes: scopes, authProvider: authProvider, clientApplication: MicrosoftGraphSetup.constants.clientApplication)
}

init(identifier: String, scopes: [String], authProvider: MSAuthenticationProvider, clientApplication: MSALPublicClientApplication) {
self.identifier = identifier
let clientApplication = MicrosoftGraphSetup.constants.clientApplication
let authProvider = MicrosoftGraphAuthenticationProvider(identifier: identifier, clientApplication: clientApplication, scopes: Self.scopes)
self.clientApplication = clientApplication
self.scopes = scopes
self.authProvider = authProvider
self.clientApplication = clientApplication
}

public func getUsername() throws -> String {
Expand All @@ -46,3 +47,12 @@ public class MicrosoftGraphCredential {
}
}

public extension MicrosoftGraphCredential {
static func createForOneDrive(with identifier: String) -> MicrosoftGraphCredential {
return MicrosoftGraphCredential(identifier: identifier, scopes: MicrosoftGraphScopes.oneDrive)
}

static func createForSharePoint(with identifier: String) -> MicrosoftGraphCredential {
return MicrosoftGraphCredential(identifier: identifier, scopes: MicrosoftGraphScopes.sharePoint)
}
}
Loading

0 comments on commit b6fe6dd

Please sign in to comment.