Skip to content

Commit 185c063

Browse files
authored
[swift6] simplify authentication (#19839)
1 parent 715f6b6 commit 185c063

File tree

12 files changed

+60
-252
lines changed

12 files changed

+60
-252
lines changed

modules/openapi-generator/src/main/resources/swift6/libraries/urlsession/URLSessionImplementations.mustache

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ extension URLSessionDataTask: URLSessionDataTaskProtocol {}
5252
}
5353
}
5454

55-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} typealias {{projectName}}APIChallengeHandler = ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))
56-
5755
fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
5856
private init() {
5957
defaultURLSession = URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: nil)
@@ -67,20 +65,12 @@ fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
6765
// Store the URLSession to retain its reference
6866
let defaultURLSession: URLSession
6967

70-
// Store current taskDidReceiveChallenge for every URLSessionTask
71-
var challengeHandlerStore = SynchronizedDictionary<Int, {{projectName}}APIChallengeHandler>()
72-
7368
// Store current URLCredential for every URLSessionTask
7469
var credentialStore = SynchronizedDictionary<Int, URLCredential>()
7570
}
7671

7772
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
7873
79-
/**
80-
May be assigned if you want to control the authentication challenges.
81-
*/
82-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var taskDidReceiveChallenge: {{projectName}}APIChallengeHandler?
83-
8474
required {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool, openAPIClient: OpenAPIClient = OpenAPIClient.shared) {
8575
super.init(method: method, URLString: URLString, parameters: parameters, headers: headers, requiresAuthentication: requiresAuthentication, openAPIClient: openAPIClient)
8676
}
@@ -187,7 +177,6 @@ fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
187177

188178
self.onProgressReady?(dataTask.progress)
189179

190-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[dataTask.taskIdentifier] = self.taskDidReceiveChallenge
191180
URLSessionRequestBuilderConfiguration.shared.credentialStore[dataTask.taskIdentifier] = self.credential
192181

193182
self.requestTask.set(task: dataTask)
@@ -211,7 +200,6 @@ fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
211200

212201
private func cleanupRequest() {
213202
if let task = requestTask.get() {
214-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] = nil
215203
URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] = nil
216204
}
217205
}
@@ -408,17 +396,13 @@ fileprivate final class SessionDelegate: NSObject, URLSessionTaskDelegate {
408396
409397
var credential: URLCredential?
410398
411-
if let taskDidReceiveChallenge = URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] {
412-
(disposition, credential) = taskDidReceiveChallenge(session, task, challenge)
399+
if challenge.previousFailureCount > 0 {
400+
disposition = .rejectProtectionSpace
413401
} else {
414-
if challenge.previousFailureCount > 0 {
415-
disposition = .rejectProtectionSpace
416-
} else {
417-
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
402+
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
418403
419-
if credential != nil {
420-
disposition = .useCredential
421-
}
404+
if credential != nil {
405+
disposition = .useCredential
422406
}
423407
}
424408

samples/client/petstore/swift6/asyncAwaitLibrary/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public class URLSessionRequestBuilderFactory: RequestBuilderFactory {
5252
}
5353
}
5454

55-
public typealias PetstoreClientAPIChallengeHandler = ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))
56-
5755
fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
5856
private init() {
5957
defaultURLSession = URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: nil)
@@ -67,20 +65,12 @@ fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
6765
// Store the URLSession to retain its reference
6866
let defaultURLSession: URLSession
6967

70-
// Store current taskDidReceiveChallenge for every URLSessionTask
71-
var challengeHandlerStore = SynchronizedDictionary<Int, PetstoreClientAPIChallengeHandler>()
72-
7368
// Store current URLCredential for every URLSessionTask
7469
var credentialStore = SynchronizedDictionary<Int, URLCredential>()
7570
}
7671

7772
open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
7873

79-
/**
80-
May be assigned if you want to control the authentication challenges.
81-
*/
82-
public var taskDidReceiveChallenge: PetstoreClientAPIChallengeHandler?
83-
8474
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool, openAPIClient: OpenAPIClient = OpenAPIClient.shared) {
8575
super.init(method: method, URLString: URLString, parameters: parameters, headers: headers, requiresAuthentication: requiresAuthentication, openAPIClient: openAPIClient)
8676
}
@@ -187,7 +177,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
187177

188178
self.onProgressReady?(dataTask.progress)
189179

190-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[dataTask.taskIdentifier] = self.taskDidReceiveChallenge
191180
URLSessionRequestBuilderConfiguration.shared.credentialStore[dataTask.taskIdentifier] = self.credential
192181

193182
self.requestTask.set(task: dataTask)
@@ -211,7 +200,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
211200

