let credential = await navigator.credentials.create({
publicKey: {
challenge: new Uint8Array([117, 61, 252, 231, 191, 241 /* … */]),
rp: { id: "acme.com", name: "ACME Corporation" },
user: {
id: new Uint8Array([79, 252, 83, 72, 214, 7, 89, 26]),
name: "jamiedoe",
displayName: "Jamie Doe",
},
pubKeyCredParams: [{ type: "public-key", alg: -7 }],
},
});Web Authentication API / notebook
Web Authentication API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
async function signInWithConditionalCreate() {
const accountDetails = await signIn();
try {
conditionalCreate(accountDetails);
} catch (e) {
// Conditional create failed: ignore the error
}
}
async function conditionalCreate(account) {
const capabilities = await PublicKeyCredential.getClientCapabilities();
// Only attempt conditional create if it is supported,
// and if the user doesn't already have a passkey
if (capabilities.conditionalCreate && !account.hasPasskey) {
// Abort any existing WebAuthn calls
webAuthnController.abort();
// Get creation options, including the challenge value from the server
const pkOptions = await getNewPasskeyOptions();
const credential = await navigator.credentials.create({
publicKey: pkOptions,
mediation: "conditional",
});
// Conditional create succeeded: send the new credential to the server
// for validation and storage
await processNewPasskey(credential);
}
} let credential = await navigator.credentials.get({
publicKey: {
challenge: new Uint8Array([139, 66, 181, 87, 7, 203 /* … */]),
rpId: "acme.com",
allowCredentials: [
{
type: "public-key",
id: new Uint8Array([64, 66, 25, 78, 168, 226, 174 /* … */]),
},
],
userVerification: "required",
},
});<input type="text" name="username" autocomplete="username webauthn" />const support = await PublicKeyCredential.getClientCapabilities();
const supported = support.conditionalGet;
if (supported) {
const options = {
challenge: challengeFromServer,
rpId: "example.com",
userVerification: "required",
// allowCredentials is omitted here
};
const assertion = await navigator.credentials.get({
publicKey: options,
mediation: "conditional",
});
}async function checkIsUserVerifyingPlatformAuthenticatorAvailable() {
const capabilities = await PublicKeyCredential.getClientCapabilities();
// Check the capability: userVerifyingPlatformAuthenticator
if (capabilities.userVerifyingPlatformAuthenticator) {
// Perform actions if biometric support is available
} else {
// Perform actions if biometric support is not available.
}
} <iframe
src="https://auth.provider.com"
allow="publickey-credentials-get *">
</iframe> <iframe
src="https://auth.provider.com"
allow="publickey-credentials-create 'self' https://a.auth.provider.com https://b.auth.provider.com">
</iframe>// sample arguments for registration
const createCredentialDefaultArgs = {
publicKey: {
// Relying Party (a.k.a. - Service):
rp: {
name: "Acme",
},
// User:
user: {
id: new Uint8Array(16),
name: "carina.p.anand@example.com",
displayName: "Carina P. Anand",
},
pubKeyCredParams: [
{
type: "public-key",
alg: -7,
},
],
attestation: "direct",
timeout: 60000,
challenge: new Uint8Array([
// must be a cryptographically random number sent from a server
0x8c, 0x0a, 0x26, 0xff, 0x22, 0x91, 0xc1, 0xe9, 0xb9, 0x4e, 0x2e, 0x17,
0x1a, 0x98, 0x6a, 0x73, 0x71, 0x9d, 0x43, 0x48, 0xd5, 0xa7, 0x6a, 0x15,
0x7e, 0x38, 0x94, 0x52, 0x77, 0x97, 0x0f, 0xef,
]).buffer,
},
};
// sample arguments for login
const getCredentialDefaultArgs = {
publicKey: {
timeout: 60000,
// allowCredentials: [newCredential] // see below
challenge: new Uint8Array([
// must be a cryptographically random number sent from a server
0x79, 0x50, 0x68, 0x71, 0xda, 0xee, 0xee, 0xb9, 0x94, 0xc3, 0xc2, 0x15,
0x67, 0x65, 0x26, 0x22, 0xe3, 0xf3, 0xab, 0x3b, 0x78, 0x2e, 0xd5, 0x6f,
0x81, 0x26, 0xe2, 0xa6, 0x01, 0x7d, 0x74, 0x50,
]).buffer,
},
};
// register / create a new credential
navigator.credentials
.create(createCredentialDefaultArgs)
.then((cred) => {
console.log("NEW CREDENTIAL", cred);
// normally the credential IDs available for an account would come from a server
// but we can just copy them from above…
const idList = [
{
id: cred.rawId,
transports: ["usb", "nfc", "ble"],
type: "public-key",
},
];
getCredentialDefaultArgs.publicKey.allowCredentials = idList;
return navigator.credentials.get(getCredentialDefaultArgs);
})
.then((assertion) => {
console.log("ASSERTION", assertion);
})
.catch((err) => {
console.log("ERROR", err);
});