WebAuthnKit (iOS)
这个库为您提供了一种简单处理 W3C 网络身份验证 API(也称为 WebAuthn / FIDO 2.0)的方法。
(您也可以在这里获取 Android 版本 https://github.com/lyokato/WebAuthnKit-Android)
示例应用
安装
CocoaPods
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!
target '<Your Target Name>' do
pod 'WebAuthnKit', '~> 0.9.6'
end
$ pod install
入门
Info.plist
在开始之前,请修改您的 Info.plist 以设置 FaceID 权限。
添加 隐私 - Face ID 使用描述(NSFaceIDUsageDescription)
项,并写明您的目的。
设置您的 WebAuthnClient
首先,按照以下方式组合您的客户端对象。
import WebAuthnKit
let userConsentUI = UserConsentUI(viewController: self)
let authenticator = InternalAuthenticator(ui: userConsentUI)
self.webAuthnClient = WebAuthnClient(
origin: "https://example.org",
authenticator: authenticator
)
注册流程
使用以下文档中描述的流程,如果成功,WebAuthnClient 将创建一个凭证。
var options = PublicKeyCredentialCreationOptions()
options.challenge = Bytes.fromHex(challenge) // must be Array<UInt8>
options.user.id = Bytes.fromString(userId) // must be Array<UInt8>
options.user.name = userName
options.user.displayName = displayName
options.user.icon = iconURL // Optional
options.rp.id = "https://example.org"
options.rp.name = "your_service_name"
options.rp.icon = yourServiceIconURL // Optional
options.attestation = .direct // (choose from .direct, .indirect, .none)
options.addPubKeyCredParam(alg: .es256)
options.authenticatorSelection = AuthenticatorSelectionCriteria(
requireResidentKey: true, // this flag is ignored by InternalAuthenticator
userVerification: .required // (choose from .required, .preferred, .discouraged)
)
self.webAuthnClient.create(options).then { credential in
// send parameters to your server
// credential.id
// credential.rawId
// credential.response.attestationObject
// credential.response.clientDataJSON
}.catch { error in
// error handling
}
每个选项参数对应于在网页浏览器上实现的 JavaScript API。
使用 PromiseKit 的流程
WebAuthnKit 目前采用 PromiseKit,因此,整个注册过程可以写成如下。
import PromiseKit
firstly {
self.yourServiceHTTPClient.getRegistrationOptions()
}.then { response in
let options = self.createCreationOptionsFromHTTPResponse(response)
self.webAuthnClient.create(options)
}.then { credential in
let request = self.createHTTPRequestFromCredential(credential)
self.yourServiceHTTPClient.postdRegistrationCredential(request)
}.done { resp
// show completion message on UI
}.catch { error in
// error handling
}
如果希望在客户端进行中时停止,可以调用 cancel
方法。
self.webAuthnClient.cancel()
WAKError.cancelled
将作为等待 Promise 的 Error 分发。
认证流程
使用以下文档中描述的流程,WebAuthnClient 查找凭证,并让用户选择一个(如果有多项)来用其签名响应。
var options = PublicKeyCredentialRequestOptions()
options.challenge = Bytes.fromHex(challenge) // must be Array<UInt8>
options.rpId = "https://example.org"
options.userVerification = .required // (choose from .required, .preferred, .discouraged)
options.addAllowCredential(
credentialId: Bytes.fromHex(credId),
transports: [.internal_]
)
self.webAuthnClient.get(options).then { assertion in
// send parameters to your server
// assertion.id
// assertion.rawId
// assertion.response.authenticatorData
// assertion.response.signature
// assertion.response.userHandle
// assertion.response.clientDataJSON
}.catch {
// error handling
}
您可能想要按照以下方式编写整个断言过程。
firstly {
self.yourServiceHTTPClient.getAuthenticationOptions()
}.then { response in
let options = self.createRequestOptionsFromHTTPResponse(response)
self.webAuthnClient.get(options)
}.then { assertion in
let request = self.createHTTPRequestFromAssertion(assertion)
self.yourServiceHTTPClient.postAssertion(request)
}.done {
// show completion message on UI
}.catch { error in
// error handling
}
特色功能
尚未实现
- 令牌绑定
- 扩展
- BLE 认证器
- BLE 游牧服务
密钥算法支持
- ES256
本地密钥
InternalAuthenticator 强制使用本地密钥。
另请参阅
- https://www.w3.org/TR/webauthn/
- https://fidoalliance.org/specs/fido-v2.0-rd-20170927/fido-client-to-authenticator-protocol-v2.0-rd-20170927.html
许可
MIT-LICENSE
作者
Lyo Kato <lyo.kato 在 gmail.com>