AuthHelper.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\Config;
  13. use Composer\IO\IOInterface;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class AuthHelper
  18. {
  19. protected $io;
  20. protected $config;
  21. public function __construct(IOInterface $io, Config $config)
  22. {
  23. $this->io = $io;
  24. $this->config = $config;
  25. }
  26. public function storeAuth($originUrl, $storeAuth)
  27. {
  28. $store = false;
  29. $configSource = $this->config->getAuthConfigSource();
  30. if ($storeAuth === true) {
  31. $store = $configSource;
  32. } elseif ($storeAuth === 'prompt') {
  33. $answer = $this->io->askAndValidate(
  34. 'Do you want to store credentials for '.$originUrl.' in '.$configSource->getName().' ? [Yn] ',
  35. function ($value) {
  36. $input = strtolower(substr(trim($value), 0, 1));
  37. if (in_array($input, array('y','n'))) {
  38. return $input;
  39. }
  40. throw new \RuntimeException('Please answer (y)es or (n)o');
  41. },
  42. null,
  43. 'y'
  44. );
  45. if ($answer === 'y') {
  46. $store = $configSource;
  47. }
  48. }
  49. if ($store) {
  50. $store->addConfigSetting(
  51. 'http-basic.'.$originUrl,
  52. $this->io->getAuthentication($originUrl)
  53. );
  54. }
  55. }
  56. }