WebAuthn 与 FIDO2 无密码认证实践:注册、登录与兼容性验证技术背景WebAuthn/FIDO2 通过平台认证器(如 Windows Hello、Touch ID)或跨平台安全密钥(如 YubiKey)实现无密码登录。核心是基于挑战(challenge)的公钥注册与断言流程,配合严格的 RP ID、用户验证与允许传输通道设置,提供更强的抗钓鱼与抗中间人能力。核心内容注册(凭据创建)interface RegistrationOptionsRes { challenge: string; // Base64URL rp: { name: string; id: string }; user: { id: string; name: string; displayName: string }; pubKeyCredParams: Array<{ type: 'public-key'; alg: number }>; authenticatorSelection?: { authenticatorAttachment?: 'platform' | 'cross-platform'; residentKey?: 'required' | 'preferred' | 'discouraged'; userVerification?: 'required' | 'preferred' | 'discouraged'; }; timeout?: number; } function b64uToArrayBuffer(b64u: string): ArrayBuffer { const b64 = b64u.replace(/-/g, '+').replace(/_/g, '/'); const pad = '='.repeat((4 - (b64.length % 4)) % 4); const data = atob(b64 + pad); const buf = new ArrayBuffer(data.length); const view = new Uint8Array(buf); for (let i = 0; i < data.length; i++) view[i] = data.charCodeAt(i); return buf; } async function registerWebAuthn() { const res = await fetch('/api/webauthn/registration/options', { credentials: 'include' }); const options: RegistrationOptionsRes = await res.json(); const publicKey: PublicKeyCredentialCreationOptions = { challenge: b64uToArrayBuffer(options.challenge), rp: options.rp, user: { id: new TextEncoder().encode(options.user.id), name: options.user.name, displayName: options.user.displayName }, pubKeyCredParams: options.pubKeyCredParams, authenticatorSelection: options.authenticatorSelection, timeout: options.timeout || 60000, attestation: 'none' // 生产通常选择 none,减少隐私暴露 }; const cred = await navigator.credentials.create({ publicKey }) as PublicKeyCredential; const attestation = cred.response as AuthenticatorAttestationResponse; const payload = { id: cred.id, rawId: Array.from(new Uint8Array(cred.rawId)), type: cred.type, response: { clientDataJSON: Array.from(new Uint8Array(attestation.clientDataJSON)), attestationObject: Array.from(new Uint8Array(attestation.attestationObject)) } }; const verify = await fetch('/api/webauthn/registration/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(payload) }); return await verify.json(); } 登录(断言)interface AssertionOptionsRes { challenge: string; // Base64URL rpId: string; allowCredentials?: Array<{ id: string; type: 'public-key'; transports?: AuthenticatorTransport[] }>; userVerification?: 'required' | 'preferred' | 'discouraged'; timeout?: number; } async function loginWebAuthn(username: string) { const res = await fetch(`/api/webauthn/assertion/options?username=${encodeURIComponent(username)}`, { credentials: 'include' }); const options: AssertionOptionsRes = await res.json(); const publicKey: PublicKeyCredentialRequestOptions = { challenge: b64uToArrayBuffer(options.challenge), rpId: options.rpId, allowCredentials: options.allowCredentials?.map(c => ({ id: new Uint8Array(b64uToArrayBuffer(c.id)), type: c.type, transports: c.transports })), userVerification: options.userVerification || 'preferred', timeout: options.timeout || 60000 }; const assertion = await navigator.credentials.get({ publicKey }) as PublicKeyCredential; const resp = assertion.response as AuthenticatorAssertionResponse; const payload = { id: assertion.id, rawId: Array.from(new Uint8Array(assertion.rawId)), type: assertion.type, response: { clientDataJSON: Array.from(new Uint8Array(resp.clientDataJSON)), authenticatorData: Array.from(new Uint8Array(resp.authenticatorData)), signature: Array.from(new Uint8Array(resp.signature)), userHandle: resp.userHandle ? Array.from(new Uint8Array(resp.userHandle)) : null } }; const verify = await fetch('/api/webauthn/assertion/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(payload) }); return await verify.json(); } 安全配置与兼容性策略- RP ID 与域:必须与页面来源一致(含子域策略),避免跨域风险 - userVerification:优先 required/preferred,确保强验证 - 允许传输:platform 与 cross‑platform 并存,提升可用性 - 备份方案:提供 TOTP/邮件魔法链接作为兜底登录 - 前端检测:通过 `PublicKeyCredential` 能力检测与引导 技术验证参数在 Chrome 128/Edge 130/Safari 17(Windows 11/macOS 14/iOS 17/Android 13)下:注册成功率:≥ 98%(平台认证器 + 安全密钥)登录断言耗时:P95 280–650ms(本地认证器)失败重试引导命中率:≥ 90%兼容性覆盖:WebAuthn Level 2 功能基本可用应用场景企业单点登录(SSO)与高安全业务入口面向消费者的无密码登录与降摩擦注册后台与管理系统的强验证与二次确认最佳实践采用 `attestation: 'none'` 降低隐私暴露;如需设备证明,服务端严格校验可信根严格校验挑战与来源,开启 HTTPS 与 CSP 防护提供降级登录路径与账号恢复流程,避免锁定用户在 UI 中明确设备与步骤提示,减少失败率

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部