212201
private func cleanupRequest() {
213202
if let task = requestTask.get() {
214-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] = nil
215203
URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] = nil
216204
}
217205
}
@@ -408,17 +396,13 @@ fileprivate final class SessionDelegate: NSObject, URLSessionTaskDelegate {
408396

409397
var credential: URLCredential?
410398

411-
if let taskDidReceiveChallenge = URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] {
412-
(disposition, credential) = taskDidReceiveChallenge(session, task, challenge)
399+
if challenge.previousFailureCount > 0 {
400+
disposition = .rejectProtectionSpace
413401
} else {
414-
if challenge.previousFailureCount > 0 {
415-
disposition = .rejectProtectionSpace
416-
} else {
417-
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
402+
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
418403

419-
if credential != nil {
420-
disposition = .useCredential
421-
}
404+
if credential != nil {
405+
disposition = .useCredential
422406
}
423407
}
424408

samples/client/petstore/swift6/combineDeferredLibrary/PetstoreClient/Classes/OpenAPIs/Infrastructure/URLSessionImplementations.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public class URLSessionRequestBuilderFactory: RequestBuilderFactory {
5252
}
5353
}
5454

55-
public typealias PetstoreClientAPIChallengeHandler = ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))
56-
5755
fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
5856
private init() {
5957
defaultURLSession = URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: nil)
@@ -67,20 +65,12 @@ fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
6765
// Store the URLSession to retain its reference
6866
let defaultURLSession: URLSession
6967

70-
// Store current taskDidReceiveChallenge for every URLSessionTask
71-
var challengeHandlerStore = SynchronizedDictionary<Int, PetstoreClientAPIChallengeHandler>()
72-
7368
// Store current URLCredential for every URLSessionTask
7469
var credentialStore = SynchronizedDictionary<Int, URLCredential>()
7570
}
7671

7772
open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
7873

79-
/**
80-
May be assigned if you want to control the authentication challenges.
81-
*/
82-
public var taskDidReceiveChallenge: PetstoreClientAPIChallengeHandler?
83-
8474
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool, openAPIClient: OpenAPIClient = OpenAPIClient.shared) {
8575
super.init(method: method, URLString: URLString, parameters: parameters, headers: headers, requiresAuthentication: requiresAuthentication, openAPIClient: openAPIClient)
8676
}
@@ -187,7 +177,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
187177

188178
self.onProgressReady?(dataTask.progress)
189179

190-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[dataTask.taskIdentifier] = self.taskDidReceiveChallenge
191180
URLSessionRequestBuilderConfiguration.shared.credentialStore[dataTask.taskIdentifier] = self.credential
192181

193182
self.requestTask.set(task: dataTask)
@@ -211,7 +200,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
211200

212201
private func cleanupRequest() {
213202
if let task = requestTask.get() {
214-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] = nil
215203
URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] = nil
216204
}
217205
}
@@ -408,17 +396,13 @@ fileprivate final class SessionDelegate: NSObject, URLSessionTaskDelegate {
408396

409397
var credential: URLCredential?
410398

411-
if let taskDidReceiveChallenge = URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] {
412-
(disposition, credential) = taskDidReceiveChallenge(session, task, challenge)
399+
if challenge.previousFailureCount > 0 {
400+
disposition = .rejectProtectionSpace
413401
} else {
414-
if challenge.previousFailureCount > 0 {
415-
disposition = .rejectProtectionSpace
416-
} else {
417-
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
402+
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
418403

419-
if credential != nil {
420-
disposition = .useCredential
421-
}
404+
if credential != nil {
405+
disposition = .useCredential
422406
}
423407
}
424408

samples/client/petstore/swift6/combineLibrary/Sources/CombineLibrary/Infrastructure/URLSessionImplementations.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public class URLSessionRequestBuilderFactory: RequestBuilderFactory {
5252
}
5353
}
5454

55-
public typealias PetstoreClientAPIChallengeHandler = ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))
56-
5755
fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
5856
private init() {
5957
defaultURLSession = URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: nil)
@@ -67,20 +65,12 @@ fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
6765
// Store the URLSession to retain its reference
6866
let defaultURLSession: URLSession
6967

70-
// Store current taskDidReceiveChallenge for every URLSessionTask
71-
var challengeHandlerStore = SynchronizedDictionary<Int, PetstoreClientAPIChallengeHandler>()
72-
7368
// Store current URLCredential for every URLSessionTask
7469
var credentialStore = SynchronizedDictionary<Int, URLCredential>()
7570
}
7671

7772
open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
7873

