Core\Session::getSecret
Core\Session::getSecret() reads a secret from the session, for example an access token of the user. It reads a secret that Core\Session::setShortLivedSecret() or Core\Session::setLongLivedSecret() saved. A short-lived secret ends when the visitor closes the browser. A long-lived secret stays after that.
The secret is saved encrypted, and getSecret() tries every key in $keys. So you can add a new key at the front and still read secrets that an older key encrypted.
The result is null when nothing is saved under the key, and when no key can decrypt the secret. Both cases give the same null, so a program learns nothing about a key it does not have. Core\Session::get() returns null for a secret.
Outside a request, getSecret() throws a LogicError. An empty $keys list, or a key that is not 32 bytes long, also throws a LogicError. A store that cannot be opened throws a RuntimeError, and a store that cannot be reached throws an IOError.
The examples below show how to read a secret of each lifetime, how to change keys without losing secrets, and a page that needs the user's access token.
Parameters
$keystringThe key the secret was saved under. A secret saved under one key cannot be read under another key.$keysarray<secret bytes>A list of encryption keys, newest first. Every key in the list is tried. When you add a new key at the front, secrets saved with the older keys can still be read.
Return value
Returns ?secret string. The secret saved under $key, or null. The result is null when nothing is saved under $key, when no key in $keys decrypts it, and when the saved bytes were changed.
Errors
LogicErrorThe program is not handling a request. A command-line program, a job and a test have no session.RuntimeErrorThe session store, where the server keeps session data, cannot be opened. This happens only at the first call in the request that uses the session. With the default store, the cause is that the server has no data folder. With[session] backend = "shared",[cache.shared] urlis not set, or the program does not have thecache.sharedcapability.IOErrorThe store cannot be reached. The user may still have a session there, so do not treat them as signed out.LogicError$keysis empty, or a key in it is not 32 bytes long.ParseErrorThe value under the secret's key contains an object of a class this program does not declare. This happens only when a program saved a plain value there.
Examples
The examples below run with this configuration file.
# A session keeps its data in a store. These examples use the Redis server# on this computer at port 16379, and allow the program to use it.[capabilities.cache]shared = true
[session]backend = "shared"
[cache.shared]url = "redis://127.0.0.1:16379"Read a secret
<?nvs// Shows how to read a short-lived secret and a long-lived secret from the session.// requires: redis
// A real program loads its keys from its configuration. This example creates one.secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// The access token belongs to the login. The device token stays after the browser closes.secret string $token = 'at-7f3c9b21';secret string $device = 'dv-0c55e1a9';Core\Session::setShortLivedSecret('access-token', $token, $keys);Core\Session::setLongLivedSecret('device-token', $device, $keys);
// `getSecret()` reads a secret of either lifetime. This prints `token: found` and `device: found`.echo 'token: ', (Core\Session::getSecret('access-token', $keys) ?? '') == $token ? 'found' : 'missing', "\n";echo 'device: ', (Core\Session::getSecret('device-token', $keys) ?? '') == $device ? 'found' : 'missing', "\n";
// Nothing is saved under this key, so the result is `null`. This prints `other: null`.echo 'other: ', Core\Session::getSecret('api-token', $keys) == null ? 'null' : 'a value', "\n";token: founddevice: foundother: nullA key list after a rotation
<?nvs// Shows how to replace an encryption key and still read the secrets the old key encrypted.// requires: redis
secret bytes $oldKey = Core\Crypto::generateKey();secret bytes $newKey = Core\Crypto::generateKey();
secret string $token = 'at-91abf042';
// Before the change the program has one key, so that key encrypts the token.array<secret bytes> $before = [$oldKey];Core\Session::setShortLivedSecret('access-token', $token, $before);
// After the change the new key is first, and the old key stays in the list.// Every key is tried, so this prints `with both keys: found`.array<secret bytes> $after = [$newKey, $oldKey];echo 'with both keys: ', (Core\Session::getSecret('access-token', $after) ?? '') == $token ? 'found' : 'null', "\n";
// The new key alone cannot decrypt the old token. This prints `new key only: null`.array<secret bytes> $newOnly = [$newKey];echo 'new key only: ', (Core\Session::getSecret('access-token', $newOnly) ?? '') == $token ? 'found' : 'null', "\n";with both keys: foundnew key only: nullA page that needs the users token
<?nvs// Shows a page that calls a service with the user's access token, or asks the user to sign in.// requires: redis
class Calendar{ public static function page(array<secret bytes> $keys): string { // `null` means the user has not signed in to the service yet. var $token = Core\Session::getSecret('calendar-token', $keys); if ($token == null) { return 'please connect your calendar'; } // A real program sends the token to the service here. return 'showing your calendar'; }}
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// This prints `please connect your calendar`.echo Calendar::page($keys), "\n";
// The user connects the calendar, and the program saves the token it got.Core\Session::setShortLivedSecret('calendar-token', 'at-2d40c8e7', $keys);
// This prints `showing your calendar`.echo Calendar::page($keys), "\n";please connect your calendarshowing your calendar