Finish OAuth (kind of)

This commit is contained in:
Shadowfacts 2018-08-20 17:23:35 -04:00
parent 8d268fad18
commit 2be0b119ff
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 29 additions and 106 deletions

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

View File

@ -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
}
}

View File

@ -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")
// }
// }
// }
}

View File

@ -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 {

View File

@ -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
}
/*