1 |
6c9cb54a
|
Jan Pašek
|
/**
|
2 |
|
|
* Generate a file to be downloaded by the browser
|
3 |
|
|
* @param filename name of the file to be downloaded
|
4 |
|
|
* @param text content of the downloaded file
|
5 |
|
|
*/
|
6 |
e77db3b6
|
Jan Pašek
|
function download(filename, text) {
|
7 |
6c9cb54a
|
Jan Pašek
|
// first a hidden <a> element is created and download content is assigned
|
8 |
|
|
var element = document.createElement('a');
|
9 |
|
|
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
|
10 |
|
|
element.setAttribute('download', filename);
|
11 |
e77db3b6
|
Jan Pašek
|
|
12 |
6c9cb54a
|
Jan Pašek
|
element.style.display = 'none';
|
13 |
|
|
document.body.appendChild(element);
|
14 |
e77db3b6
|
Jan Pašek
|
|
15 |
6c9cb54a
|
Jan Pašek
|
// generate artificial click event to download the file
|
16 |
|
|
element.click();
|
17 |
e77db3b6
|
Jan Pašek
|
|
18 |
6c9cb54a
|
Jan Pašek
|
// remove the download link from the webpage
|
19 |
|
|
document.body.removeChild(element);
|
20 |
9cac7fd4
|
Jan Pašek
|
}
|
21 |
|
|
|
22 |
6c9cb54a
|
Jan Pašek
|
// Adds toDateInputValue() method to Date object
|
23 |
|
|
// toDateInputValue() produces date in YYYY-MM-DD format
|
24 |
9cac7fd4
|
Jan Pašek
|
Date.prototype.toDateInputValue = (function() {
|
25 |
|
|
var local = new Date(this);
|
26 |
|
|
local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
|
27 |
|
|
return local.toJSON().slice(0,10);
|
28 |
7a423499
|
Jan Pašek
|
});
|
29 |
|
|
|
30 |
|
|
// Get the certificate PEM data from the server and downloads it to users computer
|
31 |
|
|
function onCertificateDownload(id) {
|
32 |
|
|
axios.get(API_URL + "certificates/" + id)
|
33 |
|
|
.then(function (response) {
|
34 |
|
|
if (response.data["success"]) {
|
35 |
|
|
download(id + ".pem", response.data["data"])
|
36 |
|
|
} else
|
37 |
|
|
console.log("Error occurred while downloading the certificate") // TODO more action may be required
|
38 |
|
|
})
|
39 |
|
|
.catch(function (error) {
|
40 |
|
|
console.log(error);
|
41 |
|
|
});
|
42 |
|
|
}
|
43 |
|
|
|
44 |
|
|
// Get the certificate's chain of trust in PEM format from the server and downloads it to users computer
|
45 |
|
|
function onCertificateChainDownload(id) {
|
46 |
|
|
axios.get(API_URL + "certificates/" + id + "/chain")
|
47 |
|
|
.then(function (response) {
|
48 |
|
|
if(response.data["success"]) {
|
49 |
|
|
download(id + "_chain.pem", response.data["data"])
|
50 |
|
|
}
|
51 |
|
|
else
|
52 |
|
|
console.log("Error occurred while downloading the certificate's chain of trust") // TODO more action may be required
|
53 |
|
|
})
|
54 |
|
|
.catch(function (error) {
|
55 |
|
|
console.log(error);
|
56 |
|
|
});
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
// Get the certificate's root CA as PEM data from the server and downloads it to users computer
|
60 |
|
|
function onCertificateRootDownload(id) {
|
61 |
|
|
axios.get(API_URL + "certificates/" + id + "/root")
|
62 |
|
|
.then(function (response) {
|
63 |
|
|
if(response.data["success"]) {
|
64 |
|
|
download(id + "_root.pem", response.data["data"])
|
65 |
|
|
}
|
66 |
|
|
else
|
67 |
|
|
console.log("Error occurred while downloading the certificate's root CA") // TODO more action may be required
|
68 |
|
|
})
|
69 |
|
|
.catch(function (error) {
|
70 |
|
|
console.log(error);
|
71 |
|
|
});
|
72 |
|
|
}
|