Skip to content

Core\Session::setLongLivedSecret

Core\Session::setLongLivedSecret() saves a secret in the session of the current request. A later request from the same visitor reads it back with Core\Session::getSecret().

A long-lived secret stays after the visitor closes the browser, for [session] long_lived. Signing out with Core\Session::clearShortLived() keeps it. Core\Session::clearLongLived() deletes it.

The session cookie alone is enough to read it after the browser closes. Anybody who opens that browser can read it, without a login. So use it only for a secret of the device. A secret that belongs to a login, such as an access token, belongs in Core\Session::setShortLivedSecret().

The value is encrypted with the first key in $keys, so the session store only sees encrypted bytes. Core\Session::get() returns null for the secret.

An empty $keys list throws a LogicError, and so does a first key that is not 32 bytes long. A program that is not handling a request has no session, and the call throws a LogicError.

The examples below show how to save a device token, which clear deletes it, and how to change to a new key.

Parameters

  • $keystringThe key to save the secret under. get() returns null for this key. Only getSecret() with the same $keys can read the secret.
  • $valuesecret stringThe secret to save.
  • $keysarray<secret bytes>A list of encryption keys, newest first. Core\Crypto::generateKey() creates one. The first key encrypts the secret.

Return value

Returns void. Nothing. The secret is saved to the store when the request ends, and it is kept for [session] long_lived. Anybody with the session cookie can use it, also after the browser closes. Save a secret that belongs to a login with setShortLivedSecret().

Errors

  • LogicErrorThe program is not handling a request. A command-line program, a job and a test have no session.
  • LogicErrorThe call is inside Core\Html::later or Core\Task::runAfterResponse. The response headers are already sent there, so the session cannot change. Most of these calls do not compile.
  • 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] url is not set, or the program does not have the cache.shared capability.
  • IOErrorThe store cannot be reached. The user may still have a session there, so do not treat them as signed out.
  • LogicError$keys is empty, or its first key is not 32 bytes long.

Changelog

VersionChange
0.0.5Core\Session::setShortLivedSecret and Core\Session::setLongLivedSecret save an encrypted secret with a short or a long lifetime. Core\Session::getSecret reads both, and Core\Session::get returns null for them.

Examples

The examples below run with this configuration file.

nvs.toml
# 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"

Save a device token

<?nvs
// Shows how to save a long-lived secret that belongs to the device and read it back.
// 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];
// `setLongLivedSecret()` encrypts the device token with the first key and saves it.
// The token stays in the session after the visitor closes the browser.
secret string $token = 'dev-4e81a0c2';
Core\Session::setLongLivedSecret('device-token', $token, $keys);
// `getSecret()` decrypts it with the same keys. This prints `the token is saved`.
echo (Core\Session::getSecret('device-token', $keys) ?? '') == $token
? 'the token is saved'
: 'the token is missing', "\n";
Output
the token is saved

Sign out keeps it

<?nvs
// Shows which clear deletes a long-lived secret and which one keeps it.
// requires: redis
secret bytes $key = Core\Crypto::generateKey();
array<secret bytes> $keys = [$key];
Core\Session::setLongLivedSecret('device-token', 'dev-4e81a0c2', $keys);
Core\Session::setShortLivedSecret('access-token', 'at-7f3c9b21', $keys);
// `clearShortLived()` signs the user out. It deletes only the short-lived secret.
// This prints `access token: null` and `device token: a value`.
Core\Session::clearShortLived();
echo 'access token: ', Core\Session::getSecret('access-token', $keys) == null ? 'null' : 'a value', "\n";
echo 'device token: ', Core\Session::getSecret('device-token', $keys) == null ? 'null' : 'a value', "\n";
// `clearLongLived()` deletes the long-lived secret too. This prints `device token: null`.
Core\Session::clearLongLived();
echo 'device token: ', Core\Session::getSecret('device-token', $keys) == null ? 'null' : 'a value', "\n";
Output
access token: null
device token: a value
device token: null

Rotate the keys

<?nvs
// Shows how a long-lived secret stays readable when the program starts to use a new key.
// requires: redis
secret bytes $oldKey = Core\Crypto::generateKey();
secret bytes $newKey = Core\Crypto::generateKey();
// The secret was saved while the old key was the only key.
array<secret bytes> $oldKeys = [$oldKey];
Core\Session::setLongLivedSecret('device-token', 'dev-4e81a0c2', $oldKeys);
// The new list has the new key first and the old key second.
// `getSecret()` can still decrypt the secret. This prints `with both keys: a value`.
array<secret bytes> $keys = [$newKey, $oldKey];
secret string $token = Core\Session::getSecret('device-token', $keys) ?? '';
echo 'with both keys: ', $token == '' ? 'null' : 'a value', "\n";
// Save the secret again. The first key in the list encrypts it, so now the new key alone can read it.
// This prints `with the new key: a value`.
Core\Session::setLongLivedSecret('device-token', $token, $keys);
array<secret bytes> $newKeys = [$newKey];
echo 'with the new key: ',
Core\Session::getSecret('device-token', $newKeys) == null ? 'null' : 'a value', "\n";
Output
with both keys: a value
with the new key: a value