1
|
/**
|
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
|
function download(filename, text) {
|
7
|
// 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
|
|
12
|
element.style.display = 'none';
|
13
|
document.body.appendChild(element);
|
14
|
|
15
|
// generate artificial click event to download the file
|
16
|
element.click();
|
17
|
|
18
|
// remove the download link from the webpage
|
19
|
document.body.removeChild(element);
|
20
|
}
|
21
|
|
22
|
// Adds toDateInputValue() method to Date object
|
23
|
// toDateInputValue() produces date in YYYY-MM-DD format
|
24
|
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
|
});
|
29
|
|
30
|
/**
|
31
|
* Download certificate given by id
|
32
|
* @param id identifier of certificate to be downloaded
|
33
|
*/
|
34
|
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
|
}
|
45
|
|
46
|
/**
|
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
|
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
|
}
|
61
|
|
62
|
/**
|
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
|
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
|
}
|
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
|
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
|
}
|
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
|
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
|
}
|