В настоящее время соединение работает по некоторым путям (например, «https://xxxx.yyy/api/» пользователей"), и я возвращаю правильный статус 200.
Теперь я попытался достичь "https://xxxx.yyy/api/users/" (с тем же сеансом) и возвращенный ответ был
Код: Выделить всё
Task . finished with error [-1206] Error Domain=NSURLErrorDomain Code=-1206 "The server “xxxx.yyy” requires a client certificate."
URL-адрес вызывается из:
Код: Выделить всё
let usedURL = URL(string: "https://xxxx.yyy/api/users/")!
var request = URLRequest(url: usedURL)
request.httpMethod = "GET"
let (data, response) = try await sessionDelegate.getSession().data(for: request)
let statusCode = (response as? HTTPURLResponse)?.statusCode
if statusCode != 200 {
Logger.shared.error("Token request response was not 200 but \(statusCode ?? -1, privacy: .public), \(String(decoding: data, as: UTF8.self))")
if statusCode == 400 {
errorController.push(error: LoginError.invalidLogin)
}
return
}
Код: Выделить всё
class SessionDelegate: NSObject, URLSessionDelegate {
private var certData: Data? = nil
private var certPass: String? = nil
var session: URLSession? = nil
public func getSession() -> URLSession {
if session == nil {
self.session = URLSession(configuration: .default, delegate: self, delegateQueue: nil)
}
return session!
}
public func setCertPass(certPass: String) -> Void {
self.certPass = certPass
}
public func setCertData(certData: Data) -> Void {
self.certData = certData
}
public func getCertData() -> Data? {
return certData
}
public func getCertPass() -> String? {
return certPass
}
func testUrl(url: String) async -> String{
do {
let (_, response) = try await getSession().data(from: URL(string: url)!)
guard let httpResponse = response as? HTTPURLResponse else { return "" }
return "HTTP Status Code \(httpResponse.statusCode)"
} catch {
return error.localizedDescription
}
}
public func loadCertData(data: Data) -> Bool{
certData = data
return true
}
public func urlSession(_: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod
== NSURLAuthenticationMethodClientCertificate
else {
print("Requied other url auth: \(challenge.protectionSpace.authenticationMethod)")
completionHandler(.performDefaultHandling, nil)
return
}
print("Using Delegate")
guard
let p12Data = certData
else {
print("CertData not loaded")
completionHandler(.performDefaultHandling, nil)
return
}
guard
let pass = certPass
else {
print("CertPass not loaded")
completionHandler(.performDefaultHandling, nil)
return
}
do {
let p12Contents = try PKCS12(pkcs12Data: p12Data, password: pass)
guard let identity = p12Contents.identity else {
print("Error loading identity")
completionHandler(.performDefaultHandling, nil)
return
}
let credential = URLCredential(identity: identity, certificates: nil, persistence: .none)
challenge.sender?.use(credential, for: challenge)
completionHandler(.useCredential, credential)
print("Client auth complete")
return
}catch {
print(error)
completionHandler(.performDefaultHandling, nil)
}
}
}
Я новичок в мире iOS и Swift, так что, возможно, я чего-то не хватает
Подробнее здесь: https://stackoverflow.com/questions/786 ... -some-urls
Мобильная версия