PublicKeyCredential
Baseline 广泛可用 *
PublicKeyCredential 接口提供了有关公钥/私钥对的信息,这是一个用于通过防钓鱼和抗数据泄露的不对称密钥对(而不是密码)登录服务的凭证。它继承自 Credential,并且是 Web Authentication API 对 Credential Management API 的扩展的一部分。
注意: 此 API 仅限于顶层上下文。在 <iframe> 元素内使用将不起作用。
实例属性
PublicKeyCredential.authenticatorAttachment只读-
一个字符串,指示在相应的
navigator.credentials.create()或navigator.credentials.get()调用完成时,WebAuthn 实现连接到身份验证器的方式。 PublicKeyCredential.id只读-
继承自
Credential,并被重写为 base64url 编码的PublicKeyCredential.rawId。 PublicKeyCredential.rawId只读-
一个
ArrayBuffer,其中包含此PublicKeyCredential的全局唯一标识符。此标识符可用于在将来的navigator.credentials.get()调用中查找凭证。 PublicKeyCredential.response只读-
一个
AuthenticatorResponse对象的实例。如果PublicKeyCredential是navigator.credentials.create()调用结果,则其类型为AuthenticatorAttestationResponse;如果PublicKeyCredential是navigator.credentials.get()调用结果,则其类型为AuthenticatorAssertionResponse。 PublicKeyCredential.type只读-
继承自
Credential。对于PublicKeyCredential实例,始终设置为public-key。
静态方法
PublicKeyCredential.getClientCapabilities()-
返回一个
Promise,该 Promise 解析为一个对象,可用于检查是否支持特定的 WebAuthn 功能和 扩展。 -
返回一个
Promise,该 Promise 解析为true(如果条件中介可用)。 PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable()-
返回一个
Promise,该 Promise 解析为true(如果绑定到平台的身份验证器能够验证用户)。 PublicKeyCredential.parseCreationOptionsFromJSON()-
在注册用户凭证时,用于反序列化服务器发送的凭证注册数据的便捷方法。
PublicKeyCredential.parseRequestOptionsFromJSON()-
在认证(已注册)用户时,用于反序列化服务器发送的凭证请求数据的便捷方法。
PublicKeyCredential.signalAllAcceptedCredentials()PublicKeyCredential.signalCurrentUserDetails()-
向身份验证器发出信号,表明特定用户已更新其用户名和/或显示名称。
PublicKeyCredential.signalUnknownCredential()
实例方法
PublicKeyCredential.getClientExtensionResults()-
如果请求了任何扩展,此方法将返回处理这些扩展的结果。
PublicKeyCredential.toJSON()-
在注册用户凭证和认证已注册用户时,用于创建
PublicKeyCredential的 JSON 字符串表示并发送到服务器的便捷方法。
示例
创建 PublicKeyCredential 的新实例
在这里,我们使用 navigator.credentials.create() 来生成一个新凭证。
const createCredentialOptions = {
publicKey: {
challenge: new Uint8Array([
21, 31, 105 /* 29 more random bytes generated by the server */,
]),
rp: {
name: "Example CORP",
id: "login.example.com",
},
user: {
id: new Uint8Array(16),
name: "canand@example.com",
displayName: "Carina Anand",
},
pubKeyCredParams: [
{
type: "public-key",
alg: -7,
},
],
},
};
navigator.credentials
.create(createCredentialOptions)
.then((newCredentialInfo) => {
const response = newCredentialInfo.response;
const clientExtensionsResults =
newCredentialInfo.getClientExtensionResults();
})
.catch((err) => {
console.error(err);
});
获取 PublicKeyCredential 的现有实例
在这里,我们使用 navigator.credentials.get() 从身份验证器获取一个现有凭证。
const requestCredentialOptions = {
publicKey: {
challenge: new Uint8Array([
/* bytes sent from the server */
]),
},
};
navigator.credentials
.get(requestCredentialOptions)
.then((credentialInfoAssertion) => {
// send assertion response back to the server
// to proceed with the control of the credential
})
.catch((err) => {
console.error(err);
});
规范
| 规范 |
|---|
| Web Authentication:访问公钥凭证的 API - 第 3 级 # iface-pkcredential |
浏览器兼容性
加载中…
另见
- 父接口
Credential