BaseIO.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\IO;
  12. use Composer\Config;
  13. use Composer\Util\ProcessExecutor;
  14. abstract class BaseIO implements IOInterface
  15. {
  16. protected $authentications = array();
  17. /**
  18. * {@inheritDoc}
  19. */
  20. public function getAuthentications()
  21. {
  22. return $this->authentications;
  23. }
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function hasAuthentication($repositoryName)
  28. {
  29. return isset($this->authentications[$repositoryName]);
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function getAuthentication($repositoryName)
  35. {
  36. if (isset($this->authentications[$repositoryName])) {
  37. return $this->authentications[$repositoryName];
  38. }
  39. return array('username' => null, 'password' => null);
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function setAuthentication($repositoryName, $username, $password = null)
  45. {
  46. $this->authentications[$repositoryName] = array('username' => $username, 'password' => $password);
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function loadConfiguration(Config $config)
  52. {
  53. $githubOauth = $config->get('github-oauth') ?: array();
  54. $gitlabOauth = $config->get('gitlab-oauth') ?: array();
  55. $httpBasic = $config->get('http-basic') ?: array();
  56. // reload oauth token from config if available
  57. foreach ($githubOauth as $domain => $token) {
  58. if (!preg_match('{^[a-z0-9]+$}', $token)) {
  59. throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"');
  60. }
  61. $this->setAuthentication($domain, $token, 'x-oauth-basic');
  62. }
  63. foreach ($gitlabOauth as $domain => $token) {
  64. $this->setAuthentication($domain, $token, 'oauth2');
  65. }
  66. // reload http basic credentials from config if available
  67. foreach ($httpBasic as $domain => $cred) {
  68. $this->setAuthentication($domain, $cred['username'], $cred['password']);
  69. }
  70. // setup process timeout
  71. ProcessExecutor::setTimeout((int) $config->get('process-timeout'));
  72. }
  73. }