protected function configureAccount(InputInterface $input, OutputInterface $output)
{
/** @var \Platformsh\Cli\Helper\QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = new Question('Your email address: ');
$question->setValidator(function ($answer) {
if (empty($answer) || !filter_var($answer, FILTER_VALIDATE_EMAIL)) {
throw new \RuntimeException('Please provide a valid email address.');
}
return $answer;
});
$question->setMaxAttempts(5);
$email = $helper->ask($input, $output, $question);
$question = new Question('Your password: ');
$question->setValidator(function ($answer) {
if (trim($answer) == '') {
throw new \RuntimeException('The password cannot be empty');
}
return $answer;
});
$question->setHidden(true);
$question->setMaxAttempts(5);
$password = $helper->ask($input, $output, $question);
try {
$this->api()->getClient(false)->getConnector()->logIn($email, $password, true);
} catch (BadResponseException $e) {
// If a two-factor authentication challenge is received, then ask
// the user for their TOTP code, and then retry authenticateUser().
if ($e->getResponse()->getHeader('X-Drupal-TFA')) {
$question = new Question("Your application verification code: ");
$question->setValidator(function ($answer) use($email, $password) {
if (trim($answer) == '') {
throw new \RuntimeException("The code cannot be empty.");
}
try {
$this->api()->getClient(false)->getConnector()->logIn($email, $password, true, $answer);
} catch (BadResponseException $e) {
// If there is a two-factor authentication error, show
// the error description that the server provides.
//
// A RuntimeException here causes the user to be asked
// again for their TOTP code.
if ($e->getResponse()->getHeader('X-Drupal-TFA')) {
$json = $e->getResponse()->json();
throw new \RuntimeException($json['error_description']);
} else {
throw $e;
}
}
return $answer;
});
$question->setMaxAttempts(5);
$output->writeln("\nTwo-factor authentication is required.");
$helper->ask($input, $output, $question);
} elseif ($e->getResponse()->getStatusCode() === 401) {
$output->writeln("\n<error>Login failed. Please check your credentials.</error>\n");
$output->writeln("Forgot your password? Or don't have a password yet? Visit:");
$output->writeln(" <comment>" . self::$config->get('service.accounts_url') . "/user/password</comment>\n");
$this->configureAccount($input, $output);
} else {
throw $e;
}
}
}