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
|
function onCertificateDownload(id) {
|
35
|
axios.get(API_URL + "certificates/" + id)
|
36
|
.then(function (response) {
|
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
|
})
|
42
|
.catch(function (error) {
|
43
|
console.log(error);
|
44
|
});
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Download root of chain of trust excluding root of given certificate
|
49
|
* @param id identifier of certificate whose chain shall be downloaded
|
50
|
*/
|
51
|
function onCertificateChainDownload(id) {
|
52
|
axios.get(API_URL + "certificates/" + id + "/chain")
|
53
|
.then(function (response) {
|
54
|
if(response.data["success"]) {
|
55
|
download(id + "_chain.pem", response.data["data"])
|
56
|
}
|
57
|
else
|
58
|
console.log("Error occurred while downloading the certificate's chain of trust") // TODO more action may be required
|
59
|
})
|
60
|
.catch(function (error) {
|
61
|
console.log(error);
|
62
|
});
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Download root of chain of trust corresponding to the given certificate
|
67
|
* @param id identifier of certificate whose root shall be downloaded
|
68
|
*/
|
69
|
function onCertificateRootDownload(id) {
|
70
|
axios.get(API_URL + "certificates/" + id + "/root")
|
71
|
.then(function (response) {
|
72
|
if(response.data["success"]) {
|
73
|
download(id + "_root.pem", response.data["data"])
|
74
|
}
|
75
|
else
|
76
|
console.log("Error occurred while downloading the certificate's root CA") // TODO more action may be required
|
77
|
})
|
78
|
.catch(function (error) {
|
79
|
console.log(error);
|
80
|
});
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Download public key associated with the selected certificate
|
85
|
* @param id identifier of certificate whose pub. key shall be downloaded
|
86
|
*/
|
87
|
function onPublicKeyDownload(id) {
|
88
|
axios.get(API_URL + "certificates/" + id + "/publickey")
|
89
|
.then(function (response) {
|
90
|
if (response.data["success"]) {
|
91
|
download(id + "_public_key.pem", response.data["data"])
|
92
|
} else
|
93
|
console.log("Error occurred while downloading the certificate's public key") // TODO more action may be required
|
94
|
})
|
95
|
.catch(function (error) {
|
96
|
console.log(error);
|
97
|
});
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Download public key associated with the selected certificate
|
102
|
* @param id identifier of certificate whose pub. key shall be downloaded
|
103
|
*/
|
104
|
function onPrivateKeyDownload(id) {
|
105
|
axios.get(API_URL + "certificates/" + id + "/privatekey")
|
106
|
.then(function (response) {
|
107
|
if (response.data["success"]) {
|
108
|
download(id + "_private_key.pem", response.data["data"])
|
109
|
} else
|
110
|
console.log("Error occurred while downloading the certificate's private key") // TODO more action may be required
|
111
|
})
|
112
|
.catch(function (error) {
|
113
|
console.log(error);
|
114
|
});
|
115
|
}
|