forked from shadowfacts/Tusker
107 lines
4.9 KiB
Swift
107 lines
4.9 KiB
Swift
//
|
|
// OnboardingViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/18/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import AuthenticationServices
|
|
|
|
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
|
|
}
|
|
|
|
}
|
|
|
|
extension OnboardingViewController: InstanceSelectorTableViewControllerDelegate {
|
|
func didSelectInstance(url instanceURL: URL) {
|
|
let mastodonController = MastodonController(instanceURL: instanceURL)
|
|
mastodonController.registerApp { (clientID, clientSecret) in
|
|
|
|
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")
|
|
]
|
|
let authorizeURL = components.url!
|
|
|
|
self.authenticationSession = ASWebAuthenticationSession(url: authorizeURL, callbackURLScheme: "tusker") { url, error in
|
|
guard error == nil,
|
|
let url = url,
|
|
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
|
|
let item = components.queryItems?.first(where: { $0.name == "code" }),
|
|
let authCode = item.value else { return }
|
|
|
|
mastodonController.authorize(authorizationCode: authCode) { (accessToken) in
|
|
// construct a temporary UserAccountInfo instance for the MastodonController to use to fetch it's own account
|
|
let tempAccountInfo = LocalData.UserAccountInfo(id: "temp", instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: nil, accessToken: accessToken)
|
|
mastodonController.accountInfo = tempAccountInfo
|
|
|
|
mastodonController.getOwnAccount { (result) in
|
|
DispatchQueue.main.async {
|
|
switch result {
|
|
case let .failure(error):
|
|
let alert = UIAlertController(title: "Unable to Verify Credentials", message: "Your account could not be fetched at this time: \(error.localizedDescription)", preferredStyle: .alert)
|
|
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
|
|
self.present(alert, animated: true)
|
|
|
|
case let .success(account):
|
|
// this needs to happen on the main thread because it publishes a new value for the ObservableObject
|
|
let accountInfo = LocalData.shared.addAccount(instanceURL: instanceURL, clientID: clientID, clientSecret: clientSecret, username: account.username, accessToken: accessToken)
|
|
mastodonController.accountInfo = accountInfo
|
|
|
|
self.onboardingDelegate?.didFinishOnboarding(account: accountInfo)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
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: ASWebAuthenticationPresentationContextProviding {
|
|
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
|
return view.window!
|
|
}
|
|
}
|