Skip to content

Commit

Permalink
Cleaned up BoxAuthenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
tobihagemann committed Aug 20, 2024
1 parent d6c8528 commit cb72af7
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions Sources/CryptomatorCloudAccess/Box/BoxAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,31 @@ import UIKit
public enum BoxAuthenticatorError: Error {
case authenticationFailed
case invalidContext
case loginCancelled
case userCanceled
}

public enum BoxAuthenticator {
public static func authenticate(from viewController: UIViewController, tokenStorage: TokenStorage) -> Promise<BoxCredential> {
let pendingPromise = Promise<BoxCredential>.pending()

_Concurrency.Task {
do {
guard let context = viewController as? ASWebAuthenticationPresentationContextProviding else {
throw BoxAuthenticatorError.invalidContext
}

let config = OAuthConfig(clientId: BoxSetup.constants.clientId, clientSecret: BoxSetup.constants.clientSecret, tokenStorage: tokenStorage)
let oauth = BoxOAuth(config: config)

// Run the login flow and store the access token using tokenStorage
try await oauth.runLoginFlow(options: .init(), context: context)

try await oauth.runLoginFlow(options: .init(), context: context) // access token is implictly saved in token storage
pendingPromise.fulfill(BoxCredential(tokenStorage: tokenStorage))
} catch let error as ASWebAuthenticationSessionError {
if error.code == .canceledLogin {
// Handle the login cancellation
CloudAccessDDLogDebug("BoxAuthenticator: Login flow cancelled by the user.")
pendingPromise.reject(BoxAuthenticatorError.loginCancelled)
CloudAccessDDLogDebug("BoxAuthenticator: Login flow canceled by the user.")
pendingPromise.reject(BoxAuthenticatorError.userCanceled)
} else {
// Handle other authentication errors
CloudAccessDDLogDebug("BoxAuthenticator: Authentication failed with error: \(error.localizedDescription).")
pendingPromise.reject(BoxAuthenticatorError.authenticationFailed)
}
}
}

return pendingPromise
}
}

0 comments on commit cb72af7

Please sign in to comment.