Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistInstallCertificateWorkflow.php
| Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | public function requiresConduit() { | ||||
| return false; | return false; | ||||
| } | } | ||||
| public function requiresWorkingCopy() { | public function requiresWorkingCopy() { | ||||
| return false; | return false; | ||||
| } | } | ||||
| public function run() { | public function run() { | ||||
| $console = PhutilConsole::getConsole(); | |||||
| $uri = $this->determineConduitURI(); | $uri = $this->determineConduitURI(); | ||||
| $this->setConduitURI($uri); | $this->setConduitURI($uri); | ||||
| $configuration_manager = $this->getConfigurationManager(); | $configuration_manager = $this->getConfigurationManager(); | ||||
| echo "Installing certificate for '{$uri}'...\n"; | |||||
| $config = $configuration_manager->readUserConfigurationFile(); | $config = $configuration_manager->readUserConfigurationFile(); | ||||
| echo "Trying to connect to server...\n"; | $console->writeOut( | ||||
| "%s\n", | |||||
| pht('Trying to connect to server...')); | |||||
| $conduit = $this->establishConduit()->getConduit(); | $conduit = $this->establishConduit()->getConduit(); | ||||
| try { | try { | ||||
| $conduit->callMethodSynchronous('conduit.ping', array()); | $conduit->callMethodSynchronous('conduit.ping', array()); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| throw new ArcanistUsageException( | throw new ArcanistUsageException( | ||||
| 'Failed to connect to server: '.$ex->getMessage()); | pht( | ||||
| 'Failed to connect to server (%s): %s', | |||||
| $uri, | |||||
| $ex->getMessage())); | |||||
| } | } | ||||
| echo "Connection OK!\n"; | |||||
| $token_uri = new PhutilURI($uri); | $token_uri = new PhutilURI($uri); | ||||
| $token_uri->setPath('/conduit/token/'); | $token_uri->setPath('/conduit/token/'); | ||||
| echo "\n"; | // Check if this server supports the more modern token-based login. | ||||
| $is_token_auth = false; | |||||
| try { | |||||
| $capabilities = $conduit->callMethodSynchronous( | |||||
| 'conduit.getcapabilities', | |||||
| array()); | |||||
| $auth = idx($capabilities, 'authentication', array()); | |||||
| if (in_array('token', $auth)) { | |||||
| $token_uri->setPath('/conduit/login/'); | |||||
| $is_token_auth = true; | |||||
| } | |||||
| } catch (Exception $ex) { | |||||
| // Ignore. | |||||
| } | |||||
| echo phutil_console_format("**LOGIN TO PHABRICATOR**\n"); | echo phutil_console_format("**LOGIN TO PHABRICATOR**\n"); | ||||
| echo "Open this page in your browser and login to Phabricator if ". | echo "Open this page in your browser and login to Phabricator if ". | ||||
| "necessary:\n"; | "necessary:\n"; | ||||
| echo "\n"; | echo "\n"; | ||||
| echo " {$token_uri}\n"; | echo " {$token_uri}\n"; | ||||
| echo "\n"; | echo "\n"; | ||||
| echo 'Then paste the token on that page below.'; | echo 'Then paste the API Token on that page below.'; | ||||
| do { | do { | ||||
| $token = phutil_console_prompt('Paste token from that page:'); | $token = phutil_console_prompt('Paste API Token from that page:'); | ||||
| $token = trim($token); | $token = trim($token); | ||||
| if (strlen($token)) { | if (strlen($token)) { | ||||
| break; | break; | ||||
| } | } | ||||
| } while (true); | } while (true); | ||||
| if ($is_token_auth) { | |||||
| if (strlen($token) != 32) { | |||||
| throw new ArcanistUsageException( | |||||
| pht( | |||||
| 'The token "%s" is not formatted correctly. API tokens should '. | |||||
| 'be 32 characters long. Make sure you visited the correct URI '. | |||||
| 'and copy/pasted the token correctly.', | |||||
| $token)); | |||||
| } | |||||
| if (strncmp($token, 'cli-', 4) !== 0) { | |||||
| throw new ArcanistUsageException( | |||||
| pht( | |||||
| 'The token "%s" is not formatted correctly. Valid API tokens '. | |||||
| 'should begin "cli-" and be 32 characters long. Make sure you '. | |||||
| 'visited the correct URI and copy/pasted the token correctly.', | |||||
| $token)); | |||||
| } | |||||
| $conduit->setConduitToken($token); | |||||
| try { | |||||
| $conduit->callMethodSynchronous('user.whoami', array()); | |||||
| } catch (Exception $ex) { | |||||
| throw new ArcanistUsageException( | |||||
| pht( | |||||
| 'The token "%s" is not a valid API Token. The server returned '. | |||||
| 'this response when trying to use it as a token: %s', | |||||
| $token, | |||||
| $ex->getMessage())); | |||||
| } | |||||
| $config['hosts'][$uri] = array( | |||||
| 'token' => $token, | |||||
| ); | |||||
| } else { | |||||
| echo "\n"; | echo "\n"; | ||||
| echo "Downloading authentication certificate...\n"; | echo "Downloading authentication certificate...\n"; | ||||
| $info = $conduit->callMethodSynchronous( | $info = $conduit->callMethodSynchronous( | ||||
| 'conduit.getcertificate', | 'conduit.getcertificate', | ||||
| array( | array( | ||||
| 'token' => $token, | 'token' => $token, | ||||
| 'host' => $uri, | 'host' => $uri, | ||||
| )); | )); | ||||
| $user = $info['username']; | $user = $info['username']; | ||||
| echo "Installing certificate for '{$user}'...\n"; | echo "Installing certificate for '{$user}'...\n"; | ||||
| $config['hosts'][$uri] = array( | $config['hosts'][$uri] = array( | ||||
| 'user' => $user, | 'user' => $user, | ||||
| 'cert' => $info['certificate'], | 'cert' => $info['certificate'], | ||||
| ); | ); | ||||
| } | |||||
| echo "Writing ~/.arcrc...\n"; | echo "Writing ~/.arcrc...\n"; | ||||
| $configuration_manager->writeUserConfigurationFile($config); | $configuration_manager->writeUserConfigurationFile($config); | ||||
| if ($is_token_auth) { | |||||
| echo phutil_console_format( | |||||
| "<bg:green>** SUCCESS! **</bg> API Token installed.\n"); | |||||
| } else { | |||||
| echo phutil_console_format( | echo phutil_console_format( | ||||
| "<bg:green>** SUCCESS! **</bg> Certificate installed.\n"); | "<bg:green>** SUCCESS! **</bg> Certificate installed.\n"); | ||||
| } | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| private function determineConduitURI() { | private function determineConduitURI() { | ||||
| $uri = $this->getArgument('uri'); | $uri = $this->getArgument('uri'); | ||||
| if (count($uri) > 1) { | if (count($uri) > 1) { | ||||
| throw new ArcanistUsageException('Specify at most one URI.'); | throw new ArcanistUsageException('Specify at most one URI.'); | ||||
| Show All 19 Lines | |||||