From 55b67ca4d45e2309a4456fc052bc1f61f2ad7d92 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 17 Oct 2018 16:11:02 +0100 Subject: [PATCH] people: Allow downloading SSH keys Signed-off-by: Michael Tremer --- src/backend/accounts.py | 8 ++++++++ src/web/__init__.py | 1 + src/web/people.py | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/backend/accounts.py b/src/backend/accounts.py index 9f29da82..361f29b7 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -590,6 +590,14 @@ class Account(Object): return ret + def get_ssh_key_by_hash_md5(self, hash_md5): + for key in self.ssh_keys: + if not key.hash_md5() == hash_md5: + continue + + return key + + if __name__ == "__main__": a = Accounts() diff --git a/src/web/__init__.py b/src/web/__init__.py index a25a1a00..f0099c19 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -261,6 +261,7 @@ class Application(tornado.web.Application): (r"/users/(\w+)/edit", people.UserEditHandler), (r"/users/(\w+)/passwd", people.UserPasswdHandler), (r"/users/(\w+)/ssh-keys", people.SSHKeysIndexHandler), + (r"/users/(\w+)/ssh-keys/(MD5\:.*)", people.SSHKeysDownloadHandler), (r"/users/(\w+)/sip", people.SIPHandler), ] + authentication_handlers) diff --git a/src/web/people.py b/src/web/people.py index 5cad8e2a..d714cae3 100644 --- a/src/web/people.py +++ b/src/web/people.py @@ -113,6 +113,24 @@ class SSHKeysIndexHandler(base.BaseHandler): self.render("people/ssh-keys/index.html", account=account) +class SSHKeysDownloadHandler(base.BaseHandler): + @tornado.web.authenticated + def get(self, uid, hash_md5): + account = self.backend.accounts.get_by_uid(uid) + if not account: + raise tornado.web.HTTPError(404, "Could not find account %s" % uid) + + # Get SSH key + key = account.get_ssh_key_by_hash_md5(hash_md5) + if not key: + raise tornado.web.HTTPError(404, "Could not find key: %s" % hash_md5) + + # Set HTTP Headers + self.add_header("Content-Type", "text/plain") + + self.finish(key.keydata) + + class SIPHandler(base.BaseHandler): @tornado.web.authenticated def get(self, uid): -- 2.47.3