Core\Router::urlSigned
Core\Router::urlSigned(
callable $route,
array<mixed> $params,
{ keys: array<secret bytes>, until: ?Core\Time\Instant } $settings,
): stringBuilds a link to one of your routes that nobody can change. Core\Router::urlSigned builds the same link as Core\Router::url and adds a signature in the _sig query parameter. You give it the method that has the #[Route], such as Reports::download, the values, your keys, and the time when the link stops working. The values are always required, even an empty array.
The signature covers the route's path, such as /unsubscribe/{reader}, every value and the end time. If somebody changes a value in the link, the signature does not match any more. Core\Router::signedRoute checks the signature when the request arrives.
The method's name is not in the signature. If you rename the method, links that you sent before still work. If you change the path in its #[Route], those links stop working.
Use a signed link when the link itself is the permission: a download that works for one hour, an unsubscribe link, or an invitation. Keep the keys secret. Anybody who has a key can make links.
The examples show a link that works for one hour, how the signature follows every value, and unsubscribe links for a newsletter.
Parameters
$routecallableThe method that has the#[Route], written as a method reference such asShop::show. The link does not call the method. So the method may be an instance method or a private method. A method reference written directly in the call is checked when the program compiles. A method without a#[Route]is a compile error.$paramsarray<mixed>The path's captures by name, plus any query parameters. A key written directly in the code must be a capture or a declared#[Query]parameter. Any other key is a compile error. A value written directly in the code must have a type that its parameter can take, or the program does not compile. ACore\Time\Datefor auintcapture is an example. Astringvalue is always allowed.urlandurlAbsoluteuse[]when you leave it out.$settings{ keys: array<secret bytes>, until: ?Core\Time\Instant }The keys and the time when the link stops working. Both are required.keysarray<secret bytes>The keys, newest first.$keys[0]signs the link. The older keys are there so thatsignedRoutestill accepts links made before you changed the key. A signature made byCore\Signatureor$uri->signis not accepted here, even with the same keys.until?Core\Time\InstantWhen the link stops working. It is part of the signature, so nobody can change it.nullmeans the link never stops working. You must writenullyourself, because anybody who has such a link can use it forever.
Return value
Returns string. The link from url with _sig=… at the end, for example /users/42?page=2&_sig=…. The same route, values, keys and end time always give the same link. The signature adds about 4/3 × (path + params + 40) characters.
Errors
RuntimeErrorFor every case whereurlthrows an error. When$paramshas a_sigkey, because this method writes that key itself.LogicErrorWhen$settings.keysis empty. When its first key is not 32 bytes long.
Examples
A link that works for one hour
<?nvs// Shows how to build a signed link that works for one hour.
class Reports{ #[Core\Route(path: '/reports/{year}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function download(uint $year): string { return 'the report'; }}
// A real program reads its key from its configuration. This one makes a new key.secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// `until` is the time when the link stops working.Core\Time\Instant $until = Core\Time::now()->plus(1h);
string $plain = Core\Router::url(Reports::download, ['year' => 2024]);string $signed = Core\Router::urlSigned(Reports::download, ['year' => 2024], { keys: $keys, until: $until });
// This prints `/reports/2024`.echo $plain, "\n";
// The signed link is the same link with `?_sig=` and the signature at the end.// This prints `yes`.echo Core\Str::startsWith($signed, $plain . '?_sig=') ? 'yes' : 'no', "\n";/reports/2024yesEvery value is signed
<?nvs// Shows that the signature changes when any value of the link changes.
class Files{ #[Core\Route(path: '/files/{id}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $id, #[Core\Query] string $size = ''): string { return 'one file'; }}
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
string $first = Core\Router::urlSigned(Files::show, ['id' => 7, 'size' => 'large'], { keys: $keys, until: null });string $again = Core\Router::urlSigned(Files::show, ['id' => 7, 'size' => 'large'], { keys: $keys, until: null });string $other = Core\Router::urlSigned(Files::show, ['id' => 7, 'size' => 'small'], { keys: $keys, until: null });
// The same values and the same key give the same link. This prints `same`.echo $first == $again ? 'same' : 'different', "\n";
// `size` is in the query string, and it is signed too. This prints `different`.string $firstSignature = Core\Str::after($first, '_sig=') as string;string $otherSignature = Core\Str::after($other, '_sig=') as string;echo $firstSignature == $otherSignature ? 'same' : 'different', "\n";
// The method writes `_sig` itself, so `$params` may not contain it.// This throws a `RuntimeError`.try { array<mixed> $params = ['id' => 7, '_sig' => 'my own']; Core\Router::urlSigned(Files::show, $params, { keys: $keys, until: null });} catch (RuntimeError $e) { echo '`_sig` is not allowed in the values', "\n";}samedifferent`_sig` is not allowed in the valuesAn unsubscribe link for each reader
<?nvs// Shows how to build an unsubscribe link for each reader of a newsletter.
class Newsletter{ #[Core\Route(path: '/unsubscribe/{reader}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function unsubscribe(uint $reader): string { return 'you are unsubscribed'; }}
// The first key signs new links. The second key is an older one.// Links that were signed with it still work when you check them.secret bytes $current = Core\Crypto::generateKey();secret bytes $older = Core\Crypto::generateKey();array<secret bytes> $keys = [$current, $older];
// The links work for 30 days. Nobody can change a reader number in a link,// because the signature would not match any more.Core\Time\Instant $until = Core\Time::now()->plus(720h);
array<uint> $readers = [3, 17, 42];foreach ($readers as uint $reader) { string $link = Core\Router::urlSigned(Newsletter::unsubscribe, ['reader' => $reader], { keys: $keys, until: $until }); string $path = Core\Router::url(Newsletter::unsubscribe, ['reader' => $reader]); // This prints `/unsubscribe/3: signed`, and the same for the other readers. echo $path, ': ', Core\Str::startsWith($link, $path . '?_sig=') ? 'signed' : 'not signed', "\n";}/unsubscribe/3: signed/unsubscribe/17: signed/unsubscribe/42: signed