/**
  * Creates an Oauth token for a named application.
  * 
  * @param $username string The username to create the oauth token for
  * @param $password string The password of the user
  * @param $enterPasswordCallback callable A callback to get the one-time password
  * if the user has two factor auth enabled on their account.
  * @param $scopes array The scopes/permissions that the token should 
  * have e.g. \GithubService\Github::SCOPE_USER_EMAIL and
  * https://developer.github.com/v3/oauth/#scopes 
  * @param $applicationName string The name of the application
  * @param $noteURL string The URL of application i.e. where a user should go to
  * find help for the application.
  * @param $maxAttempts int The maximum number of attempts. This only allows retries
  * for the two-factor auth failing, not the password
  *  
  * @return \GithubService\Model\OauthAccess
  */
 function createOrRetrieveAuth($username, $password, callable $enterPasswordCallback, $scopes, $note, $noteURL = "http://www.github.com/danack/GithubArtaxService", $maxAttempts = 3)
 {
     $basicToken = new BasicAuthToken($username, $password);
     $otp = false;
     for ($i = 0; $i < $maxAttempts; $i++) {
         try {
             $createAuthToken = $this->createAuthorization($basicToken->__toString(), $scopes, $note);
             $createAuthToken->setNote_url($noteURL);
             if ($otp) {
                 $createAuthToken->setOtp($otp);
             }
             $authResult = $createAuthToken->execute();
             return $authResult;
         } catch (OneTimePasswordAppException $otpae) {
             $otp = $enterPasswordCallback("Please enter the code from your 2nd factor auth app:");
         } catch (OneTimePasswordSMSException $otse) {
             $otp = $enterPasswordCallback("Please enter the code from the SMS Github should have sent you:");
         }
     }
     throw new GithubArtaxServiceException("Failed to create or retrieve oauth token.");
 }
function getToken(GithubService $githubService)
{
    echo "Enter username:\n";
    $username = trim(fgets(STDIN));
    echo "Enter password:\n";
    $password = trim(fgets(STDIN));
    $otp = false;
    $token = false;
    $basicToken = new BasicAuthToken($username, $password);
    $applicationName = 'BastionInteractiveTest';
    $token = null;
    if ($token) {
        echo "Yay we stored token\n";
        exit(0);
    }
    for ($i = 0; $i < 5 && $token == false; $i++) {
        try {
            //$currentAuthCommand = $githubService->getAuthorizations($basicToken);
            $authCommand = $githubService->createAuthorization($basicToken->__toString(), [], $applicationName . '_' . date('YmdHis'));
            if ($otp) {
                $authCommand->setOtp($otp);
            }
            $authResult = $authCommand->execute();
            $response = $authCommand->getResponse();
            echo "Response was: ";
            var_dump($response->getBody());
            //            $authCommand = $githubService->getOrCreateAuthorization(
            //                $basicToken->__toString(),
            //                [],
            //                $applicationName,
            //                'https://www.bastionrpm.com',
            //                GITHUB_CLIENT_ID,
            //                GITHUB_CLIENT_SECRET
            //            );
            //
            //            if ($otp) {
            //                $authCommand->setOtp($otp);
            //            }
            //            $authResult = $authCommand->execute();
            var_dump($authResult);
            //            $currentAuth = $currentAuths->findNoteAuthorization($applicationName);
            //
            //            if ($currentAuth) {
            //                echo "Already have current auth: ".$currentAuth->token.PHP_EOL;
            //                $token = $currentAuth->token;
            //                break;
            //            }
            //
            //            $createAuthToken = $githubService->createAuthToken(
            //                $basicToken,
            //                [],
            //                $applicationName,
            //                'https://www.bastionrpm.com'
            //            );
            //
            //            if ($otp) {
            //                $createAuthToken->setOtp($otp);
            //            }
            //
            //            $authResult = $createAuthToken->execute();
            //
            //            var_dump($authResult);
            echo "Token is " . $authResult->token;
            $token = $authResult->token;
            $i = 5;
        } catch (OneTimePasswordAppException $otpae) {
            echo "Please enter the code from your 2nd factor auth app:\n";
            $otp = trim(fgets(STDIN));
        } catch (OneTimePasswordSMSException $otse) {
            echo "Please enter the code from the SMS Github should have sent you:\n";
            $otp = trim(fgets(STDIN));
        }
    }
    return $token;
}