PublicKeyCredential - 提供有关公钥 / 私钥对的信息
PublicKeyCredential 接口提供有关公钥 / 私钥对的信息,它是用于使用不可仿冒且抗数据泄露的非对称密钥对(而不是密码)登录到服务的凭据。它继承自 Credential,属于 Web 身份验证 API,扩展到 Credential Management API。从 Credential 继承的其他接口是 PasswordCredential 和 FederatedCredential。
注意: 此 API 仅限于顶级上下文。在
<iframe>元素内使用不会有任何效果。
属性
PublicKeyCredential.type 只读 安全上下文
继承自 Credential。对于 PublicKeyCredential 实例,值始终为 "public-key"。
PublicKeyCredential.id 只读 安全上下文
继承自 Credential,并覆盖为 PublicKeyCredential.rawId 的 base64url 编码 。
PublicKeyCredential.rawId 只读 安全上下文
一个 ArrayBuffer,包含此 PublicKeyCredential 的全局唯一标识符。此标识符可用于查找凭据,以便将来对 CredentialsContainer.get 进行调用。
PublicKeyCredential.response 只读 安全上下文
一个 AuthenticatorResponse 对象的实例。如果 PublicKeyCredential 是调用 navigator.credentials.create() 返回的, 则其类型为 AuthenticatorAttestationResponse,如果 PublicKeyCredential 是调用 navigator.credentials.get() 返回的,则其类型为 AuthenticatorAssertionResponse。
方法
PublicKeyCredential.getClientExtensionResults() 安全上下文
如果请求包含了扩展,则此方法将返回处理这些扩展的结果。
PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable() 安全上下文
一个静态方法,返回了 Promise,如果绑定到平台的身份验证器能够“验证” 用户,则解析为 true。在当前的实现状态下,仅当系统上 Windows Hello 可用时,此方法才解析为 true。
实例
创建一个新的 PublicKeyCredential 实例
在这里,我们使用 navigator.credentials.create() 生成新的凭据。
var publicKey = {
challenge: /* 来自服务器 */,
rp: {
name: "示例组织",
id : "login.example.com"
},
user: {
id: new Uint8Array(16),
name: "jdoe@example.com",
displayName: "John Doe"
},
pubKeyCredParams: [
{
type: "public-key",
alg: -7
}
]
};
navigator.credentials.create({ publicKey })
.then(function (newCredentialInfo) {
var response = newCredentialInfo.response;
var clientExtensionsResults = newCredentialInfo.getClientExtensionResults();
}).catch(function (err) {
console.error(err);
});
获取现有的 PublicKeyCredential 实例
在这里,我们使用 navigator.credentials.get() 从身份验证器获取现有凭证。
var options = {
challenge: new Uint8Array([/* 服务器发送的字节 */])
};
navigator.credentials.get({ "publicKey": options })
.then(function (credentialInfoAssertion) {
// 将断言响应发送回服务器以继续控制证书
}).catch(function (err) {
console.error(err);
});
规范
| 规范 | 状态 | 备注 |
|---|---|---|
| Web Authentication: An API for accessing Public Key Credentials Level 1 PublicKeyCredential interface 的定义 |
候选推荐 | 初始定义。 |
桌面浏览器兼容性
| 特性 | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| 基础支持 | 67 651 | 18 | 601 | 不支持 | 不支持 | 不支持 |
getClientExtensionResults | 67 651 | 18 | 601 | 不支持 | 不支持 | 不支持 |
isUserVerifyingPlatformAuthenticatorAvailable | 67 651 | 18 | 601 | 不支持 | 不支持 | 不支持 |
rawId | 67 651 | 18 | 601 | 不支持 | 不支持 | 不支持 |
response | 67 651 | 18 | 601 | 不支持 | 不支持 | 不支持 |
移动浏览器兼容性
| 特性 | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
|---|---|---|---|---|---|---|---|
| 基础支持 | 70 | 70 | 未知 | 601 | 未知 | 不支持 | 不支持 |
getClientExtensionResults | 70 | 70 | 未知 | 601 | 未知 | 不支持 | 不支持 |
isUserVerifyingPlatformAuthenticatorAvailable | 70 | 70 | 未知 | 601 | 未知 | 不支持 | 不支持 |
rawId | 70 | 70 | 未知 | 601 | 未知 | 不支持 | 不支持 |
response | 70 | 70 | 未知 | 601 | 未知 | 不支持 | 不支持 |
1. 仅支持 USB U2F 令牌。
相关链接
- 父接口
Credential