Skip to content

Core\Cli::askSecret

Asks for a password at the terminal, without showing what the person types.

Core\Cli::askSecret writes the question and reads one line back. The terminal is told not to show the characters while they are typed, so they are never on the screen at all. Nothing is left behind in the scrollback buffer or in a screen recording.

The answer is a secret tainted string. secret means the value cannot be printed, logged or put in an error message. A program that tries does not compile. tainted means a person typed it, so check it before you use it. Core\Password::hash and Core\Password::verify take a secret value as it is. Core\Secret::reveal returns the plain text where a program really needs it, and it takes a written reason.

There is no default. Where nobody can type, a nightly job for example, the call throws Core\Cli\NotInteractive at once. A password nobody typed is not a password.

The examples below ask for a password, ask for a new one twice, and check a password at a login prompt.

Parameters

  • $questionstringThe text to show on the terminal. Control bytes are replaced as in ask.

Return value

Returns secret tainted string. The line typed, both secret and tainted. It cannot be used in output, logs, dumps, Throwable messages or serialization. Like any tainted value, it must still pass the check for each place where it is used.

Errors

  • Core\Cli\NotInteractiveThere is no terminal, its input ended, or nobody answered before the prompt deadline. askSecret has no default, because a password that nobody typed is not a password.

Changelog

VersionChange
0.0.6The Core\Cli methods that prompt or draw now say so: askSecret, showLive, showProgress and selectMany. The old names were secret, live, progress and multiSelect.

Examples

Ask for a password

<?nvs
// Shows how to ask for a password at the terminal. The characters are not shown
// on the screen while they are typed. This example answers itself with
// `Core\Test::answerCli`, so that it prints the same thing everywhere.
Core\Test::answerCli(['correct horse battery']);
secret tainted string $password = Core\Cli::askSecret('Password?');
// The answer is a `secret string`, so `echo $password` does not compile.
// `Core\Secret::reveal` returns the text, and takes a reason. This prints 21.
tainted string $plain = Core\Secret::reveal($password, 'sending it to the mail server');
echo 'the password is ', Core\Str::length($plain), " characters long\n";
Output
the password is 21 characters long

Ask for it twice

<?nvs
// Shows how to ask for a new password twice, so that a typing mistake is found
// before the password is stored. This example answers both questions itself with
// `Core\Test::answerCli`.
Core\Test::answerCli(['river-otter-42', 'river-ottor-42']);
secret tainted string $first = Core\Cli::askSecret('New password?');
secret tainted string $again = Core\Cli::askSecret('Type it again?');
// You can compare two secret values with `==`. The two answers above are
// different, so this prints the second message.
if ($first == $again) {
echo "the new password was saved\n";
} else {
echo "the two answers are different, nothing was saved\n";
}
Output
the two answers are different, nothing was saved

Log in at the terminal

<?nvs
// Shows the everyday use: a command line tool asks who you are and checks your
// password. A real program reads the stored hash from its database. This example
// answers both questions itself with `Core\Test::answerCli`.
string $stored = Core\Password::hash('opensesame');
Core\Test::answerCli(['ada', 'opensesame']);
tainted string $user = Core\Cli::ask('User name?');
secret tainted string $password = Core\Cli::askSecret('Password?');
// `Core\Password::verify` takes the secret answer as it is. Nothing has to be
// revealed here. This prints "welcome, ada".
if (Core\Password::verify($password, $stored)) {
echo 'welcome, ', $user, "\n";
} else {
echo "that password is wrong\n";
}
Output
welcome, ada