Core\Passkey::credentialId
Core\Passkey::credentialId(string $response): bytesCore\Passkey::credentialId( string $response, ): bytes
Returns the passkey id in the browser's answer to Core\Passkey::signInOptions, as bytes.
Use the id to find the saved passkey and its account. Then pass the answer and that passkey to Core\Passkey::verifySignIn. The id is what the browser sent, and nothing checks it here. So when the answer comes from a request, the result is tainted. Compare it with the ids you saved, or use it as a key to look one up.
The answer is the JSON of credential.toJSON() in the browser. The id is from 1 to 1023 bytes. An answer that is not JSON, or that has no valid id, throws a RuntimeError.
The examples below read the id from an answer, find the account it belongs to, and show both halves of a sign-in page.
Parameters
$responsestringThe credential thatnavigator.credentials.get()returned, as JSON. In the browser,JSON.stringify(credential)makes it.
Return value
Returns bytes. The passkey id, from 1 to 1023 bytes.
Errors
RuntimeErrorThe answer is not JSON or is longer than 64 KB. It is also thrown when theidis missing, is not base64url or is too long.
Examples
The id in a sign in answer
<?nvs// Shows how to read the passkey id from the browser's answer to a sign-in.
// This is the answer a browser sends. The values in `response` are shortened here.string $answer = '{"id":"ax8MmiTTXneBpMLw6TtdFg","rawId":"ax8MmiTTXneBpMLw6TtdFg","type":"public-key",' . '"response":{"clientDataJSON":"eyJ0","authenticatorData":"SZYN","signature":"MEUC"}}';
// `credentialId` reads the `id` field and decodes it from base64url.bytes $id = Core\Passkey::credentialId($answer);
// This prints `16 bytes`, and the id as hexadecimal digits.echo Core\Bytes::length($id), " bytes\n";echo Core\Encoding::toHex($id), "\n";16 bytes6b1f0c9a24d35e7781a4c2f0e93b5d16Find the account
<?nvs// Shows how to find the account that a passkey id in a request belongs to.
class Account{ public function constructor(public string $email, public bytes $passkeyId) {}}
// The application saved one passkey id for each account.array<Account> $accounts = [ new Account('ada@example.com', Core\Encoding::fromHex('c2e93b5d166b1f0c9a24d35e7781a4f0')), new Account('grace@example.com', Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16')),];
// The id comes from the request body, so it is `tainted`.// It is what the browser sent, and nothing has checked it yet.tainted bytes $id = Core\Passkey::credentialId(Core\Request::body());
// Compare the id with each saved id. The result of `==` is a plain `bool`.?Account $found = null;foreach ($accounts as Account $account) { if ($account->passkeyId == $id) { $found = $account; }}
// This prints `grace@example.com`. The next step checks the answer with `Core\Passkey::verifySignIn`.echo $found == null ? 'no account' : $found->email, "\n";grace@example.comA sign in page
<?nvs// Shows the browser half and the server half of a page that signs in with a passkey.
class Account{ public function constructor(public string $email, public Core\Passkey\Credential $passkey) {}}
class SignInPage{ // The browser half. This JavaScript gets the options from the server, // lets the user sign in with a passkey and sends the answer back to the server. public static function script(): string { return "const response = await fetch('/sign-in/options', { method: 'POST' });\n" . "const options = PublicKeyCredential.parseRequestOptionsFromJSON(await response.json());\n" . "const credential = await navigator.credentials.get({ publicKey: options });\n" . "await fetch('/sign-in', { method: 'POST', body: JSON.stringify(credential.toJSON()) });\n"; }
// The server half. It answers the second `fetch` in the script. // It returns the account with the passkey id in the answer, or `null`. public static function find(tainted string $answer, array<Account> $accounts): ?Account { var $id = Core\Passkey::credentialId($answer); foreach ($accounts as Account $account) { if ($account->passkey->id() == $id) { return $account; } } return null; }}
// The page contains this script.echo SignInPage::script(), "\n";
// The application saved a passkey for each account. Here the keys are made up with `Core\Crypto`.var $key = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);bytes $raw = $key->publicKey()->write(Core\Crypto\PublicKey::Format::Raw);bytes $publicKey = Core\Bytes::join([Core\Encoding::fromHex('a4010103272006215820'), $raw]);array<Account> $accounts = [];array<string> $saved = [ 'ada@example.com' => 'c2e93b5d166b1f0c9a24d35e7781a4f0', 'grace@example.com' => '6b1f0c9a24d35e7781a4c2f0e93b5d16',];foreach ($saved as string $email => string $hex) { var $passkey = Core\Passkey\Credential::of(Core\Encoding::fromHex($hex), $publicKey, Core\Passkey\Credential::Algorithm::Ed25519, 0); $accounts[] = new Account($email, $passkey);}
// This request is the script's second `fetch`. Its body is the browser's answer.?Account $account = SignInPage::find(Core\Request::body(), $accounts);
// The account is found, but the user is not signed in yet.// Pass the answer and `$account->passkey` to `Core\Passkey::verifySignIn` to check the signature.echo $account == null ? 'unknown passkey' : 'passkey of ' . $account->email, "\n";const response = await fetch('/sign-in/options', { method: 'POST' });const options = PublicKeyCredential.parseRequestOptionsFromJSON(await response.json());const credential = await navigator.credentials.get({ publicKey: options });await fetch('/sign-in', { method: 'POST', body: JSON.stringify(credential.toJSON()) });
passkey of grace@example.com