/
OAuthSwift.swift
125 lines (102 loc) · 4.02 KB
/
OAuthSwift.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// OAuthSwift.swift
// OAuthSwift
//
// Created by phimage on 04/12/15.
// Copyright © 2015 Dongri Jin. All rights reserved.
//
import Foundation
open class OAuthSwift: NSObject, OAuthSwiftRequestHandle {
// MARK: Properties
/// Client to make signed request
open var client: OAuthSwiftClient
/// Version of the protocol
open var version: OAuthSwiftCredential.Version { return self.client.credential.version }
/// Handle the authorize url into a web view or browser
open var authorizeURLHandler: OAuthSwiftURLHandlerType = OAuthSwiftURLHandlerTypeFactory.default
fileprivate var currentRequests: [String: OAuthSwiftRequestHandle] = [:]
// MARK: init
init(consumerKey: String, consumerSecret: String) {
self.client = OAuthSwiftClient(consumerKey: consumerKey, consumerSecret: consumerSecret)
}
// MARK: callback notification
struct CallbackNotification {
static let optionsURLKey = "OAuthSwiftCallbackNotificationOptionsURLKey"
}
/// Handle callback url which contains now token information
open class func handle(url: URL) {
let notification = Notification(name: OAuthSwift.didHandleCallbackURL, object: nil,
userInfo: [CallbackNotification.optionsURLKey: url])
notificationCenter.post(notification)
}
var observer: NSObjectProtocol?
open class var notificationCenter: NotificationCenter {
return NotificationCenter.default
}
open class var notificationQueue: OperationQueue {
return OperationQueue.main
}
func observeCallback(_ block: @escaping (_ url: URL) -> Void) {
self.observer = OAuthSwift.notificationCenter.addObserver(
forName: OAuthSwift.didHandleCallbackURL,
object: nil,
queue: OperationQueue.main) { [weak self] notification in
self?.removeCallbackNotificationObserver()
if let urlFromUserInfo = notification.userInfo?[CallbackNotification.optionsURLKey] as? URL {
block(urlFromUserInfo)
} else {
// Internal error
assertionFailure()
}
}
}
/// Remove internal observer on authentification
public func removeCallbackNotificationObserver() {
if let observer = self.observer {
OAuthSwift.notificationCenter.removeObserver(observer)
self.observer = nil
}
}
/// Function to call when web view is dismissed without authentification
public func cancel() {
self.removeCallbackNotificationObserver()
for (_, request) in self.currentRequests {
request.cancel()
}
self.currentRequests = [:]
}
func putHandle(_ handle: OAuthSwiftRequestHandle, withKey key: String) {
// self.currentRequests[withKey] = handle
// TODO before storing handle, find a way to remove it when network request end (ie. all failure and success ie. complete)
}
/// Run block in main thread
static func main(block: @escaping () -> Void) {
if Thread.isMainThread {
block()
} else {
DispatchQueue.main.async {
block()
}
}
}
}
// MARK: - alias
extension OAuthSwift {
public typealias Parameters = [String: Any]
public typealias Headers = [String: String]
public typealias ConfigParameters = [String: String]
// MARK: callback alias
public typealias TokenSuccess = (credential: OAuthSwiftCredential, response: OAuthSwiftResponse?, parameters: Parameters)
public typealias TokenCompletionHandler = (Result<TokenSuccess, OAuthSwiftError>) -> Void
public typealias TokenRenewedHandler = (Result<OAuthSwiftCredential, Never>) -> Void
}
// MARK: - Logging
extension OAuthSwift {
public static var log: OAuthLogProtocol?
/// Enables the log level
/// And instantiates the log object
public static func setLogLevel(_ level: OAuthLogLevel) {
Self.log = OAuthDebugLogger(level)
OAuthSwift.log?.trace("Logging enabled with level: \(level)")
}
}