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
|
/**
|
23
|
* Generate a file to be downloaded by the browser as a binary file
|
24
|
* @param filename name of the file to be downloaded
|
25
|
* @param data content of the downloaded file
|
26
|
*/
|
27
|
function downloadBinary(filename, data) {
|
28
|
var blob = new Blob([data], {type: "application/x-pkcs12"});
|
29
|
saveAs(blob, filename);
|
30
|
}
|
31
|
|
32
|
// Adds toDateInputValue() method to Date object
|
33
|
// toDateInputValue() produces date in YYYY-MM-DD format
|
34
|
Date.prototype.toDateInputValue = (function () {
|
35
|
var local = new Date(this);
|
36
|
local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
|
37
|
return local.toJSON().slice(0, 10);
|
38
|
});
|
39
|
|
40
|
/**
|
41
|
* Download certificate given by id
|
42
|
* @param id identifier of certificate to be downloaded
|
43
|
*/
|
44
|
async function onCertificateDownload(id) {
|
45
|
var success = false;
|
46
|
try {
|
47
|
const response = await axios.get(API_URL + "certificates/" + id);
|
48
|
if (response.data["success"]) {
|
49
|
success = true;
|
50
|
download(id + ".pem", response.data["data"])
|
51
|
} else {
|
52
|
console.error("Error occurred while downloading the certificate");
|
53
|
console.error(response.data["data"]);
|
54
|
}
|
55
|
} catch (error) {
|
56
|
console.log(error);
|
57
|
}
|
58
|
if (!success) throw new Error("Error occurred while obtaining the requested data!");
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Download root of chain of trust excluding root of given certificate
|
63
|
* @param id identifier of certificate whose chain shall be downloaded
|
64
|
*/
|
65
|
async function onCertificateChainDownload(id) {
|
66
|
var success = false;
|
67
|
try {
|
68
|
const response = await axios.get(API_URL + "certificates/" + id + "/chain");
|
69
|
if (response.data["success"]) {
|
70
|
success = true;
|
71
|
download(id + "_chain.pem", response.data["data"])
|
72
|
} else {
|
73
|
console.error("Error occurred while downloading the certificate's chain of trust");
|
74
|
console.error(response.data["data"]);
|
75
|
}
|
76
|
} catch (error) {
|
77
|
console.log(error);
|
78
|
}
|
79
|
if (!success) throw new Error("Error occurred while obtaining the requested data!");
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Download root of chain of trust corresponding to the given certificate
|
84
|
* @param id identifier of certificate whose root shall be downloaded
|
85
|
*/
|
86
|
async function onCertificateRootDownload(id) {
|
87
|
var success = false;
|
88
|
try {
|
89
|
const response = await axios.get(API_URL + "certificates/" + id + "/root");
|
90
|
if (response.data["success"]) {
|
91
|
success = true;
|
92
|
download(id + "_root.pem", response.data["data"])
|
93
|
} else {
|
94
|
console.error("Error occurred while downloading the certificate's root CA");
|
95
|
console.error(response.data["data"]);
|
96
|
}
|
97
|
} catch (error) {
|
98
|
console.log(error);
|
99
|
}
|
100
|
if (!success) throw new Error("Error occurred while obtaining the requested data!");
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Download public key associated with the selected certificate
|
105
|
* @param id identifier of certificate whose pub. key shall be downloaded
|
106
|
*/
|
107
|
async function onPublicKeyDownload(id) {
|
108
|
var success = false;
|
109
|
try {
|
110
|
const response = await axios.get(API_URL + "certificates/" + id + "/publickey");
|
111
|
if (response.data["success"]) {
|
112
|
success = true;
|
113
|
download(id + "_public_key.pem", response.data["data"])
|
114
|
} else {
|
115
|
console.error("Error occurred while downloading the certificate's public key")
|
116
|
console.error(response.data["data"]);
|
117
|
}
|
118
|
} catch (error) {
|
119
|
console.log(error);
|
120
|
}
|
121
|
if (!success) throw new Error("Error occurred while obtaining the requested data!");
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Download public key associated with the selected certificate
|
126
|
* @param id identifier of certificate whose pub. key shall be downloaded
|
127
|
*/
|
128
|
async function onPrivateKeyDownload(id) {
|
129
|
var success = false;
|
130
|
try {
|
131
|
const response = await axios.get(API_URL + "certificates/" + id + "/privatekey");
|
132
|
if (response.data["success"]) {
|
133
|
success = true;
|
134
|
download(id + "_private_key.pem", response.data["data"])
|
135
|
} else {
|
136
|
console.error("Error occurred while downloading the certificate's private key");
|
137
|
console.error(response.data["data"]);
|
138
|
}
|
139
|
} catch (error) {
|
140
|
console.log(error);
|
141
|
}
|
142
|
if (!success) throw new Error("Error occurred while obtaining the requested data!");
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Download identity given by certificate id, name and password
|
147
|
* @param id identifier of certificate to be used for creating the identity
|
148
|
* @param name name of the resulting identity
|
149
|
* @param passphrase password protecting the identity
|
150
|
*/
|
151
|
async function onIdentityDownload(id, name, passphrase) {
|
152
|
var success = false;
|
153
|
try {
|
154
|
var identityData = {
|
155
|
name: name,
|
156
|
password: passphrase
|
157
|
}
|
158
|
const response = await axios.post(API_URL + "certificates/" + id + "/identity", identityData, {
|
159
|
responseType: "arraybuffer",
|
160
|
headers: {
|
161
|
'Content-Type': 'application/json',
|
162
|
'Accept': 'application/x-pkcs12 '
|
163
|
}
|
164
|
});
|
165
|
success = true;
|
166
|
downloadBinary(name + ".p12", response.data)
|
167
|
} catch (error) {
|
168
|
console.log(error);
|
169
|
}
|
170
|
if (!success) throw new Error("Error occurred while obtaining the requested data!");
|
171
|
}
|