Kiểm tra khả năng phát hiện tính năng của Credential Management API

Tóm tắt

WebAuthn giúp tăng cường bảo mật bằng cách đưa phương thức xác thực dựa trên thông tin xác thực khoá công khai lên Web và sẽ sớm được hỗ trợ trong Chrome, Firefox và Edge (với thông số kỹ thuật mới). Tính năng này bổ sung một loại đối tượng Credential mới, tuy nhiên, có thể làm hỏng các trang web sử dụng API Quản lý thông tin xác thực mà không phát hiện tính năng của các loại thông tin xác thực cụ thể mà họ đang sử dụng.

Nếu bạn đang làm việc này để phát hiện tính năng

if (navigator.credentials && navigator.credentials.preventSilentAccess) {
    // use CM API
}

Hãy làm những việc sau

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()
}

Hãy xem các thay đổi đối với mã mẫu để biết ví dụ.

Hãy đọc tiếp để tìm hiểu thêm.

Credential Management API là gì?

API Quản lý thông tin xác thực (CM API) cung cấp cho các trang web quyền truy cập có lập trình vào kho lưu trữ thông tin xác thực của tác nhân người dùng để lưu trữ/truy xuất thông tin xác thực của người dùng cho nguồn gốc gọi.

Các API cơ bản là:

  • navigator.credentials.get()
  • navigator.credentials.store()
  • navigator.credentials.create()
  • navigator.credentials.preventSilentAccess()

Quy cách ban đầu của CM API xác định 2 loại thông tin xác thực:

  • PasswordCredential
  • FederatedCredential

PasswordCredential là thông tin xác thực chứa mã nhận dạng và mật khẩu của người dùng. FederatedCredential là thông tin xác thực chứa mã nhận dạng của người dùng và một chuỗi đại diện cho nhà cung cấp danh tính.

Với 2 thông tin xác thực này, các trang web có thể:

  • Cho phép người dùng đăng nhập bằng thông tin đăng nhập liên kết hoặc dựa trên mật khẩu đã lưu trước đó ngay khi họ truy cập (tự động đăng nhập),
  • Lưu trữ thông tin đăng nhập dựa trên mật khẩu hoặc thông tin đăng nhập liên kết mà người dùng đã đăng nhập,
  • Luôn cập nhật thông tin đăng nhập của người dùng (ví dụ: sau khi thay đổi mật khẩu)

WebAuthn là gì

WebAuthn (Xác thực web) thêm thông tin xác thực khoá công khai vào CM API. Ví dụ: API này cung cấp cho các trang web một cách thức chuẩn hoá để triển khai quy trình xác thực hai yếu tố bằng các thiết bị xác thực tuân thủ FIDO 2.0.

Ở cấp độ kỹ thuật, WebAuthn mở rộng CM API bằng giao diện PublicKeyCredential.

Vấn đề là gì?

Trước đây, chúng tôi đã hướng dẫn nhà phát triển phát hiện tính năng của API CM bằng mã sau:

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()
}

Hãy xem các thay đổi thực tế đối với mã mẫu để biết ví dụ.

Để tham khảo, sau đây là cách phát hiện PublicKeyCredential được thêm vào WebAuthn:

if (window.PublicKeyCredential) {
    // use CM API with PublicKeyCredential added in the WebAuthn spec
}

Dòng thời gian

Firefox là trình duyệt đầu tiên triển khai WebAuthn và dự kiến sẽ ổn định vào khoảng đầu tháng 5 năm 2018.

Cuối cùng

Nếu bạn có thắc mắc, hãy gửi câu hỏi đến @agektmr hoặc agektmr@chromium.org.