forked from shadowfacts/Tusker
Fix password auth
This commit is contained in:
parent
fcfc169416
commit
7ad6f65891
|
@ -7,7 +7,7 @@
|
||||||
<key>Tusker.xcscheme</key>
|
<key>Tusker.xcscheme</key>
|
||||||
<dict>
|
<dict>
|
||||||
<key>orderHint</key>
|
<key>orderHint</key>
|
||||||
<integer>3</integer>
|
<integer>0</integer>
|
||||||
</dict>
|
</dict>
|
||||||
</dict>
|
</dict>
|
||||||
<key>SuppressBuildableAutocreation</key>
|
<key>SuppressBuildableAutocreation</key>
|
||||||
|
|
|
@ -17,39 +17,31 @@ class MastodonController {
|
||||||
|
|
||||||
var client: Client!
|
var client: Client!
|
||||||
|
|
||||||
var clientID: String!
|
lazy var clientID: String? = self.userDefaults.string(forKey: "clientID")
|
||||||
var clientSecret: String!
|
lazy var clientSecret: String? = self.userDefaults.string(forKey: "clientSecret")
|
||||||
|
|
||||||
|
lazy var accessToken: String? = self.userDefaults.string(forKey: "accessToken")
|
||||||
|
|
||||||
private init() {
|
private init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect() {
|
func connect() {
|
||||||
// TODO: OAuth
|
|
||||||
let url = ProcessInfo.processInfo.environment["mastodon_url"]!
|
let url = ProcessInfo.processInfo.environment["mastodon_url"]!
|
||||||
let username = ProcessInfo.processInfo.environment["mastodon_username"]!
|
|
||||||
let password = ProcessInfo.processInfo.environment["mastodon_password"]!
|
|
||||||
|
|
||||||
|
if let accessToken = accessToken {
|
||||||
|
client = Client(baseURL: url, accessToken: accessToken)
|
||||||
|
} else {
|
||||||
client = Client(baseURL: url)
|
client = Client(baseURL: url)
|
||||||
|
|
||||||
register() {
|
login()
|
||||||
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() }
|
|
||||||
print("access token: \(loginSettings.accessToken)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func register(completion: @escaping () -> Void) {
|
private func register(completion: @escaping () -> Void) {
|
||||||
if let clientId = userDefaults.string(forKey: "clientID"),
|
if clientID != nil,
|
||||||
let clientSecret = userDefaults.string(forKey: "clientSecret") {
|
clientSecret != nil {
|
||||||
self.clientID = clientId
|
|
||||||
self.clientSecret = clientSecret
|
|
||||||
completion()
|
completion()
|
||||||
return
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
let registerRequest = Clients.register(clientName: "Tusker", scopes: [.read, .write, .follow])
|
let registerRequest = Clients.register(clientName: "Tusker", scopes: [.read, .write, .follow])
|
||||||
|
|
||||||
client.run(registerRequest) { result in
|
client.run(registerRequest) { result in
|
||||||
|
@ -61,5 +53,22 @@ class MastodonController {
|
||||||
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue