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

Breadcrumb

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

function GitHub::authorizeOAuthInteractively

Authorizes a GitHub domain interactively via OAuth

Parameters

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

string $message The reason this authorization is required:

Return value

bool true on success

Throws

\RuntimeException

TransportException|\Exception

File

vendor/composer/composer/src/Composer/Util/GitHub.php, line 82

Class

GitHub
@author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Util

Code

public function authorizeOAuthInteractively(string $originUrl, ?string $message = null) : bool {
    if ($message) {
        $this->io
            ->writeError($message);
    }
    $note = 'Composer';
    if ($this->config
        ->get('github-expose-hostname') === true && 0 === $this->process
        ->execute([
        'hostname',
    ], $output)) {
        $note .= ' on ' . trim($output);
    }
    $note .= ' ' . date('Y-m-d Hi');
    $url = 'https://' . $originUrl . '/settings/tokens/new?scopes=&description=' . str_replace('%20', '+', rawurlencode($note));
    $this->io
        ->writeError('When working with _public_ GitHub repositories only, head here to retrieve a token:');
    $this->io
        ->writeError($url);
    $this->io
        ->writeError('This token will have read-only permission for public information only.');
    $localAuthConfig = $this->config
        ->getLocalAuthConfigSource();
    $url = 'https://' . $originUrl . '/settings/tokens/new?scopes=repo&description=' . str_replace('%20', '+', rawurlencode($note));
    $this->io
        ->writeError('When you need to access _private_ GitHub repositories as well, go to:');
    $this->io
        ->writeError($url);
    $this->io
        ->writeError('Note that such tokens have broad read/write permissions on your behalf, even if not needed by Composer.');
    $this->io
        ->writeError(sprintf('Tokens will be stored in plain text in "%s" for future use by Composer.', ($localAuthConfig !== null ? $localAuthConfig->getName() . ' OR ' : '') . $this->config
        ->getAuthConfigSource()
        ->getName()));
    $this->io
        ->writeError('For additional information, check https://getcomposer.org/doc/articles/authentication-for-private-packages.md#github-oauth');
    $storeInLocalAuthConfig = false;
    if ($localAuthConfig !== null) {
        $storeInLocalAuthConfig = $this->io
            ->askConfirmation('A local auth config source was found, do you want to store the token there?', true);
    }
    $token = trim((string) $this->io
        ->askAndHideAnswer('Token (hidden): '));
    if ($token === '') {
        $this->io
            ->writeError('<warning>No token given, aborting.</warning>');
        $this->io
            ->writeError('You can also add it manually later by using "composer config --global --auth github-oauth.github.com <token>"');
        return false;
    }
    $this->io
        ->setAuthentication($originUrl, $token, 'x-oauth-basic');
    try {
        $apiUrl = 'github.com' === $originUrl ? 'api.github.com/' : $originUrl . '/api/v3/';
        $this->httpDownloader
            ->get('https://' . $apiUrl, [
            'retry-auth-failure' => false,
        ]);
    } catch (TransportException $e) {
        if (in_array($e->getCode(), [
            403,
            401,
        ])) {
            $this->io
                ->writeError('<error>Invalid token provided.</error>');
            $this->io
                ->writeError('You can also add it manually later by using "composer config --global --auth github-oauth.github.com <token>"');
            return false;
        }
        throw $e;
    }
    // store value in local/user config
    $authConfigSource = $storeInLocalAuthConfig && $localAuthConfig !== null ? $localAuthConfig : $this->config
        ->getAuthConfigSource();
    $this->config
        ->getConfigSource()
        ->removeConfigSetting('github-oauth.' . $originUrl);
    $authConfigSource->addConfigSetting('github-oauth.' . $originUrl, $token);
    $this->io
        ->writeError('<info>Token stored successfully.</info>');
    return true;
}
RSS feed
Powered by Drupal