-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathOAuth2Client.swift
675 lines (592 loc) · 28.4 KB
/
OAuth2Client.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
//
// Copyright (c) 2021-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//
import Foundation
#if os(Linux)
import FoundationNetworking
#endif
/// Delegate protocol used by ``OAuth2Client`` to communicate important events.
public protocol OAuth2ClientDelegate: APIClientDelegate {
/// Sent before a token will begin to refresh.
func oauth(client: OAuth2Client, willRefresh token: Token)
/// Sent when a token has finished refreshing.
func oauth(client: OAuth2Client, didRefresh token: Token, replacedWith newToken: Token?)
}
extension OAuth2ClientDelegate {
public func oauth(client: OAuth2Client, willRefresh token: Token) {}
public func oauth(client: OAuth2Client, didRefresh token: Token, replacedWith newToken: Token?) {}
}
// swiftlint:disable type_body_length
/// An OAuth2 client, used to interact with a given authorization server.
public final class OAuth2Client {
/// The URLSession used by this client for network requests.
public let session: URLSessionProtocol
/// The configuration that identifies this OAuth2 client.
public let configuration: Configuration
/// Additional HTTP headers to include in outgoing network requests.
public var additionalHttpHeaders: [String: String]?
/// The OpenID configuration for this org.
///
/// This value will be `nil` until the configuration has been retrieved through the ``openIdConfiguration(completion:)`` or ``openIdConfiguration()`` functions.
public private(set) var openIdConfiguration: OpenIdConfiguration?
/// The ``JWKS`` key set for this org.
///
/// This value will be `nil` until the keys have been retrieved through the ``jwks(completion:)`` or ``jwks()`` functions.
public private(set) var jwks: JWKS?
/// Constructs an OAuth2Client for the given domain.
/// - Parameters:
/// - domain: Okta domain to use for the base URL.
/// - clientId: The unique client ID representing this client.
/// - scopes: The list of OAuth2 scopes requested for this client.
/// - authentication: The client authentication model to use (Default: `.none`)
/// - session: Optional URLSession to use for network requests.
public convenience init(domain: String,
clientId: String,
scopes: String,
authentication: ClientAuthentication = .none,
session: URLSessionProtocol? = nil) throws
{
self.init(try Configuration(domain: domain,
clientId: clientId,
scopes: scopes,
authentication: authentication),
session: session)
}
/// Constructs an OAuth2Client for the given domain.
/// - Parameters:
/// - baseURL: The base URL for operations against this client.
/// - clientId: The unique client ID representing this client.
/// - scopes: The list of OAuth2 scopes requested for this client.
/// - authentication: The client authentication model to use (Default: `.none`)
/// - session: Optional URLSession to use for network requests.
public convenience init(baseURL: URL,
clientId: String,
scopes: String,
authentication: ClientAuthentication = .none,
session: URLSessionProtocol? = nil)
{
self.init(Configuration(baseURL: baseURL,
clientId: clientId,
scopes: scopes,
authentication: authentication),
session: session)
}
/// Constructs an OAuth2Client for the given base URL.
/// - Parameters:
/// - configuration: The pre-formed configuration for this client.
/// - session: Optional URLSession to use for network requests.
public init(_ configuration: Configuration, session: URLSessionProtocol? = nil) {
// Ensure this SDK's static version is included in the user agent.
SDKVersion.register(sdk: Version)
// Ensure the time coordinator is properly initialized
_ = Date.coordinator
self.configuration = configuration
self.session = session ?? URLSession(configuration: .ephemeral)
NotificationCenter.default.post(name: .oauth2ClientCreated, object: self)
// Ensure the Credential Coordinator can monitor this client for token refresh changes.
Credential.coordinator.observe(oauth2: self)
}
/// Retrieves the org's OpenID configuration.
///
/// If this value has recently been retrieved, the cached result is returned.
/// - Parameter completion: Completion block invoked with the result.
public func openIdConfiguration(completion: @escaping (Result<OpenIdConfiguration, OAuth2Error>) -> Void) {
configurationQueue.sync {
if let openIdConfiguration = openIdConfiguration {
completion(.success(openIdConfiguration))
} else {
guard openIdConfigurationAction == nil else {
openIdConfigurationAction?.add(completion)
return
}
let action: CoalescedResult<Result<OpenIdConfiguration, OAuth2Error>> = CoalescedResult()
action.add(completion)
openIdConfigurationAction = action
let request = OpenIdConfigurationRequest(url: configuration.discoveryURL)
request.send(to: self) { result in
self.configurationQueue.sync(flags: .barrier) {
self.openIdConfigurationAction = nil
switch result {
case .success(let response):
self.openIdConfiguration = response.result
self.configurationQueue.async {
action.finish(.success(response.result))
}
case .failure(let error):
self.configurationQueue.async {
action.finish(.failure(.network(error: error)))
}
}
}
}
}
}
}
/// Attempts to refresh the supplied token, using the ``Token/refreshToken`` if it is available.
///
/// This method prevents multiple concurrent refresh requests to be performed for a given token, though all applicable completion blocks will be invoked once the token refresh has completed.
/// - Parameters:
/// - token: Token to refresh.
/// - completion: Completion bock invoked with the result.
public func refresh(_ token: Token, completion: @escaping (Result<Token, OAuth2Error>) -> Void) {
guard let clientSettings = token.context.clientSettings,
token.refreshToken != nil
else {
completion(.failure(.missingToken(type: .refreshToken)))
return
}
refreshQueue.sync {
guard token.refreshAction == nil else {
token.refreshAction?.add(completion)
return
}
token.refreshAction = CoalescedResult()
token.refreshAction?.add(completion)
performRefresh(token: token, clientSettings: clientSettings)
}
}
private(set) lazy var refreshQueue: DispatchQueue = {
DispatchQueue(label: "com.okta.refreshQueue.\(baseURL.host ?? "unknown")",
qos: .userInitiated,
attributes: .concurrent)
}()
private func performRefresh(token: Token, clientSettings: [String: String]) {
guard let action = token.refreshAction else { return }
delegateCollection.invoke { $0.oauth(client: self, willRefresh: token) }
guard let refreshToken = token.refreshToken else {
action.finish(.failure(.missingToken(type: .refreshToken)))
token.refreshAction = nil
return
}
openIdConfiguration { result in
switch result {
case .success(let configuration):
let request = Token.RefreshRequest(openIdConfiguration: configuration,
clientConfiguration: self.configuration,
refreshToken: refreshToken,
id: token.id,
configuration: clientSettings)
request.send(to: self) { result in
self.refreshQueue.sync(flags: .barrier) {
switch result {
case .success(let response):
let newToken = response.result.token(merging: token)
self.delegateCollection.invoke { $0.oauth(client: self, didRefresh: token, replacedWith: newToken) }
NotificationCenter.default.post(name: .tokenRefreshed, object: newToken)
action.finish(.success(newToken))
case .failure(let error):
self.delegateCollection.invoke { $0.oauth(client: self, didRefresh: token, replacedWith: nil) }
NotificationCenter.default.post(name: .tokenRefreshFailed,
object: token,
userInfo: ["error": error])
action.finish(.failure(.network(error: error)))
}
token.refreshAction = nil
}
}
case .failure(let error):
action.finish(.failure(error))
self.delegateCollection.invoke { $0.oauth(client: self, didRefresh: token, replacedWith: nil) }
token.refreshAction = nil
}
}
}
/// Attempts to revoke the given token.
///
/// A ``Token`` object may represent multiple token types, such as ``Token/accessToken`` or ``Token/refreshToken``. These individual token types can be targeted to be revoked.
///
/// - Parameters:
/// - token: Token object.
/// - type: Type of token to revoke.
/// - completion: Completion block to invoke once complete.
public func revoke(_ token: Token, type: Token.RevokeType, completion: @escaping (Result<Void, OAuth2Error>) -> Void) {
guard type != .all else {
revokeAll(token, completion: completion)
return
}
guard let tokenType = type.tokenType else {
completion(.failure(.cannotRevoke(type: type)))
return
}
guard let tokenString = token.token(of: tokenType) else {
completion(.failure(.missingToken(type: tokenType)))
return
}
guard let clientSettings = token.context.clientSettings else {
completion(.failure(.missingClientConfiguration))
return
}
openIdConfiguration { result in
switch result {
case .success(let configuration):
let request = Token.RevokeRequest(openIdConfiguration: configuration,
clientAuthentication: self.configuration.authentication,
token: tokenString,
hint: tokenType,
configuration: clientSettings)
request.send(to: self) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
completion(.failure(.network(error: error)))
}
}
case .failure(let error):
completion(.failure(error))
}
}
}
/// Introspects the given token information.
/// - Parameters:
/// - token: Token to introspect
/// - type: The type of value to introspect.
/// - completion: Completion block to invoke once complete.
public func introspect(token: Token, type: Token.Kind, completion: @escaping (Result<TokenInfo, OAuth2Error>) -> Void) {
openIdConfiguration { result in
switch result {
case .success(let configuration):
let request: Token.IntrospectRequest
do {
request = try Token.IntrospectRequest(openIdConfiguration: configuration,
clientConfiguration: self.configuration,
token: token,
type: type)
} catch let error as OAuth2Error {
completion(.failure(error))
return
} catch {
completion(.failure(.error(error)))
return
}
request.send(to: self) { result in
switch result {
case .success(let response):
completion(.success(response.result))
case .failure(let error):
completion(.failure(.network(error: error)))
}
}
case .failure(let error):
completion(.failure(error))
}
}
}
/// Fetches the ``UserInfo`` associated with the given token.
/// - Parameters:
/// - token: Token to retrieve user information for.
/// - completion: Completion block invoked with the result.
public func userInfo(token: Token, completion: @escaping (Result<UserInfo, OAuth2Error>) -> Void) {
openIdConfiguration { result in
switch result {
case .success(let configuration):
let request: UserInfo.Request
do {
request = try UserInfo.Request(openIdConfiguration: configuration,
token: token)
} catch let error as OAuth2Error {
completion(.failure(error))
return
} catch {
completion(.failure(.error(error)))
return
}
request.send(to: self) { result in
switch result {
case .success(let response):
completion(.success(response.result))
case .failure(let error):
completion(.failure(.network(error: error)))
}
}
case .failure(let error):
completion(.failure(error))
}
}
}
/// Retrieves the org's ``JWKS`` key configuration.
///
/// If this value has recently been retrieved, the cached result is returned.
/// - Parameter completion: Completion block invoked with the result.
public func jwks(completion: @escaping (Result<JWKS, OAuth2Error>) -> Void) {
if let jwks = jwks {
completion(.success(jwks))
} else {
jwksQueue.sync {
guard jwksAction == nil else {
jwksAction?.add(completion)
return
}
let action: CoalescedResult<Result<JWKS, OAuth2Error>> = CoalescedResult()
action.add(completion)
jwksAction = action
openIdConfiguration { result in
switch result {
case .success(let configuration):
let request = KeysRequest(openIdConfiguration: configuration,
clientId: self.configuration.clientId)
request.send(to: self) { result in
self.jwksQueue.sync(flags: .barrier) {
self.jwksAction = nil
switch result {
case .success(let response):
self.jwks = response.result
self.jwksQueue.async {
action.finish(.success(response.result))
}
case .failure(let error):
self.jwksQueue.async {
action.finish(.failure(.network(error: error)))
}
}
}
}
case .failure(let error):
self.jwksAction = nil
self.jwksQueue.async {
action.finish(.failure(error))
}
}
}
}
}
}
/// Attempts to exchange, and verify, a token from the supplied request.
///
/// This also ensures the ``JWKS`` keyset is retrieved in parallel (if it hasn't already been cached), and verifies the ID and Access tokens to ensure validity.
public func exchange<T: OAuth2TokenRequest>(token request: T, completion: @escaping (Result<APIResponse<Token>, APIClientError>) -> Void) {
// Fetch the JWKS keys in parallel if necessary
let group = DispatchGroup()
var keySet = jwks
if keySet == nil {
group.enter()
jwks { result in
defer { group.leave() }
if case let .success(response) = result {
keySet = response
}
}
}
// Exchange the token
request.send(to: self) { result in
// Wait for the JWKS keys, if necessary
group.notify(queue: DispatchQueue.global()) {
// Perform idToken/accessToken validation
self.validateToken(request: request,
keySet: keySet,
oauthTokenResponse: result,
completion: completion)
}
}
}
private func revokeAll(_ token: Token, completion: @escaping (Result<Void, OAuth2Error>) -> Void) {
let types: [Token.RevokeType] = [.accessToken, .refreshToken, .deviceSecret]
var errors = [OAuth2Error]()
let group = DispatchGroup()
for type in types {
guard let revokeType = type.tokenType,
token.token(of: revokeType) != nil
else {
continue
}
group.enter()
revoke(token, type: type) { result in
if case let .failure(error) = result {
errors.append(error)
}
group.leave()
}
}
group.notify(queue: DispatchQueue.global()) {
switch errors.count {
case 0:
completion(.success(()))
case 1:
if let error = errors.first {
completion(.failure(error))
} else {
fallthrough
}
default:
completion(.failure(.multiple(errors: errors)))
}
}
}
private func validateToken<T: OAuth2TokenRequest>(request: T,
keySet: JWKS?,
oauthTokenResponse: Result<APIResponse<Token>, APIClientError>,
completion: @escaping (Result<APIResponse<Token>, APIClientError>) -> Void)
{
guard case let .success(response) = oauthTokenResponse else {
completion(oauthTokenResponse)
return
}
// Retrieves the org's OpenID configuration
self.openIdConfiguration { result in
switch result {
case .failure(let error):
completion(.failure(.serverError(error)))
case .success:
do {
try response.result.validate(using: self, with: request as? IDTokenValidatorContext)
} catch {
completion(.failure(.validation(error: error)))
return
}
guard let idToken = response.result.idToken else {
completion(oauthTokenResponse)
return
}
guard let keySet = keySet else {
completion(.failure(.validation(error: JWTError.invalidKey)))
return
}
do {
if try idToken.validate(using: keySet) == false {
completion(.failure(.validation(error: JWTError.signatureInvalid)))
return
}
} catch {
completion(.failure(.validation(error: error)))
return
}
completion(oauthTokenResponse)
}
}
}
// MARK: Private properties / methods
private let delegates = DelegateCollection<OAuth2ClientDelegate>()
private lazy var configurationQueue: DispatchQueue = {
DispatchQueue(label: "com.okta.configurationQueue.\(baseURL.host ?? "unknown")",
qos: .userInitiated,
attributes: .concurrent)
}()
internal var openIdConfigurationAction: CoalescedResult<Result<OpenIdConfiguration, OAuth2Error>>?
private lazy var jwksQueue: DispatchQueue = {
DispatchQueue(label: "com.okta.jwksQueue.\(baseURL.host ?? "unknown")",
qos: .userInitiated,
attributes: .concurrent)
}()
internal var jwksAction: CoalescedResult<Result<JWKS, OAuth2Error>>?
}
// swiftlint:enable type_body_length
#if swift(>=5.5.1)
@available(iOS 13.0, tvOS 13.0, macOS 10.15, watchOS 6, *)
extension OAuth2Client {
/// Asynchronously retrieves the org's OpenID configuration.
///
/// If this value has recently been retrieved, the cached result is returned.
/// - Returns: The OpenID configuration for the org identified by the client's base URL.
public func openIdConfiguration() async throws -> OpenIdConfiguration {
try await withCheckedThrowingContinuation { continuation in
openIdConfiguration { result in
continuation.resume(with: result)
}
}
}
/// Asynchronously retrieves the org's ``JWKS`` key configuration.
///
/// If this value has recently been retrieved, the cached result is returned.
/// - Returns: The ``JWKS`` configuration for the org identified by the client's base URL.
public func jwks() async throws -> JWKS {
try await withCheckedThrowingContinuation { continuation in
jwks { result in
continuation.resume(with: result)
}
}
}
/// Attempts to refresh the supplied token, using the ``Token/refreshToken`` if it is available.
///
/// This method prevents multiple concurrent refresh requests to be performed for a given token, though all applicable results will be returned once the token refresh has completed.
/// - Parameters:
/// - token: Token to refresh.
public func refresh(_ token: Token) async throws -> Token {
try await withCheckedThrowingContinuation { continuation in
refresh(token) { result in
continuation.resume(with: result)
}
}
}
/// Attempts to revoke the given token.
///
/// A ``Token`` object may represent multiple token types, such as ``Token/accessToken`` or ``Token/refreshToken``. These individual token types can be targeted to be revoked.
///
/// - Parameters:
/// - token: Token object.
/// - type: Type of token to revoke, default: ``Token/RevokeType/all``
public func revoke(_ token: Token, type: Token.RevokeType = .all) async throws {
try await withCheckedThrowingContinuation { continuation in
revoke(token, type: type) { result in
continuation.resume(with: result)
}
}
}
}
#endif
extension OAuth2Client: APIClient {
/// Exposes the base URL this authorization server is represented by.
public var baseURL: URL { configuration.baseURL }
/// Transforms HTTP response data into the appropriate error type, when requests are unsuccessful.
/// - Parameter data: HTTP response body data for a failed URL request.
/// - Returns: ``OktaAPIError`` or ``OAuth2ServerError``, depending on the type of error.
public func error(from data: Data) -> Error? {
if let error = try? decode(OktaAPIError.self, from: data) {
return error
}
if let error = try? decode(OAuth2ServerError.self, from: data) {
return error
}
return nil
}
public func decode<T>(_ type: T.Type, from data: Data, userInfo: [CodingUserInfoKey: Any]? = nil) throws -> T where T: Decodable {
var info: [CodingUserInfoKey: Any] = userInfo ?? [:]
if info[.apiClientConfiguration] == nil {
info[.apiClientConfiguration] = configuration
}
let jsonDecoder: JSONDecoder
if let jsonType = type as? JSONDecodable.Type {
jsonDecoder = jsonType.jsonDecoder
} else {
jsonDecoder = defaultJSONDecoder
}
jsonDecoder.userInfo = info
return try jsonDecoder.decode(type, from: data)
}
public func willSend(request: inout URLRequest) {
delegateCollection.invoke { $0.api(client: self, willSend: &request) }
}
public func didSend(request: URLRequest, received error: APIClientError, requestId: String?, rateLimit: APIRateLimit?) {
delegateCollection.invoke { $0.api(client: self, didSend: request, received: error, requestId: requestId, rateLimit: rateLimit) }
}
public func shouldRetry(request: URLRequest) -> APIRetry {
return delegateCollection.call({ $0.api(client: self, shouldRetry: request) }).first ?? .default
}
public func didSend(request: URLRequest, received response: HTTPURLResponse) {
delegateCollection.invoke { $0.api(client: self, didSend: request, received: response) }
}
public func didSend<T>(request: URLRequest, received response: APIResponse<T>) where T: Decodable {
delegateCollection.invoke { $0.api(client: self, didSend: request, received: response) }
}
}
extension OAuth2Client: UsesDelegateCollection {
public typealias Delegate = OAuth2ClientDelegate
public var delegateCollection: DelegateCollection<OAuth2ClientDelegate> {
delegates
}
}
extension Notification.Name {
/// Notification broadcast when a new ``OAuth2Client`` instance is created.
public static let oauth2ClientCreated = Notification.Name("com.okta.oauth2client.created")
/// Notification broadcast when a ``Token`` is refreshed.
public static let tokenRefreshed = Notification.Name("com.okta.token.refresh.success")
/// Notification broadcast when a ``Token`` refresh fails.
public static let tokenRefreshFailed = Notification.Name("com.okta.token.refresh.failed")
}