-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsslPinningURLSession
32 lines (28 loc) · 1.2 KB
/
sslPinningURLSession
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
import Foundation
final class SpNetworkManager: NSObject {
private lazy var certificates: [Data] = {
let url = Bundle.main.url(forResource: "sp", withExtension: "der")!
let data = try! Data(contentsOf: url)
return [data]
}()
private var session: URLSession!
override init() {
super.init()
session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
}
}
// MARK: - URLSessionDelegate
extension SpNetworkManager: URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let trust = challenge.protectionSpace.serverTrust, SecTrustGetCertificateCount(trust) > 0 {
if let certificate = SecTrustGetCertificateAtIndex(trust, 0) {
let data = SecCertificateCopyData(certificate) as Data
if certificates.contains(data) {
completionHandler(.useCredential, URLCredential(trust: trust))
return
}
}
}
completionHandler(.cancelAuthenticationChallenge, nil)
}
}