Skip to content

Core\Passkey\Credential::of

Creates a Core\Passkey\Credential again from the four values that you saved with the account.

When a user registers a passkey, Core\Passkey::verifyRegistration returns a Core\Passkey\Credential. Save its id(), publicKey(), algorithm() and signCount() with the account. When the user signs in, pass the four saved values to of. Then pass the passkey to Core\Passkey::verifySignIn. After each sign-in, save the new signCount().

of checks the values again. The public key must be a COSE key of the algorithm that you pass. An ES256 key must be a point on the P-256 curve, and an Ed25519 key must be 32 bytes. The id must be from 1 to 1023 bytes. The sign counter must be from 0 to 4294967295. If a check fails, of throws a LogicError. A saved row that was changed, or saved with a mistake, does not become a passkey that a user can sign in with.

The passkey contains no secret. A public key can only check signatures, so you can save it like any other value.

The examples below create a passkey from its four values, save one as text and read it back, and show the errors for values that do not fit together.

Parameters

  • $idbytesThe value that id() returned.
  • $publicKeybytesThe value that publicKey() returned.
  • $algorithmCore\Passkey\Credential::AlgorithmThe value that algorithm() returned.
  • $signCountintThe value that signCount() returned.

Return value

Returns Core\Passkey\Credential. The passkey.

Errors

  • LogicError$publicKey is not a key of $algorithm, or $id is empty or longer than 1023 bytes. It is also thrown when $signCount is below 0 or above 4294967295.

Examples

Build a saved passkey again

<?nvs
// Shows how to create a passkey again from the four values that you saved with the account.
// These four values were saved when the user registered the passkey.
// The public key is an Ed25519 key in the COSE format that WebAuthn uses.
bytes $id = Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16');
bytes $publicKey = Core\Encoding::fromHex(
'a4010103272006215820d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a',
);
Core\Passkey\Credential::Algorithm $algorithm = Core\Passkey\Credential::Algorithm::Ed25519;
int $signCount = 12;
// `of` checks that the public key is an Ed25519 key, and returns the passkey.
Core\Passkey\Credential $passkey = Core\Passkey\Credential::of($id, $publicKey, $algorithm, $signCount);
// The methods of the passkey return the saved values. This prints the id, `42 bytes` and `12`.
echo Core\Encoding::toHex($passkey->id()), "\n";
echo Core\Bytes::length($passkey->publicKey()), " bytes\n";
echo $passkey->signCount(), "\n";
Output
6b1f0c9a24d35e7781a4c2f0e93b5d16
42 bytes
12

A passkey saved as text

<?nvs
// Shows how to save a passkey as text, for example in a database row, and create it again.
class Saved
{
// The algorithm is saved as its name, so the row is easy to read.
public static function algorithmName(Core\Passkey\Credential::Algorithm $algorithm): string
{
return match ($algorithm) {
Core\Passkey\Credential::Algorithm::ES256 => 'ES256',
Core\Passkey\Credential::Algorithm::Ed25519 => 'Ed25519',
};
}
// Any other name in the row throws an error.
public static function algorithm(string $name): Core\Passkey\Credential::Algorithm
{
return match ($name) {
'ES256' => Core\Passkey\Credential::Algorithm::ES256,
'Ed25519' => Core\Passkey\Credential::Algorithm::Ed25519,
default => throw new LogicError("unknown passkey algorithm {$name}"),
};
}
}
// This passkey has an ES256 key. Its point is the generator of the P-256 curve.
Core\Passkey\Credential $registered = Core\Passkey\Credential::of(
Core\Encoding::fromHex('c2e93b5d166b1f0c'),
Core\Encoding::fromHex(
'a5010203262001215820'
. '6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296'
. '225820'
. '4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5',
),
Core\Passkey\Credential::Algorithm::ES256,
0,
);
// Each value becomes text. The bytes are saved in base64url.
array<string> $row = [
'id' => Core\Encoding::toBase64Url($registered->id()),
'public_key' => Core\Encoding::toBase64Url($registered->publicKey()),
'algorithm' => Saved::algorithmName($registered->algorithm()),
'sign_count' => $registered->signCount() as string,
];
echo $row['id'], ' ', $row['algorithm'], ' ', $row['sign_count'], "\n";
// Later, the row is read back, and `of` creates the passkey from it.
Core\Passkey\Credential $passkey = Core\Passkey\Credential::of(
Core\Encoding::fromBase64Url($row['id']),
Core\Encoding::fromBase64Url($row['public_key']),
Saved::algorithm($row['algorithm']),
$row['sign_count'] as int,
);
// This prints `true`. The key that was read back is the key that was saved.
echo $passkey->publicKey() == $registered->publicKey() ? 'true' : 'false', "\n";
Output
wuk7XRZrHww ES256 0
true

A saved record that was changed

<?nvs
// Shows the errors that `of` throws when the saved values do not fit together.
class Load
{
// Creates the passkey. If a value is wrong, it returns the error message.
public static function check(bytes $id, bytes $publicKey, Core\Passkey\Credential::Algorithm $algorithm, int $signCount): tainted string
{
try {
Core\Passkey\Credential::of($id, $publicKey, $algorithm, $signCount);
return 'the passkey is ready';
} catch (LogicError $e) {
return Core\Str::after($e->message, '(): ') ?? $e->message;
}
}
}
bytes $id = Core\Encoding::fromHex('6b1f0c9a24d35e7781a4c2f0e93b5d16');
bytes $key = Core\Encoding::fromHex(
'a4010103272006215820d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a',
);
// The saved values are correct, so this prints `the passkey is ready`.
echo Load::check($id, $key, Core\Passkey\Credential::Algorithm::Ed25519, 3), "\n";
// The key is an Ed25519 key, but the record says ES256.
echo Load::check($id, $key, Core\Passkey\Credential::Algorithm::ES256, 3), "\n";
// The key was cut short, so it is not a COSE key.
echo Load::check($id, Core\Bytes::slice($key, 0, 20), Core\Passkey\Credential::Algorithm::Ed25519, 3), "\n";
// The id is empty, and the sign counter is below 0.
echo Load::check(Core\Encoding::fromHex(''), $key, Core\Passkey\Credential::Algorithm::Ed25519, 3), "\n";
echo Load::check($id, $key, Core\Passkey\Credential::Algorithm::Ed25519, -1), "\n";
Output
the passkey is ready
$publicKey is not a key of $algorithm.
the public key is not a COSE key.
$id is 0 bytes. It must be from 1 to 1023 bytes.
$signCount is -1. It must be from 0 to 4294967295.