Authing 文档文档
快速开始
概念
使用指南
开发集成 V2 arrow
  • V2 文档
  • V3 文档
元数据
应用集成
身份自动化
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
快速开始
概念
使用指南
开发集成 V2 arrow
  • V2 文档
  • V3 文档
元数据
应用集成
身份自动化
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
旧版
开发集成
  • JavaScript SDK 索引
  • 单点登录(SSO)
  • 登录组件 (Guard)

  • 多因素认证组件(MFA)

  • JavaScript / Node.js

  • Java / Kotlin

  • Python

  • C#

  • PHP

  • Go

  • Ruby
  • Delphi
  • Android

  • iOS

    • 快速开始
    • 托管页
    • 超组件

    • APIs

    • 第三方身份源

    • 典型场景

      • 应用常用配置
      • 凭证管理
      • WebView
      • 退出登录
      • 闪屏界面
      • 自定义 WebAuthn 认证流程
      • Authing OTP
    • 私有化部署
    • 生物认证
    • iOS Guard 更新日志
    • 返回码对照表
  • Flutter

  • 微信小程序
  • 微信网页授权
  • React Native
  • 框架集成

  • Radius
  • 错误代码

¶ 自定义 WebAuthn 认证流程

更新时间: 2025-02-18 09:00:47
编辑

由于 WebAuthn 认证器分为两部分:本地凭证及服务端凭证,所以开发者在使用 WebAuthn 时,需要本地校验成功后,再去请求服务端验证器。 自定义 WebAuthn 认证流程需要调用 WebAuthn 原生 SDK API,以下将为开发者提供最完整的开发方案及代码示例。

¶ 绑定 WebAuthn

在绑定之前,需要通过 getWebauthnRegistrationParam() 获取到 WebAuthn 初始化配置:

AuthClient().getWebauthnRegistrationParam() {code, message, res
    // res 将返回,下一步时将需要以下参数:
    - challenge: 挑战码
    - userId: 用户 ID
    - userName: 用户名称
    - displayName:  用户展示名称
    - rpId: 信赖方 domain
    - rpName: 信赖方名称
    - timeout: 超时时间,毫秒
    - ticket: ticket 参数
}

当获取到 WebAuthn 初始化配置后,即可调用本地绑定接口,在本地绑定成功后,调用绑定 API 去服务端进行绑定:

let attestation = [
    AttestationConveyancePreference.direct,
    AttestationConveyancePreference.indirect,
    AttestationConveyancePreference.none,
][0]

let verification = [
    UserVerificationRequirement.required,
    UserVerificationRequirement.preferred,
    UserVerificationRequirement.discouraged
][0]

let requireResidentKey = true

let store = KeychainCredentialStore()
let authenticator = InternalAuthenticator(ui: self.userConsentUI, credentialStore: store)

self.webAuthnClient = WebAuthnClient(
    origin:        "https://\(rpId)",
    authenticator: authenticator
)

var options = PublicKeyCredentialCreationOptions()
options.challenge = Util.stringEncodeToUInt8Array(challenge)
options.user.id = Bytes.fromString(userId)
options.user.name = userName
options.user.displayName = displayName
options.rp.id = rpId
options.rp.name = rpName 
options.attestation = attestation
options.addPubKeyCredParam(alg: .es256)
options.authenticatorSelection = AuthenticatorSelectionCriteria(
    requireResidentKey: requireResidentKey,
    userVerification: verification
)
 options.timeout = UInt64(timeout ?? 60000)

firstly {
    
    self.webAuthnClient.create(options)
    
}.done { credential in
    
    let cid = credential.id
    let rid = Base64.encodeBase64URL(credential.rawId)
    let att = Base64.encodeBase64URL(credential.response.attestationObject)
    let clidata = Base64.encodeBase64URL(credential.response.clientDataJSON.data(using: .utf8)!)
    
    AuthClient().webauthnRegistration(ticket: ticket ?? "", credentialId: cid, rawId: rid, attestationObject: att, clientDataJSON: clidata, authenticatorCode: Util.isFullScreenIphone() == true ? "face" : "fingerprint") { code, message, res in
    }

}.catch { error in

}

