该软件包提供了 WebAuthn API 的 Swift 实现,专注于让您轻松利用 WebAuthn 的强大功能来支持通行密钥和安全密钥。
添加依赖项
在您的 Package.swift
文件中添加以下条目以开始使用 WebAuthn
.package(url: "https://github.com/swift-server/swift-webauthn.git", from: "1.0.0-alpha.2")
并将 WebAuthn
依赖项添加到您的目标
.target(name: "MyApp", dependencies: [.product(name: "WebAuthn", package: "swift-webauthn")])
使用 WebAuthnManager
实例配置您的依赖方
let webAuthnManager = WebAuthnManager(
configuration: WebAuthnManager.Configuration(
relyingPartyID: "example.com",
relyingPartyName: "My Fancy Web App",
relyingPartyOrigin: "https://example.com"
)
)
对于注册流程,请使用以下两个方法
WebAuthnManager.beginRegistration()
WebAuthnManager.finishRegistration()
对于身份验证流程,请使用以下两个方法
WebAuthnManager.beginAuthentication()
WebAuthnManager.finishAuthentication()
如果您添加任何新文件,请在仓库根目录运行以下命令,以识别任何缺失的许可证标头
% PROJECTNAME="Swift WebAuthn" /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/swiftlang/github-workflows/refs/heads/main/.github/workflows/scripts/check-license-header.sh)"
Swift WebAuthn 很大程度上受到了现有 WebAuthn 库的启发,例如 py_webauthn 和 go-webauthn。