Projekt

Obecné

Profil

Stáhnout (4.92 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
    try {
46
        const response = await axios.get(API_URL + "certificates/" + id);
47
        if (response.data["success"]) {
48
            download(id + ".pem", response.data["data"])
49
        } else
50
            console.log("Error occurred while downloading the certificate") // TODO more action may be required
51
    } catch (error) {
52
        console.log(error);
53
    }
54 7a423499 Jan Pašek
}
55
56 780c6d9c Jan Pašek
/**
57
 * Download root of chain of trust excluding root of given certificate
58
 * @param id identifier of certificate whose chain shall be downloaded
59
 */
60 4a772c43 Jan Pašek
async function onCertificateChainDownload(id) {
61
    try {
62
        const response = await axios.get(API_URL + "certificates/" + id + "/chain");
63
        if (response.data["success"]) {
64
            download(id + "_chain.pem", response.data["data"])
65
        } else
66
            console.log("Error occurred while downloading the certificate's chain of trust") // TODO more action may be required
67
    } catch (error) {
68
        console.log(error);
69
    }
70 7a423499 Jan Pašek
}
71
72 780c6d9c Jan Pašek
/**
73
 * Download root of chain of trust corresponding to the given certificate
74
 * @param id identifier of certificate whose root shall be downloaded
75
 */
76 4a772c43 Jan Pašek
async function onCertificateRootDownload(id) {
77
    try {
78
        const response = await axios.get(API_URL + "certificates/" + id + "/root");
79
        if (response.data["success"]) {
80
            download(id + "_root.pem", response.data["data"])
81
        } else
82
            console.log("Error occurred while downloading the certificate's root CA") // TODO more action may be required
83
    } catch (error) {
84
        console.log(error);
85
    }
86 780c6d9c Jan Pašek
}
87
88
/**
89
 * Download public key associated with the selected certificate
90
 * @param id identifier of certificate whose pub. key shall be downloaded
91
 */
92 4a772c43 Jan Pašek
async function onPublicKeyDownload(id) {
93
    try {
94
        const response = await axios.get(API_URL + "certificates/" + id + "/publickey");
95
        if (response.data["success"]) {
96
            download(id + "_public_key.pem", response.data["data"])
97
        } else
98
            console.log("Error occurred while downloading the certificate's public key") // TODO more action may be required
99
    } catch (error) {
100
        console.log(error);
101
    }
102 780c6d9c Jan Pašek
}
103
104
/**
105
 * Download public key associated with the selected certificate
106
 * @param id identifier of certificate whose pub. key shall be downloaded
107
 */
108 4a772c43 Jan Pašek
async function onPrivateKeyDownload(id) {
109
    try {
110
        const response = await axios.get(API_URL + "certificates/" + id + "/privatekey");
111
        if (response.data["success"]) {
112
            download(id + "_private_key.pem", response.data["data"])
113
        } else
114
            console.log("Error occurred while downloading the certificate's private key") // TODO more action may be required
115
    } catch (error) {
116
        console.log(error);
117
    }
118 76cd3805 Jan Pašek
}
119
120
/**
121
 * Download identity given by certificate id, name and password
122
 * @param id identifier of certificate to be used for creating the identity
123
 * @param name name of the resulting identity
124
 * @param passphrase password protecting the identity
125
 */
126
async function onIdentityDownload(id, name, passphrase) {
127
    try {
128
        var identityData = {
129
            name: name,
130
            password: passphrase
131
        }
132 736b3327 Jan Pašek
        const response = await axios.post(API_URL + "certificates/" + id + "/identity", identityData, {
133
            responseType: "arraybuffer",
134
            headers: {
135
                'Content-Type': 'application/json',
136
                'Accept': 'application/x-pkcs12 '
137
            }
138
        });
139
        downloadBinary(name + ".p12", response.data)
140 76cd3805 Jan Pašek
    } catch (error) {
141
        console.log(error);
142
    }
143 7a423499 Jan Pašek
}