Projekt

Obecné

Profil

Stáhnout (4.92 KB) Statistiky
| Větev: | Tag: | Revize:
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
    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
}
55

    
56
/**
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
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
}
71

    
72
/**
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
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
}
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
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
}
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
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
}
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
        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
    } catch (error) {
141
        console.log(error);
142
    }
143
}
(13-13/13)