From 2be0b119ff8671e67b363ea109c045fe26cef573 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 20 Aug 2018 17:23:35 -0400 Subject: [PATCH] Finish OAuth (kind of) --- MastodonKit | 2 +- Tusker/AppDelegate.swift | 29 ++---------- Tusker/Controllers/MastodonController.swift | 45 +++---------------- Tusker/LocalData.swift | 18 ++------ .../OnboardingViewController.swift | 41 +++++++---------- 5 files changed, 29 insertions(+), 106 deletions(-) diff --git a/MastodonKit b/MastodonKit index 6a03c64b..30aa01aa 160000 --- a/MastodonKit +++ b/MastodonKit @@ -1 +1 @@ -Subproject commit 6a03c64b6788faf5915c2918d429e5031af04fe6 +Subproject commit 30aa01aa0ffb7cdd27030869d816cc0bc718ab3d diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index 5155880e..69befaf5 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -16,38 +16,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. -// MastodonController.shared.connect() - - if LocalData.shared.hasLaunchedBefore { - MastodonController.shared.createClient() { - } + if LocalData.shared.onboardingComplete { + MastodonController.shared.createClient() } else { showOnboarding() } return true } - - func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { - guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return false } - - print("opened with url: \(url)") - - if components.host == "oauth" { - let code = components.queryItems?.first { - $0.name == "code" - } - if let authCode = code?.value { -// LocalData.shared.refreshToken = refreshToken - MastodonController.shared.authorize(authorizationCode: authCode) { - } - } - return true - } else { - return false - } - - } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. @@ -93,6 +69,7 @@ extension AppDelegate: OnboardingViewControllerDelegate { func didFinishOnboarding() { hideOnboarding() + LocalData.shared.onboardingComplete = true } } diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index 70b3a753..fa23084f 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -13,33 +13,22 @@ class MastodonController { static let shared = MastodonController() -// var userDefaults = UserDefaults() - var client: Client! -// lazy var clientID: String? = self.userDefaults.string(forKey: "clientID") -// lazy var clientSecret: String? = self.userDefaults.string(forKey: "clientSecret") -// -// lazy var accessToken: String? = self.userDefaults.string(forKey: "accessToken") - private init() { } - func createClient(completion: @escaping () -> Void) { + func createClient() { guard let url = LocalData.shared.instanceURL else { fatalError("Can't connect without instance URL") } client = Client(baseURL: url) - if let refreshToken = LocalData.shared.refreshToken { -// client.accessToken = accessToken -// completion() - authorize(authorizationCode: refreshToken, completion: completion) - } else { - register(completion: completion) + if let accessToken = LocalData.shared.accessToken { + client.accessToken = accessToken } } - private func register(completion: @escaping () -> Void) { + func registerApp(completion: @escaping () -> Void) { guard LocalData.shared.clientID == nil, LocalData.shared.clientSecret == nil else { completion() @@ -57,37 +46,13 @@ class MastodonController { } func authorize(authorizationCode: String, completion: @escaping () -> Void) { -// let parameters = [ -// Parameter(name: "client_id", value: LocalData.shared.clientID), -// Parameter(name: "client_secret", value: LocalData.shared.clientSecret), -// Parameter(name: "grant_type", value: "refresh_token"), -// Parameter(name: "refresh_token", value: LocalData.shared.refreshToken) -// ] -// let method = HTTPMethod.post(.parameters(parameters)) - let authorizeRequest = Login.authorize(code: authorizationCode, clientID: LocalData.shared.clientID!, clientSecret: LocalData.shared.clientSecret!) + let authorizeRequest = Login.authorize(code: authorizationCode, clientID: LocalData.shared.clientID!, clientSecret: LocalData.shared.clientSecret!, redirectURI: "tusker://oauth") client.run(authorizeRequest) { result in guard case let .success(settings, _) = result else { fatalError() } - LocalData.shared.refreshToken = settings.refreshToken LocalData.shared.accessToken = settings.accessToken self.client.accessToken = settings.accessToken completion() } } -// private func login() { -// // TODO: OAuth -// let username = ProcessInfo.processInfo.environment["mastodon_username"]! -// let password = ProcessInfo.processInfo.environment["mastodon_password"]! -// -// register() { -// let loginReq = Login.silent(clientID: self.clientID!, clientSecret: self.clientSecret!, scopes: [.read, .write, .follow], username: username, password: password) -// -// self.client.run(loginReq) { result in -// guard case let .success(loginSettings, _) = result else { fatalError() } -// self.accessToken = loginSettings.accessToken -// self.userDefaults.set(self.accessToken, forKey: "accessToken") -// } -// } -// } - } diff --git a/Tusker/LocalData.swift b/Tusker/LocalData.swift index 7a7f8b58..d2cf95ac 100644 --- a/Tusker/LocalData.swift +++ b/Tusker/LocalData.swift @@ -14,13 +14,13 @@ class LocalData { let defaults = UserDefaults() - private let hasLaunchedBeforeKey = "hasLaunchedBefore" - var hasLaunchedBefore: Bool { + private let onboardingCompleteKey = "onboardingComplete" + var onboardingComplete: Bool { get { - return defaults.bool(forKey: hasLaunchedBeforeKey) + return defaults.bool(forKey: onboardingCompleteKey) } set { - defaults.set(newValue, forKey: hasLaunchedBeforeKey) + defaults.set(newValue, forKey: onboardingCompleteKey) } } @@ -54,16 +54,6 @@ class LocalData { } } - private let refreshTokenKey = "refreshToken" - var refreshToken: String? { - get { - return defaults.string(forKey: refreshTokenKey) - } - set { - defaults.set(newValue, forKey: refreshTokenKey) - } - } - private let accessTokenKey = "accessToken" var accessToken: String? { get { diff --git a/Tusker/View Controllers/OnboardingViewController.swift b/Tusker/View Controllers/OnboardingViewController.swift index dfc115f2..54939233 100644 --- a/Tusker/View Controllers/OnboardingViewController.swift +++ b/Tusker/View Controllers/OnboardingViewController.swift @@ -32,7 +32,8 @@ class OnboardingViewController: UIViewController { var components = URLComponents(string: text) else { return } LocalData.shared.instanceURL = text - MastodonController.shared.createClient { + MastodonController.shared.createClient() + MastodonController.shared.registerApp { let clientID = LocalData.shared.clientID! let callbackURL = "tusker://oauth" @@ -46,30 +47,20 @@ class OnboardingViewController: UIViewController { ] let url = components.url! - print("oauth url: \(url)") - - DispatchQueue.main.async { - self.delegate?.didFinishOnboarding() - UIApplication.shared.open(url, options: [:], completionHandler: nil) + self.authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURL) { 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 { fatalError() } + + MastodonController.shared.authorize(authorizationCode: authCode) { + DispatchQueue.main.async { + self.delegate?.didFinishOnboarding() + } + } } - -// self.delegate?.didFinishOnboarding() -// self.authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURL) { url, error in -// guard error == nil, -// let url = url, -// let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { fatalError() } -// -// print("callback url: \(url)") -// -// let item = components.queryItems?.first { $0.name == "code" } -// if let accessToken = item?.value { -// LocalData.shared.accessToken = accessToken -// MastodonController.shared.client.accessToken = accessToken -// self.delegate?.didFinishOnboarding() -// self.authenticationSession = nil -// } -// } -// self.authenticationSession!.start() + self.authenticationSession!.start() } } @@ -77,7 +68,7 @@ class OnboardingViewController: UIViewController { LocalData.shared.instanceURL = nil LocalData.shared.clientID = nil LocalData.shared.clientSecret = nil - LocalData.shared.refreshToken = nil + LocalData.shared.accessToken = nil } /*