From 48e5dc6763b664fb307d2894cedab0a9aaf09630 Mon Sep 17 00:00:00 2001 From: Leonidas Spyropoulos Date: Thu, 27 Oct 2022 15:49:48 +0100 Subject: [PATCH] feat: remove empty lines from ssh_keys text area, and show helpful message Signed-off-by: Leonidas Spyropoulos --- aurweb/util.py | 2 +- po/aurweb.pot | 4 ++++ templates/partials/account_form.html | 7 +++++++ test/test_util.py | 29 +++++++++++++++++++++++++--- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/aurweb/util.py b/aurweb/util.py index 0a39cd3d..7b997609 100644 --- a/aurweb/util.py +++ b/aurweb/util.py @@ -192,7 +192,7 @@ def parse_ssh_key(string: str) -> Tuple[str, str]: def parse_ssh_keys(string: str) -> list[Tuple[str, str]]: """Parse a list of SSH public keys.""" - return [parse_ssh_key(e) for e in string.splitlines()] + return [parse_ssh_key(e) for e in string.strip().splitlines(True) if e.strip()] def shell_exec(cmdline: str, cwd: str) -> Tuple[int, str, str]: diff --git a/po/aurweb.pot b/po/aurweb.pot index 1838fae5..ff1bde8b 100644 --- a/po/aurweb.pot +++ b/po/aurweb.pot @@ -1398,6 +1398,10 @@ msgid "" "the Arch User Repository." msgstr "" +#: templates/partials/account_form.html +msgid "Specify multiple SSH Keys separated by new line, empty lines are ignored." +msgstr "" + #: template/account_edit_form.php msgid "SSH Public Key" msgstr "" diff --git a/templates/partials/account_form.html b/templates/partials/account_form.html index 007fb389..a433a57d 100644 --- a/templates/partials/account_form.html +++ b/templates/partials/account_form.html @@ -264,6 +264,13 @@

+

+ + {{ + "Specify multiple SSH Keys separated by new line, empty lines are ignored." | tr + }} + +

diff --git a/test/test_util.py b/test/test_util.py index 2e8b2e4e..fd7d8655 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -96,14 +96,37 @@ YbxDwGimZZslg0OZu9UzoAT6xEGyiZsqJkTMbRp1ZYIOv9jHCJxRuxxuN3fzxyT3xE69+vhq2/NJX\ vTNJCD6JtMClxbIXW9q74nNqG+2SD/VQNMUz/505TK1PbY/4uyFfq5HquHJXQVCBll03FRerNHH2N\ schFne6BFHpa48PCoZNH45wLjFXwUyrGU1HrNqh6ZPdRfBTrTOkgs+BKBxGNeV45aYUPu/cFBSPcB\ fRSo6OFcejKc=""" + assert_multiple_keys(pks) + + +def test_parse_ssh_keys_with_extra_lines(): + pks = """ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyN\ +TYAAABBBEURnkiY6JoLyqDE8Li1XuAW+LHmkmLDMW/GL5wY7k4/A+Ta7bjA3MOKrF9j4EuUTvCuNX\ +ULxvpfSqheTFWZc+g= + + + + +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDmqEapFMh/ajPHnm1dBweYPeLOUjC0Ydp6uw7rB\ +S5KCggUVQR8WfIm+sRYTj2+smGsK6zHMBjFnbzvV11vnMqcnY+Sa4LhIAdwkbt/b8HlGaLj1hCWSh\ +a5b5/noeK7L+CECGHdvfJhpxBbhq38YEdFnCGbslk/4NriNcUp/DO81CXb1RzJ9GBFH8ivPW1mbe9\ +YbxDwGimZZslg0OZu9UzoAT6xEGyiZsqJkTMbRp1ZYIOv9jHCJxRuxxuN3fzxyT3xE69+vhq2/NJX\ +8aRsxGPL9G/XKcaYGS6y6LW4quIBCz/XsTZfx1GmkQeZPYHH8FeE+XC/+toXL/kamxdOQKFYEEpWK\ +vTNJCD6JtMClxbIXW9q74nNqG+2SD/VQNMUz/505TK1PbY/4uyFfq5HquHJXQVCBll03FRerNHH2N\ +schFne6BFHpa48PCoZNH45wLjFXwUyrGU1HrNqh6ZPdRfBTrTOkgs+BKBxGNeV45aYUPu/cFBSPcB\ +fRSo6OFcejKc= + + +""" + assert_multiple_keys(pks) + + +def assert_multiple_keys(pks): keys = util.parse_ssh_keys(pks) assert len(keys) == 2 - pfx1, key1, pfx2, key2 = pks.split() k1, k2 = keys - assert pfx1 == k1[0] assert key1 == k1[1] - assert pfx2 == k2[0] assert key2 == k2[1]