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 |
4a772c43
|
Jan Pašek
|
Date.prototype.toDateInputValue = (function () {
|
25 |
9cac7fd4
|
Jan Pašek
|
var local = new Date(this);
|
26 |
|
|
local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
|
27 |
4a772c43
|
Jan Pašek
|
return local.toJSON().slice(0, 10);
|
28 |
7a423499
|
Jan Pašek
|
});
|
29 |
|
|
|
30 |
780c6d9c
|
Jan Pašek
|
/**
|
31 |
|
|
* Download certificate given by id
|
32 |
|
|
* @param id identifier of certificate to be downloaded
|
33 |
|
|
*/
|
34 |
4a772c43
|
Jan Pašek
|
async function onCertificateDownload(id) {
|
35 |
|
|
try {
|
36 |
|
|
const response = await axios.get(API_URL + "certificates/" + id);
|
37 |
|
|
if (response.data["success"]) {
|
38 |
|
|
download(id + ".pem", response.data["data"])
|
39 |
|
|
} else
|
40 |
|
|
console.log("Error occurred while downloading the certificate") // TODO more action may be required
|
41 |
|
|
} catch (error) {
|
42 |
|
|
console.log(error);
|
43 |
|
|
}
|
44 |
7a423499
|
Jan Pašek
|
}
|
45 |
|
|
|
46 |
780c6d9c
|
Jan Pašek
|
/**
|
47 |
|
|
* Download root of chain of trust excluding root of given certificate
|
48 |
|
|
* @param id identifier of certificate whose chain shall be downloaded
|
49 |
|
|
*/
|
50 |
4a772c43
|
Jan Pašek
|
async function onCertificateChainDownload(id) {
|
51 |
|
|
try {
|
52 |
|
|
const response = await axios.get(API_URL + "certificates/" + id + "/chain");
|
53 |
|
|
if (response.data["success"]) {
|
54 |
|
|
download(id + "_chain.pem", response.data["data"])
|
55 |
|
|
} else
|
56 |
|
|
console.log("Error occurred while downloading the certificate's chain of trust") // TODO more action may be required
|
57 |
|
|
} catch (error) {
|
58 |
|
|
console.log(error);
|
59 |
|
|
}
|
60 |
7a423499
|
Jan Pašek
|
}
|
61 |
|
|
|
62 |
780c6d9c
|
Jan Pašek
|
/**
|
63 |
|
|
* Download root of chain of trust corresponding to the given certificate
|
64 |
|
|
* @param id identifier of certificate whose root shall be downloaded
|
65 |
|
|
*/
|
66 |
4a772c43
|
Jan Pašek
|
async function onCertificateRootDownload(id) {
|
67 |
|
|
try {
|
68 |
|
|
const response = await axios.get(API_URL + "certificates/" + id + "/root");
|
69 |
|
|
if (response.data["success"]) {
|
70 |
|
|
download(id + "_root.pem", response.data["data"])
|
71 |
|
|
} else
|
72 |
|
|
console.log("Error occurred while downloading the certificate's root CA") // TODO more action may be required
|
73 |
|
|
} catch (error) {
|
74 |
|
|
console.log(error);
|
75 |
|
|
}
|
76 |
780c6d9c
|
Jan Pašek
|
}
|
77 |
|
|
|
78 |
|
|
/**
|
79 |
|
|
* Download public key associated with the selected certificate
|
80 |
|
|
* @param id identifier of certificate whose pub. key shall be downloaded
|
81 |
|
|
*/
|
82 |
4a772c43
|
Jan Pašek
|
async function onPublicKeyDownload(id) {
|
83 |
|
|
try {
|
84 |
|
|
const response = await axios.get(API_URL + "certificates/" + id + "/publickey");
|
85 |
|
|
if (response.data["success"]) {
|
86 |
|
|
download(id + "_public_key.pem", response.data["data"])
|
87 |
|
|
} else
|
88 |
|
|
console.log("Error occurred while downloading the certificate's public key") // TODO more action may be required
|
89 |
|
|
} catch (error) {
|
90 |
|
|
console.log(error);
|
91 |
|
|
}
|
92 |
780c6d9c
|
Jan Pašek
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* Download public key associated with the selected certificate
|
96 |
|
|
* @param id identifier of certificate whose pub. key shall be downloaded
|
97 |
|
|
*/
|
98 |
4a772c43
|
Jan Pašek
|
async function onPrivateKeyDownload(id) {
|
99 |
|
|
try {
|
100 |
|
|
const response = await axios.get(API_URL + "certificates/" + id + "/privatekey");
|
101 |
|
|
if (response.data["success"]) {
|
102 |
|
|
download(id + "_private_key.pem", response.data["data"])
|
103 |
|
|
} else
|
104 |
|
|
console.log("Error occurred while downloading the certificate's private key") // TODO more action may be required
|
105 |
|
|
} catch (error) {
|
106 |
|
|
console.log(error);
|
107 |
|
|
}
|
108 |
7a423499
|
Jan Pašek
|
}
|