Example #1
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage Invalid GitLab credentials 5 times in a row, aborting.
  */
 public function testUsernamePasswordFailure()
 {
     $io = $this->getIOMock();
     $io->expects($this->exactly(5))->method('ask')->with('Username: '******'askAndHideAnswer')->with('Password: '******'getContents')->will($this->throwException(new TransportException('', 401)));
     $config = $this->getConfigMock();
     $config->expects($this->exactly(1))->method('getAuthConfigSource')->willReturn($this->getAuthJsonMock());
     $gitLab = new GitLab($io, $config, null, $rfs);
     $gitLab->authorizeOAuthInteractively('https', $this->origin);
 }
Example #2
0
 public function promptAuth(HttpGetResponse $res, CConfig $config, IO\IOInterface $io)
 {
     $httpCode = $res->info['http_code'];
     $message = "\nCould not fetch {$this->getURL()}, enter your {$this->origin} credentials ";
     if (401 === $httpCode) {
         $message .= 'to access private repos';
     } else {
         $message .= 'to go over the API rate limit';
     }
     $gitlab = new Util\GitLab($io, $config, null);
     if ($gitlab->authorizeOAuth($this->origin)) {
         return true;
     }
     if ($io->isInteractive() && $gitlab->authorizeOAuthInteractively($this->origin, $message)) {
         return true;
     }
     throw new Downloader\TransportException("Could not authenticate against {$this->origin}", 401);
 }
Example #3
0
 protected function promptAuthAndRetry($httpStatus, $reason = null)
 {
     if ($this->config && in_array($this->originUrl, $this->config->get('github-domains'), true)) {
         $message = "\n" . 'Could not fetch ' . $this->fileUrl . ', please create a GitHub OAuth token ' . ($httpStatus === 404 ? 'to access private repos' : 'to go over the API rate limit');
         $gitHubUtil = new GitHub($this->io, $this->config, null);
         if (!$gitHubUtil->authorizeOAuth($this->originUrl) && (!$this->io->isInteractive() || !$gitHubUtil->authorizeOAuthInteractively($this->originUrl, $message))) {
             throw new TransportException('Could not authenticate against ' . $this->originUrl, 401);
         }
     } elseif ($this->config && in_array($this->originUrl, $this->config->get('gitlab-domains'), true)) {
         $message = "\n" . 'Could not fetch ' . $this->fileUrl . ', enter your ' . $this->originUrl . ' credentials ' . ($httpStatus === 401 ? 'to access private repos' : 'to go over the API rate limit');
         $gitLabUtil = new GitLab($this->io, $this->config, null);
         if (!$gitLabUtil->authorizeOAuth($this->originUrl) && (!$this->io->isInteractive() || !$gitLabUtil->authorizeOAuthInteractively($this->scheme, $this->originUrl, $message))) {
             throw new TransportException('Could not authenticate against ' . $this->originUrl, 401);
         }
     } else {
         // 404s are only handled for github
         if ($httpStatus === 404) {
             return;
         }
         // fail if the console is not interactive
         if (!$this->io->isInteractive()) {
             if ($httpStatus === 401) {
                 $message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console to authenticate";
             }
             if ($httpStatus === 403) {
                 $message = "The '" . $this->fileUrl . "' URL could not be accessed: " . $reason;
             }
             throw new TransportException($message, $httpStatus);
         }
         // fail if we already have auth
         if ($this->io->hasAuthentication($this->originUrl)) {
             throw new TransportException("Invalid credentials for '" . $this->fileUrl . "', aborting.", $httpStatus);
         }
         $this->io->overwriteError('    Authentication required (<info>' . parse_url($this->fileUrl, PHP_URL_HOST) . '</info>):');
         $username = $this->io->ask('      Username: '******'      Password: '******'store-auths');
     }
     $this->retry = true;
     throw new TransportException('RETRY');
 }
