Core\Secret::reveal
Core\Secret::reveal(string $value, string $reason): stringCore\Secret::reveal( string $value, string $reason, ): string
Core\Secret::reveal() returns the text of a secret string as a plain string.
A value with the type secret string is a password, a token or another text that must stay private. Your program can pass it to functions that are written for secrets, such as a password check. It cannot print it, write it to a log, put it in JSON or send it anywhere else. When one line of your program really must use the text, you call reveal() on that line.
Every call needs a second argument: a short reason, written for the people who read the code. The program never reads it, so it never appears in the result. reveal() removes secret and nothing else. A value that was also tainted stays tainted.
In plain words: a sealed envelope. You can carry it around freely, and opening it is one visible step with a note that says why.
The examples below show a new token printed once, a key shown with only its last four characters, and a token placed in the header of a web request.
Parameters
$valuestringThe secret text. A plainstringis also allowed, and is returned unchanged.$reasonstringWhy this line needs the secret. It is for the people who read the code, and the program never reads it. Asecretvalue is not allowed here.
Return value
Returns string. The same text, as a string. If $value was also tainted, the result is still tainted.
Examples
Print a new token once
<?nvs// Shows how to print a secret token one time, on the line that needs it.
secret string $token = 'tok_7Hq2Lm9Xa4Rb';
// `echo $token` does not compile, because `$token` is `secret`.// `Core\Secret::reveal` returns the text as a plain `string`.string $shown = Core\Secret::reveal($token, 'the user must copy the new token now');
// This prints the token: tok_7Hq2Lm9Xa4Rbecho 'Your new token: ', $shown, "\n";echo "Copy it now. It is not shown again.\n";Your new token: tok_7Hq2Lm9Xa4RbCopy it now. It is not shown again.Show the last four characters
<?nvs// Shows how to display only the end of a secret key, as a settings page does.
secret string $apiKey = 'sk_live_51Hx8QeZr2';
// The reason can be a variable. It is not part of the result.string $why = 'the settings page shows the last four characters';string $key = Core\Secret::reveal($apiKey, $why);
// `Core\Str::slice` with -4 returns the last four characters.// This prints "Your key ends in eZr2" and "It is 18 characters long".echo 'Your key ends in ', Core\Str::slice($key, -4), "\n";echo 'It is ', Core\Str::length($key), " characters long\n";Your key ends in eZr2It is 18 characters longPut a token in a request header
<?nvs// Shows how to put a secret token into the headers of a web request to another service.
class PaymentClient{ public static function headers(secret string $token): array<string> { // The token is revealed here and nowhere else in the program. string $plain = Core\Secret::reveal($token, 'the payment service needs the token in its header'); return [ 'Authorization: Bearer ' . $plain, 'Accept: application/json', ]; }}
secret string $token = 'pay_9fK2mQ7xR1';
// This prints each header. The first line is "Authorization: Bearer pay_9fK2mQ7xR1".foreach (PaymentClient::headers($token) as string $header) { echo $header, "\n";}Authorization: Bearer pay_9fK2mQ7xR1Accept: application/json