¶ 登录 WebAuthn

在登录之前,需要通过 getWebauthnAuthenticationParam() 获取到 WebAuthn 初始化配置:

AuthClient().getWebauthnAuthenticationParam() {code, message, res
    // res 将返回,下一步时将需要以下参数:
    - challenge: 挑战码
    - rpId: 信赖方 domain
    - timeout: 超时时间,毫秒
    - ticket: ticket 参数,验证时回传
    - userVerification
}

当获取到 WebAuthn 初始化配置后,即可调用登录接口,在本地绑定登录后,调用登录 API 去服务端进行验证:

let store = KeychainCredentialStore()
let authticator = InternalAuthenticator(ui: self.userConsentUI, credentialStore: store)

self.webAuthnClient = WebAuthnClient(
    origin:        "https://\(rpId)",
    authenticator: authticator
)
        
var options = PublicKeyCredentialRequestOptions()
options.challenge = Util.stringEncodeToUInt8Array(challenge)
options.rpId = rpId
switch userVerification {
case "required":
    options.userVerification = UserVerificationRequirement.required
    break
case "preferred":
    options.userVerification = UserVerificationRequirement.preferred
    break
case "discouraged":
    options.userVerification = UserVerificationRequirement.discouraged
    break
default:
    options.userVerification = UserVerificationRequirement.required
    break
}
options.timeout = UInt64(timeout ?? 60000)

firstly {
    
    self.webAuthnClient.get(options)
    
}.done { assertion in
    
    let cid = assertion.id
    let rawId = Base64.encodeBase64URL(assertion.rawId)
    let attData = Base64.encodeBase64URL(assertion.response.authenticatorData)
    let clientData = Base64.encodeBase64URL(assertion.response.clientDataJSON.data(using: .utf8)!)
    let sig = Base64.encodeBase64URL(assertion.response.signature)
    let userHandle = Base64.encodeBase64URL(assertion.response.userHandle!)
                
    AuthClient().webauthnAuthentication(ticket: ticket ?? "", credentialId: cid, rawId: rawId, authenticatorData: attData, userHandle: userHandle, clientDataJSON: clientData, signature: sig) { code, message, res in
    }
}.catch { error in
}

¶ 解绑 WebAuthn

解绑 WebAuthn 需要先本地获取到凭证 ID,调用解绑 API 去服务端进行解绑,解绑成功后,将本地凭证一并删除:

AuthClient().webauthnRemoveCredential(credentialID: cid) { code, message, res in
    if let statusCode = res?["statusCode"] as? Int,
        statusCode == 200 {
        // 删除本地凭证
       for publicKey in store.loadAllCredentialSources(rpId: rpid) {
            if Base64.encodeBase64URL(publicKey.id) == cid {
                _ = store.deleteCredentialSource(publicKey)
            }
        }
    }
}
上一篇: 闪屏界面 下一篇: Authing OTP
  • 绑定 WebAuthn
  • 登录 WebAuthn
  • 解绑 WebAuthn

用户身份管理

集成第三方登录
手机号闪验 (opens new window)
通用登录表单组件
自定义认证流程

企业内部管理

单点登录
多因素认证
权限管理

开发者

开发文档
框架集成
博客 (opens new window)
GitHub (opens new window)
社区用户中心 (opens new window)

公司

400 888 2106
sales@authing.cn
北京市朝阳区北辰世纪中心 B 座 16 层(总)
成都市高新区天府五街 200 号 1 号楼 B 区 4 楼 406 室(分)

京ICP备19051205号

beian京公网安备 11010802035968号

© 北京蒸汽记忆科技有限公司