Jack Spacie
Jack Spacie

Reputation: 15

Untrusted Certificate with Alamofire. I've tried every answer I can find

This is my info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>https://chargepoints.dft.gov.uk</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>
        
    </dict>
</dict>

This is how I've tried setting the session manager on alamofire

private static var Manager: Alamofire.SessionManager = {
    
    // Create the server trust policies
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "https://chargepoints.dft.gov.uk": .disableEvaluation
    ]
    
    // Create custom manager
    let configuration = URLSessionConfiguration.default
    configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
    let manager = Alamofire.SessionManager(
        configuration: URLSessionConfiguration.default,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )
    
    return manager
}()

And this is my code for doing the request

Downloader.Manager.request("https://chargepoints.dft.gov.uk/api/retrieve/registry/format/json").responseJSON { response in
        print("Request: \(String(describing: response.request))")   // original url request
        print("Response: \(String(describing: response.response))") // http url response
        print("Result: \(String(describing: response.result))")                         // response serialization result
        
        print("Error: \(String(describing: response.error))")
        
        if let json = response.result.value {
            print("JSON: \(json)") // serialized json response
        }
        
        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("Data: \(utf8Text)") // original server data as UTF8 string
        }
    }

Oh using iOS 10.3

XCode 8.3.2

Swift 3.0

Upvotes: 0

Views: 302

Answers (2)

AdamM
AdamM

Reputation: 4430

Try this

var afManager : SessionManager?

  afManager!.delegate.sessionDidReceiveChallenge = { session, challenge in
        var disposition = URLSession.AuthChallengeDisposition.performDefaultHandling

        var credential : URLCredential?

        if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
        {
            disposition = URLSession.AuthChallengeDisposition.useCredential
            credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        }
        else

            if(challenge.previousFailureCount > 0)
            {
                disposition = URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge
            }
            else
            {
                credential = self.afManager!.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)
                if(credential != nil)
                {
                    disposition = URLSession.AuthChallengeDisposition.useCredential
                }

        }
        return (disposition, credential)
    }

Then make your request

  afManager?.request("YOUR-URL-HERE", method: .get).responseJSON { response in

            switch response.result {
            case .success:
                print(response.result.value)
                break
            case .failure(let error):
                print(error)
            }
        }

Upvotes: 1

Jack Spacie
Jack Spacie

Reputation: 15

For anyone who wants to know, I got around this problem but changing the Https:// to plain Http as per one of the comments on this question

Transport security has blocked a cleartext HTTP

I banged my head against the wall for days but finally got some data.

Then changing the domain to chargepoints.dft.gov.uk omitting http: or https: finally got the rules to start working.

All the best Jack

Upvotes: 0

Related Questions