1
0
Fork 0

Reuse github existing tokens instead of failing, fixes #2724

pull/2760/merge
Jordi Boggiano 2014-02-26 17:20:47 +01:00
parent 1851c29dd3
commit 5b0dc99fec
1 changed files with 38 additions and 12 deletions

View File

@ -101,25 +101,51 @@ class GitHub
$appName .= ' on ' . trim($output); $appName .= ' on ' . trim($output);
} }
$headers = array('Content-Type: application/json'); $headers = array();
if ($otp) { if ($otp) {
$headers[] = 'X-GitHub-OTP: ' . $otp; $headers = array('X-GitHub-OTP: ' . $otp);
} }
$contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://'. $apiUrl . '/authorizations', false, array( // try retrieving an existing token with the same name
$contents = null;
$auths = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://'. $apiUrl . '/authorizations', false, array(
'retry-auth-failure' => false, 'retry-auth-failure' => false,
'http' => array( 'http' => array(
'method' => 'POST', 'header' => $headers
'follow_location' => false,
'header' => $headers,
'content' => json_encode(array(
'scopes' => array('repo'),
'note' => $appName,
'note_url' => 'https://getcomposer.org/',
)),
) )
))); )));
foreach ($auths as $auth) {
if (
isset($auth['app']['name'])
&& 0 === strpos($auth['app']['name'], $appName)
&& $auth['app']['url'] === 'https://getcomposer.org/'
) {
$this->io->write('An existing OAuth token for Composer is present and will be reused');
$contents['token'] = $auth['token'];
break;
}
}
// no existing token, create one
if (empty($contents['token'])) {
$headers[] = array('Content-Type: application/json');
$contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://'. $apiUrl . '/authorizations', false, array(
'retry-auth-failure' => false,
'http' => array(
'method' => 'POST',
'follow_location' => false,
'header' => $headers,
'content' => json_encode(array(
'scopes' => array('repo'),
'note' => $appName,
'note_url' => 'https://getcomposer.org/',
)),
)
)));
$this->io->write('Token successfully created');
}
} catch (TransportException $e) { } catch (TransportException $e) {
if (in_array($e->getCode(), array(403, 401))) { if (in_array($e->getCode(), array(403, 401))) {
// 401 when authentication was supplied, handle 2FA if required. // 401 when authentication was supplied, handle 2FA if required.