Example #4
0
 /**
  * {@inheritDoc}
  */
 protected function getContents($url, $fetchingRepoData = false)
 {
     try {
         return parent::getContents($url);
     } catch (TransportException $e) {
         $gitLabUtil = new GitLab($this->io, $this->config, $this->process, $this->remoteFilesystem);
         switch ($e->getCode()) {
             case 401:
             case 404:
                 // try to authorize only if we are fetching the main /repos/foo/bar data, otherwise it must be a real 404
                 if (!$fetchingRepoData) {
                     throw $e;
                 }
                 if ($gitLabUtil->authorizeOAuth($this->originUrl)) {
                     return parent::getContents($url);
                 }
                 if (!$this->io->isInteractive()) {
                     return $this->attemptCloneFallback();
                 }
                 $this->io->writeError('<warning>Failed to download ' . $this->owner . '/' . $this->repository . ':' . $e->getMessage() . '</warning>');
                 $gitLabUtil->authorizeOAuthInteractively($this->scheme, $this->originUrl, 'Your credentials are required to fetch private repository metadata (<info>' . $this->url . '</info>)');
                 return parent::getContents($url);
             case 403:
                 if (!$this->io->hasAuthentication($this->originUrl) && $gitLabUtil->authorizeOAuth($this->originUrl)) {
                     return parent::getContents($url);
                 }
                 if (!$this->io->isInteractive() && $fetchingRepoData) {
                     return $this->attemptCloneFallback();
                 }
                 throw $e;
             default:
                 throw $e;
         }
     }
 }
 protected function promptAuth(Aspects\HttpGetRequest $req, Aspects\HttpGetResponse $res)
 {
     $io = $this->io;
     $httpCode = $res->info['http_code'];
     if ('github' === $req->special) {
         $message = "\nCould not fetch {$req->getURL()}, please create a GitHub OAuth token ";
         if (404 === $httpCode) {
             $message .= 'to access private repos';
         } else {
             $message .= 'to go over the API rate limit';
         }
         $github = new Util\GitHub($io, $this->config, null);
         if ($github->authorizeOAuth($req->origin)) {
             $this->retry = true;
             return;
         }
         if ($io->isInteractive() && $github->authorizeOAuthInteractively($req->origin, $message)) {
             $this->retry = true;
             return;
         }
         throw new Downloader\TransportException("Could not authenticate against {$req->origin}", 401);
     }
     if ('gitlab' === $req->special) {
         $message = "\nCould not fetch {$req->getURL()}, enter your {$req->origin} credentials ";
         if (401 === $httpCode) {
             $message .= 'to access private repos';
         } else {
             $message .= 'to go over the API rate limit';
         }
         $gitlab = new Util\GitLab($io, $this->config, null);
         if ($gitlab->authorizeOAuth($req->origin)) {
             $this->retry = true;
             return;
         }
         if ($io->isInteractive() && $gitlab->authorizeOAuthInteractively($req->origin, $message)) {
             $this->retry = true;
             return;
         }
         throw new Downloader\TransportException("Could not authenticate against {$req->origin}", 401);
     }
     // 404s are only handled for github
     if (404 === $httpCode) {
         return;
     }
     // fail if the console is not interactive
     if (!$io->isInteractive()) {
         switch ($httpCode) {
             case 401:
                 $message = "The '{$req->getURL()}' URL required authentication.\nYou must be using the interactive console to authenticate";
                 break;
             case 403:
                 $message = "The '{$req->getURL()}' URL could not be accessed.";
                 break;
         }
         throw new Downloader\TransportException($message, $httpCode);
     }
     // fail if we already have auth
     if ($io->hasAuthentication($req->origin)) {
         throw new Downloader\TransportException("Invalid credentials for '{$req->getURL()}', aborting.", $res->info['http_code']);
     }
     $io->overwrite("    Authentication required (<info>{$req->host}</info>):");
     $username = $io->ask('      Username: '******'      Password: ');
     $io->setAuthentication($req->origin, $username, $password);
     $this->retry = true;
 }