I am using Alamofire 4 in Swift 3 to build an application to PUT data to an API for an MDM server for mass updates.
Some users use a built-in CA or self-signed SSL certificate for their on-prem hosted servers, and if that certificate is not downloaded and installed to the keychain, Alamofire will not allow the communication.
I found the Security section of the Readme and wrote the following code in:
private static var Manager: Alamofire.SessionManager = {
// Create the server trust policies
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"my.on-prem-server.com": .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
}()
The problem is that I'm not the only person using this, and there's no way for me to statically set everyone's server information in there. Not everyone's server is going to be "my.on-prem-server.com"
I have a string variable called "allowUntrustedURL" which get's populated from a delegate on viewWillAppear()
(they enter their server info on a different view), but when I try
let serverTrustPolicies: [String: ServerTrustPolicy] = [
allowUntrustedURL: .disableEvaluation
]
I get the following error: Instance member 'allowUntrustedURL' cannot be used on type 'myViewController'
So I guess I'm open to options. I thought about making it a requirement that people download their SSL cert and install it to the keychain--but I think I'd rather have a checkbox to disable that requirement if at all possible.
I would be fine with either a way to globally disable Alamofire's trusted SSL requirement (preferably after checking a box or something to allow untrusted), or a method of passing in a variable which I can populate with their URL as they're filling things out.
EDIT: I've also tried some sort of wildcard in the URL, and I can't seem to get any sort of wildcard or global allowance to work.