Swift NSURLSession HTTPS Self Sign - the request never reaches the server

I'm having trouble making an HTTPS request to my django rest api. I have django-sslserver working to set the api to port 8000. It seems that everything is fine when I make a request in the browser https://server-ip-addr:8000/api_view/, my browser complains: "Hey, this certificate is signed by the guys!" I say “yes, I know that I am,” and continue the dangerous answer.

Anyway, I'm trying to do the same in Swift for an iOS app. I found from this link here about the implementation of NSURLSessiondelegated protocols / functions NSURLSessionDelegate.URLSession()and NSURLSessionTaskDelegate.URLSession(). I changed the example so that it atomically attempts to login to my sslserver.

I did this in the following code, a class that implements the two protocols described above, designed to pass the credentials of the username and password to the server and wait for a response.

class SecureLogin: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate {

    func attemptLogin(username: String, password: String,
        callback: ((NSData!,NSURLResponse!,NSError!) -> Void)?) {

            println("inside attempt login")

            var request = NSMutableURLRequest(URL: NSURL(string: "https://147.222.164.91:8000/ldapauth/")!)
            request.HTTPMethod = "POST"


            var params = ["username":username, "password":password] as Dictionary<String, String>

            var err: NSError?
            request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.addValue("application/json", forHTTPHeaderField: "Accept")

            var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
            var session = NSURLSession(configuration: configuration,
                delegate: self,
                delegateQueue:NSOperationQueue.mainQueue())
            var task = session.dataTaskWithRequest(request,callback)

            task.resume()
    }

    func URLSession(session: NSURLSession,
        didReceiveChallenge challenge: NSURLAuthenticationChallenge,
        completionHandler: (NSURLSessionAuthChallengeDisposition,NSURLCredential!) -> Void) {
            println("Challenge received")
            completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust))
    }

    func URLSession(session: NSURLSession,
        task: NSURLSessionTask,
        willPerformHTTPRedirection response: NSHTTPURLResponse,
        newRequest request: NSURLRequest,
        completionHandler: (NSURLRequest!) -> Void) {
            println("Redirection received")
            var newRequest : NSURLRequest? = request
            println(newRequest?.description)
            completionHandler(newRequest)
    }
}

So I'm trying to execute a function attemptLogin()by providing a simple callback function to confirm the answer

var gatekeeper = SecureLogin()

gatekeeper.attemptLogin(username, password: password, callback: {data, response, error -> Void in

    println("inside gatekeeper")

}

println("beginning wait")

sleep(25)

I sleep for 25 seconds to keep the process long enough for the answer to come.

The console output is as follows:

inside login attempt

start of waiting

Then the program dies, the response message "internal gatekeeper" does not receive, my django server server also does not show any received requests. I checked the health check: I commented on the implementation of the delegate methods, and the server received the request, responds:

inside login attempt

start of waiting

2015-01-27 11: 29: 37.192 LdapAuthSecure [12783: 1475994] NSURLConnection/CFURLConnection HTTP (kCFStreamErrorDomainSSL, -9812)

, .

- - NSURLSession ? , . !

+4
2

iOS 9, SDK iOS 9 ( ), Info.plist, , . URL- .

+1

All Articles