Fix password auth

This commit is contained in:
Shadowfacts 2018-08-16 20:11:56 -04:00
parent fcfc169416
commit 7ad6f65891
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 37 additions and 28 deletions

View File

@ -7,7 +7,7 @@
<key>Tusker.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>3</integer>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>

View File

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