Credential Management API 功能偵測檢查

摘要

WebAuthn 將以公開金鑰憑證為基礎的驗證機制導入網路,有助於提高安全性,且 Chrome、Firefox 和 Edge 即將支援這項機制 (採用更新後的規格)。這項功能會新增一種 Credential 物件,但如果網站使用憑證管理 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 browser<s**
table class="properties with-head><ing-t><in>t<&q><uot>;<tb>odytr
th/th
thPasswordCredential / Feder<ate>d<Cr>edential/th
thPubli<cKe>y<Cre><de><nt>ial/th
</tr><tr>thChrome
/<tht><dA>vailable
/tdtdI<n d><eve><lo><pm>ent
/td/<trt><rt>hFir<efo><x<>/span>
/thtdN/A
/tdtdAiming <to ><shi><p ><on> 60
/<td/><tr>trth<Edg><e<>/span>
/thtdN/A
/tdtdImp<lemented with a href="https://blogs.windows.com/msedgedev/2016/04/12/a-world-without-passwords-windows-hello->in-micros<of>t-edge/"older API/a. New API (navigator.cr<ede><nti><als) c><oming >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
}

時間安排

Firefox 是最早實作 WebAuthn 的瀏覽器,預計在 2018 年 5 月初推出穩定版本。

最終結果

如有任何問題,請傳送至 @agektmr 或 agektmr@chromium.org。