final public function authenticateConduit()

in src/workflow/ArcanistWorkflow.php [304:433]


  final public function authenticateConduit() {
    if ($this->isConduitAuthenticated()) {
      return $this;
    }

    $this->establishConduit();
    $credentials = $this->conduitCredentials;

    try {
      if (!$credentials) {
        throw new Exception(
          pht(
            'Set conduit credentials with %s before authenticating conduit!',
            'setConduitCredentials()'));
      }

      // If we have `token`, this server supports the simpler, new-style
      // token-based authentication. Use that instead of all the certificate
      // stuff.
      $token = idx($credentials, 'token');
      if (strlen($token)) {
        $conduit = $this->getConduit();

        $conduit->setConduitToken($token);

        try {
          // UBER CODE
          $usso = new UberUSSO();
          $usso->enhanceConduitClient($conduit);
          try {
            $result = $this->getConduit()->callMethodSynchronous(
              'user.whoami',
              array());
          } catch (HTTPFutureHTTPResponseStatus $ex) {
            if (!$usso->enhanceConduitClient($conduit, $ex)) {
              // if no enhancements came from error, then fail
              // otherwise continue trying
              throw $ex;
            }
            $result = $this->getConduit()->callMethodSynchronous(
              'user.whoami',
               array());
          }
          // UBER CODE END

          $this->userName = $result['userName'];
          $this->userPHID = $result['phid'];

          $this->conduitAuthenticated = true;

          return $this;
        } catch (Exception $ex) {
          $conduit->setConduitToken(null);
          throw $ex;
        }
      }

      if (empty($credentials['user'])) {
        throw new ConduitClientException(
          'ERR-INVALID-USER',
          pht('Empty user in credentials.'));
      }
      if (empty($credentials['certificate'])) {
        throw new ConduitClientException(
          'ERR-NO-CERTIFICATE',
          pht('Empty certificate in credentials.'));
      }

      $description = idx($credentials, 'description', '');
      $user        = $credentials['user'];
      $certificate = $credentials['certificate'];

      $connection = $this->getConduit()->callMethodSynchronous(
        'conduit.connect',
        array(
          'client'              => 'arc',
          'clientVersion'       => $this->getConduitVersion(),
          'clientDescription'   => php_uname('n').':'.$description,
          'user'                => $user,
          'certificate'         => $certificate,
          'host'                => $this->conduitURI,
        ));
    } catch (ConduitClientException $ex) {
      if ($ex->getErrorCode() == 'ERR-NO-CERTIFICATE' ||
          $ex->getErrorCode() == 'ERR-INVALID-USER' ||
          $ex->getErrorCode() == 'ERR-INVALID-AUTH') {
        $conduit_uri = $this->conduitURI;
        $message = phutil_console_format(
          "\n%s\n\n    %s\n\n%s\n%s",
          pht('YOU NEED TO __INSTALL A CERTIFICATE__ TO LOGIN TO PHABRICATOR'),
          pht('To do this, run: **%s**', 'arc install-certificate'),
          pht("The server '%s' rejected your request:", $conduit_uri),
          $ex->getMessage());
        throw new ArcanistUsageException($message);
      } else if ($ex->getErrorCode() == 'NEW-ARC-VERSION') {

        // Cleverly disguise this as being AWESOME!!!

        echo phutil_console_format("**%s**\n\n", pht('New Version Available!'));
        echo phutil_console_wrap($ex->getMessage());
        echo "\n\n";
        echo pht('In most cases, arc can be upgraded automatically.')."\n";

        $ok = phutil_console_confirm(
          pht('Upgrade arc now?'),
          $default_no = false);
        if (!$ok) {
          throw $ex;
        }

        $root = dirname(phutil_get_library_root('arcanist'));

        chdir($root);
        $err = phutil_passthru('%s upgrade', $root.'/bin/arc');
        if (!$err) {
          echo "\n".pht('Try running your arc command again.')."\n";
        }
        exit(1);
      } else {
        throw $ex;
      }
    }

    $this->userName = $user;
    $this->userPHID = $connection['userPHID'];

    $this->conduitAuthenticated = true;

    return $this;
  }