Finish OAuth (kind of)

This commit is contained in:
Shadowfacts 2018-08-20 17:23:35 -04:00
parent 7808f24150
commit 7db0f4ff77
Signed by: shadowfacts
GPG Key ID: 83FB3304046BADA4
5 changed files with 29 additions and 106 deletions

@ -1 +1 @@
Subproject commit 6a03c64b6788faf5915c2918d429e5031af04fe6 Subproject commit 30aa01aa0ffb7cdd27030869d816cc0bc718ab3d

View File

@ -16,11 +16,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch. // Override point for customization after application launch.
// MastodonController.shared.connect() if LocalData.shared.onboardingComplete {
MastodonController.shared.createClient()
if LocalData.shared.hasLaunchedBefore {
MastodonController.shared.createClient() {
}
} else { } else {
showOnboarding() showOnboarding()
} }
@ -28,27 +25,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
return true 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) { 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. // 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.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
@ -93,6 +69,7 @@ extension AppDelegate: OnboardingViewControllerDelegate {
func didFinishOnboarding() { func didFinishOnboarding() {
hideOnboarding() hideOnboarding()
LocalData.shared.onboardingComplete = true
} }
} }

View File

@ -13,33 +13,22 @@ class MastodonController {
static let shared = MastodonController() static let shared = MastodonController()
// var userDefaults = UserDefaults()
var client: Client! 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() { private init() {
} }
func createClient(completion: @escaping () -> Void) { func createClient() {
guard let url = LocalData.shared.instanceURL else { fatalError("Can't connect without instance URL") } guard let url = LocalData.shared.instanceURL else { fatalError("Can't connect without instance URL") }
client = Client(baseURL: url) client = Client(baseURL: url)
if let refreshToken = LocalData.shared.refreshToken { if let accessToken = LocalData.shared.accessToken {
// client.accessToken = accessToken client.accessToken = accessToken
// completion()
authorize(authorizationCode: refreshToken, completion: completion)
} else {
register(completion: completion)
} }
} }
private func register(completion: @escaping () -> Void) { func registerApp(completion: @escaping () -> Void) {
guard LocalData.shared.clientID == nil, guard LocalData.shared.clientID == nil,
LocalData.shared.clientSecret == nil else { LocalData.shared.clientSecret == nil else {
completion() completion()
@ -57,37 +46,13 @@ class MastodonController {
} }
func authorize(authorizationCode: String, completion: @escaping () -> Void) { func authorize(authorizationCode: String, completion: @escaping () -> Void) {
// let parameters = [ let authorizeRequest = Login.authorize(code: authorizationCode, clientID: LocalData.shared.clientID!, clientSecret: LocalData.shared.clientSecret!, redirectURI: "tusker://oauth")
// 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!)
client.run(authorizeRequest) { result in client.run(authorizeRequest) { result in
guard case let .success(settings, _) = result else { fatalError() } guard case let .success(settings, _) = result else { fatalError() }
LocalData.shared.refreshToken = settings.refreshToken
LocalData.shared.accessToken = settings.accessToken LocalData.shared.accessToken = settings.accessToken
self.client.accessToken = settings.accessToken self.client.accessToken = settings.accessToken
completion() 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")
// }
// }
// }
} }

View File

@ -14,13 +14,13 @@ class LocalData {
let defaults = UserDefaults() let defaults = UserDefaults()
private let hasLaunchedBeforeKey = "hasLaunchedBefore" private let onboardingCompleteKey = "onboardingComplete"
var hasLaunchedBefore: Bool { var onboardingComplete: Bool {
get { get {
return defaults.bool(forKey: hasLaunchedBeforeKey) return defaults.bool(forKey: onboardingCompleteKey)
} }
set { 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" private let accessTokenKey = "accessToken"
var accessToken: String? { var accessToken: String? {
get { get {

View File

@ -32,7 +32,8 @@ class OnboardingViewController: UIViewController {
var components = URLComponents(string: text) else { return } var components = URLComponents(string: text) else { return }
LocalData.shared.instanceURL = text LocalData.shared.instanceURL = text
MastodonController.shared.createClient { MastodonController.shared.createClient()
MastodonController.shared.registerApp {
let clientID = LocalData.shared.clientID! let clientID = LocalData.shared.clientID!
let callbackURL = "tusker://oauth" let callbackURL = "tusker://oauth"
@ -46,30 +47,20 @@ class OnboardingViewController: UIViewController {
] ]
let url = components.url! let url = components.url!
print("oauth url: \(url)") 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() }
DispatchQueue.main.async { MastodonController.shared.authorize(authorizationCode: authCode) {
self.delegate?.didFinishOnboarding() DispatchQueue.main.async {
UIApplication.shared.open(url, options: [:], completionHandler: nil) self.delegate?.didFinishOnboarding()
}
}
} }
self.authenticationSession!.start()
// 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()
} }
} }
@ -77,7 +68,7 @@ class OnboardingViewController: UIViewController {
LocalData.shared.instanceURL = nil LocalData.shared.instanceURL = nil
LocalData.shared.clientID = nil LocalData.shared.clientID = nil
LocalData.shared.clientSecret = nil LocalData.shared.clientSecret = nil
LocalData.shared.refreshToken = nil LocalData.shared.accessToken = nil
} }
/* /*