LetMeIn 是一个精简的 Swift 包,用于在网络请求上执行客户端证书认证。客户端证书认证 (CCA) 允许服务器验证客户端是合法的,并授权其访问服务器或 API。
一个例子是 Cloudflare 的新 API Shield 功能,它使要求 CCA 访问 API 变得容易,允许你为你的应用程序创建一个私有 API,并防止它在你的应用程序之外被滥用。
这个包深受 Jeroen 关于 Swift 中 CCA 的精彩博文 的启发并从中改编。
LetMeIn 需要 iOS 14+ 或 macOS 11+。不需要其他依赖项。
你可以将 LetMeIn 用作 Swift 包,或者手动将其添加到你的项目中。
Swift 包管理器是一种将依赖项添加到你的应用程序的方法,并且与 Xcode 原生集成。
要使用 SPM 添加 LetMeIn,请单击 File
► Swift Packages
► Add Package Dependency...
,然后键入此 Github 仓库的 URL。 然后 Xcode 会将包添加到你的项目并执行构建它所需的所有工作。
https://github.com/julianschiavo/LetMeIn
或者,将包添加到你的 Package.swift
文件中。
let package = Package(
// ...
dependencies: [
.package(url: "https://github.com/julianschiavo/LetMeIn.git", from: "1.0.0")
],
// ...
)
请参阅 SPM 文档 了解更多信息。
如果你不想使用 SPM,你也可以通过从此存储库构建 Xcode 项目来将 LetMeIn 添加为普通框架。(请参阅其他来源以获取有关执行此操作的说明。)
创建一个身份验证器实例,然后在执行 URLSession
请求时将其用作委托。
// Create a certificate file representation
let certificate = CertificateFile(fileName: "certificate", password: "12345678")
// Create an authenticator
let authenticator = ClientCertificateAuthenticator(certificateFile: certificate)
// Make a custom URLSession using the authenticator as the delegate
let urlSession = URLSession(configuration: .default, delegate: authenticator, delegateQueue: nil)
// Perform a URL request
let request = URLRequest(url: ...)
urlSession.downloadTask(with: request) { (_, response, error) in
...
}
如果你已经有一个自定义 URLSessionDelegate
,或者想以其他方式使用此包(例如,使用 Alamofire),你可以直接使用身份验证器对象。 这是一种更高级的技术,并且由 LetMeIn 内部使用以提供 AVAsset
支持。
// Create a certificate file representation
let certificate = ...
// Create an authenticator
let authenticator = ...
// A `URLAuthenticationChallenge` that was received by your client
let challenge = ...
authenticator.handleChallenge(authenticationChallenge) { result, credential in
switch result {
case .cancelAuthenticationChallenge, .rejectProtectionSpace:
challenge.sender?.cancel(challenge)
case .useCredential:
guard let credential = credential else { return }
challenge.sender?.use(credential, for: challenge)
case .performDefaultHandling:
challenge.sender?.performDefaultHandling?(for: challenge)
}
}
例如,为了防止其他客户端或浏览器访问你的内容和资源,你可以将 LetMeIn 与 SDWebImage
一起使用,以使用客户端证书加载图像。
// Create a certificate file representation
let certificate = CertificateFile(fileName: "certificate", password: "12345678")
// Create an authenticator
let authenticator = ClientCertificateAuthenticator(certificateFile: certificate)
// Create a custom downloader operation (see SDWebImage documentation for more info)
class MyDownloaderOperation: SDWebImageDownloaderOperation {
override func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
authenticator.urlSession(session, didReceive: challenge, completionHandler: completionHandler)
}
override func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
authenticator.urlSession(session, task: task, didReceive: challenge, completionHandler: completionHandler)
}
}
// Make SDWebImage use our custom downloader operation when downloading images
SDWebImageDownloader.shared.config.operationClass = MyDownloaderOperation.self
// Load an image using SDWebImage
imageView.sd_setImage(with: URL(string: "http://www.domain.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
欢迎任何人提出贡献和拉取请求! 如果你发现 LetMeIn 存在问题,请提交 Github Issue,或者,如果你知道如何修复它,请提交拉取请求。
LetMeIn 由 Julian Schiavo 创建,并根据 MIT 许可证 提供。 一些代码改编自 Jeroen 关于 Swift 中 CCA 的博文。
根据 MIT 许可证提供。 有关更多信息,请参阅 许可证。