Created
May 31, 2022 17:13
-
-
Save RogelioHIT/697319b8ee6ffeba29bc5e882701742c to your computer and use it in GitHub Desktop.
Use client certificate on api request usng Alamofire
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a session for Alamofire where we add a certificate. This in case the client server has s self signed certficate. | |
You can use this constante inside your network manager and use it as | |
session.request(...) | |
.validate() | |
.response() | |
NOTE: The certificate has to be encoded in a der format not in pem, because apple only supports der as noted in this stackoverflow answer https://stackoverflow.com/a/51742988 | |
HOSTNAME: is the name of the host e.g. www.google.com | |
"AlamofireExtension(Bundle.main).certificates" this returns all valid certificates in the bundle | |
*/ | |
private let session: Session = { | |
let manager = ServerTrustManager(evaluators: [<HOSTNAME>: PinnedCertificatesTrustEvaluator(certificates: AlamofireExtension(Bundle.main).certificates, acceptSelfSignedCertificates: true, performDefaultValidation: false, validateHost: false)]) | |
let config = URLSessionConfiguration.default | |
config.timeoutIntervalForRequest = 60 | |
config.timeoutIntervalForResource = 60 | |
config.httpMaximumConnectionsPerHost = 2 | |
return Session(configuration: config, serverTrustManager: manager) | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment