samridhgupta
samridhgupta

Reputation: 1725

Getting client certificate to work for Https call authentication using Swift 4 and Alamofire

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.

Upvotes: 1

Views: 910

Answers (1)

Nijas Jamal
Nijas Jamal

Reputation: 21

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)
        )
    }()
}

Upvotes: 1

Related Questions