Core\Signature::verify
Core\Signature::verify checks a token that Core\Signature::sign made, and returns the values that were signed. It tries each key in the list you give it. If no key made the token, or somebody changed the token, it throws a RuntimeError. If the token has expired, it also throws a RuntimeError, with a message that gives the time it expired.
Every value it returns is a tainted string. Tainted means Novis treats the value as input from outside your program. A valid signature shows that your program made the token. It does not show that the values are safe to put in an HTML page or a database query. The values come back sorted by name.
You can add a new key at the start of the list and keep the old keys after it. Tokens made with an old key still work until you remove that key.
The examples below read the values from a token, catch a changed and an expired token, and change to a new key without breaking the tokens users already have.
Parameters
$tokenstringThe token as your program received it, for example from a URL. Ataintedstring is allowed here.$keysarray<secret bytes>The keys you gavesign, newest first. A token made with any key in this list is accepted. A token made with a key you removed from the list is not.
Return value
Returns array<tainted string>. The signed payload, sorted by name. Every value is a tainted string.
Errors
LogicError$keysis empty, or one of its keys is not 32 bytes long.RuntimeError$tokenwas not made with any key in$keys. This includes a changed token, text that is not a token, and a token for a signed URL. All of these give the same message, so an attacker learns nothing from it. An expired token also throwsRuntimeError, with a different message that gives the time it expired.
Examples
Read the signed values
<?nvs// Shows how to check a token and read the values in it.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
array<string> $cart = ['shop' => '7', 'coupon' => 'SPRING10'];string $token = Core\Signature::sign($cart, { keys: $keys, until: null });
// `verify` returns the values. Each value is a `tainted string`.array<tainted string> $back = Core\Signature::verify($token, $keys);echo 'Coupon ', $back['coupon'], ' for shop ', $back['shop'], "\n";
// The values come back sorted by name, so this loop prints `coupon` first and `shop` second.foreach ($back as tainted string $name => tainted string $value) { echo $name, "\n";}Coupon SPRING10 for shop 7couponshopA changed or expired token
<?nvs// Shows the errors `verify` throws for a changed token and for an expired token.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];array<string> $claims = ['user' => '42'];
// Somebody adds text to the end of a valid token.string $token = Core\Signature::sign($claims, { keys: $keys, until: null });try { Core\Signature::verify($token . 'AAAA', $keys); echo "accepted\n";} catch (RuntimeError $e) { echo "The changed token throws a RuntimeError.\n";}
// This token expired one hour ago.Core\Time\Instant $hourAgo = Core\Time::now()->minus(1h);string $old = Core\Signature::sign($claims, { keys: $keys, until: $hourAgo });try { Core\Signature::verify($old, $keys); echo "accepted\n";} catch (RuntimeError $e) { // Only an expired token has the word "expired" in its message. echo Core\Str::contains($e->message, 'expired') ? "The token has expired.\n" : "Other error.\n";}The changed token throws a RuntimeError.The token has expired.Change to a new key
<?nvs// Shows how to start signing with a new key while old tokens still work.
secret bytes $oldKey = Core\Crypto::generateKey();array<string> $claims = ['user' => '42'];string $oldToken = Core\Signature::sign($claims, { keys: [$oldKey], until: null });
// The new key goes first, so `sign` uses it. The old key stays second.secret bytes $newKey = Core\Crypto::generateKey();array<secret bytes> $keys = [$newKey, $oldKey];string $newToken = Core\Signature::sign($claims, { keys: $keys, until: null });
// Both tokens are accepted, so this prints 42 two times.echo Core\Signature::verify($oldToken, $keys)['user'], "\n";echo Core\Signature::verify($newToken, $keys)['user'], "\n";
// Later, the old key is removed. Tokens made with it now throw an error.array<secret bytes> $newOnly = [$newKey];try { Core\Signature::verify($oldToken, $newOnly); echo "accepted\n";} catch (RuntimeError $e) { echo "The old token is no longer accepted.\n";}4242The old token is no longer accepted.