Skip to content

Commit 3fb0651

Browse files
authored
Fix Self Signed Certificates (#17)
* Fix self signed cert * Fix spacing * support ios
1 parent 0b1b181 commit 3fb0651

3 files changed

Lines changed: 29 additions & 30 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ SwiftyRequest is an HTTP networking library built for Swift.
2222

2323
## Swift version
2424
The 0.0.x releases were tested on macOS and Linux using the Swift 3.1 and 3.1.1 binaries.
25-
The 1.0.x releases were tested on macOS and Linux using the Swift 4.0.3
25+
26+
The 1.x.x releases were tested on macOS and Linux using the Swift 4.0.3
2627

2728
*NOTE:* Because of issues with URLSession/URLRequest in Swift 4.0, Swift 4.0 projects should use the 0.0.x release of SwiftyRequest.
2829

Sources/SwiftyRequest/RestRequest.swift

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -794,19 +794,28 @@ extension RestRequest: URLSessionDelegate {
794794
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
795795
let method = challenge.protectionSpace.authenticationMethod
796796
let host = challenge.protectionSpace.host
797+
798+
guard let url = URLComponents(string: self.url), let baseHost = url.host else {
799+
completionHandler(.performDefaultHandling, nil)
800+
return
801+
}
802+
803+
let warning = "Attempting to establish a secure connection; This is only supported by macOS 10.6 or higher. Resorting to default handling."
804+
797805
switch (method, host) {
798-
case (NSURLAuthenticationMethodServerTrust, self.url):
799-
#if MAC_OS_X_VERSION_10_6
800-
if let trust = challenge.protectionSpace.serverTrust {
801-
let credential = URLCredential(trust: trust)
802-
completionHandler(.useCredential, credential)
803-
} else {
804-
Log.warning("Attempting to establish a secure connection; no server trust established. Resorting to default handling.")
805-
completionHandler(.performDefaultHandling, nil)
806-
}
806+
case (NSURLAuthenticationMethodServerTrust, baseHost):
807+
#if !os(Linux)
808+
guard #available(iOS 3.0, macOS 10.6, *), let trust = challenge.protectionSpace.serverTrust else {
809+
Log.warning(warning)
810+
fallthrough
811+
}
812+
813+
let credential = URLCredential(trust: trust)
814+
completionHandler(.useCredential, credential)
815+
807816
#else
808-
Log.warning("Attempting to establish a secure connection; macOS 10.6 or higher must be used to achieve this. Resorting to default handling.")
809-
completionHandler(.performDefaultHandling, nil)
817+
Log.warning(warning)
818+
fallthrough
810819
#endif
811820
default:
812821
completionHandler(.performDefaultHandling, nil)

Tests/SwiftyRequestTests/SwiftyRequestTests.swift

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import CircuitBreaker
55
/// URL for the weather underground that many of the tests use
66
let apiKey = "96318a1fc52412b1" // We don't know if API Key for the wunderground API could expire at some point...
77
let echoURL = "http://httpbin.org/post"
8-
let echoURLSecure = "https://httpbin.org/post"
8+
let echoURLSecure = "https://self-signed.badssl.com/"
99
let apiURL = "http://api.wunderground.com/api/\(apiKey)/conditions/q/CA/San_Francisco.json"
1010
let geolookupURL = "http://api.wunderground.com/api/\(apiKey)/geolookup/q/CA/San_Francisco.json"
1111
let templetedAPIURL = "http://api.wunderground.com/api/\(apiKey)/conditions/q/{state}/{city}.json"
@@ -45,7 +45,6 @@ class SwiftyRequestTests: XCTestCase {
4545

4646
static var allTests = [
4747
("testEchoData", testEchoData),
48-
("testEchoDataSecure", testEchoDataSecure),
4948
("testResponseData", testResponseData),
5049
("testResponseObject", testResponseObject),
5150
("testResponseArray", testResponseArray),
@@ -60,6 +59,7 @@ class SwiftyRequestTests: XCTestCase {
6059
("testURLTemplateNoTemplateValues", testURLTemplateNoTemplateValues),
6160
("testQueryParamUpdating", testQueryParamUpdating),
6261
("testQueryTemplateParams", testQueryTemplateParams)
62+
6363
]
6464

6565
// MARK: Helper methods
@@ -133,35 +133,24 @@ class SwiftyRequestTests: XCTestCase {
133133
waitForExpectations(timeout: 20)
134134
}
135135

136-
func testEchoDataSecure() {
136+
func testGetSelfSignedCert() {
137+
#if !os(Linux)
137138
let expectation = self.expectation(description: "Data Echoed Back")
138139

139-
let origJson: [String: Any] = ["Data": "string"]
140-
141-
guard let data = try? JSONSerialization.data(withJSONObject: origJson, options: []) else {
142-
XCTFail("Could not encode json")
143-
return
144-
}
145-
146-
let request = RestRequest(method: .post, url: echoURLSecure, containsSelfSignedCert: true)
147-
request.messageBody = data
140+
let request = RestRequest(method: .get, url: echoURLSecure, containsSelfSignedCert: true)
148141

149142
request.responseData { response in
150143
switch response.result {
151144
case .success(let retval):
152-
guard let decoded = try? JSONSerialization.jsonObject(with: retval, options: []),
153-
let json = decoded as? [String: Any] else {
154-
XCTFail("Could not decode json")
155-
return
156-
}
157-
XCTAssertEqual("{\"Data\":\"string\"}", json["data"] as? String)
145+
XCTAssert(retval.count != 0)
158146
case .failure(let error):
159147
XCTFail("Failed to get data response: \(error)")
160148
}
161149
expectation.fulfill()
162150
}
163151

164152
waitForExpectations(timeout: 20)
153+
#endif
165154
}
166155

167156
// API Key (96318a1fc52412b1) for the wunderground API may expire at some point.

0 commit comments

Comments
 (0)