GitHub.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. use Composer\IO\IOInterface;
  13. use Composer\Config;
  14. use Composer\Downloader\TransportException;
  15. use Composer\Json\JsonFile;
  16. /**
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class GitHub
  20. {
  21. protected $io;
  22. protected $config;
  23. protected $process;
  24. protected $remoteFilesystem;
  25. /**
  26. * Constructor.
  27. *
  28. * @param IOInterface $io The IO instance
  29. * @param Config $config The composer configuration
  30. * @param ProcessExecutor $process Process instance, injectable for mocking
  31. * @param RemoteFilesystem $remoteFilesystem Remote Filesystem, injectable for mocking
  32. */
  33. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null)
  34. {
  35. $this->io = $io;
  36. $this->config = $config;
  37. $this->process = $process ?: new ProcessExecutor;
  38. $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io);
  39. }
  40. /**
  41. * Authorizes a GitHub domain via OAuth
  42. *
  43. * @param string $originUrl The host this GitHub instance is located at
  44. * @param string $message The reason this authorization is required
  45. */
  46. public function authorizeOAuth($originUrl, $message = null)
  47. {
  48. // if available use token from git config
  49. if (0 === $this->process->execute('git config github.accesstoken', $output)) {
  50. $this->io->setAuthorization($originUrl, trim($output), 'x-oauth-basic');
  51. return;
  52. }
  53. $attemptCounter = 0;
  54. if ($message) {
  55. $this->io->write($message);
  56. }
  57. $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');
  58. $this->io->write('To revoke access to this token you can visit https://github.com/settings/applications');
  59. while ($attemptCounter++ < 5) {
  60. try {
  61. $username = $this->io->ask('Username: ');
  62. $password = $this->io->askAndHideAnswer('Password: ');
  63. $this->io->setAuthorization($originUrl, $username, $password);
  64. // build up OAuth app name
  65. $appName = 'Composer';
  66. if (0 === $this->process->execute('hostname', $output)) {
  67. $appName .= ' on ' . trim($output);
  68. }
  69. $contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://api.github.com/authorizations', false, array(
  70. 'http' => array(
  71. 'method' => 'POST',
  72. 'header' => "Content-Type: application/json\r\n",
  73. 'content' => json_encode(array(
  74. 'scopes' => array('repo'),
  75. 'note' => $appName,
  76. 'note_url' => 'https://getcomposer.org/',
  77. )),
  78. )
  79. )));
  80. } catch (TransportException $e) {
  81. if (in_array($e->getCode(), array(403, 401))) {
  82. $this->io->write('Invalid credentials.');
  83. continue;
  84. }
  85. throw $e;
  86. }
  87. $this->io->setAuthorization($originUrl, $contents['token'], 'x-oauth-basic');
  88. // store value in user config
  89. $githubTokens = $this->config->get('github-oauth') ?: array();
  90. $githubTokens[$originUrl] = $contents['token'];
  91. $this->config->getConfigSource()->addConfigSetting('github-oauth', $githubTokens);
  92. return;
  93. }
  94. throw new \RuntimeException("Invalid GitHub credentials 5 times in a row, aborting.");
  95. }
  96. }