commit 780c6d9cbf82407a893a287677c08d945dba80c2
Author: Jan Pašek <janpasek97@gmail.com>
Date:   Fri Apr 16 15:47:53 2021 +0200

    Re #8583 - Downloading certificate keys

diff --git a/app.py b/app.py
index b5aeeb0..75f5ee5 100644
--- a/app.py
+++ b/app.py
@@ -51,11 +51,11 @@ def get_cert_root(id, certificate_controller: CertController):
 def get_cert_chain(id, certificate_controller: CertController):
     return certificate_controller.get_certificate_trust_chain_by_id(id)
 
-@app.route('/api/certificates/<id>/private_key', methods=["GET"])
+@app.route('/api/certificates/<id>/privatekey', methods=["GET"])
 def get_private_key_of_a_certificate(id, certificate_controller: CertController):
     return certificate_controller.get_private_key_of_a_certificate(id)
 
-@app.route('/api/certificates/<id>/public_key', methods=["GET"])
+@app.route('/api/certificates/<id>/publickey', methods=["GET"])
 def get_public_key_of_a_certificate(id, certificate_controller: CertController):
     return certificate_controller.get_public_key_of_a_certificate(id)
 
diff --git a/static/js/certificate.js b/static/js/certificate.js
index 171ceed..78ed2e6 100644
--- a/static/js/certificate.js
+++ b/static/js/certificate.js
@@ -2,6 +2,7 @@
     var certificateDetailsApp = new Vue({
         el: "#certificate-detailed-view-content",
         data: {
+            id: null,
             certificate: {
                 subject: {
                     C: "",
@@ -36,19 +37,19 @@
         },
         methods: {
             onCertificateDownload: function () {
-
+                onCertificateDownload(this.id);
             },
             onRootDownload: function () {
-
+                onCertificateRootDownload(this.id);
             },
             onChainDownload: function () {
-
+                onCertificateChainDownload(this.id);
             },
             onPublicKeyDownload: function () {
-
+                onPublicKeyDownload(this.id);
             },
             onPrivateKeyDownload: function () {
-
+                onPrivateKeyDownload(this.id);
             },
             onRevoke: function () {
 
@@ -65,6 +66,7 @@
         const urlParams = new URLSearchParams(params);
         if (urlParams.get("id") != null) {
             const id = urlParams.get("id");
+            certificateDetailsApp.id = id;
             axios.get(API_URL + "certificates/"+id+"/details")
                 .then(function (response) {
                     if (response.data["success"])
diff --git a/static/js/utilities.js b/static/js/utilities.js
index d33d602..d6576d8 100644
--- a/static/js/utilities.js
+++ b/static/js/utilities.js
@@ -27,7 +27,10 @@ Date.prototype.toDateInputValue = (function() {
     return local.toJSON().slice(0,10);
 });
 
-// Get the certificate PEM data from the server and downloads it to users computer
+/**
+ * Download certificate given by id
+ * @param id identifier of certificate to be downloaded
+ */
 function onCertificateDownload(id) {
     axios.get(API_URL + "certificates/" + id)
         .then(function (response) {
@@ -41,7 +44,10 @@ function onCertificateDownload(id) {
         });
 }
 
-// Get the certificate's chain of trust in PEM format from the server and downloads it to users computer
+/**
+ * Download root of chain of trust excluding root of given certificate
+ * @param id identifier of certificate whose chain shall be downloaded
+ */
 function onCertificateChainDownload(id) {
     axios.get(API_URL + "certificates/" + id + "/chain")
         .then(function (response) {
@@ -56,7 +62,10 @@ function onCertificateChainDownload(id) {
         });
 }
 
-// Get the certificate's root CA as PEM data from the server and downloads it to users computer
+/**
+ * Download root of chain of trust corresponding to the given certificate
+ * @param id identifier of certificate whose root shall be downloaded
+ */
 function onCertificateRootDownload(id) {
     axios.get(API_URL + "certificates/" + id + "/root")
         .then(function (response) {
@@ -69,4 +78,38 @@ function onCertificateRootDownload(id) {
         .catch(function (error) {
             console.log(error);
         });
+}
+
+/**
+ * Download public key associated with the selected certificate
+ * @param id identifier of certificate whose pub. key shall be downloaded
+ */
+function onPublicKeyDownload(id) {
+    axios.get(API_URL + "certificates/" + id + "/publickey")
+        .then(function (response) {
+            if (response.data["success"]) {
+                download(id + "_public_key.pem", response.data["data"])
+            } else
+                console.log("Error occurred while downloading the certificate's public key") // TODO more action may be required
+        })
+        .catch(function (error) {
+            console.log(error);
+        });
+}
+
+/**
+ * Download public key associated with the selected certificate
+ * @param id identifier of certificate whose pub. key shall be downloaded
+ */
+function onPrivateKeyDownload(id) {
+    axios.get(API_URL + "certificates/" + id + "/privatekey")
+        .then(function (response) {
+            if (response.data["success"]) {
+                download(id + "_private_key.pem", response.data["data"])
+            } else
+                console.log("Error occurred while downloading the certificate's private key") // TODO more action may be required
+        })
+        .catch(function (error) {
+            console.log(error);
+        });
 }
\ No newline at end of file
