private function handle2fa(Client $client, TwoFactorAuthenticationRequiredException $e) { $authenticationAttempts = 0; $authorization = []; $type = trim($e->getType()); // Stupid API gives text with spaces $message = ['Username and password were correct.', 'Two factor authentication is required to continue authentication.']; if ('app' === $type) { $message[] = 'Open the two-factor authentication app on your device to view your authentication code and verify your identity.'; } elseif ('sms' === $type) { $message[] = 'You have been sent an SMS message with an authentication code to verify your identity.'; } $this->styleHelper->note($message); // We already know the password is valid, we just need a valid code // Don't want to fill in everything again when you know it's valid ;) while (!isset($authorization['token'])) { if ($authenticationAttempts > 0) { $this->styleHelper->error('Two factor authentication code was invalid, please try again.'); } try { $code = $this->styleHelper->ask('Two factor authentication code', null, [$this, 'validateNoneEmpty']); $authorization = $this->createAuthorization($client, $code); } catch (TwoFactorAuthenticationRequiredException $e) { // No-op, continue the loop, try again } catch (\Exception $e) { $this->styleHelper->error($e->getMessage()); $this->styleHelper->newLine(); } ++$authenticationAttempts; } return $authorization; }