Path: blob/master/src/applications/auth/controller/PhabricatorAuthSSHKeyGenerateController.php
12256 views
<?php12final class PhabricatorAuthSSHKeyGenerateController3extends PhabricatorAuthSSHKeyController {45public function handleRequest(AphrontRequest $request) {6$viewer = $this->getViewer();78$key = $this->newKeyForObjectPHID($request->getStr('objectPHID'));9if (!$key) {10return new Aphront404Response();11}1213$cancel_uri = $key->getObject()->getSSHPublicKeyManagementURI($viewer);1415$token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(16$viewer,17$request,18$cancel_uri);1920if ($request->isFormPost()) {21$default_name = $key->getObject()->getSSHKeyDefaultName();2223$keys = PhabricatorSSHKeyGenerator::generateKeypair();24list($public_key, $private_key) = $keys;2526$key_name = $default_name.'.key';2728$file = PhabricatorFile::newFromFileData(29$private_key,30array(31'name' => $key_name,32'ttl.relative' => phutil_units('10 minutes in seconds'),33'viewPolicy' => $viewer->getPHID(),34));3536$public_key = PhabricatorAuthSSHPublicKey::newFromRawKey($public_key);3738$type = $public_key->getType();39$body = $public_key->getBody();40$comment = pht('Generated');4142$entire_key = "{$type} {$body} {$comment}";4344$type_create = PhabricatorTransactions::TYPE_CREATE;45$type_name = PhabricatorAuthSSHKeyTransaction::TYPE_NAME;46$type_key = PhabricatorAuthSSHKeyTransaction::TYPE_KEY;4748$xactions = array();4950$xactions[] = id(new PhabricatorAuthSSHKeyTransaction())51->setTransactionType(PhabricatorTransactions::TYPE_CREATE);5253$xactions[] = id(new PhabricatorAuthSSHKeyTransaction())54->setTransactionType($type_name)55->setNewValue($default_name);5657$xactions[] = id(new PhabricatorAuthSSHKeyTransaction())58->setTransactionType($type_key)59->setNewValue($entire_key);6061$editor = id(new PhabricatorAuthSSHKeyEditor())62->setActor($viewer)63->setContentSourceFromRequest($request)64->applyTransactions($key, $xactions);6566$download_link = phutil_tag(67'a',68array(69'href' => $file->getDownloadURI(),70),71array(72id(new PHUIIconView())->setIcon('fa-download'),73' ',74pht('Download Private Key (%s)', $key_name),75));76$download_link = phutil_tag('strong', array(), $download_link);7778// NOTE: We're disabling workflow on cancel so the page reloads, showing79// the new key.8081return $this->newDialog()82->setTitle(pht('Download Private Key'))83->appendParagraph(84pht(85'A keypair has been generated, and the public key has been '.86'added as a recognized key.'))87->appendParagraph($download_link)88->appendParagraph(89pht(90'After you download the private key, it will be destroyed. '.91'You will not be able to retrieve it if you lose your copy.'))92->setDisableWorkflowOnCancel(true)93->addCancelButton($cancel_uri, pht('Done'));94}9596try {97PhabricatorSSHKeyGenerator::assertCanGenerateKeypair();9899return $this->newDialog()100->setTitle(pht('Generate New Keypair'))101->addHiddenInput('objectPHID', $key->getObject()->getPHID())102->appendParagraph(103pht(104'This workflow will generate a new SSH keypair, add the public '.105'key, and let you download the private key.'))106->appendParagraph(107pht('The private key will not be retained.'))108->addSubmitButton(pht('Generate New Keypair'))109->addCancelButton($cancel_uri);110} catch (Exception $ex) {111return $this->newDialog()112->setTitle(pht('Unable to Generate Keys'))113->appendParagraph($ex->getMessage())114->addCancelButton($cancel_uri);115}116}117118}119120121