-
Notifications
You must be signed in to change notification settings - Fork 207
/
OAuthMobile.swift
449 lines (396 loc) · 19.1 KB
/
OAuthMobile.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
///
/// Copyright (c) 2016 Dropbox, Inc. All rights reserved.
///
#if canImport(UIKit)
import Foundation
import SafariServices
import UIKit
import WebKit
extension DropboxClientsManager {
/// Starts a "token" flow.
///
/// - Parameters:
/// - sharedApplication: The shared UIApplication instance in your app.
/// - controller: A UIViewController to present the auth flow from. Reference is weakly held.
/// - openURL: Handler to open a URL.
public static func authorizeFromController(_ sharedApplication: UIApplication, controller: UIViewController?, openURL: @escaping ((URL) -> Void)) {
precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` before calling this method")
let sharedMobileApplication = MobileSharedApplication(sharedApplication: sharedApplication, controller: controller, openURL: openURL)
MobileSharedApplication.sharedMobileApplication = sharedMobileApplication
DropboxOAuthManager.sharedOAuthManager.authorizeFromSharedApplication(sharedMobileApplication)
}
/// Starts the OAuth 2 Authorization Code Flow with PKCE.
///
/// PKCE allows "authorization code" flow without "client_secret"
/// It enables "native application", which is ensafe to hardcode client_secret in code, to use "authorization code".
/// PKCE is more secure than "token" flow. If authorization code is compromised during
/// transmission, it can't be used to exchange for access token without random generated
/// code_verifier, which is stored inside this SDK.
///
/// - Parameters:
/// - sharedApplication: The shared UIApplication instance in your app.
/// - controller: A UIViewController to present the auth flow from. Reference is weakly held.
/// - loadingStatusDelegate: An optional delegate to handle loading experience during auth flow.
/// e.g. Show a loading spinner and block user interaction while loading/waiting.
/// If a delegate is not provided, the SDK will show a default loading spinner when necessary.
/// - openURL: Handler to open a URL.
/// - scopeRequest: Contains requested scopes to obtain.
/// - NOTE:
/// If auth completes successfully, A short-lived Access Token and a long-lived Refresh Token will be granted.
/// API calls with expired Access Token will fail with AuthError. An expired Access Token must be refreshed
/// in order to continue to access Dropbox APIs.
///
/// API clients set up by `DropboxClientsManager` will get token refresh logic for free.
/// If you need to set up `DropboxClient`/`DropboxTeamClient` without `DropboxClientsManager`,
/// you will have to set up the clients with an appropriate `AccessTokenProvider`.
public static func authorizeFromControllerV2(
_ sharedApplication: UIApplication, controller: UIViewController?, loadingStatusDelegate: LoadingStatusDelegate?, openURL: @escaping ((URL) -> Void), scopeRequest: ScopeRequest?
) {
precondition(DropboxOAuthManager.sharedOAuthManager != nil, "Call `DropboxClientsManager.setupWithAppKey` or `DropboxClientsManager.setupWithTeamAppKey` before calling this method")
let sharedMobileApplication = MobileSharedApplication(sharedApplication: sharedApplication, controller: controller, openURL: openURL)
sharedMobileApplication.loadingStatusDelegate = loadingStatusDelegate
MobileSharedApplication.sharedMobileApplication = sharedMobileApplication
DropboxOAuthManager.sharedOAuthManager.authorizeFromSharedApplication(sharedMobileApplication, usePKCE: true, scopeRequest: scopeRequest)
}
public static func setupWithAppKey(_ appKey: String, transportClient: DropboxTransportClient? = nil) {
setupWithOAuthManager(appKey, oAuthManager: DropboxMobileOAuthManager(appKey: appKey), transportClient: transportClient)
}
public static func setupWithAppKeyMultiUser(_ appKey: String, transportClient: DropboxTransportClient? = nil, tokenUid: String?) {
setupWithOAuthManagerMultiUser(appKey, oAuthManager: DropboxMobileOAuthManager(appKey: appKey), transportClient: transportClient, tokenUid: tokenUid)
}
public static func setupWithTeamAppKey(_ appKey: String, transportClient: DropboxTransportClient? = nil) {
setupWithOAuthManagerTeam(appKey, oAuthManager: DropboxMobileOAuthManager(appKey: appKey), transportClient: transportClient)
}
public static func setupWithTeamAppKeyMultiUser(_ appKey: String, transportClient: DropboxTransportClient? = nil, tokenUid: String?) {
setupWithOAuthManagerMultiUserTeam(appKey, oAuthManager: DropboxMobileOAuthManager(appKey: appKey), transportClient: transportClient, tokenUid: tokenUid)
}
}
open class DropboxMobileOAuthManager: DropboxOAuthManager {
let dauthRedirectURL: URL
public override init(appKey: String, host: String) {
self.dauthRedirectURL = URL(string: "db-\(appKey)://1/connect")!
super.init(appKey: appKey, host:host)
self.urls.append(self.dauthRedirectURL)
}
internal override func extractFromUrl(_ url: URL, completion: @escaping DropboxOAuthCompletion) {
if let host = url.host, host == dauthRedirectURL.host { // dauth
extractfromDAuthURL(url, completion: completion)
} else {
extractFromRedirectURL(url, completion: completion)
}
}
internal override func checkAndPresentPlatformSpecificAuth(_ sharedApplication: SharedApplication) -> Bool {
if !self.hasApplicationQueriesSchemes() {
let message = "DropboxSDK: unable to link; app isn't registered to query for URL schemes dbapi-2 and dbapi-8-emm. Add a dbapi-2 entry and a dbapi-8-emm entry to LSApplicationQueriesSchemes"
let title = "SwiftyDropbox Error"
sharedApplication.presentErrorMessage(message, title: title)
return true
}
if let scheme = dAuthScheme(sharedApplication) {
let url: URL
if let authSession = authSession {
// Code flow
url = dAuthURL(scheme, authSession: authSession)
} else {
// Token flow
let nonce = UUID().uuidString
UserDefaults.standard.set(nonce, forKey: kDBLinkNonce)
url = dAuthURL(scheme, nonce: nonce)
}
sharedApplication.presentExternalApp(url)
return true
}
return false
}
open override func handleRedirectURL(_ url: URL, completion: @escaping DropboxOAuthCompletion) -> Bool {
super.handleRedirectURL(url, completion: {
if let sharedMobileApplication = MobileSharedApplication.sharedMobileApplication {
sharedMobileApplication.dismissAuthController()
}
completion($0)
})
}
fileprivate func dAuthURL(_ scheme: String, nonce: String?) -> URL {
var components = dauthUrlCommonComponents(with: scheme)
if let n = nonce {
let state = "oauth2:\(n)"
components.queryItems?.append(URLQueryItem(name: OAuthConstants.stateKey, value: state))
}
guard let url = components.url else { fatalError("Failed to create dauth url.") }
return url
}
private func dAuthURL(_ scheme: String, authSession: OAuthPKCESession) -> URL {
var components = dauthUrlCommonComponents(with: scheme)
let extraQueryParams = Self.createExtraQueryParamsString(for: authSession)
components.queryItems?.append(contentsOf: [
URLQueryItem(name: OAuthConstants.stateKey, value: authSession.state),
URLQueryItem(name: OAuthConstants.extraQueryParamsKey, value: extraQueryParams),
])
guard let url = components.url else { fatalError("Failed to create dauth url.") }
return url
}
private func dauthUrlCommonComponents(with scheme: String) -> URLComponents {
var components = URLComponents()
components.scheme = scheme
components.host = "1"
components.path = "/connect"
components.queryItems = [
URLQueryItem(name: "k", value: appKey),
URLQueryItem(name: "s", value: ""),
]
return components
}
fileprivate func dAuthScheme(_ sharedApplication: SharedApplication) -> String? {
if sharedApplication.canPresentExternalApp(dAuthURL("dbapi-2", nonce: nil)) {
return "dbapi-2"
} else if sharedApplication.canPresentExternalApp(dAuthURL("dbapi-8-emm", nonce: nil)) {
return "dbapi-8-emm"
} else {
return nil
}
}
func extractfromDAuthURL(_ url: URL, completion: @escaping DropboxOAuthCompletion) {
switch url.path {
case "/connect":
if let authSession = authSession {
handleCodeFlowUrl(url, authSession: authSession, completion: completion)
} else {
completion(extractFromTokenFlowUrl(url))
}
default:
completion(.error(.accessDenied, "User cancelled Dropbox link"))
}
}
/// Handles code flow response URL from DBApp.
/// Auth results are passed back in URL query parameters.
/// Expect results look like below:
/// 1. DBApp that can handle dauth code flow properly
/// ```
/// [
/// "state": "<state_string>",
/// "oauth_code": "<oauth_code>"
/// ]
/// ```
/// 2. Legacy DBApp that calls legacy dauth api, oauth_token should be "oauth2code:" and the code is stored under
/// "oauth_token_secret" key.
/// ```
/// [
/// "state": "<state_string>",
/// "oauth_token": "oauth2code:",
/// "oauth_token_secret": "<oauth_code>"
/// ]
/// ```
private func handleCodeFlowUrl(
_ url: URL, authSession: OAuthPKCESession, completion: @escaping DropboxOAuthCompletion
) {
let parametersMap = OAuthUtils.extractDAuthResponseFromUrl(url)
let state = parametersMap[OAuthConstants.stateKey]
guard state == authSession.state else {
completion(.error(.unknown, "Unable to verify link request"))
return
}
let authCode: String?
if let code = parametersMap[OAuthConstants.oauthCodeKey] {
authCode = code
} else if parametersMap[OAuthConstants.oauthTokenKey] == "oauth2code:",
let code = parametersMap[OAuthConstants.oauthSecretKey] {
authCode = code
} else {
authCode = nil
}
if let authCode = authCode {
finishPkceOAuth(
authCode: authCode, codeVerifier: authSession.pkceData.codeVerifier, completion: completion
)
} else {
completion(.error(.unknown, "Unable to verify link request"))
}
}
/// Handles token flow response URL from DBApp.
/// Auth results are passed back in URL query parameters.
/// Expect results look like below:
/// ```
/// [
/// "state": "oauth2:<nonce>",
/// "oauth_token_secret": "<oauth2_access_token>",
/// "uid": "<uid>"
/// ]
/// ```
private func extractFromTokenFlowUrl(_ url: URL) -> DropboxOAuthResult {
let parametersMap = OAuthUtils.extractDAuthResponseFromUrl(url)
let state = parametersMap[OAuthConstants.stateKey]
if let nonce = UserDefaults.standard.object(forKey: kDBLinkNonce) as? String, state == "oauth2:\(nonce)",
let accessToken = parametersMap[OAuthConstants.oauthSecretKey],
let uid = parametersMap[OAuthConstants.uidKey] {
return .success(DropboxAccessToken(accessToken: accessToken, uid: uid))
} else {
return .error(.unknown, "Unable to verify link request")
}
}
fileprivate func hasApplicationQueriesSchemes() -> Bool {
let queriesSchemes = Bundle.main.object(forInfoDictionaryKey: "LSApplicationQueriesSchemes") as? [String] ?? []
var foundApi2 = false
var foundApi8Emm = false
for scheme in queriesSchemes {
if scheme == "dbapi-2" {
foundApi2 = true
} else if scheme == "dbapi-8-emm" {
foundApi8Emm = true
}
if foundApi2 && foundApi8Emm {
return true
}
}
return false
}
/// Creates a string that contains all code flow query parameters.
private static func createExtraQueryParamsString(for authSession: OAuthPKCESession) -> String {
let pkceData = authSession.pkceData
var extraQueryParams = "\(OAuthConstants.codeChallengeKey)=\(pkceData.codeChallenge)"
+ "&\(OAuthConstants.codeChallengeMethodKey)=\(pkceData.codeChallengeMethod)"
+ "&\(OAuthConstants.tokenAccessTypeKey)=\(authSession.tokenAccessType)"
+ "&\(OAuthConstants.responseTypeKey)=\(authSession.responseType)"
if let scopeRequest = authSession.scopeRequest {
if let scopeString = scopeRequest.scopeString {
extraQueryParams += "&\(OAuthConstants.scopeKey)=\(scopeString)"
}
if scopeRequest.includeGrantedScopes {
extraQueryParams += "&\(OAuthConstants.includeGrantedScopesKey)=\(scopeRequest.scopeType.rawValue)"
}
}
return extraQueryParams
}
}
open class MobileSharedApplication: SharedApplication {
public static var sharedMobileApplication: MobileSharedApplication?
let sharedApplication: UIApplication
weak var controller: UIViewController?
let openURL: ((URL) -> Void)
weak var loadingStatusDelegate: LoadingStatusDelegate?
public init(sharedApplication: UIApplication, controller: UIViewController?, openURL: @escaping ((URL) -> Void)) {
// fields saved for app-extension safety
self.sharedApplication = sharedApplication
self.controller = controller
self.openURL = openURL
}
open func presentErrorMessage(_ message: String, title: String) {
let alertController = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertController.Style.alert)
if let controller = controller {
controller.present(alertController, animated: true, completion: { fatalError(message) })
}
}
open func presentErrorMessageWithHandlers(_ message: String, title: String, buttonHandlers: Dictionary<String, () -> Void>) {
let alertController = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertController.Style.alert)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel) { (_) in
if let handler = buttonHandlers["Cancel"] {
handler()
}
})
alertController.addAction(UIAlertAction(title: "Retry", style: .default) { (_) in
if let handler = buttonHandlers["Retry"] {
handler()
}
})
if let controller = controller {
controller.present(alertController, animated: true, completion: {})
}
}
open func presentPlatformSpecificAuth(_ authURL: URL) -> Bool {
presentExternalApp(authURL)
return true
}
open func presentAuthChannel(_ authURL: URL, tryIntercept: @escaping ((URL) -> Bool), cancelHandler: @escaping (() -> Void)) {
if let controller = self.controller {
let safariViewController = MobileSafariViewController(url: authURL, cancelHandler: cancelHandler)
controller.present(safariViewController, animated: true, completion: nil)
}
}
open func presentExternalApp(_ url: URL) {
self.openURL(url)
}
open func canPresentExternalApp(_ url: URL) -> Bool {
return self.sharedApplication.canOpenURL(url)
}
open func dismissAuthController() {
if let controller = self.controller {
if let presentedViewController = controller.presentedViewController {
if presentedViewController.isBeingDismissed == false && presentedViewController is MobileSafariViewController {
controller.dismiss(animated: true, completion: nil)
}
}
}
}
public func presentLoading() {
if isWebOAuthFlow {
presentLoadingInWeb()
} else {
presentLoadingInApp()
}
}
public func dismissLoading() {
if isWebOAuthFlow {
dismissLoadingInWeb()
} else {
dismissLoadingInApp()
}
}
private var isWebOAuthFlow: Bool {
return controller?.presentedViewController is MobileSafariViewController
}
/// Web OAuth flow, present the spinner over the MobileSafariViewController.
private func presentLoadingInWeb() {
let safariViewController = controller?.presentedViewController as? MobileSafariViewController
let loadingVC = LoadingViewController(nibName: nil, bundle: nil)
loadingVC.modalPresentationStyle = .overFullScreen
safariViewController?.present(loadingVC, animated: false)
}
// Web OAuth flow, dismiss loading view on the MobileSafariViewController.
private func dismissLoadingInWeb() {
let safariViewController = controller?.presentedViewController as? MobileSafariViewController
let loadingView = safariViewController?.presentedViewController as? LoadingViewController
loadingView?.dismiss(animated: false)
}
/// Delegate to app to present loading if delegate is set.
/// Otherwise, present the spinner in the view controller.
private func presentLoadingInApp() {
if let loadingStatusDelegate = loadingStatusDelegate {
loadingStatusDelegate.showLoading()
} else {
let loadingVC = LoadingViewController(nibName: nil, bundle: nil)
loadingVC.modalPresentationStyle = .overFullScreen
controller?.present(loadingVC, animated: false)
}
}
/// Delegate to app to dismiss loading if delegate is set.
/// Otherwise, dismiss the spinner in the view controller.
private func dismissLoadingInApp() {
if let loadingStatusDelegate = loadingStatusDelegate {
loadingStatusDelegate.dismissLoading()
} else if let loadingView = controller?.presentedViewController as? LoadingViewController {
loadingView.dismiss(animated: false)
}
}
}
open class MobileSafariViewController: SFSafariViewController, SFSafariViewControllerDelegate {
var cancelHandler: (() -> Void) = {}
public init(url: URL, cancelHandler: @escaping (() -> Void)) {
super.init(url: url, entersReaderIfAvailable: false)
self.cancelHandler = cancelHandler
self.delegate = self;
}
public func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
if (!didLoadSuccessfully) {
controller.dismiss(animated: true, completion: nil)
}
}
public func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
self.cancelHandler()
}
}
#endif