Core\Session::setShortLivedSecret
Core\Session::setShortLivedSecret(
string $key,
secret string $value,
array<secret bytes> $keys,
): voidCore\Session::setShortLivedSecret() saves a secret, such as an access token, in the session of the current request. A later request from the same visitor reads it back with Core\Session::getSecret().
A short-lived secret is deleted when the visitor closes the browser. Core\Session::clearShortLived() also deletes it when the user signs out. This is the right lifetime for a secret that belongs to a login. A secret that the device keeps after the browser closes belongs in Core\Session::setLongLivedSecret().
The value is encrypted with the first key in $keys, so the session store only sees encrypted bytes. Core\Crypto::generateKey() creates a key. 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 token, that get() cannot read it, and a sign-in and sign-out with an access token.
Parameters
$keystringThe key to save the secret under.get()returnsnullfor this key. OnlygetSecret()with the same$keyscan read the secret.$valuesecret stringThe secret to save, for example an access token or a refresh token of the user.$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. It is deleted with the other short-lived values.
Errors
LogicErrorThe program is not handling a request. A command-line program, a job and a test have no session.LogicErrorThe call is insideCore\Html::laterorCore\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] 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 its first key is not 32 bytes long.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\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.
# 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 an access token
<?nvs// Shows how to save a short-lived secret in the session 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];
// `setShortLivedSecret()` encrypts the token with the first key and saves it.// The token is deleted when the visitor closes the browser.secret string $token = 'at-7f3c9b21';Core\Session::setShortLivedSecret('access-token', $token, $keys);
// `getSecret()` decrypts it with the same keys. This prints `the token is saved`.echo (Core\Session::getSecret('access-token', $keys) ?? '') == $token ? 'the token is saved' : 'the token is missing', "\n";the token is savedGet cannot read a secret
<?nvs// Shows that a secret can only be read with `getSecret()` and the right keys.// requires: redis
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
secret string $token = 'rt-51d0e8aa';Core\Session::setShortLivedSecret('refresh-token', $token, $keys);
// `get()` does not return secrets. This prints `get: null`.echo 'get: ', Core\Session::get('refresh-token') == null ? 'null' : 'a value', "\n";
// Another key cannot decrypt the secret. This prints `other key: null`.secret bytes $otherKey = Core\Crypto::generateKey();array<secret bytes> $otherKeys = [$otherKey];echo 'other key: ', Core\Session::getSecret('refresh-token', $otherKeys) == null ? 'null' : 'a value', "\n";
// The right key can. This prints `right key: a value`.echo 'right key: ', Core\Session::getSecret('refresh-token', $keys) == null ? 'null' : 'a value', "\n";get: nullother key: nullright key: a valueSign out deletes the token
<?nvs// Shows a sign-in that saves an access token, and a sign-out that deletes it.// requires: redis
class Login{ // A real program gets this token from the login service. public static function exchangeCode(string $code): secret string { return 'at-' . $code; }
public static function signIn(string $code, array<secret bytes> $keys): void { // A new session identifier after sign-in protects the user from session fixation. Core\Session::regenerate(); Core\Session::setShortLived('user', 'ada'); Core\Session::setShortLivedSecret('access-token', Login::exchangeCode($code), $keys); }
// `clearShortLived()` deletes every short-lived value, and the token with them. public static function signOut(): void { Core\Session::clearShortLived(); }}
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// The visitor's choice of language is long-lived, so the sign-out keeps it.Core\Session::setLongLived('language', 'en');
Login::signIn('c0de42', $keys);// This prints `signed in: yes`.echo 'signed in: ', Core\Session::getSecret('access-token', $keys) == null ? 'no' : 'yes', "\n";
Login::signOut();// This prints `signed in: no` and `language: en`.echo 'signed in: ', Core\Session::getSecret('access-token', $keys) == null ? 'no' : 'yes', "\n";echo 'language: ', Core\Session::get('language') ?? '(none)', "\n";signed in: yessigned in: nolanguage: en