79-
/**
80-
May be assigned if you want to control the authentication challenges.
81-
*/
82-
public var taskDidReceiveChallenge: PetstoreClientAPIChallengeHandler?
83-
8474
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool, openAPIClient: OpenAPIClient = OpenAPIClient.shared) {
8575
super.init(method: method, URLString: URLString, parameters: parameters, headers: headers, requiresAuthentication: requiresAuthentication, openAPIClient: openAPIClient)
8676
}
@@ -187,7 +177,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
187177

188178
self.onProgressReady?(dataTask.progress)
189179

190-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[dataTask.taskIdentifier] = self.taskDidReceiveChallenge
191180
URLSessionRequestBuilderConfiguration.shared.credentialStore[dataTask.taskIdentifier] = self.credential
192181

193182
self.requestTask.set(task: dataTask)
@@ -211,7 +200,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
211200

212201
private func cleanupRequest() {
213202
if let task = requestTask.get() {
214-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] = nil
215203
URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] = nil
216204
}
217205
}
@@ -408,17 +396,13 @@ fileprivate final class SessionDelegate: NSObject, URLSessionTaskDelegate {
408396

409397
var credential: URLCredential?
410398

411-
if let taskDidReceiveChallenge = URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] {
412-
(disposition, credential) = taskDidReceiveChallenge(session, task, challenge)
399+
if challenge.previousFailureCount > 0 {
400+
disposition = .rejectProtectionSpace
413401
} else {
414-
if challenge.previousFailureCount > 0 {
415-
disposition = .rejectProtectionSpace
416-
} else {
417-
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
402+
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
418403

419-
if credential != nil {
420-
disposition = .useCredential
421-
}
404+
if credential != nil {
405+
disposition = .useCredential
422406
}
423407
}
424408

samples/client/petstore/swift6/default/Sources/PetstoreClient/Infrastructure/URLSessionImplementations.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public class URLSessionRequestBuilderFactory: RequestBuilderFactory {
5252
}
5353
}
5454

55-
public typealias PetstoreClientAPIChallengeHandler = ((URLSession, URLSessionTask, URLAuthenticationChallenge) -> (URLSession.AuthChallengeDisposition, URLCredential?))
56-
5755
fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
5856
private init() {
5957
defaultURLSession = URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: nil)
@@ -67,20 +65,12 @@ fileprivate class URLSessionRequestBuilderConfiguration: @unchecked Sendable {
6765
// Store the URLSession to retain its reference
6866
let defaultURLSession: URLSession
6967

70-
// Store current taskDidReceiveChallenge for every URLSessionTask
71-
var challengeHandlerStore = SynchronizedDictionary<Int, PetstoreClientAPIChallengeHandler>()
72-
7368
// Store current URLCredential for every URLSessionTask
7469
var credentialStore = SynchronizedDictionary<Int, URLCredential>()
7570
}
7671

7772
open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
7873

79-
/**
80-
May be assigned if you want to control the authentication challenges.
81-
*/
82-
public var taskDidReceiveChallenge: PetstoreClientAPIChallengeHandler?
83-
8474
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool, openAPIClient: OpenAPIClient = OpenAPIClient.shared) {
8575
super.init(method: method, URLString: URLString, parameters: parameters, headers: headers, requiresAuthentication: requiresAuthentication, openAPIClient: openAPIClient)
8676
}
@@ -187,7 +177,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
187177

188178
self.onProgressReady?(dataTask.progress)
189179

190-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[dataTask.taskIdentifier] = self.taskDidReceiveChallenge
191180
URLSessionRequestBuilderConfiguration.shared.credentialStore[dataTask.taskIdentifier] = self.credential
192181

193182
self.requestTask.set(task: dataTask)
@@ -211,7 +200,6 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T>, @unchecked Sendable {
211200

212201
private func cleanupRequest() {
213202
if let task = requestTask.get() {
214-
URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] = nil
215203
URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] = nil
216204
}
217205
}
@@ -408,17 +396,13 @@ fileprivate final class SessionDelegate: NSObject, URLSessionTaskDelegate {
408396

409397
var credential: URLCredential?
410398

411-
if let taskDidReceiveChallenge = URLSessionRequestBuilderConfiguration.shared.challengeHandlerStore[task.taskIdentifier] {
412-
(disposition, credential) = taskDidReceiveChallenge(session, task, challenge)
399+
if challenge.previousFailureCount > 0 {
400+
disposition = .rejectProtectionSpace
413401
} else {
414-
if challenge.previousFailureCount > 0 {
415-
disposition = .rejectProtectionSpace
416-
} else {
417-
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
402+
credential = URLSessionRequestBuilderConfiguration.shared.credentialStore[task.taskIdentifier] ?? session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
418403

419-
if credential != nil {
420-
disposition = .useCredential
421-
}
404+
if credential != nil {
405+
disposition = .useCredential
422406
}
423407
}
424408

0 commit comments

Comments
 (0)