Core\Passkey\Credential::signCount
$credential->signCount(): intReturns the sign counter of the passkey. The counter is a whole number from 0 to 4294967295.
Some devices count each sign-in, and send the count with each sign-in. Core\Passkey::verifySignIn returns the passkey with the new count. Save that count. At the next sign-in, the device must send a higher count. If it does not, verifySignIn throws a RuntimeError. A count that does not rise can mean that somebody copied the passkey.
Many devices do not count, and always send 0. When the saved count and the new count are both 0, the sign-in succeeds, and the counter stays 0.
The examples below read the counter, save the new counter after a sign-in, and sign in with a device that does not count.
Return value
Returns int. A whole number from 0 to 4294967295.
Examples
The examples below run with this configuration file.
# A sign-in saves its challenge in the session, and a session keeps its data in a store.# These examples use the Redis server on this computer at port 16379,# and allow the program to use it.[capabilities.cache]shared = true
[session]backend = "shared"
[cache.shared]url = "redis://127.0.0.1:16379"Read the counter
<?nvs// Shows how to read the sign counter of a saved passkey.
// The application saved these values after the last sign-in.Core\Passkey\Credential $passkey = Core\Passkey\Credential::of( Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16'), Core\Encoding::fromHex('a4010103272006215820fcce9008a33565248561295a7ce1cccb040b6129bba8ff24744953bbc01261ac'), Core\Passkey\Credential::Algorithm::Ed25519, 41,);
// `signCount()` returns the number that was passed to `of`. This prints `41`.echo $passkey->signCount(), "\n";
// The next sign-in must send a higher count. This prints `the next count must be 42 or higher`.echo 'the next count must be ', $passkey->signCount() + 1, " or higher\n";41the next count must be 42 or higherSave the new counter
<?nvs// Shows how to save the new sign counter after each sign-in.// requires: redis
// This class plays the browser and the device. In real use, the browser makes this answer.// It follows the WebAuthn standard, and you do not write this code yourself.final class Device{ public static function signIn(string $options, Core\Crypto\KeyPair $key, bytes $id, int $count): tainted string { var $read = Core\Json::decode($options) as array<mixed>; string $challenge = Core\Taint::assertTrusted($read['challenge'] as string, 'this program wrote the options'); bytes $client = Core\Encoding::encodeText( '{"type":"webauthn.get","challenge":"' . $challenge . '","origin":"https://example.com"}', Core\Charset::Utf8, ); bytes $data = Core\Bytes::join([ Core\Hash::of('example.com', Core\Hash::Digest::Sha256), Core\Bytes::pack('C', 0x05), Core\Bytes::pack('N', $count), ]); bytes $signature = Core\Crypto::sign(Core\Bytes::join([$data, Core\Hash::of($client, Core\Hash::Digest::Sha256)]), $key); return '{"type":"public-key","id":"' . Core\Encoding::toBase64Url($id) . '","response":{"clientDataJSON":"' . Core\Encoding::toBase64Url($client) . '","authenticatorData":"' . Core\Encoding::toBase64Url($data) . '","signature":"' . Core\Encoding::toBase64Url($signature) . '"}}'; }}
// The application saved this passkey with the sign counter 4.Core\Crypto\KeyPair $key = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);bytes $publicKey = Core\Bytes::join([ Core\Encoding::fromHex('a4010103272006215820'), $key->publicKey()->write(Core\Crypto\PublicKey::Format::Raw),]);Core\Passkey\Credential $saved = Core\Passkey\Credential::of(Core\Random::bytes(16), $publicKey, Core\Passkey\Credential::Algorithm::Ed25519, 4);
// The device signs in three times. Its counts are 5, 9 and 12.// After each sign-in, the application saves the passkey that `verifySignIn` returns.array<int> $counts = [5, 9, 12];foreach ($counts as int $count) { string $options = Core\Passkey::signInOptions('example.com', [$saved]); tainted string $answer = Device::signIn($options, $key, $saved->id(), $count); $saved = Core\Passkey::verifySignIn($answer, $saved, 'example.com', 'https://example.com'); echo 'signed in, saved counter: ', $saved->signCount(), "\n";}
// A copy of the passkey sends the count 10. That is lower than the saved count 12,// so `verifySignIn` throws a `RuntimeError`.string $next = Core\Passkey::signInOptions('example.com', [$saved]);try { Core\Passkey::verifySignIn(Device::signIn($next, $key, $saved->id(), 10), $saved, 'example.com', 'https://example.com'); echo "the copy signed in\n";} catch (RuntimeError $e) { echo $e->message, "\n";}signed in, saved counter: 5signed in, saved counter: 9signed in, saved counter: 12Core\Passkey::verifySignIn(): the sign counter is 10, and the saved passkey has 12. The counter must increase, so the passkey may have been copied.A device that does not count
<?nvs// Shows the sign counter of a device that does not count its sign-ins.// requires: redis
// This class plays the browser and the device. This device always sends the count 0.// It follows the WebAuthn standard, and you do not write this code yourself.final class Device{ public static function signIn(string $options, Core\Crypto\KeyPair $key, bytes $id): tainted string { var $read = Core\Json::decode($options) as array<mixed>; string $challenge = Core\Taint::assertTrusted($read['challenge'] as string, 'this program wrote the options'); bytes $client = Core\Encoding::encodeText( '{"type":"webauthn.get","challenge":"' . $challenge . '","origin":"https://example.com"}', Core\Charset::Utf8, ); bytes $data = Core\Bytes::join([ Core\Hash::of('example.com', Core\Hash::Digest::Sha256), Core\Bytes::pack('C', 0x05), Core\Bytes::pack('N', 0), ]); bytes $signature = Core\Crypto::sign(Core\Bytes::join([$data, Core\Hash::of($client, Core\Hash::Digest::Sha256)]), $key); return '{"type":"public-key","id":"' . Core\Encoding::toBase64Url($id) . '","response":{"clientDataJSON":"' . Core\Encoding::toBase64Url($client) . '","authenticatorData":"' . Core\Encoding::toBase64Url($data) . '","signature":"' . Core\Encoding::toBase64Url($signature) . '"}}'; }}
Core\Crypto\KeyPair $key = Core\Crypto::generateKeyPair(Core\Crypto\KeyKind::Ed25519);bytes $publicKey = Core\Bytes::join([ Core\Encoding::fromHex('a4010103272006215820'), $key->publicKey()->write(Core\Crypto\PublicKey::Format::Raw),]);Core\Passkey\Credential $saved = Core\Passkey\Credential::of(Core\Random::bytes(16), $publicKey, Core\Passkey\Credential::Algorithm::Ed25519, 0);
// The saved count and the new count are both 0, so each sign-in succeeds.// The counter stays 0. This prints `signed in, counter 0` three times.int $n = 0;while ($n < 3) { string $options = Core\Passkey::signInOptions('example.com', [$saved]); $saved = Core\Passkey::verifySignIn(Device::signIn($options, $key, $saved->id()), $saved, 'example.com', 'https://example.com'); echo 'signed in, counter ', $saved->signCount(), "\n"; $n = $n + 1;}signed in, counter 0signed in, counter 0signed in, counter 0