From e15462fd6137af115e35f46ac2482c5c315c553e Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 14 Apr 2020 23:19:06 -0500 Subject: [PATCH] Persist twitter tokens in the keychain. --- .../Twitter/TwitterFeedProvider.swift | 36 ++++++++++++++----- .../ExtensionPointManager.swift | 2 +- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift b/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift index a84182d91..0ccf83ea2 100644 --- a/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift +++ b/Frameworks/FeedProvider/Twitter/TwitterFeedProvider.swift @@ -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 } } diff --git a/Shared/ExtensionPoints/ExtensionPointManager.swift b/Shared/ExtensionPoints/ExtensionPointManager.swift index d0c3c857e..947e5e2af 100644 --- a/Shared/ExtensionPoints/ExtensionPointManager.swift +++ b/Shared/ExtensionPoints/ExtensionPointManager.swift @@ -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()