GitHub.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class GitHub
  19. {
  20. protected $io;
  21. protected $config;
  22. protected $process;
  23. protected $remoteFilesystem;
  24. /**
  25. * Constructor.
  26. *
  27. * @param IOInterface $io The IO instance
  28. * @param Config $config The composer configuration
  29. * @param ProcessExecutor $process Process instance, injectable for mocking
  30. * @param RemoteFilesystem $remoteFilesystem Remote Filesystem, injectable for mocking
  31. */
  32. public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null)
  33. {
  34. $this->io = $io;
  35. $this->config = $config;
  36. $this->process = $process ?: new ProcessExecutor;
  37. $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io, $config);
  38. }
  39. /**
  40. * Attempts to authorize a GitHub domain via OAuth
  41. *
  42. * @param string $originUrl The host this GitHub instance is located at
  43. * @return bool true on success
  44. */
  45. public function authorizeOAuth($originUrl)
  46. {
  47. if (!in_array($originUrl, $this->config->get('github-domains'))) {
  48. return false;
  49. }
  50. // if available use token from git config
  51. if (0 === $this->process->execute('git config github.accesstoken', $output)) {
  52. $this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic');
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * Authorizes a GitHub domain interactively via OAuth
  59. *
  60. * @param string $originUrl The host this GitHub instance is located at
  61. * @param string $message The reason this authorization is required
  62. * @throws \RuntimeException
  63. * @throws TransportException|\Exception
  64. * @return bool true on success
  65. */
  66. public function authorizeOAuthInteractively($originUrl, $message = null)
  67. {
  68. if ($message) {
  69. $this->io->writeError($message);
  70. }
  71. $note = 'Composer';
  72. if ($this->config->get('github-expose-hostname') === true && 0 === $this->process->execute('hostname', $output)) {
  73. $note .= ' on ' . trim($output);
  74. }
  75. $note .= ' ' . date('Y-m-d Hi');
  76. $url = 'https://'.$originUrl.'/settings/tokens/new?scopes=repo&description=' . str_replace('%20', '+', rawurlencode($note));
  77. $this->io->writeError(sprintf('Head to %s', $url));
  78. $this->io->writeError(sprintf('to retrieve a token. It will be stored in "%s" for future use by Composer.', $this->config->getAuthConfigSource()->getName()));
  79. $token = trim($this->io->askAndHideAnswer('Token (hidden): '));
  80. if (!$token) {
  81. $this->io->writeError('<warning>No token given, aborting.</warning>');
  82. $this->io->writeError('You can also add it manually later by using "composer config github-oauth.github.com <token>"');
  83. return false;
  84. }
  85. $this->io->setAuthentication($originUrl, $token, 'x-oauth-basic');
  86. try {
  87. $apiUrl = ('github.com' === $originUrl) ? 'api.github.com' : $originUrl . '/api/v3';
  88. $this->remoteFilesystem->getContents($originUrl, 'https://'. $apiUrl . '/rate_limit', false, array(
  89. 'retry-auth-failure' => false,
  90. ));
  91. } catch (TransportException $e) {
  92. if (in_array($e->getCode(), array(403, 401))) {
  93. $this->io->writeError('<error>Invalid token provided.</error>');
  94. $this->io->writeError('You can also add it manually later by using "composer config github-oauth.github.com <token>"');
  95. return false;
  96. }
  97. throw $e;
  98. }
  99. // store value in user config
  100. $this->config->getConfigSource()->removeConfigSetting('github-oauth.'.$originUrl);
  101. $this->config->getAuthConfigSource()->addConfigSetting('github-oauth.'.$originUrl, $token);
  102. $this->io->writeError('<info>Token stored successfully.</info>');
  103. return true;
  104. }
  105. }