Example #1
0
 /**
  * Repositories requests credentials, let's put them in.
  *
  * @return \Composer\Util\Svn
  */
 protected function doAuthDance()
 {
     $this->io->write("The Subversion server ({$this->url}) requested credentials:");
     $this->hasAuth = true;
     $this->credentials['username'] = $this->io->ask("Username: "******"Password: "******"Should Subversion cache these credentials? (yes/no) ", true);
     return $this;
 }
Example #2
0
 /**
  * Get a patch-data
  *
  * @param   string          $section
  * @param   string          $key
  * @param   string          $ask
  * @param   string          $default
  * @param   string|callable $validator
  * @param   int|false       $attempts
  * @param   bool            $throwIfEmpty
  * @return  string
  * @throws  Exception\DomainException
  */
 public function get($section, $key, $ask = null, $default = null, $validator = null, $attempts = 3, $throwIfEmpty = true)
 {
     if (!empty($this->data[$section][$key])) {
         return $this->data[$section][$key];
     }
     if (!$this->io->isInteractive() || empty($ask)) {
         $result = $default;
     } else {
         $question = (string) $ask;
         if (null !== $default) {
             $question .= ' (default: <info>' . $default . '</info>)';
         }
         $ask = static::PADDING . $question . ': ';
         $hidden = false;
         if (true === $validator) {
             $hidden = true;
             $validator = null;
         }
         if (is_string($validator) && !function_exists($validator)) {
             $pattern = (string) $validator;
             $validator = function ($value) use($pattern) {
                 $matches = array();
                 if (!preg_match($pattern, $value, $matches)) {
                     throw new LogicException(sprintf('"%s" does not match "%s"', $value, $pattern));
                 }
                 return $matches[0];
             };
         }
         if (is_array($validator) && (count($validator) != 2 || !function_exists($validator))) {
             $values = (array) $validator;
             $validator = function ($value) use($values) {
                 if (!in_array($value, $values)) {
                     throw new LogicException(sprintf('"%s" is not available, only "%s" accepted', $value, implode('", "', $values)));
                 }
                 return $value;
             };
         }
         if (is_callable($validator)) {
             $result = $this->io->askAndValidate($ask, $validator, $attempts, $default);
         } else {
             if ($hidden) {
                 $result = $this->io->askAndHideAnswer($ask) ?: $default;
             } else {
                 $result = $this->io->ask($ask, $default);
             }
         }
     }
     if (empty($result)) {
         if ($throwIfEmpty) {
             throw new Exception\DomainException(sprintf('%s: patch-data "%s": "%s", asked as "%s" should not be empty', __METHOD__, $section, $key, $ask));
         }
     } else {
         $this->data[$section][$key] = $result;
     }
     return $result;
 }
Example #3
0
 /**
  * Repositories requests credentials, let's put them in.
  *
  * @throws \RuntimeException
  * @return \Composer\Util\Svn
  */
 protected function doAuthDance()
 {
     // cannot ask for credentials in non interactive mode
     if (!$this->io->isInteractive()) {
         throw new \RuntimeException('can not ask for authentication in non interactive mode');
     }
     $this->io->writeError("The Subversion server ({$this->url}) requested credentials:");
     $this->hasAuth = true;
     $this->credentials['username'] = $this->io->ask("Username: "******"Password: "******"Should Subversion cache these credentials? (yes/no) ", true);
     return $this;
 }
Example #4
0
 /**
  * Generate database config. will store in: etc/secret.yml.
  *
  * @param IOInterface $io
  *
  * @return  void
  */
 protected static function genSecretConfig(IOInterface $io)
 {
     $etc = __DIR__ . '/../../../etc';
     $secret = Yaml::parse(file_get_contents($etc . '/secret.dist.yml'));
     $driver = 'mysql';
     $host = $io->ask("Database host [localhost]: ", 'localhost');
     $name = $io->ask("Database name [natika]: ", 'natika');
     $user = $io->ask("Database user [root]: ", 'root');
     $pass = $io->askAndHideAnswer("Database password: ");
     $prefix = '';
     $secret['database'] = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $pass, 'name' => $name, 'prefix' => $prefix);
     file_put_contents($etc . '/secret.yml', Yaml::dump($secret, 4));
     $io->write('');
     $io->write('Database config setting complete.');
     $io->write('');
 }
 public static function start(Configuration $configuration, IOInterface $io)
 {
     $params = array();
     $params['database-model'] = $io->ask("> Database model? (MYSQLI) ", "MYSQLI");
     $params['database-host'] = $io->ask("> Database host? (localhost) ", "localhost");
     $params['database-port'] = $io->askAndValidate("> Database port? (3306) ", function ($value) {
         return is_int($value);
     }, 3, 3306);
     $params['database-name'] = $io->ask("> Database name? (comodojo) ", "comodojo");
     $params['database-user'] = $io->ask("> Database user? (comodojo) ", "comodojo");
     $params['database-password'] = $io->askAndHideAnswer("> Database password? ");
     $params['database-prefix'] = $io->ask("> Common prefix for database tables? (cmdj_) ", "cmdj_");
     foreach ($params as $param => $value) {
         $configuration->set($param, $value);
     }
 }
Example #6
0
 /**
  * Generate database config. will store in: etc/secret.yml.
  *
  * @param IOInterface $io
  *
  * @return  void
  */
 protected static function genSecretConfig(IOInterface $io)
 {
     $etc = __DIR__ . '/../../../etc';
     $secret = Yaml::parse(file_get_contents($etc . '/secret.dist.yml'));
     if ($io->askConfirmation("\nDo you want to use database? [Y/n]: ", true)) {
         $io->write('');
         $io->write('Database driver only support mysql/postgresql now.');
         $driver = $io->ask("Database driver [mysql]: ", 'mysql');
         $host = $io->ask("Database host [localhost]: ", 'localhost');
         $name = $io->ask("Database name [acme]: ", 'acme');
         $user = $io->ask("Database user [root]: ", 'root');
         $pass = $io->askAndHideAnswer("Database password: "******"Table prefix [wind_]: ", 'wind_');
         $secret['database'] = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $pass, 'name' => $name, 'prefix' => $prefix);
     }
     file_put_contents($etc . '/secret.yml', Yaml::dump($secret, 4));
     $io->write('');
     $io->write('Database config setting complete.');
     $io->write('');
 }
Example #7
0
 public function runCommand($commandCallable, $url, $cwd, $initialClone = false)
 {
     if (preg_match('{^(http|git):}i', $url) && $this->config->get('secure-http')) {
         throw new TransportException("Your configuration does not allow connection to {$url}. See https://getcomposer.org/doc/06-config.md#secure-http for details.");
     }
     if ($initialClone) {
         $origCwd = $cwd;
         $cwd = null;
     }
     if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $url)) {
         throw new \InvalidArgumentException('The source URL ' . $url . ' is invalid, ssh URLs should have a port number after ":".' . "\n" . 'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.');
     }
     if (!$initialClone) {
         // capture username/password from URL if there is one
         $this->process->execute('git remote -v', $output, $cwd);
         if (preg_match('{^(?:composer|origin)\\s+https?://(.+):(.+)@([^/]+)}im', $output, $match)) {
             $this->io->setAuthentication($match[3], urldecode($match[1]), urldecode($match[2]));
         }
     }
     $protocols = $this->config->get('github-protocols');
     if (!is_array($protocols)) {
         throw new \RuntimeException('Config value "github-protocols" must be an array, got ' . gettype($protocols));
     }
     // public github, autoswitch protocols
     if (preg_match('{^(?:https?|git)://' . self::getGitHubDomainsRegex($this->config) . '/(.*)}', $url, $match)) {
         $messages = array();
         foreach ($protocols as $protocol) {
             if ('ssh' === $protocol) {
                 $protoUrl = "git@" . $match[1] . ":" . $match[2];
             } else {
                 $protoUrl = $protocol . "://" . $match[1] . "/" . $match[2];
             }
             if (0 === $this->process->execute(call_user_func($commandCallable, $protoUrl), $ignoredOutput, $cwd)) {
                 return;
             }
             $messages[] = '- ' . $protoUrl . "\n" . preg_replace('#^#m', '  ', $this->process->getErrorOutput());
             if ($initialClone) {
                 $this->filesystem->removeDirectory($origCwd);
             }
         }
         // failed to checkout, first check git accessibility
         $this->throwException('Failed to clone ' . self::sanitizeUrl($url) . ' via ' . implode(', ', $protocols) . ' protocols, aborting.' . "\n\n" . implode("\n", $messages), $url);
     }
     // if we have a private github url and the ssh protocol is disabled then we skip it and directly fallback to https
     $bypassSshForGitHub = preg_match('{^git@' . self::getGitHubDomainsRegex($this->config) . ':(.+?)\\.git$}i', $url) && !in_array('ssh', $protocols, true);
     $command = call_user_func($commandCallable, $url);
     $auth = null;
     if ($bypassSshForGitHub || 0 !== $this->process->execute($command, $ignoredOutput, $cwd)) {
         // private github repository without git access, try https with auth
         if (preg_match('{^git@' . self::getGitHubDomainsRegex($this->config) . ':(.+?)\\.git$}i', $url, $match)) {
             if (!$this->io->hasAuthentication($match[1])) {
                 $gitHubUtil = new GitHub($this->io, $this->config, $this->process);
                 $message = 'Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos';
                 if (!$gitHubUtil->authorizeOAuth($match[1]) && $this->io->isInteractive()) {
                     $gitHubUtil->authorizeOAuthInteractively($match[1], $message);
                 }
             }
             if ($this->io->hasAuthentication($match[1])) {
                 $auth = $this->io->getAuthentication($match[1]);
                 $authUrl = 'https://' . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $match[1] . '/' . $match[2] . '.git';
                 $command = call_user_func($commandCallable, $authUrl);
                 if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
                     return;
                 }
             }
         } elseif ($this->isAuthenticationFailure($url, $match)) {
             // private non-github repo that failed to authenticate
             if (strpos($match[2], '@')) {
                 list($authParts, $match[2]) = explode('@', $match[2], 2);
             }
             $storeAuth = false;
             if ($this->io->hasAuthentication($match[2])) {
                 $auth = $this->io->getAuthentication($match[2]);
             } elseif ($this->io->isInteractive()) {
                 $defaultUsername = null;
                 if (isset($authParts) && $authParts) {
                     if (false !== strpos($authParts, ':')) {
                         list($defaultUsername, ) = explode(':', $authParts, 2);
                     } else {
                         $defaultUsername = $authParts;
                     }
                 }
                 $this->io->writeError('    Authentication required (<info>' . parse_url($url, PHP_URL_HOST) . '</info>):');
                 $auth = array('username' => $this->io->ask('      Username: '******'password' => $this->io->askAndHideAnswer('      Password: '******'store-auths');
             }
             if ($auth) {
                 $authUrl = $match[1] . rawurlencode($auth['username']) . ':' . rawurlencode($auth['password']) . '@' . $match[2] . $match[3];
                 $command = call_user_func($commandCallable, $authUrl);
                 if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) {
                     $this->io->setAuthentication($match[2], $auth['username'], $auth['password']);
                     $authHelper = new AuthHelper($this->io, $this->config);
                     $authHelper->storeAuth($match[2], $storeAuth);
                     return;
                 }
             }
         }
         if ($initialClone) {
             $this->filesystem->removeDirectory($origCwd);
         }
         $this->throwException('Failed to execute ' . self::sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput(), $url);
     }
 }
Example #8
0
 public function queryP4Password(IOInterface $io)
 {
     if (isset($this->p4Password)) {
         return $this->p4Password;
     }
     $password = $this->getP4variable('P4PASSWD');
     if (strlen($password) <= 0) {
         $password = $io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': ');
     }
     $this->p4Password = $password;
     return $password;
 }
 public function promptAuth(HttpGetResponse $res, CConfig $config, IO\IOInterface $io)
 {
     $httpCode = $res->info['http_code'];
     // 404s are only handled for github
     if (404 === $httpCode) {
         return false;
     }
     // fail if the console is not interactive
     if (!$io->isInteractive()) {
         switch ($httpCode) {
             case 401:
                 $message = "The '{$this->getURL()}' URL required authentication.\nYou must be using the interactive console to authenticate";
                 break;
             case 403:
                 $message = "The '{$this->getURL()}' URL could not be accessed.";
                 break;
         }
         throw new Downloader\TransportException($message, $httpCode);
     }
     // fail if we already have auth
     if ($io->hasAuthentication($this->origin)) {
         throw new Downloader\TransportException("Invalid credentials for '{$this->getURL()}', aborting.", $httpCode);
     }
     $io->overwrite("    Authentication required (<info>{$this->host}</info>):");
     $username = $io->ask('      Username: '******'      Password: ');
     $io->setAuthentication($this->origin, $username, $password);
     return true;
 }