Explorar el Código

add gitlab token auth for git clone

Andrew Gillis hace 6 años
padre
commit
dec2b5cd50
Se han modificado 1 ficheros con 27 adiciones y 0 borrados
  1. 27 0
      src/Composer/Util/Git.php

+ 27 - 0
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<user>[^@]+?):(?P<password>.+?)@}', function ($m) {