From 7ad6f6589180bbb6dcfb33474d8c16353051d691 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 16 Aug 2018 20:11:56 -0400 Subject: [PATCH] Fix password auth --- .../xcschemes/xcschememanagement.plist | 2 +- Tusker/Controllers/MastodonController.swift | 63 +++++++++++-------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/Tusker.xcodeproj/xcuserdata/shadowfacts.xcuserdatad/xcschemes/xcschememanagement.plist b/Tusker.xcodeproj/xcuserdata/shadowfacts.xcuserdatad/xcschemes/xcschememanagement.plist index ad87b120..0d45b3f4 100644 --- a/Tusker.xcodeproj/xcuserdata/shadowfacts.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Tusker.xcodeproj/xcuserdata/shadowfacts.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ Tusker.xcscheme orderHint - 3 + 0 SuppressBuildableAutocreation diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index 12055823..5e48a338 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -17,48 +17,57 @@ class MastodonController { var client: Client! - var clientID: String! - var clientSecret: String! + 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 connect() { - // TODO: OAuth let url = ProcessInfo.processInfo.environment["mastodon_url"]! - let username = ProcessInfo.processInfo.environment["mastodon_username"]! - let password = ProcessInfo.processInfo.environment["mastodon_password"]! - client = Client(baseURL: url) - - register() { - let loginReq = Login.silent(clientID: self.clientID, clientSecret: self.clientSecret, scopes: [.read, .write, .follow], username: username, password: password) + if let accessToken = accessToken { + client = Client(baseURL: url, accessToken: accessToken) + } else { + client = Client(baseURL: url) - self.client.run(loginReq) { result in - guard case let .success(loginSettings, _) = result else { fatalError() } - print("access token: \(loginSettings.accessToken)") - } + login() } } private func register(completion: @escaping () -> Void) { - if let clientId = userDefaults.string(forKey: "clientID"), - let clientSecret = userDefaults.string(forKey: "clientSecret") { - self.clientID = clientId - self.clientSecret = clientSecret + if clientID != nil, + clientSecret != nil { completion() - return + } else { + let registerRequest = Clients.register(clientName: "Tusker", scopes: [.read, .write, .follow]) + + client.run(registerRequest) { result in + guard case let .success(application, _) = result else { fatalError() } + self.clientID = application.clientID + self.clientSecret = application.clientSecret + self.userDefaults.set(self.clientID, forKey: "clientID") + self.userDefaults.set(self.clientSecret, forKey: "clientSecret") + completion() + } } + } + + private func login() { + // TODO: OAuth + let username = ProcessInfo.processInfo.environment["mastodon_username"]! + let password = ProcessInfo.processInfo.environment["mastodon_password"]! - let registerRequest = Clients.register(clientName: "Tusker", scopes: [.read, .write, .follow]) - - client.run(registerRequest) { result in - guard case let .success(application, _) = result else { fatalError() } - self.clientID = application.clientID - self.clientSecret = application.clientSecret - self.userDefaults.set(self.clientID, forKey: "clientID") - self.userDefaults.set(self.clientSecret, forKey: "clientSecret") - completion() + 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") + } } }