Core\Uri::signed
Adds a signature to an address, so that nobody can change the address without the server noticing. signed returns a new Uri with a _sig parameter at the end of the query. Later, verifySignature checks it with the same keys.
The signature covers the scheme, the user info, the host, the port, the path and every query parameter. If somebody adds, removes or changes a parameter, verifySignature throws an error. The order of the parameters does not change the signature. The fragment (the part after #) does not change it either.
You always write two settings. keys is a list of 32-byte keys, and the first key signs. until is the moment the link stops working. null means it never stops, and you must write it.
Good to know: the signature makes the address about twice as long. A relative address, such as /download?file=a, has no host in its signature, so it is valid on any site.
The examples below sign a download link for one hour, and sign a link with no end date. The last one sends a password reset link that the server checks.
Parameters
$settings{ keys: array<secret bytes>, until: ?Core\Time\Instant }The keys and the end date. You must write both.keysarray<secret bytes>A list of 32-byte keys, newest first.$keys[0]signs. The older keys are there so thatverifySignaturestill accepts links signed before you changed the key.Core\Signaturetakes the same list, but a token from one does not work in the other.until?Core\Time\InstantThe moment the link stops working. It is inside the signature, so nobody can change it.nullmeans the link never expires. A link that never expires works for anybody who finds it later, for example in a browser history.
Return value
Returns Core\Uri. A new Uri with _sig set. You can use with and toString on it. If the address already has _sig, it is replaced. The same address, keys and end date always give the same link. The signature makes the address about twice as long. A relative address, such as /download?file=a, has no host in its signature, so it is valid on any site.
Errors
LogicErrorkeysis empty, or its first key is not 32 bytes long.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Uri->resolved() and signed() return a new address and leave the original unchanged. The old names were resolve and sign. |
Examples
Sign a download link
<?nvs// Shows how to sign a download link that works for one hour.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// `signed` returns a new `Uri`. It is the same address with a `_sig` parameter added at the end.Core\Uri $link = Core\Uri::parse('https://files.example.com/download?file=report.pdf') ->signed({ keys: $keys, until: Core\Time::now()->plus(1h) });
// These three lines print true, report.pdf and verified.echo Core\Str::startsWith($link->toString(), 'https://files.example.com/download?file=report.pdf&_sig=') ? 'true' : 'false', "\n";echo $link->queryParameter('file') as string, "\n";$link->verifySignature($keys);echo 'verified', "\n";truereport.pdfverifiedA link that does not expire
<?nvs// Shows a signed link with no end date, and that signing the same address twice gives the same link.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// `until: null` means the link never expires. You must write it, because it cannot be left out.Core\Uri $first = Core\Uri::parse('https://example.com/terms?version=3')->signed({ keys: $keys, until: null });Core\Uri $second = Core\Uri::parse('https://example.com/terms?version=3')->signed({ keys: $keys, until: null });
// The same address, key and end date give the same link. This prints true.echo $first->toString() == $second->toString() ? 'true' : 'false', "\n";
// Signing a signed link again replaces its `_sig` parameter. This prints true.Core\Uri $again = $first->signed({ keys: $keys, until: null });echo $again->toString() == $first->toString() ? 'true' : 'false', "\n";truetrueA password reset link
<?nvs// Shows a password reset link that a server sends by email and checks when the user opens it.
class Reset{ public static function link(uint $user, array<secret bytes> $keys): string { Core\Uri $page = Core\Uri::parse('https://example.com/reset') ->withQueryParameter('user', $user as string); // The link stops working 30 minutes after it is sent. return $page->signed({ keys: $keys, until: Core\Time::now()->plus(30m) })->toString(); }
public static function open(string $request, array<secret bytes> $keys): tainted string { Core\Uri $uri = Core\Uri::parse($request); try { $uri->verifySignature($keys); } catch (RuntimeError $error) { return 'this link is not valid'; } return 'choose a new password for user ' . ($uri->queryParameter('user') as string); }}
array<secret bytes> $keys = [Core\Crypto::generateKey()];string $mailed = Reset::link(42, $keys);
// The first line is the link as it was sent. The second changes the user from 42 to 43.// This prints "choose a new password for user 42" and "this link is not valid".echo Reset::open($mailed, $keys), "\n";echo Reset::open(Core\Str::replace($mailed, 'user=42', 'user=43'), $keys), "\n";choose a new password for user 42this link is not valid