Skip to content

Core\Uri::verifySignature

$uri->verifySignature(array<secret bytes> $keys): void$uri->verifySignature(
    array<secret bytes> $keys,
): void

Checks a link that signed made. It checks that nobody changed the address, and that the link has not expired. It returns nothing. When the check fails, it throws a RuntimeError, so the code after the call runs only for a valid link.

You give it the same list of keys that signed used. A link signed with any key in the list is accepted. To change to a new key, put the new key first and keep the old one for a while. Links signed with the old key keep working until you remove it.

A changed parameter, a changed _sig value, a missing _sig and a wrong key all give the same error message. An expired link gives a different message, so you can tell the user to ask for a new link.

The examples below check a link and a changed copy of it, and change to a new key. The last one gives one reply to an expired download link and another reply to a changed one.

Parameters

  • $keysarray<secret bytes>The same list of keys that signed used, newest first. A link signed with any key in the list is accepted. A link signed with a key you removed from the list is not.

Return value

Returns void. Nothing. When the method returns, the link is valid and has not expired.

Errors

  • LogicError$keys is empty, or one of its keys is not 32 bytes long.
  • RuntimeErrorThe link was not signed with these keys. A parameter was added, removed or changed, or the _sig value was changed. Or the key is no longer in the list. Or the link has no _sig, or two of them. All of these give the same message. An expired link gives a different message.

Examples

Check a signed link

<?nvs
// Shows how to check a signed link, and what happens when somebody changes it.
array<secret bytes> $keys = [Core\Crypto::generateKey()];
Core\Uri $link = Core\Uri::parse('https://example.com/invoice?id=7')->signed({ keys: $keys, until: null });
// `verifySignature` returns nothing. The next line runs only when the link is valid.
$link->verifySignature($keys);
echo 'the link is valid', "\n";
// Somebody changes the invoice number from 7 to 8. `verifySignature` throws a `RuntimeError`.
try {
$link->withQueryParameter('id', '8')->verifySignature($keys);
echo 'the changed link is valid', "\n";
} catch (RuntimeError $error) {
echo 'the changed link is not valid', "\n";
}
Output
the link is valid
the changed link is not valid

Change the key

<?nvs
// Shows how to change to a new key and still accept links signed with the old one.
secret bytes $old = Core\Crypto::generateKey();
secret bytes $new = Core\Crypto::generateKey();
Core\Uri $link = Core\Uri::parse('/download?file=a.pdf')->signed({ keys: [$old], until: null });
// The new key is first, so it signs new links. The old key is still in the list,
// so this link is accepted. This prints "accepted".
$link->verifySignature([$new, $old]);
echo 'accepted', "\n";
// After the old key is removed from the list, the link is not accepted. This prints "not accepted".
try {
$link->verifySignature([$new]);
echo 'accepted', "\n";
} catch (RuntimeError $error) {
echo 'not accepted', "\n";
}
Output
accepted
not accepted

An expired download link

<?nvs
// Shows a download handler that tells an expired link apart from a changed one.
class Download
{
public static function open(string $request, array<secret bytes> $keys): string
{
try {
Core\Uri::parse($request)->verifySignature($keys);
} catch (RuntimeError $error) {
// An expired link has its own message. It says "expired".
if (Core\Str::contains($error->message, 'expired')) {
return '410 this link has expired, please ask for a new one';
}
return '403 this link is not valid';
}
return '200 sending the file';
}
}
array<secret bytes> $keys = [Core\Crypto::generateKey()];
string $live = Core\Uri::parse('https://example.com/files?name=report.pdf')
->signed({ keys: $keys, until: Core\Time::now()->plus(1h) })
->toString();
string $old = Core\Uri::parse('https://example.com/files?name=report.pdf')
->signed({ keys: $keys, until: Core\Time::now()->minus(1h) })
->toString();
// This prints the 200, 410 and 403 lines.
echo Download::open($live, $keys), "\n";
echo Download::open($old, $keys), "\n";
echo Download::open(Core\Str::replace($live, 'report.pdf', 'salaries.pdf'), $keys), "\n";
Output
200 sending the file
410 this link has expired, please ask for a new one
403 this link is not valid