added iOS source code
[wl-app.git] / iOS / Pods / OAuthSwift / Sources / OAuthSwift.swift
1 //
2 //  OAuthSwift.swift
3 //  OAuthSwift
4 //
5 //  Created by phimage on 04/12/15.
6 //  Copyright © 2015 Dongri Jin. All rights reserved.
7 //
8
9 import Foundation
10
11 open class OAuthSwift: NSObject, OAuthSwiftRequestHandle {
12
13     // MARK: Properties
14
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 }
19
20     /// Handle the authorize url into a web view or browser
21     open var authorizeURLHandler: OAuthSwiftURLHandlerType = OAuthSwiftOpenURLExternally.sharedInstance
22
23     fileprivate var currentRequests: [String: OAuthSwiftRequestHandle] = [:]
24
25     // MARK: init
26     init(consumerKey: String, consumerSecret: String) {
27         self.client = OAuthSwiftClient(consumerKey: consumerKey, consumerSecret: consumerSecret)
28     }
29
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"
35     }
36
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)
42     }
43
44     var observer: NSObjectProtocol?
45     open class var notificationCenter: NotificationCenter {
46         return NotificationCenter.default
47     }
48     open class var notificationQueue: OperationQueue {
49         return OperationQueue.main
50     }
51
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()
55
56             if let urlFromUserInfo = notification.userInfo?[CallbackNotification.optionsURLKey] as? URL {
57                 block(urlFromUserInfo)
58             } else {
59                 // Internal error
60                 assertionFailure()
61             }
62         }
63     }
64
65     /// Remove internal observer on authentification
66     public func removeCallbackNotificationObserver() {
67         if let observer = self.observer {
68             OAuthSwift.notificationCenter.removeObserver(observer)
69         }
70     }
71
72     /// Function to call when web view is dismissed without authentification
73     public func cancel() {
74         self.removeCallbackNotificationObserver()
75         for (_, request) in self.currentRequests {
76             request.cancel()
77         }
78         self.currentRequests = [:]
79     }
80
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)
84     }
85
86     /// Run block in main thread
87     static func main(block: @escaping () -> Void) {
88         if Thread.isMainThread {
89             block()
90         } else {
91             DispatchQueue.main.async {
92                 block()
93             }
94         }
95     }
96
97 }
98
99 // MARK: - alias
100 extension OAuthSwift {
101
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
110 }