摘要
WebAuthn 通过将基于公钥凭据的身份验证引入 Web,帮助提高安全性,并且很快将在 Chrome、Firefox 和 Edge 中得到支持 (使用更新后的规范)。它添加了一种新的 Credential
对象,但是,这可能会破坏使用 Credential Management API 但未对其使用的特定凭据类型进行功能检测的网站。
如果您目前正在执行此操作进行功能检测
if (navigator.credentials && navigator.credentials.preventSilentAccess) {
// use CM API
}
改为执行这些操作
if (window.PasswordCredential || window.FederatedCredential) {
// Call navigator.credentials.get() to retrieve stored
// PasswordCredentials or FederatedCredentials.
}
if (window.PasswordCredential) {
// Get/Store PasswordCredential
}
if (window.FederatedCredential) {
// Get/Store FederatedCredential
}
if (navigator.credentials && navigator.credentials.preventSilentAccess) {
// Call navigator.credentials.preventSilentAccess()
}
请参阅作为示例对示例代码所做的更改。
继续阅读以了解更多信息。
什么是 Credential Management API
Credential Management API (CM API) 使网站能够以编程方式访问用户代理的凭据存储,以存储/检索调用来源的用户凭据。
基本 API 包括
navigator.credentials.get()
navigator.credentials.store()
navigator.credentials.create()
navigator.credentials.preventSilentAccess()
原始 CM API 规范定义了 2 种凭据类型
PasswordCredential
FederatedCredential
PasswordCredential
是一种包含用户 ID 和密码的凭据。FederatedCredential
是一种包含用户 ID 和表示身份提供商的字符串的凭据。
使用这 2 种凭据,网站可以
- 让用户在登录后立即使用先前保存的基于密码或联合凭据登录(自动登录),
- 存储用户已登录的基于密码或联合凭据,
- 保持用户登录凭据为最新状态(例如,在密码更改后)
什么是 WebAuthn
WebAuthn (Web Authentication) 向 CM API 添加了公钥凭据。例如,它为网站提供了一种使用 FIDO 2.0 兼容的身份验证器设备实现双因素身份验证的标准化方法。
在技术层面上,WebAuthn 使用 PublicKeyCredential
接口扩展了 CM API。
问题是什么?
以前我们一直指导开发人员使用以下代码来检测 CM API 功能
if (navigator.credentials && navigator.credentials.preventSilentAccess) {
// Use CM API
}
But as you can see from the descriptions above, the `navigator.credentials` is
now expanded to support public-key credentials in addition to password
credentials and federated credentials.
The problem is that user agents don't necessarily support all kinds of
credentials. If you continue feature detect using `navigator.credentials`, your
website may break when you are using a certain credential type not supported by
the browser.
**Supported credential types by browsers**
<table class="properties with-heading-tint"><tbody><tr>
<th></th>
<th>PasswordCredential / FederatedCredential</th>
<th>PublicKeyCredential</th>
</tr><tr><th>Chrome
</th><td>Available
</td><td>In development
</td></tr><tr><th>Firefox
</th><td>N/A
</td><td>Aiming to ship on 60
</td></tr><tr><th>Edge
</th><td>N/A
</td><td>Implemented with <a href="https://blogs.windows.com/msedgedev/2016/04/12/a-world-without-passwords-windows-hello-in-microsoft-edge/">older API</a>. New API (navigator.credentials) coming soon.
</td></tr></tbody></table>
## The solution
You can avoid this by modifying feature detection code as follows to explicitly
test for the credential type that you intend to use.
```js
if (window.PasswordCredential || window.FederatedCredential) {
// Call navigator.credentials.get() to retrieve stored
// PasswordCredentials or FederatedCredentials.
}
if (window.PasswordCredential) {
// Get/Store PasswordCredential
}
if (window.FederatedCredential) {
// Get/Store FederatedCredential
}
if (navigator.credentials && navigator.credentials.preventSilentAccess) {
// Call navigator.credentials.preventSilentAccess()
}
请参阅作为示例对示例代码所做的实际更改。
作为参考,以下是如何检测 WebAuthn 中添加的 PublicKeyCredential
if (window.PublicKeyCredential) {
// use CM API with PublicKeyCredential added in the WebAuthn spec
}
时间线
WebAuthn 最早的可用实现是 Firefox,并且计划在 2018 年 5 月初左右稳定。
最后
如果您有任何问题,请发送至 @agektmr 或 agektmr@chromium.org。