Skip to content

Core\Cache\Store::putSecret

Stores a secret in a cache store, sealed with one of your keys.

putSecret encrypts the value before it reaches the store. The store holds the encrypted bytes and nothing else, so code that knows the name of the entry but not your keys cannot read the value.

$keys is a list of keys, newest first. The first key of the list seals the value, and every key of the list can open it again. So you can put a new key at the front and still read what an older key sealed. Core\Crypto::generateKey makes a key.

The lifetime is required here. It is sealed into the entry as well as given to the store, so an entry a store kept for too long is still gone.

Read the value back with Core\Cache\Store::getSecret. A plain Core\Cache\Store::get on the same name returns null.

The examples below show one secret written and read back, then a key list after a rotation, then an access token kept for as long as the service said it lasts.

Parameters

  • $keystringThe name to store under. A tainted string is allowed, as on put. The name is part of the encrypted entry. If the entry is moved to another name, it cannot be decrypted.
  • $valuesecret stringThe secret to store. A plain string is also accepted. The call does not change whether a value is secret. Only the type of the value decides that.
  • $ttlCore\Time\DurationHow long the secret stays readable, counted from this call. It is required. The lifetime is also stored inside the encrypted entry, so a copy of the entry written back later still expires at the same time.
  • $keysarray<secret bytes>The list of keys, newest first, in the same format as for Core\SignedCookie. The newest key encrypts. Every key in the list can decrypt. After you add a new key, entries written with an older key in the list can still be read.

Return value

Returns void. Nothing. Code that knows the entry's name but not the keys cannot read it. A changed, moved or replayed entry reads as missing. This does not protect a secret from a compromised process, because the keys are in the same memory.

Errors

  • LogicError$keys is empty, or one of its keys does not have the required length. This is a bug in the program, so it throws an error and is not treated as missing.
  • IOErrorOnly on a shared cache: the store cannot be reached, or it does not accept the write.

Examples

Storing a secret

<?nvs
// Shows how to store a secret in a cache store and read it back.
// A key seals the value. `Core\Crypto::generateKey` makes one. A real program
// reads its keys from the configuration instead.
secret bytes $key = Core\Crypto::generateKey();
array<secret bytes> $keys = [$key];
var $cache = Core\Cache::memory();
secret string $token = 'sk-8f2c41';
// `putSecret` encrypts the value and stores it under the name "api-token".
// The lifetime of one hour is required.
$cache->putSecret('api-token', $token, 1h, $keys);
// `getSecret` opens the entry with the same key list. This prints the token is
// back.
echo ($cache->getSecret('api-token', $keys) ?? '') == $token
? 'the token is back'
: 'the token is gone', "\n";
// The store holds the encrypted bytes, so a plain `get` on the same name finds
// nothing. This prints get returns null.
echo $cache->get('api-token') == null ? 'get returns null' : 'get returns a value', "\n";
// Another key list does not open the entry. That is a miss and not an error,
// so this prints another key list is a miss.
array<secret bytes> $other = [Core\Crypto::generateKey()];
echo $cache->getSecret('api-token', $other) == null
? 'another key list is a miss'
: 'another key list opened it', "\n";
Output
the token is back
get returns null
another key list is a miss

A key list after a rotation

<?nvs
// Shows how a key list lets you replace a key without losing the secrets the
// old key sealed.
secret bytes $newKey = Core\Crypto::generateKey();
secret bytes $oldKey = Core\Crypto::generateKey();
var $cache = Core\Cache::memory();
secret string $password = 's3cr3t-db-pass';
// Before the rotation the program has one key, so that key seals the entry.
array<secret bytes> $before = [$oldKey];
$cache->putSecret('db-password', $password, 30m, $before);
// After the rotation the new key is first and the old key stays in the list.
// So this prints the old entry still opens.
array<secret bytes> $after = [$newKey, $oldKey];
echo ($cache->getSecret('db-password', $after) ?? '') == $password
? 'the old entry still opens'
: 'the old entry is gone', "\n";
// Every new write seals with the first key of the list.
$cache->putSecret('db-password', $password, 30m, $after);
// Once every entry has been written again you can drop the old key. This
// prints the new key alone opens it.
array<secret bytes> $newOnly = [$newKey];
echo ($cache->getSecret('db-password', $newOnly) ?? '') == $password
? 'the new key alone opens it'
: 'the new key alone does not open it', "\n";
Output
the old entry still opens
the new key alone opens it

Keeping an access token

<?nvs
// Shows how a program keeps an access token it fetched, so the next request
// does not ask the service for a new one.
class Tokens
{
// The work the cache saves. A real program calls the service here. The
// counter below shows how often that happened.
public static function fetch(): secret string
{
var $cache = Core\Cache::memory();
var $held = $cache->get('token-fetches') ?? 0;
int $done = $held as int;
$cache->put('token-fetches', $done + 1);
return 'at-7f3c9b21';
}
public static function ensure(array<secret bytes> $keys): void
{
var $cache = Core\Cache::memory();
// A hit needs no service call. A miss is also what an expired entry
// looks like, so the token is fetched again then.
if ($cache->getSecret('service-token', $keys) != null) {
return;
}
// The service said the token lasts fifteen minutes, so that is the
// lifetime of the entry. A shorter one is safe. A longer one is not.
secret string $token = Tokens::fetch();
$cache->putSecret('service-token', $token, 15m, $keys);
}
}
secret bytes $key = Core\Crypto::generateKey();
array<secret bytes> $keys = [$key];
Tokens::ensure($keys);
Tokens::ensure($keys);
var $cache = Core\Cache::memory();
// The first call fetched the token and the second call read the store. This
// prints 1.
echo $cache->get('token-fetches') as int, "\n";
// The stored token is the one the service gave. This prints the stored token
// matches.
secret string $expected = 'at-7f3c9b21';
echo ($cache->getSecret('service-token', $keys) ?? '') == $expected
? 'the stored token matches'
: 'the stored token differs', "\n";
Output
1
the stored token matches