// // OnboardingViewController.swift // Tusker // // Created by Shadowfacts on 8/18/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import AuthenticationServices import Pachyderm protocol OnboardingViewControllerDelegate { func didFinishOnboarding(account: LocalData.UserAccountInfo) } class OnboardingViewController: UINavigationController { static var blocks: [NSRegularExpression] = { guard let path = Bundle.main.path(forResource: "DomainBlocks", ofType: "plist"), let array = NSArray(contentsOfFile: path) as? [String] else { return [] } return array.compactMap { try? NSRegularExpression(pattern: $0, options: .caseInsensitive) } }() var onboardingDelegate: OnboardingViewControllerDelegate? var instanceSelector = InstanceSelectorTableViewController() var authenticationSession: ASWebAuthenticationSession? init() { super.init(rootViewController: instanceSelector) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() instanceSelector.delegate = self } private func doAuthenticationSession(url: URL) async throws -> URL { return try await withCheckedThrowingContinuation { continuation in self.authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: "tusker") { url, error in defer { DispatchQueue.main.async { self.authenticationSession = nil } } if let url = url { continuation.resume(returning: url) } else { continuation.resume(throwing: error!) } } DispatchQueue.main.async { // Prefer ephemeral sessions to make it easier to sign into multiple accounts on the same instance. self.authenticationSession!.prefersEphemeralWebBrowserSession = true self.authenticationSession!.presentationContextProvider = self self.authenticationSession!.start() } } } } extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate { func didSelectInstance(url instanceURL: URL) { async { let mastodonController = MastodonController(instanceURL: instanceURL) let (clientID, clientSecret) = await mastodonController.registerApp() var components = URLComponents(url: instanceURL, resolvingAgainstBaseURL: false)! components.path = "/oauth/authorize" components.queryItems = [ URLQueryItem(name: "client_id", value: clientID), URLQueryItem(name: "response_type", value: "code"), URLQueryItem(name: "scope", value: "read write follow"), URLQueryItem(name: "redirect_uri", value: "tusker://oauth") ] guard let url = try? await self.doAuthenticationSession(url: components.url!), let components = URLComponents(url: url, resolvingAgainstBaseURL: true), let item = components.queryItems?.first(where: { $0.name == "code" }), let authCode = item.value else { return } let accessToken = await mastodonController.authorize(authorizationCode: authCode) // construct a temporary UserAccountInfo instance for the MastodonController to use to fetch its own account let tempAccountInfo = LocalData.UserAccountInfo(id: "temp", instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: nil, accessToken: accessToken) mastodonController.accountInfo = tempAccountInfo let account: Account do { account = try await mastodonController.getOwnAccount() } catch { let alert = UIAlertController(title: "Unable to Verify Credentials", message: "Your account fcould not be fetcheda this time: \(error.localizedDescription)", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil)) self.present(alert, animated: true) return } let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: account.username, accessToken: accessToken) mastodonController.accountInfo = accountInfo self.onboardingDelegate?.didFinishOnboarding(account: accountInfo) } } } extension OnboardingViewController: ASWebAuthenticationPresentationContextProviding { func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { return view.window! } }