Alamofire ServerTrustPolicy certificate not locking Charles Proxy Swift 3

I searched all over the world and could not find the answer to my question. To make our application more secure, we were told to use "certificate commit". We already use the Alamofire library for all our API calls, so it seems natural to use it ServerTrustPolicyManager, included as a means to implement certificate enforcing. I have included the appropriate certificates in my application package, and here is the code I use to configure mine SessionManagerfor Alamofire:

let url = "https://www.mycompany.com"

var manager: SessionManager? {

    let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
        certificates: ServerTrustPolicy.certificates(),
        validateCertificateChain: true,
        validateHost: true
    )

    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        url: serverTrustPolicy
    ]
    let config = URLSessionConfiguration.default

    return SessionManager(configuration: config, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))

}

Now that I want to make an API request, I have this method:

func request(routerRequest request: URLRequestConvertible) -> DataRequest {
    assert(url != "", "A base URL string must be set in order to make request")
    print("URL: \(url) : \(request)")
    return (manager ?? SessionManager.default).request(request)
}

, , , , - Charles Proxy... . , - - Charles , ?

, , - ( ) . , Uber Wells Fargo, , , , - " , ssl " ( ).

, , SessionManager. , , , , . - ? ?

. !

+4
1

, , , , - . serverTrustPolicies , String : ServerTrustPolicy, String .

let serverTrustPolicies: [String: ServerTrustPolicy] = [
    url: serverTrustPolicy
]

My url baseURL, API, https://www.mycompany.com/api - . www.mycompany.com, , ! , Charles Proxy , , , : " , , SSL ".

, - , - baseURL, :

extension String {
    public func getDomain() -> String? {
        guard let url = URL(string: self) else { return nil }
        return url.host
    }
}

- :

let serverTrustPolicies: [String: ServerTrustPolicy] = [
    url.getDomain() ?? url : serverTrustPolicy
]

, - !

+6

Source: https://habr.com/ru/post/1678301/


All Articles