2011-07-22 19:17:57 +02:00
|
|
|
<?php
|
|
|
|
|
2012-08-13 21:37:26 +02:00
|
|
|
final class PhabricatorSettingsPanelSSHKeys
|
|
|
|
extends PhabricatorSettingsPanel {
|
2011-07-22 19:17:57 +02:00
|
|
|
|
2014-04-02 21:06:05 +02:00
|
|
|
public function isEditableByAdministrators() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-13 21:37:26 +02:00
|
|
|
public function getPanelKey() {
|
|
|
|
return 'ssh';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPanelName() {
|
|
|
|
return pht('SSH Public Keys');
|
|
|
|
}
|
2011-07-22 19:17:57 +02:00
|
|
|
|
2012-08-13 21:37:26 +02:00
|
|
|
public function getPanelGroup() {
|
|
|
|
return pht('Authentication');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isEnabled() {
|
2013-10-26 23:32:47 +02:00
|
|
|
return true;
|
2011-07-24 20:02:08 +02:00
|
|
|
}
|
|
|
|
|
2012-08-13 21:37:26 +02:00
|
|
|
public function processRequest(AphrontRequest $request) {
|
2014-04-02 21:06:05 +02:00
|
|
|
$viewer = $request->getUser();
|
|
|
|
$user = $this->getUser();
|
2011-07-22 19:17:57 +02:00
|
|
|
|
2014-03-13 02:17:11 +01:00
|
|
|
$generate = $request->getStr('generate');
|
|
|
|
if ($generate) {
|
|
|
|
return $this->processGenerate($request);
|
|
|
|
}
|
|
|
|
|
2011-07-22 19:17:57 +02:00
|
|
|
$edit = $request->getStr('edit');
|
|
|
|
$delete = $request->getStr('delete');
|
|
|
|
if (!$edit && !$delete) {
|
2012-08-13 21:37:26 +02:00
|
|
|
return $this->renderKeyListView($request);
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$id = nonempty($edit, $delete);
|
|
|
|
|
|
|
|
if ($id && is_numeric($id)) {
|
2014-04-02 21:06:05 +02:00
|
|
|
// NOTE: This prevents editing/deleting of keys not owned by the user.
|
2011-07-22 19:17:57 +02:00
|
|
|
$key = id(new PhabricatorUserSSHKey())->loadOneWhere(
|
|
|
|
'userPHID = %s AND id = %d',
|
|
|
|
$user->getPHID(),
|
2012-08-13 21:37:26 +02:00
|
|
|
(int)$id);
|
2011-07-22 19:17:57 +02:00
|
|
|
if (!$key) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$key = new PhabricatorUserSSHKey();
|
|
|
|
$key->setUserPHID($user->getPHID());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($delete) {
|
2012-08-13 21:37:26 +02:00
|
|
|
return $this->processDelete($request, $key);
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$e_name = true;
|
|
|
|
$e_key = true;
|
|
|
|
$errors = array();
|
|
|
|
$entire_key = $key->getEntireKey();
|
|
|
|
if ($request->isFormPost()) {
|
|
|
|
$key->setName($request->getStr('name'));
|
|
|
|
$entire_key = $request->getStr('key');
|
|
|
|
|
|
|
|
if (!strlen($entire_key)) {
|
2013-03-03 15:52:42 +01:00
|
|
|
$errors[] = pht('You must provide an SSH Public Key.');
|
|
|
|
$e_key = pht('Required');
|
2011-07-22 19:17:57 +02:00
|
|
|
} else {
|
|
|
|
$parts = str_replace("\n", '', trim($entire_key));
|
|
|
|
$parts = preg_split('/\s+/', $parts);
|
|
|
|
if (count($parts) == 2) {
|
|
|
|
$parts[] = ''; // Add an empty comment part.
|
|
|
|
} else if (count($parts) == 3) {
|
|
|
|
// This is the expected case.
|
|
|
|
} else {
|
|
|
|
if (preg_match('/private\s*key/i', $entire_key)) {
|
|
|
|
// Try to give the user a better error message if it looks like
|
|
|
|
// they uploaded a private key.
|
2013-03-03 15:52:42 +01:00
|
|
|
$e_key = pht('Invalid');
|
|
|
|
$errors[] = pht('Provide your public key, not your private key!');
|
2011-07-22 19:17:57 +02:00
|
|
|
} else {
|
2013-03-03 15:52:42 +01:00
|
|
|
$e_key = pht('Invalid');
|
|
|
|
$errors[] = pht('Provided public key is not properly formatted.');
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
list($type, $body, $comment) = $parts;
|
2013-11-07 20:33:46 +01:00
|
|
|
|
|
|
|
$recognized_keys = array(
|
|
|
|
'ssh-dsa',
|
2013-12-05 01:53:16 +01:00
|
|
|
'ssh-dss',
|
2013-11-07 20:33:46 +01:00
|
|
|
'ssh-rsa',
|
|
|
|
'ecdsa-sha2-nistp256',
|
2013-12-05 01:53:16 +01:00
|
|
|
'ecdsa-sha2-nistp384',
|
|
|
|
'ecdsa-sha2-nistp521',
|
2013-11-07 20:33:46 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!in_array($type, $recognized_keys)) {
|
2013-03-03 15:52:42 +01:00
|
|
|
$e_key = pht('Invalid');
|
2013-11-07 20:33:46 +01:00
|
|
|
$type_list = implode(', ', $recognized_keys);
|
|
|
|
$errors[] = pht('Public key should be one of: %s', $type_list);
|
2011-07-22 19:17:57 +02:00
|
|
|
} else {
|
|
|
|
$key->setKeyType($type);
|
|
|
|
$key->setKeyBody($body);
|
2012-03-10 03:06:39 +01:00
|
|
|
$key->setKeyHash(md5($body));
|
2011-07-22 19:17:57 +02:00
|
|
|
$key->setKeyComment($comment);
|
|
|
|
|
|
|
|
$e_key = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strlen($key->getName())) {
|
2013-03-03 15:52:42 +01:00
|
|
|
$errors[] = pht('You must name this public key.');
|
|
|
|
$e_name = pht('Required');
|
2011-07-22 19:17:57 +02:00
|
|
|
} else {
|
|
|
|
$e_name = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
try {
|
|
|
|
$key->save();
|
|
|
|
return id(new AphrontRedirectResponse())
|
2012-08-13 21:37:26 +02:00
|
|
|
->setURI($this->getPanelURI());
|
2011-07-22 19:17:57 +02:00
|
|
|
} catch (AphrontQueryDuplicateKeyException $ex) {
|
2013-03-03 15:52:42 +01:00
|
|
|
$e_key = pht('Duplicate');
|
|
|
|
$errors[] = pht('This public key is already associated with a user '.
|
|
|
|
'account.');
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_new = !$key->getID();
|
|
|
|
|
|
|
|
if ($is_new) {
|
2013-03-03 15:52:42 +01:00
|
|
|
$header = pht('Add New SSH Public Key');
|
|
|
|
$save = pht('Add Key');
|
2011-07-22 19:17:57 +02:00
|
|
|
} else {
|
2013-03-03 15:52:42 +01:00
|
|
|
$header = pht('Edit SSH Public Key');
|
|
|
|
$save = pht('Save Changes');
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$form = id(new AphrontFormView())
|
2014-04-02 21:06:05 +02:00
|
|
|
->setUser($viewer)
|
2011-07-22 19:17:57 +02:00
|
|
|
->addHiddenInput('edit', $is_new ? 'true' : $key->getID())
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-03-03 15:52:42 +01:00
|
|
|
->setLabel(pht('Name'))
|
2011-07-22 19:17:57 +02:00
|
|
|
->setName('name')
|
|
|
|
->setValue($key->getName())
|
|
|
|
->setError($e_name))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextAreaControl())
|
2013-03-03 15:52:42 +01:00
|
|
|
->setLabel(pht('Public Key'))
|
2011-07-22 19:17:57 +02:00
|
|
|
->setName('key')
|
|
|
|
->setValue($entire_key)
|
|
|
|
->setError($e_key))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2012-08-13 21:37:26 +02:00
|
|
|
->addCancelButton($this->getPanelURI())
|
2011-07-22 19:17:57 +02:00
|
|
|
->setValue($save));
|
|
|
|
|
2013-11-25 04:13:57 +01:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
|
|
|
->setHeaderText($header)
|
2014-01-10 18:17:37 +01:00
|
|
|
->setFormErrors($errors)
|
2013-11-25 04:13:57 +01:00
|
|
|
->setForm($form);
|
2011-07-22 19:17:57 +02:00
|
|
|
|
2013-11-25 04:13:57 +01:00
|
|
|
return $form_box;
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|
|
|
|
|
2012-08-13 21:37:26 +02:00
|
|
|
private function renderKeyListView(AphrontRequest $request) {
|
2014-04-02 21:06:05 +02:00
|
|
|
$user = $this->getUser();
|
|
|
|
$viewer = $request->getUser();
|
2011-07-22 19:17:57 +02:00
|
|
|
|
|
|
|
$keys = id(new PhabricatorUserSSHKey())->loadAllWhere(
|
|
|
|
'userPHID = %s',
|
|
|
|
$user->getPHID());
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
$rows[] = array(
|
2013-01-18 03:43:35 +01:00
|
|
|
phutil_tag(
|
2011-07-22 19:17:57 +02:00
|
|
|
'a',
|
|
|
|
array(
|
2012-08-13 21:37:26 +02:00
|
|
|
'href' => $this->getPanelURI('?edit='.$key->getID()),
|
2011-07-22 19:17:57 +02:00
|
|
|
),
|
2013-01-18 03:43:35 +01:00
|
|
|
$key->getName()),
|
2013-02-13 23:50:15 +01:00
|
|
|
$key->getKeyComment(),
|
|
|
|
$key->getKeyType(),
|
2014-04-02 21:06:05 +02:00
|
|
|
phabricator_date($key->getDateCreated(), $viewer),
|
|
|
|
phabricator_time($key->getDateCreated(), $viewer),
|
2013-01-25 21:57:17 +01:00
|
|
|
javelin_tag(
|
2011-07-22 19:17:57 +02:00
|
|
|
'a',
|
|
|
|
array(
|
2012-08-13 21:37:26 +02:00
|
|
|
'href' => $this->getPanelURI('?delete='.$key->getID()),
|
2011-07-22 19:17:57 +02:00
|
|
|
'class' => 'small grey button',
|
|
|
|
'sigil' => 'workflow',
|
|
|
|
),
|
2013-03-03 15:52:42 +01:00
|
|
|
pht('Delete')),
|
2011-07-22 19:17:57 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$table = new AphrontTableView($rows);
|
2013-03-03 15:52:42 +01:00
|
|
|
$table->setNoDataString(pht("You haven't added any SSH Public Keys."));
|
2011-07-22 19:17:57 +02:00
|
|
|
$table->setHeaders(
|
|
|
|
array(
|
2013-03-03 15:52:42 +01:00
|
|
|
pht('Name'),
|
|
|
|
pht('Comment'),
|
|
|
|
pht('Type'),
|
|
|
|
pht('Created'),
|
|
|
|
pht('Time'),
|
2011-07-22 19:17:57 +02:00
|
|
|
'',
|
|
|
|
));
|
|
|
|
$table->setColumnClasses(
|
|
|
|
array(
|
|
|
|
'wide pri',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
'right',
|
|
|
|
'action',
|
|
|
|
));
|
|
|
|
|
2014-01-08 01:16:30 +01:00
|
|
|
$panel = new PHUIObjectBoxView();
|
|
|
|
$header = new PHUIHeaderView();
|
|
|
|
|
2014-03-13 02:17:11 +01:00
|
|
|
$upload_icon = id(new PHUIIconView())
|
|
|
|
->setSpriteSheet(PHUIIconView::SPRITE_ICONS)
|
|
|
|
->setSpriteIcon('upload');
|
|
|
|
$upload_button = id(new PHUIButtonView())
|
|
|
|
->setText(pht('Upload Public Key'))
|
|
|
|
->setHref($this->getPanelURI('?edit=true'))
|
|
|
|
->setTag('a')
|
|
|
|
->setIcon($upload_icon);
|
|
|
|
|
|
|
|
try {
|
|
|
|
PhabricatorSSHKeyGenerator::assertCanGenerateKeypair();
|
|
|
|
$can_generate = true;
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$can_generate = false;
|
|
|
|
}
|
2014-01-08 01:16:30 +01:00
|
|
|
|
2014-03-13 02:17:11 +01:00
|
|
|
$generate_icon = id(new PHUIIconView())
|
|
|
|
->setSpriteSheet(PHUIIconView::SPRITE_ICONS)
|
|
|
|
->setSpriteIcon('lock');
|
|
|
|
$generate_button = id(new PHUIButtonView())
|
|
|
|
->setText(pht('Generate Keypair'))
|
|
|
|
->setHref($this->getPanelURI('?generate=true'))
|
|
|
|
->setTag('a')
|
|
|
|
->setWorkflow(true)
|
|
|
|
->setDisabled(!$can_generate)
|
|
|
|
->setIcon($generate_icon);
|
2014-01-08 01:16:30 +01:00
|
|
|
|
|
|
|
$header->setHeader(pht('SSH Public Keys'));
|
2014-03-13 02:17:11 +01:00
|
|
|
$header->addActionLink($generate_button);
|
|
|
|
$header->addActionLink($upload_button);
|
2014-01-08 01:16:30 +01:00
|
|
|
|
|
|
|
$panel->setHeader($header);
|
2011-07-22 19:17:57 +02:00
|
|
|
$panel->appendChild($table);
|
|
|
|
|
|
|
|
return $panel;
|
|
|
|
}
|
|
|
|
|
2012-08-13 21:37:26 +02:00
|
|
|
private function processDelete(
|
|
|
|
AphrontRequest $request,
|
|
|
|
PhabricatorUserSSHKey $key) {
|
|
|
|
|
2014-04-02 21:06:05 +02:00
|
|
|
$viewer = $request->getUser();
|
|
|
|
$user = $this->getUser();
|
2011-07-22 19:17:57 +02:00
|
|
|
|
2013-02-13 23:50:15 +01:00
|
|
|
$name = phutil_tag('strong', array(), $key->getName());
|
2011-07-22 19:17:57 +02:00
|
|
|
|
|
|
|
if ($request->isDialogFormPost()) {
|
|
|
|
$key->delete();
|
|
|
|
return id(new AphrontReloadResponse())
|
2012-08-13 21:37:26 +02:00
|
|
|
->setURI($this->getPanelURI());
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$dialog = id(new AphrontDialogView())
|
2014-04-02 21:06:05 +02:00
|
|
|
->setUser($viewer)
|
2011-07-22 19:17:57 +02:00
|
|
|
->addHiddenInput('delete', $key->getID())
|
2013-03-03 15:52:42 +01:00
|
|
|
->setTitle(pht('Really delete SSH Public Key?'))
|
2013-02-13 23:50:15 +01:00
|
|
|
->appendChild(phutil_tag('p', array(), pht(
|
|
|
|
'The key "%s" will be permanently deleted, and you will not longer be '.
|
|
|
|
'able to use the corresponding private key to authenticate.',
|
|
|
|
$name)))
|
2013-03-03 15:52:42 +01:00
|
|
|
->addSubmitButton(pht('Delete Public Key'))
|
2012-08-13 21:37:26 +02:00
|
|
|
->addCancelButton($this->getPanelURI());
|
2011-07-22 19:17:57 +02:00
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())
|
|
|
|
->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:06:05 +02:00
|
|
|
private function processGenerate(AphrontRequest $request) {
|
|
|
|
$user = $this->getUser();
|
2014-03-13 02:17:11 +01:00
|
|
|
$viewer = $request->getUser();
|
|
|
|
|
2014-04-02 21:06:05 +02:00
|
|
|
$is_self = ($user->getPHID() == $viewer->getPHID());
|
|
|
|
|
2014-03-13 02:17:11 +01:00
|
|
|
if ($request->isFormPost()) {
|
|
|
|
$keys = PhabricatorSSHKeyGenerator::generateKeypair();
|
|
|
|
list($public_key, $private_key) = $keys;
|
|
|
|
|
|
|
|
$file = PhabricatorFile::buildFromFileDataOrHash(
|
|
|
|
$private_key,
|
|
|
|
array(
|
|
|
|
'name' => 'id_rsa_phabricator.key',
|
|
|
|
'ttl' => time() + (60 * 10),
|
|
|
|
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
|
|
|
|
));
|
|
|
|
|
|
|
|
$key = id(new PhabricatorUserSSHKey())
|
2014-04-02 21:06:05 +02:00
|
|
|
->setUserPHID($user->getPHID())
|
2014-03-13 02:17:11 +01:00
|
|
|
->setName('id_rsa_phabricator')
|
|
|
|
->setKeyType('rsa')
|
|
|
|
->setKeyBody($public_key)
|
|
|
|
->setKeyHash(md5($public_key))
|
|
|
|
->setKeyComment(pht('Generated Key'))
|
|
|
|
->save();
|
|
|
|
|
|
|
|
// NOTE: We're disabling workflow on submit so the download works. We're
|
|
|
|
// disabling workflow on cancel so the page reloads, showing the new
|
|
|
|
// key.
|
|
|
|
|
2014-04-02 21:06:05 +02:00
|
|
|
if ($is_self) {
|
|
|
|
$what_happened = pht(
|
|
|
|
'The public key has been associated with your Phabricator '.
|
|
|
|
'account. Use the button below to download the private key.');
|
|
|
|
} else {
|
|
|
|
$what_happened = pht(
|
|
|
|
'The public key has been associated with the %s account. '.
|
|
|
|
'Use the button below to download the private key.',
|
|
|
|
phutil_tag('strong', array(), $user->getUsername()));
|
|
|
|
}
|
|
|
|
|
2014-03-13 02:17:11 +01:00
|
|
|
$dialog = id(new AphrontDialogView())
|
|
|
|
->setTitle(pht('Download Private Key'))
|
|
|
|
->setUser($viewer)
|
|
|
|
->setDisableWorkflowOnCancel(true)
|
|
|
|
->setDisableWorkflowOnSubmit(true)
|
|
|
|
->setSubmitURI($file->getDownloadURI())
|
|
|
|
->appendParagraph(
|
|
|
|
pht(
|
|
|
|
'Successfully generated a new keypair.'))
|
2014-04-02 21:06:05 +02:00
|
|
|
->appendParagraph($what_happened)
|
2014-03-13 02:17:11 +01:00
|
|
|
->appendParagraph(
|
|
|
|
pht(
|
|
|
|
'After you download the private key, it will be destroyed. '.
|
|
|
|
'You will not be able to retrieve it if you lose your copy.'))
|
|
|
|
->addSubmitButton(pht('Download Private Key'))
|
|
|
|
->addCancelButton($this->getPanelURI(), pht('Done'));
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())
|
|
|
|
->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
$dialog = id(new AphrontDialogView())
|
|
|
|
->setUser($viewer)
|
|
|
|
->addCancelButton($this->getPanelURI());
|
|
|
|
|
|
|
|
try {
|
|
|
|
PhabricatorSSHKeyGenerator::assertCanGenerateKeypair();
|
2014-04-02 21:06:05 +02:00
|
|
|
|
|
|
|
if ($is_self) {
|
|
|
|
$explain = pht(
|
|
|
|
'This will generate an SSH keypair, associate the public key '.
|
|
|
|
'with your account, and let you download the private key.');
|
|
|
|
} else {
|
|
|
|
$explain = pht(
|
|
|
|
'This will generate an SSH keypair, associate the public key with '.
|
|
|
|
'the %s account, and let you download the private key.',
|
|
|
|
phutil_tag('strong', array(), $user->getUsername()));
|
|
|
|
}
|
|
|
|
|
2014-03-13 02:17:11 +01:00
|
|
|
$dialog
|
|
|
|
->addHiddenInput('generate', true)
|
|
|
|
->setTitle(pht('Generate New Keypair'))
|
2014-04-02 21:06:05 +02:00
|
|
|
->appendParagraph($explain)
|
2014-03-13 02:17:11 +01:00
|
|
|
->appendParagraph(
|
|
|
|
pht(
|
|
|
|
"Phabricator will not retain a copy of the private key."))
|
|
|
|
->addSubmitButton(pht('Generate Keypair'));
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$dialog
|
|
|
|
->setTitle(pht('Unable to Generate Keys'))
|
|
|
|
->appendParagraph($ex->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())
|
|
|
|
->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
2011-07-22 19:17:57 +02:00
|
|
|
}
|