RootCAChallengeResolver

master

A package to support use of URLSession to make SSL request where the service utilizes a self-signed certificate. Using RootCAChallengeResolver encourages less pervasive disabling ATS
happycodelucky/SwiftRootCAChallengeResolver

RootCAChallengeResolver

A package to support use of URLSession to make SSL request where the service utilizes a self-signed certificate. Using RootCAChallengeResolver encourages less pervasive disabling of ATS.

As always, be careful of the endpoints you trust. They should ideally be endpoints your organization hosts.

For more information see Apple's documentation on Preventing Insecure Network Connections.

Swift Version iOS Version macOS Version tvOS Version watchOS Version GitHub license Maintained Release

Release Notes

This is not yet a production-ready release.

Version Description
0.4.0 Initial commit

Installation

To use RootCAChallengeResolver within your project see how to reference package using the Swift Package Manager or in Xcode. Once installed you can import RootCAChallengeResolver as appropriate.

Usage

The intent is for RootCAChallengeResolver to be used as a URLSession's delegate for the specific host your application has implicit trust in.

First create a new requests (details here are for show only)

let host = "cdn.example.com"
let endpoint = URL("https://\(host)/conf/2020-01-01.json")!
let request = URLRequest(url: endpoint)

Create a RootCAChallengeResolver and use as the delegate to a new URLSession

// (Optional) load an embedded cert to use for trust
let certUrl = Bundle.main.url(forResource: host, withExtension: "pem")
let certData = Data(contentsOf: certUrl)

// Use a `RootCAChallengeResolver` as the delegate for a `URLSession`
//
// `cert` is optional, and only need to be provided if you want to ensure more 
// stringent trust validation
let challengeDelegate = RootCAChallengeResolver(host: host, cert: certData)

// Use the `challengeDelegate` as the session delegate
let session = URLSession(configuration: .default, 
                         delegate: challengeDelegate, 
                         delegateQueue: .current ?? .main)

Perform a task, like a download

// Download the data
let task = session.downloadTask(with: request) { localUrl, response, error in
    ///...
}

task.resume()

URLSession Conveniences

URLSession has been extended with some convenience initializers

let host = "cdn.example.com"
let certUrl = Bundle.main.url(forResource: host, withExtension: "pem")
let certData = Data(contentsOf: certUrl)

// Create a session for a host and trust cert
let session = URLSession(configuration: .default, host: host, cert: certData)

..OR..

// Create a session for just a host
let session = URLSession(configuration: .default, host: host)

Unit Testing

Testing has not yet been implemented.

This package was extracted from another project and is yet to be fully self-serving.

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Nov 08 2022 01:38:02 GMT-1000 (Hawaii-Aleutian Standard Time)