@@ -85,6 +85,11 @@ public class NetworkLayer: NSObject, URLSessionDataDelegate {
8585 /// `URLSession` manager for the `NetworkLayer`.
8686 public var _urlSession : URLSession !
8787
88+ /// If `true`, Network Layer will accept challenges, otherwise it will cancel authentication challenges.
89+ ///
90+ /// Defaults to `true`.
91+ public var allowInvalidCertificates : Bool = true
92+
8893 /// Private initializer
8994 private override init ( ) {
9095 self . mainQueue = OperationQueue ( )
@@ -334,4 +339,31 @@ public class NetworkLayer: NSObject, URLSessionDataDelegate {
334339 case error( code: Int , name: String )
335340 }
336341
342+ public func urlSession( _ session: URLSession ,
343+ didReceive challenge: URLAuthenticationChallenge ,
344+ completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) {
345+ checkChallenge ( challenge, completionHandler: completionHandler)
346+ }
347+
348+ public func urlSession( _ session: URLSession ,
349+ task: URLSessionTask ,
350+ didReceive challenge: URLAuthenticationChallenge ,
351+ completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) {
352+ checkChallenge ( challenge, completionHandler: completionHandler)
353+ }
354+
355+ private func checkChallenge( _ challenge: URLAuthenticationChallenge ,
356+ completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) {
357+ var disposition = URLSession . AuthChallengeDisposition. performDefaultHandling
358+ var credentials : URLCredential ? = nil
359+ if allowInvalidCertificates {
360+ if let trust = challenge. protectionSpace. serverTrust {
361+ credentials = URLCredential ( trust: trust)
362+ }
363+ } else {
364+ disposition = . cancelAuthenticationChallenge
365+ }
366+ completionHandler ( disposition, credentials)
367+ }
368+
337369}
0 commit comments