Refactor OAuth acquisition code to generalize it
parent
bf5f34a114
commit
39e69a3b12
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Composer\Util;
|
||||
|
||||
use Composer\IO\IOInterface;
|
||||
use Composer\Config;
|
||||
use Composer\Downloader\TransportException;
|
||||
use Composer\Json\JsonFile;
|
||||
|
||||
/**
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
class GitHub
|
||||
{
|
||||
protected $io;
|
||||
protected $config;
|
||||
protected $process;
|
||||
protected $remoteFilesystem;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param IOInterface $io The IO instance
|
||||
* @param Config $config The composer configuration
|
||||
* @param ProcessExecutor $process Process instance, injectable for mocking
|
||||
* @param RemoteFilesystem $remoteFilesystem Remote Filesystem, injectable for mocking
|
||||
*/
|
||||
public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null)
|
||||
{
|
||||
$this->io = $io;
|
||||
$this->config = $config;
|
||||
$this->process = $process ?: new ProcessExecutor;
|
||||
$this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorizes a GitHub domain via OAuth
|
||||
*
|
||||
* @param string $originUrl The host this GitHub instance is located at
|
||||
* @param string $message The reason this authorization is required
|
||||
*/
|
||||
public function authorizeOAuth($originUrl, $message = null)
|
||||
{
|
||||
// if available use token from git config
|
||||
if (0 === $this->process->execute('git config github.accesstoken', $output)) {
|
||||
$this->io->write('Using Github OAuth token stored in git config (github.accesstoken)');
|
||||
$this->io->setAuthorization($originUrl, trim($output), 'x-oauth-basic');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$attemptCounter = 0;
|
||||
|
||||
if ($message) {
|
||||
$this->io->write($message);
|
||||
}
|
||||
$this->io->write('The credentials will be swapped for an OAuth token stored in '.$this->config->get('home').'/config.json, your password will not be stored');
|
||||
$this->io->write('To revoke access to this token you can visit https://github.com/settings/applications');
|
||||
while ($attemptCounter++ < 5) {
|
||||
try {
|
||||
$username = $this->io->ask('Username: ');
|
||||
$password = $this->io->askAndHideAnswer('Password: ');
|
||||
$this->io->setAuthorization($originUrl, $username, $password);
|
||||
|
||||
// build up OAuth app name
|
||||
$appName = 'Composer';
|
||||
if (0 === $this->process->execute('hostname', $output)) {
|
||||
$appName .= ' on ' . trim($output);
|
||||
}
|
||||
|
||||
$contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://api.github.com/authorizations', false, array(
|
||||
'http' => array(
|
||||
'method' => 'POST',
|
||||
'header' => "Content-Type: application/json\r\n",
|
||||
'content' => json_encode(array(
|
||||
'scopes' => array('repo'),
|
||||
'note' => $appName,
|
||||
'note_url' => 'https://getcomposer.org/',
|
||||
)),
|
||||
)
|
||||
)));
|
||||
} catch (TransportException $e) {
|
||||
if (in_array($e->getCode(), array(403, 401))) {
|
||||
$this->io->write('Invalid credentials.');
|
||||
continue;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->io->setAuthorization($originUrl, $contents['token'], 'x-oauth-basic');
|
||||
|
||||
// store value in user config
|
||||
$githubTokens = $this->config->get('github-oauth') ?: array();
|
||||
$githubTokens[$originUrl] = $contents['token'];
|
||||
$this->config->getConfigSource()->addConfigSetting('github-oauth', $githubTokens);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Invalid GitHub credentials 5 times in a row, aborting.");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue