From dec2b5cd5008a5e69a2f47ea32310ac39ac71052 Mon Sep 17 00:00:00 2001 From: Andrew Gillis Date: Mon, 28 Jan 2019 13:36:28 -0500 Subject: [PATCH] add gitlab token auth for git clone --- src/Composer/Util/Git.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Composer/Util/Git.php b/src/Composer/Util/Git.php index 37410eecd..74e5c286f 100644 --- a/src/Composer/Util/Git.php +++ b/src/Composer/Util/Git.php @@ -153,6 +153,28 @@ class Git return; } } + } elseif (preg_match('{^(https?)://' . self::getGitLabDomainsRegex($this->config) . '/(.*)}', $url, $match)) { + if (!$this->io->hasAuthentication($match[2])) { + $gitLabUtil = new GitLab($this->io, $this->config, $this->process); + $message = 'Cloning failed, enter your GitLab credentials to access private repos'; + + if (!$gitLabUtil->authorizeOAuth($match[2]) && $this->io->isInteractive()) { + $gitLabUtil->authorizeOAuthInteractively($match[1], $match[2], $message); + } + } + + if ($this->io->hasAuthentication($match[2])) { + $auth = $this->io->getAuthentication($match[2]); + if($auth['password'] === 'private-token' || $auth['password'] === 'oauth2') { + $authUrl = $match[1] . '://' . rawurlencode($auth['password']) . ':' . rawurlencode($auth['username']) . '@' . $match[2] . '/' . $match[3]; // swap username and password + } else { + $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)) { + 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); @@ -304,6 +326,11 @@ class Git return '(' . implode('|', array_map('preg_quote', $config->get('github-domains'))) . ')'; } + public static function getGitLabDomainsRegex(Config $config) + { + return '(' . implode('|', array_map('preg_quote', $config->get('gitlab-domains'))) . ')'; + } + public static function sanitizeUrl($message) { return preg_replace_callback('{://(?P[^@]+?):(?P.+?)@}', function ($m) {