Persist twitter tokens in the keychain.

This commit is contained in:
Maurice Parker 2020-04-14 23:19:06 -05:00
parent 9e0248c494
commit e15462fd61
2 changed files with 28 additions and 10 deletions

View File

@ -12,25 +12,43 @@ import OAuthSwift
public struct TwitterFeedProvider {
private static let server = "api.twitter.com"
public var userID: String
public var screenName: String
private var oauthToken: String
private var oauthTokenSecret: String
public init(tokenSuccess: OAuthSwift.TokenSuccess) {
// TODO: beef this up
userID = tokenSuccess.parameters["user_id"] as? String ?? ""
screenName = tokenSuccess.parameters["screen_name"] as? String ?? ""
public init?(tokenSuccess: OAuthSwift.TokenSuccess) {
guard let userID = tokenSuccess.parameters["user_id"] as? String,
let screenName = tokenSuccess.parameters["screen_name"] as? String else {
return nil
}
// let token = tokenSuccess.credential.oauthToken
// let secret = tokenSuccess.credential.oauthTokenSecret
self.userID = userID
self.screenName = screenName
self.oauthToken = tokenSuccess.credential.oauthToken
self.oauthTokenSecret = tokenSuccess.credential.oauthTokenSecret
// TODO: save credentials here
let tokenCredentials = Credentials(type: .oauthAccessToken, username: userID, secret: oauthToken)
try? CredentialsManager.storeCredentials(tokenCredentials, server: Self.server)
let tokenSecretCredentials = Credentials(type: .oauthAccessTokenSecret, username: userID, secret: oauthTokenSecret)
try? CredentialsManager.storeCredentials(tokenSecretCredentials, server: Self.server)
}
public init(userID: String, screenName: String) {
public init?(userID: String, screenName: String) {
self.userID = userID
self.screenName = screenName
// TODO: load credentials here
guard let tokenCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthAccessToken, server: Self.server, username: userID),
let tokenSecretCredentials = try? CredentialsManager.retrieveCredentials(type: .oauthAccessTokenSecret, server: Self.server, username: userID) else {
return nil
}
self.oauthToken = tokenCredentials.secret
self.oauthTokenSecret = tokenSecretCredentials.secret
}
}

View File

@ -112,7 +112,7 @@ private extension ExtensionPointManager {
return nil
}
func extensionPoint(for extensionPointID: ExtensionPointIdentifer) -> ExtensionPoint {
func extensionPoint(for extensionPointID: ExtensionPointIdentifer) -> ExtensionPoint? {
switch extensionPointID {
case .marsEdit:
return SendToMarsEditCommand()