1

Disclaimers: newb to swift

I am trying to figure out how to use Alamofire with Swift 4 to send a p12 cert for an https call to a domain.

All the examples I have seen are for Swift 2.0 and not exactly what I'm looking for. I have a working API which I m able to use via postman (where I m adding the certificate to postman settings).

Goal: I m provided with .p12 certificate and its passphrase to authenticate all API call from a domain (say: "api.abc.com") So, I m looking for a mechanism to connect to api.abc.com using the provided certificate.

Any help on this is highly appreciated.Even a basic example would help.

samridhgupta
  • 1,695
  • 1
  • 18
  • 34
  • does this help you: https://stackoverflow.com/questions/39985856/getting-client-certificate-to-work-for-mutual-authentication-using-swift-3-and-a – pbodsk Jan 05 '18 at 10:43
  • @pdodsk I tried to work around with the example in the above-mentioned stack question. But still no success – samridhgupta Jan 05 '18 at 11:55

1 Answers1

1

Try this:

class NetworkServices {

    static let sharedNetworkManager = NetworkServices()

    private static var manager: Alamofire.SessionManager = {

        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "https://your.domain.com": .pinCertificates(
                certificates: ServerTrustPolicy.certificates(),
                validateCertificateChain: true,
                validateHost: true),
            "your.domain.com": .disableEvaluation
        ]

        let configuration = URLSessionConfiguration.default

        return Alamofire.SessionManager(
            configuration: configuration,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    }()
}