Stub out Twitter auth.
This commit is contained in:
parent
266c28d9be
commit
f5aac9516f
@ -7,6 +7,9 @@
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import AuthenticationServices
|
||||
import Secrets
|
||||
import OAuthSwift
|
||||
import FeedProvider
|
||||
|
||||
class ExtensionPointAddViewController: NSViewController {
|
||||
@ -16,6 +19,9 @@ class ExtensionPointAddViewController: NSViewController {
|
||||
private var availableExtensionPointTypes = [ExtensionPointType]()
|
||||
private var extensionPointAddWindowController: NSWindowController?
|
||||
|
||||
private let callbackURL = URL(string: "vincodennw://")!
|
||||
private var oauth: OAuthSwift?
|
||||
|
||||
init() {
|
||||
super.init(nibName: "ExtensionPointAdd", bundle: nil)
|
||||
}
|
||||
@ -50,8 +56,6 @@ extension ExtensionPointAddViewController: NSTableViewDataSource {
|
||||
|
||||
extension ExtensionPointAddViewController: NSTableViewDelegate {
|
||||
|
||||
private static let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "AccountCell")
|
||||
|
||||
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
|
||||
|
||||
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), owner: nil) as? ExtensionPointAddTableCellView {
|
||||
@ -73,12 +77,42 @@ extension ExtensionPointAddViewController: NSTableViewDelegate {
|
||||
let extensionPointType = availableExtensionPointTypes[selectedRow]
|
||||
switch extensionPointType {
|
||||
case .marsEdit, .microblog:
|
||||
|
||||
let windowController = ExtensionPointEnableBasicWindowController()
|
||||
windowController.extensionPointType = extensionPointType
|
||||
windowController.runSheetOnWindow(self.view.window!)
|
||||
extensionPointAddWindowController = windowController
|
||||
default:
|
||||
break
|
||||
|
||||
case .twitter:
|
||||
|
||||
let oauth = OAuth1Swift(
|
||||
consumerKey: Secrets.twitterConsumerKey,
|
||||
consumerSecret: Secrets.twitterConsumerSecret,
|
||||
requestTokenUrl: "https://api.twitter.com/oauth/request_token",
|
||||
authorizeUrl: "https://api.twitter.com/oauth/authorize",
|
||||
accessTokenUrl: "https://api.twitter.com/oauth/access_token"
|
||||
)
|
||||
|
||||
self.oauth = oauth
|
||||
oauth.authorizeURLHandler = self
|
||||
|
||||
oauth.authorize(withCallbackURL: callbackURL) { result in
|
||||
switch result {
|
||||
case .success(let tokenSuccess):
|
||||
// let token = tokenSuccess.credential.oauthToken
|
||||
// let secret = tokenSuccess.credential.oauthTokenSecret
|
||||
let screenName = tokenSuccess.parameters["screen_name"] as? String ?? ""
|
||||
|
||||
print("******************* \(screenName)")
|
||||
|
||||
case .failure(let oauthSwiftError):
|
||||
NSApplication.shared.presentError(oauthSwiftError)
|
||||
}
|
||||
|
||||
self.oauth?.cancel()
|
||||
self.oauth = nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
tableView.selectRowIndexes([], byExtendingSelection: false)
|
||||
@ -87,22 +121,37 @@ extension ExtensionPointAddViewController: NSTableViewDelegate {
|
||||
|
||||
}
|
||||
|
||||
// MARK: OAuthAccountAuthorizationOperationDelegate
|
||||
extension ExtensionPointAddViewController: OAuthSwiftURLHandlerType {
|
||||
|
||||
public func handle(_ url: URL) {
|
||||
let session = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURL.scheme, completionHandler: { (url, error) in
|
||||
if let callbackedURL = url {
|
||||
OAuth1Swift.handle(url: callbackedURL)
|
||||
}
|
||||
|
||||
guard let error = error else { return }
|
||||
|
||||
//extension AccountsAddViewController: OAuthAccountAuthorizationOperationDelegate {
|
||||
//
|
||||
// func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didCreate account: Account) {
|
||||
// account.refreshAll { [weak self] result in
|
||||
// switch result {
|
||||
// case .success:
|
||||
// break
|
||||
// case .failure(let error):
|
||||
// self?.presentError(error)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didFailWith error: Error) {
|
||||
// view.window?.presentError(error)
|
||||
// }
|
||||
//}
|
||||
self.oauth?.cancel()
|
||||
self.oauth = nil
|
||||
|
||||
if case ASWebAuthenticationSessionError.canceledLogin = error {
|
||||
print("Login cancelled.")
|
||||
} else {
|
||||
NSApplication.shared.presentError(error)
|
||||
}
|
||||
})
|
||||
|
||||
session.presentationContextProvider = self
|
||||
if !session.start() {
|
||||
print("Session failed to start!!!")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
extension ExtensionPointAddViewController: ASWebAuthenticationPresentationContextProviding {
|
||||
|
||||
public func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
|
||||
return view.window!
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user