Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. GitLab.php

function GitLab::authorizeOAuth

Attempts to authorize a GitLab domain via OAuth.

Parameters

string $originUrl The host this GitLab instance is located at:

Return value

bool true on success

File

vendor/composer/composer/src/Composer/Util/GitLab.php, line 58

Class

GitLab
@author Roshan Gautam <roshan.gautam@hotmail.com>

Namespace

Composer\Util

Code

public function authorizeOAuth(string $originUrl) : bool {
    // before composer 1.9, origin URLs had no port number in them
    $bcOriginUrl = Preg::replace('{:\\d+}', '', $originUrl);
    if (!in_array($originUrl, $this->config
        ->get('gitlab-domains'), true) && !in_array($bcOriginUrl, $this->config
        ->get('gitlab-domains'), true)) {
        return false;
    }
    // if available use token from git config
    if (0 === $this->process
        ->execute([
        'git',
        'config',
        'gitlab.accesstoken',
    ], $output)) {
        $this->io
            ->setAuthentication($originUrl, trim($output), 'oauth2');
        return true;
    }
    // if available use deploy token from git config
    if (0 === $this->process
        ->execute([
        'git',
        'config',
        'gitlab.deploytoken.user',
    ], $tokenUser) && 0 === $this->process
        ->execute([
        'git',
        'config',
        'gitlab.deploytoken.token',
    ], $tokenPassword)) {
        $this->io
            ->setAuthentication($originUrl, trim($tokenUser), trim($tokenPassword));
        return true;
    }
    // if available use token from composer config
    $authTokens = $this->config
        ->get('gitlab-token');
    if (isset($authTokens[$originUrl])) {
        $token = $authTokens[$originUrl];
    }
    if (isset($authTokens[$bcOriginUrl])) {
        $token = $authTokens[$bcOriginUrl];
    }
    if (isset($token)) {
        $username = is_array($token) ? $token["username"] : $token;
        $password = is_array($token) ? $token["token"] : 'private-token';
        // Composer expects the GitLab token to be stored as username and 'private-token' or 'gitlab-ci-token' to be stored as password
        // Detect cases where this is reversed and resolve automatically resolve it
        if (in_array($username, [
            'private-token',
            'gitlab-ci-token',
            'oauth2',
        ], true)) {
            $this->io
                ->setAuthentication($originUrl, $password, $username);
        }
        else {
            $this->io
                ->setAuthentication($originUrl, $username, $password);
        }
        return true;
    }
    return false;
}
RSS feed
Powered by Drupal