For example, all *.apple.com should use a same serverTrustPolicy.
let certificates = ServerTrustPolicy.certificatesInBundle(NSBundle.mainBundle())
let serverTrustPolicy = ServerTrustPolicy.PinCertificates(
certificates: certificates,
validateCertificateChain: true,
validateHost: true
)
let manager = Manager(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
serverTrustPolicyManager: ServerTrustPolicyManager(policies: [
"apple.com": serverTrustPolicy,
"*.apple.com": serverTrustPolicy
])
)
OK. I'm now checking the document in the source file.
Subclasses could override
this method and implement more complex mapping implementations such as wildcards.
As you can see, Alamofire doesn't currently support this. We'll keep it in mind for the future though.
👍
Example implementation if anyone needs one:
class WildcardServerTrustPolicyManager: ServerTrustPolicyManager {
override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
if let policy = policies[host] {
return policy
}
var domainComponents = host.split(separator: ".")
if domainComponents.count == 3 {
domainComponents[0] = "*"
let wildcardHost = domainComponents.joined(separator: ".")
return policies[wildcardHost]
}
return nil
}
}
If you are using Alamofire 5+:
class WildcardServerTrustPolicyManager: ServerTrustManager {
override func serverTrustEvaluator(forHost host: String) throws -> ServerTrustEvaluating? {
if let policy = evaluators[host] {
return policy
}
var domainComponents = host.split(separator: ".")
if domainComponents.count > 2 {
domainComponents[0] = "*"
let wildcardHost = domainComponents.joined(separator: ".")
return evaluators[wildcardHost]
}
return nil
}
}
Then in the implementation:
public lazy var session: Session = {
let configuration: URLSessionConfiguration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 300
let evaluators: [String: ServerTrustEvaluating] = [
"*.example.com": PinnedCertificatesTrustEvaluator()
]
let manager = WildcardServerTrustPolicyManager(evaluators: evaluators)
return Session(configuration: configuration, serverTrustManager: manager)
}()
Most helpful comment
Example implementation if anyone needs one: