1. AuthenticatorAttestationResponse/AssertionResponse/CBOR/COSE 在协议层的工程价值
请解释 WebAuthn 中的 AuthenticatorAttestationResponse、AuthenticatorAssertionResponse、CBOR 与 COSE 四种数据结构在协议层的工程价值,以及它们各自承担什么角色?
- 两种 Response 对象(attestation/assertion)的字段与差异
- CBOR 二进制编码在协议中的用途
- COSE 密钥格式与公钥/签名描述的工程价值
AuthenticatorAttestationResponse 是注册(navigator.credentials.create)返回的结果,包含 clientDataJSON、attestationObject;AuthenticatorAssertionResponse 是断言(navigator.credentials.get)返回的结果,包含 clientDataJSON、authenticatorData、signature、userHandle。CBOR(Concise Binary Object Representation)是紧凑的二进制编码格式,用于序列化 attestationObject 与 authenticatorData 中的扩展字段,比 JSON 更节省空间。COSE(CBOR Object Signing and Encryption)则是基于 CBOR 的密钥/签名描述格式,用于表达公钥算法与参数(如 EC2/RSA 曲线与密钥)。工程上,前端负责采集这些原始字节,后端用库(如 @simplewebauthn/server)解析 CBOR、还原 COSE 公钥并验证签名。
理解这些协议层结构,才能明白"前端拿到的是二进制 blob,不能直接当 JSON 用",也解释了为什么服务端必须用支持 CBOR/COSE 的解析库。这是 WebAuthn 工程落地的基础,也是区分"会调 API"与"理解协议"的分水岭。
// 注册时采集的原始响应
const cred = await navigator.credentials.create({ publicKey });
// cred.response 是 AuthenticatorAttestationResponse
console.log(cred.response.clientDataJSON, cred.response.attestationObject);
// 登录时采集的原始响应
const asserted = await navigator.credentials.get({ publicKey });
// asserted.response 是 AuthenticatorAssertionResponse
console.log(asserted.response.signature, asserted.response.authenticatorData);