5 // Created by phimage on 04/12/15.
6 // Copyright © 2015 Dongri Jin. All rights reserved.
11 open class OAuthSwift: NSObject, OAuthSwiftRequestHandle {
15 /// Client to make signed request
16 open var client: OAuthSwiftClient
17 /// Version of the protocol
18 open var version: OAuthSwiftCredential.Version { return self.client.credential.version }
20 /// Handle the authorize url into a web view or browser
21 open var authorizeURLHandler: OAuthSwiftURLHandlerType = OAuthSwiftOpenURLExternally.sharedInstance
23 fileprivate var currentRequests: [String: OAuthSwiftRequestHandle] = [:]
26 init(consumerKey: String, consumerSecret: String) {
27 self.client = OAuthSwiftClient(consumerKey: consumerKey, consumerSecret: consumerSecret)
30 // MARK: callback notification
31 struct CallbackNotification {
32 @available(*, deprecated: 0.5, message: "Use Notification.Name.OAuthSwiftHandleCallbackURL")
33 static let notificationName = Notification.Name(rawValue: "OAuthSwiftCallbackNotificationName")
34 static let optionsURLKey = "OAuthSwiftCallbackNotificationOptionsURLKey"
37 /// Handle callback url which contains now token information
38 open class func handle(url: URL) {
39 let notification = Notification(name: NSNotification.Name.OAuthSwiftHandleCallbackURL, object: nil,
40 userInfo: [CallbackNotification.optionsURLKey: url])
41 notificationCenter.post(notification)
44 var observer: NSObjectProtocol?
45 open class var notificationCenter: NotificationCenter {
46 return NotificationCenter.default
48 open class var notificationQueue: OperationQueue {
49 return OperationQueue.main
52 func observeCallback(_ block: @escaping (_ url: URL) -> Void) {
53 self.observer = OAuthSwift.notificationCenter.addObserver(forName: NSNotification.Name.OAuthSwiftHandleCallbackURL, object: nil, queue: OperationQueue.main) { [weak self] notification in
54 self?.removeCallbackNotificationObserver()
56 if let urlFromUserInfo = notification.userInfo?[CallbackNotification.optionsURLKey] as? URL {
57 block(urlFromUserInfo)
65 /// Remove internal observer on authentification
66 public func removeCallbackNotificationObserver() {
67 if let observer = self.observer {
68 OAuthSwift.notificationCenter.removeObserver(observer)
72 /// Function to call when web view is dismissed without authentification
73 public func cancel() {
74 self.removeCallbackNotificationObserver()
75 for (_, request) in self.currentRequests {
78 self.currentRequests = [:]
81 func putHandle(_ handle: OAuthSwiftRequestHandle, withKey key: String) {
82 // self.currentRequests[withKey] = handle
83 // TODO before storing handle, find a way to remove it when network request end (ie. all failure and success ie. complete)
86 /// Run block in main thread
87 static func main(block: @escaping () -> Void) {
88 if Thread.isMainThread {
91 DispatchQueue.main.async {
100 extension OAuthSwift {
102 public typealias Parameters = [String: Any]
103 public typealias Headers = [String: String]
104 public typealias ConfigParameters = [String: String]
105 /// MARK: callback alias
106 public typealias TokenSuccess = (credential: OAuthSwiftCredential, response: OAuthSwiftResponse?, parameters: Parameters)
107 public typealias TokenSuccessHandler = (_ credential: OAuthSwiftCredential, _ response: OAuthSwiftResponse?, _ parameters: Parameters) -> Void
108 public typealias FailureHandler = (_ error: OAuthSwiftError) -> Void
109 public typealias TokenRenewedHandler = (_ credential: OAuthSwiftCredential) -> Void