-
Notifications
You must be signed in to change notification settings - Fork 207
/
DropboxClientsManager.swift
166 lines (144 loc) · 7.62 KB
/
DropboxClientsManager.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
///
/// Copyright (c) 2016 Dropbox, Inc. All rights reserved.
///
import Foundation
import Alamofire
/// This is a convenience class for the typical single user case. To use this
/// class, see details in the tutorial at:
/// https://www.dropbox.com/developers/documentation/swift#tutorial
///
/// For information on the available API methods, see the documentation for DropboxClient
open class DropboxClientsManager {
/// An authorized client. This will be set to nil if unlinked.
open static var authorizedClient: DropboxClient?
/// An authorized team client. This will be set to nil if unlinked.
open static var authorizedTeamClient: DropboxTeamClient?
/// Sets up access to the Dropbox User API
static func setupWithOAuthManager(_ appKey: String, oAuthManager: DropboxOAuthManager, transportClient: DropboxTransportClient?) {
precondition(DropboxOAuthManager.sharedOAuthManager == nil, "Only call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` once")
DropboxOAuthManager.sharedOAuthManager = oAuthManager
if let token = DropboxOAuthManager.sharedOAuthManager.getFirstAccessToken() {
setupAuthorizedClient(token, transportClient:transportClient)
}
Keychain.checkAccessibilityMigrationOneTime
}
/// Sets up access to the Dropbox User API
static func setupWithOAuthManagerMultiUser(_ appKey: String, oAuthManager: DropboxOAuthManager, transportClient: DropboxTransportClient?, tokenUid: String?) {
precondition(DropboxOAuthManager.sharedOAuthManager == nil, "Only call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` once")
DropboxOAuthManager.sharedOAuthManager = oAuthManager
if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) {
setupAuthorizedClient(token, transportClient:transportClient)
}
Keychain.checkAccessibilityMigrationOneTime
}
/// Sets up access to the Dropbox Business (Team) API
static func setupWithOAuthManagerTeam(_ appKey: String, oAuthManager: DropboxOAuthManager, transportClient: DropboxTransportClient?) {
precondition(DropboxOAuthManager.sharedOAuthManager == nil, "Only call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` once")
DropboxOAuthManager.sharedOAuthManager = oAuthManager
if let token = DropboxOAuthManager.sharedOAuthManager.getFirstAccessToken() {
setupAuthorizedTeamClient(token, transportClient:transportClient)
}
Keychain.checkAccessibilityMigrationOneTime
}
/// Sets up access to the Dropbox Business (Team) API in multi-user case
static func setupWithOAuthManagerMultiUserTeam(_ appKey: String, oAuthManager: DropboxOAuthManager, transportClient: DropboxTransportClient?, tokenUid: String?) {
precondition(DropboxOAuthManager.sharedOAuthManager == nil, "Only call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` once")
DropboxOAuthManager.sharedOAuthManager = oAuthManager
if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) {
setupAuthorizedTeamClient(token, transportClient:transportClient)
}
Keychain.checkAccessibilityMigrationOneTime
}
open static func reauthorizeClient(_ tokenUid: String) {
precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method")
if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) {
setupAuthorizedClient(token, transportClient:nil)
}
Keychain.checkAccessibilityMigrationOneTime
}
open static func reauthorizeTeamClient(_ tokenUid: String) {
precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method")
if let token = DropboxOAuthManager.sharedOAuthManager.getAccessToken(tokenUid) {
setupAuthorizedTeamClient(token, transportClient:nil)
}
Keychain.checkAccessibilityMigrationOneTime
}
static func setupAuthorizedClient(_ accessToken: DropboxAccessToken?, transportClient: DropboxTransportClient?) {
if let accessToken = accessToken {
if let transportClient = transportClient {
transportClient.accessToken = accessToken.accessToken
authorizedClient = DropboxClient(transportClient: transportClient)
} else {
authorizedClient = DropboxClient(accessToken: accessToken.accessToken)
}
} else {
if let transportClient = transportClient {
authorizedClient = DropboxClient(transportClient: transportClient)
}
}
}
static func setupAuthorizedTeamClient(_ accessToken: DropboxAccessToken?, transportClient: DropboxTransportClient?) {
if let accessToken = accessToken {
if let transportClient = transportClient {
transportClient.accessToken = accessToken.accessToken
authorizedTeamClient = DropboxTeamClient(transportClient: transportClient)
} else {
authorizedTeamClient = DropboxTeamClient(accessToken: accessToken.accessToken)
}
} else {
if let transportClient = transportClient {
authorizedTeamClient = DropboxTeamClient(transportClient: transportClient)
}
}
}
/// Handle a redirect and automatically initialize the client and save the token.
open static func handleRedirectURL(_ url: URL) -> DropboxOAuthResult? {
precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` before calling this method")
if let result = DropboxOAuthManager.sharedOAuthManager.handleRedirectURL(url) {
switch result {
case .success(let accessToken):
DropboxClientsManager.authorizedClient = DropboxClient(accessToken: accessToken.accessToken)
return result
case .cancel:
return result
case .error:
return result
}
} else {
return nil
}
}
/// Handle a redirect and automatically initialize the client and save the token.
open static func handleRedirectURLTeam(_ url: URL) -> DropboxOAuthResult? {
precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithTeamAppKey` before calling this method")
if let result = DropboxOAuthManager.sharedOAuthManager.handleRedirectURL(url) {
switch result {
case .success(let accessToken):
DropboxClientsManager.authorizedTeamClient = DropboxTeamClient(accessToken: accessToken.accessToken)
return result
case .cancel:
return result
case .error:
return result
}
} else {
return nil
}
}
/// Unlink the user.
open static func unlinkClients() {
if let oAuthManager = DropboxOAuthManager.sharedOAuthManager {
_ = oAuthManager.clearStoredAccessTokens()
resetClients()
}
}
/// Unlink the user.
open static func resetClients() {
if DropboxClientsManager.authorizedClient == nil && DropboxClientsManager.authorizedTeamClient == nil {
// already unlinked
return
}
DropboxClientsManager.authorizedClient = nil
DropboxClientsManager.authorizedTeamClient = nil
}
}