Page MenuHomePhabricator

D8721.diff
No OneTemporary

D8721.diff

diff --git a/src/applications/settings/panel/PhabricatorSettingsPanelSSHKeys.php b/src/applications/settings/panel/PhabricatorSettingsPanelSSHKeys.php
--- a/src/applications/settings/panel/PhabricatorSettingsPanelSSHKeys.php
+++ b/src/applications/settings/panel/PhabricatorSettingsPanelSSHKeys.php
@@ -70,48 +70,19 @@
$errors[] = pht('You must provide an SSH Public Key.');
$e_key = pht('Required');
} 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.
- $e_key = pht('Invalid');
- $errors[] = pht('Provide your public key, not your private key!');
- } else {
- $e_key = pht('Invalid');
- $errors[] = pht('Provided public key is not properly formatted.');
- }
- }
- if (!$errors) {
- list($type, $body, $comment) = $parts;
-
- $recognized_keys = array(
- 'ssh-dsa',
- 'ssh-dss',
- 'ssh-rsa',
- 'ecdsa-sha2-nistp256',
- 'ecdsa-sha2-nistp384',
- 'ecdsa-sha2-nistp521',
- );
-
- if (!in_array($type, $recognized_keys)) {
- $e_key = pht('Invalid');
- $type_list = implode(', ', $recognized_keys);
- $errors[] = pht('Public key should be one of: %s', $type_list);
- } else {
- $key->setKeyType($type);
- $key->setKeyBody($body);
- $key->setKeyHash(md5($body));
- $key->setKeyComment($comment);
-
- $e_key = null;
- }
+ try {
+ list($type, $body, $comment) = self::parsePublicKey($entire_key);
+
+ $key->setKeyType($type);
+ $key->setKeyBody($body);
+ $key->setKeyHash(md5($body));
+ $key->setKeyComment($comment);
+
+ $e_key = null;
+ } catch (Exception $ex) {
+ $e_key = pht('Invalid');
+ $errors[] = $ex->getMessage();
}
}
@@ -314,13 +285,15 @@
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
));
+ list($type, $body, $comment) = self::parsePublicKey($public_key);
+
$key = id(new PhabricatorUserSSHKey())
->setUserPHID($user->getPHID())
->setName('id_rsa_phabricator')
- ->setKeyType('rsa')
- ->setKeyBody($public_key)
- ->setKeyHash(md5($public_key))
- ->setKeyComment(pht('Generated Key'))
+ ->setKeyType($type)
+ ->setKeyBody($body)
+ ->setKeyHash(md5($body))
+ ->setKeyComment(pht('Generated'))
->save();
// NOTE: We're disabling workflow on submit so the download works. We're
@@ -395,4 +368,46 @@
->setDialog($dialog);
}
+ private static function parsePublicKey($entire_key) {
+ $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.
+ throw new Exception(
+ pht('Provide your public key, not your private key!'));
+ } else {
+ throw new Exception(
+ pht('Provided public key is not properly formatted.'));
+ }
+ }
+
+ list($type, $body, $comment) = $parts;
+
+ $recognized_keys = array(
+ 'ssh-dsa',
+ 'ssh-dss',
+ 'ssh-rsa',
+ 'ecdsa-sha2-nistp256',
+ 'ecdsa-sha2-nistp384',
+ 'ecdsa-sha2-nistp521',
+ );
+
+ if (!in_array($type, $recognized_keys)) {
+ $type_list = implode(', ', $recognized_keys);
+ throw new Exception(
+ pht(
+ 'Public key type should be one of: %s',
+ $type_list));
+ }
+
+ return array($type, $body, $comment);
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Thu, Jul 24, 6:39 PM (6 h, 42 m ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
8597347
Default Alt Text
D8721.diff (4 KB)

Event Timeline