Skip to content

Core\Passkey\Credential::id

$credential->id(): bytes

Returns the id of the passkey, as bytes. The id is from 1 to 1023 bytes.

The device creates the id when the user registers the passkey. When the user signs in, the browser sends the same id. Core\Passkey::credentialId reads it from the browser's answer. Compare that id with the ids you saved to find the passkey and its account.

Save the id with the other three values of the passkey: publicKey(), algorithm() and signCount(). A database column for bytes stores it unchanged. To save it as text, use Core\Encoding::toBase64Url. The browser writes the id in the same way, so the two texts are equal.

The examples below read the id, find the passkey of a sign-in, and save the id as text.

Return value

Returns bytes. From 1 to 1023 bytes.

Examples

Read the id

<?nvs
// Shows how to read the id of a saved passkey.
// The application saved these values when the user registered the passkey.
bytes $id = Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16');
bytes $publicKey = Core\Encoding::fromHex(
'a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac',
);
Core\Passkey\Credential $passkey = Core\Passkey\Credential::of(
$id,
$publicKey,
Core\Passkey\Credential::Algorithm::Ed25519,
0,
);
// `id()` returns the bytes that were passed to `of`.
// This prints `16 bytes`, and the id as hexadecimal digits.
echo Core\Bytes::length($passkey->id()), " bytes\n";
echo Core\Encoding::toHex($passkey->id()), "\n";
Output
16 bytes
6b1f0c9a24d35e7781a4c2f0e93b5d16

Find the passkey of a sign in

<?nvs
// Shows how to find the saved passkey that a sign-in answer belongs to.
// The application saved two passkeys. Both use the same made-up public key here.
bytes $publicKey = Core\Encoding::fromHex(
'a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac',
);
array<Core\Passkey\Credential> $saved = [
Core\Passkey\Credential::of(Core\Encoding::fromHex('c2e93b5d166b1f0c9a24d35e7781a4f0'), $publicKey, Core\Passkey\Credential::Algorithm::Ed25519, 3),
Core\Passkey\Credential::of(Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16'), $publicKey, Core\Passkey\Credential::Algorithm::Ed25519, 8),
];
// This is the answer a browser sends. The values in `response` are shortened here.
string $answer = '{"id":"ax8MmiTTXneBpMLw6TtdFg","type":"public-key",'
. '"response":{"clientDataJSON":"eyJ0","authenticatorData":"SZYN","signature":"MEUC"}}';
bytes $sent = Core\Passkey::credentialId($answer);
// The loop compares the id in the answer with the id of each saved passkey.
?Core\Passkey\Credential $found = null;
foreach ($saved as Core\Passkey\Credential $passkey) {
if ($passkey->id() == $sent) {
$found = $passkey;
}
}
// This prints `found the passkey with the counter 8`.
// The next step checks the answer with `Core\Passkey::verifySignIn`.
echo $found == null ? 'no passkey' : 'found the passkey with the counter ' . $found->signCount(), "\n";
Output
found the passkey with the counter 8

Save the id as text

<?nvs
// Shows how to save the id of a passkey as text, and how to read it back.
bytes $publicKey = Core\Encoding::fromHex(
'a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac',
);
Core\Passkey\Credential $passkey = Core\Passkey\Credential::of(
Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16'),
$publicKey,
Core\Passkey\Credential::Algorithm::Ed25519,
0,
);
// `toBase64Url` converts the bytes to text. This prints `ax8MmiTTXneBpMLw6TtdFg`.
// The browser sends the same text in the `id` field of its answer.
string $text = Core\Encoding::toBase64Url($passkey->id());
echo $text, "\n";
// `fromBase64Url` converts the text back to bytes.
// The passkey is created again from the saved values, and its id is the same. This prints `same id`.
Core\Passkey\Credential $loaded = Core\Passkey\Credential::of(
Core\Encoding::fromBase64Url($text),
$publicKey,
Core\Passkey\Credential::Algorithm::Ed25519,
0,
);
echo $loaded->id() == $passkey->id() ? 'same id' : 'another id', "\n";
Output
ax8MmiTTXneBpMLw6TtdFg
same id