Projekt

Obecné

Profil

Stáhnout (5.89 KB) Statistiky
| Větev: | Tag: | Revize:
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 736b3327 Jan Pašek
/**
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 6c9cb54a Jan Pašek
// Adds toDateInputValue() method to Date object
33
// toDateInputValue() produces date in YYYY-MM-DD format
34 4a772c43 Jan Pašek
Date.prototype.toDateInputValue = (function () {
35 9cac7fd4 Jan Pašek
    var local = new Date(this);
36
    local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
37 4a772c43 Jan Pašek
    return local.toJSON().slice(0, 10);
38 7a423499 Jan Pašek
});
39
40 780c6d9c Jan Pašek
/**
41
 * Download certificate given by id
42
 * @param id identifier of certificate to be downloaded
43
 */
44 4a772c43 Jan Pašek
async function onCertificateDownload(id) {
45 dde3db30 Jan Pašek
    var success = false;
46 4a772c43 Jan Pašek
    try {
47
        const response = await axios.get(API_URL + "certificates/" + id);
48
        if (response.data["success"]) {
49 dde3db30 Jan Pašek
            success = true;
50 4a772c43 Jan Pašek
            download(id + ".pem", response.data["data"])
51 dde3db30 Jan Pašek
        } else {
52
            console.error("Error occurred while downloading the certificate");
53
            console.error(response.data["data"]);
54
        }
55 4a772c43 Jan Pašek
    } catch (error) {
56
        console.log(error);
57
    }
58 dde3db30 Jan Pašek
    if (!success) throw new Error("Error occurred while obtaining the requested data!");
59 7a423499 Jan Pašek
}
60
61 780c6d9c Jan Pašek
/**
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 4a772c43 Jan Pašek
async function onCertificateChainDownload(id) {
66 dde3db30 Jan Pašek
    var success = false;
67 4a772c43 Jan Pašek
    try {
68
        const response = await axios.get(API_URL + "certificates/" + id + "/chain");
69
        if (response.data["success"]) {
70 dde3db30 Jan Pašek
            success = true;
71 4a772c43 Jan Pašek
            download(id + "_chain.pem", response.data["data"])
72 dde3db30 Jan Pašek
        } else {
73
            console.error("Error occurred while downloading the certificate's chain of trust");
74
            console.error(response.data["data"]);
75
        }
76 4a772c43 Jan Pašek
    } catch (error) {
77
        console.log(error);
78
    }
79 dde3db30 Jan Pašek
    if (!success) throw new Error("Error occurred while obtaining the requested data!");
80 7a423499 Jan Pašek
}
81
82 780c6d9c Jan Pašek
/**
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 4a772c43 Jan Pašek
async function onCertificateRootDownload(id) {
87 dde3db30 Jan Pašek
    var success = false;
88 4a772c43 Jan Pašek
    try {
89
        const response = await axios.get(API_URL + "certificates/" + id + "/root");
90
        if (response.data["success"]) {
91 dde3db30 Jan Pašek
            success = true;
92 4a772c43 Jan Pašek
            download(id + "_root.pem", response.data["data"])
93 dde3db30 Jan Pašek
        } else {
94
            console.error("Error occurred while downloading the certificate's root CA");
95
            console.error(response.data["data"]);
96
        }
97 4a772c43 Jan Pašek
    } catch (error) {
98
        console.log(error);
99
    }
100 dde3db30 Jan Pašek
    if (!success) throw new Error("Error occurred while obtaining the requested data!");
101 780c6d9c Jan Pašek
}
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 4a772c43 Jan Pašek
async function onPublicKeyDownload(id) {
108 dde3db30 Jan Pašek
    var success = false;
109 4a772c43 Jan Pašek
    try {
110
        const response = await axios.get(API_URL + "certificates/" + id + "/publickey");
111
        if (response.data["success"]) {
112 dde3db30 Jan Pašek
            success = true;
113 4a772c43 Jan Pašek
            download(id + "_public_key.pem", response.data["data"])
114 dde3db30 Jan Pašek
        } else {
115
            console.error("Error occurred while downloading the certificate's public key")
116
            console.error(response.data["data"]);
117
        }
118 4a772c43 Jan Pašek
    } catch (error) {
119
        console.log(error);
120
    }
121 dde3db30 Jan Pašek
    if (!success) throw new Error("Error occurred while obtaining the requested data!");
122 780c6d9c Jan Pašek
}
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 4a772c43 Jan Pašek
async function onPrivateKeyDownload(id) {
129 dde3db30 Jan Pašek
    var success = false;
130 4a772c43 Jan Pašek
    try {
131
        const response = await axios.get(API_URL + "certificates/" + id + "/privatekey");
132
        if (response.data["success"]) {
133 dde3db30 Jan Pašek
            success = true;
134 4a772c43 Jan Pašek
            download(id + "_private_key.pem", response.data["data"])
135 dde3db30 Jan Pašek
        } else {
136
            console.error("Error occurred while downloading the certificate's private key");
137
            console.error(response.data["data"]);
138
        }
139 4a772c43 Jan Pašek
    } catch (error) {
140
        console.log(error);
141
    }
142 dde3db30 Jan Pašek
    if (!success) throw new Error("Error occurred while obtaining the requested data!");
143 76cd3805 Jan Pašek
}
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 dde3db30 Jan Pašek
    var success = false;
153 76cd3805 Jan Pašek
    try {
154
        var identityData = {
155
            name: name,
156
            password: passphrase
157
        }
158 736b3327 Jan Pašek
        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 dde3db30 Jan Pašek
        success = true;
166 736b3327 Jan Pašek
        downloadBinary(name + ".p12", response.data)
167 76cd3805 Jan Pašek
    } catch (error) {
168
        console.log(error);
169
    }
170 dde3db30 Jan Pašek
    if (!success) throw new Error("Error occurred while obtaining the requested data!");
171 7a423499 Jan Pašek
}