Projekt

Obecné

Profil

Stáhnout (8.57 KB) Statistiky
| Větev: | Tag: | Revize:
1
// VUE instance of certificate creation page
2
var createCertificateApp = new Vue({
3
    el: "#create-certificate-content",
4
    data: {
5
        notBefore: "",
6
        notAfter: "",
7
        isSelfSigned: false,
8
        invalidCN: false,
9
        invalidC: false,
10
        customKey: false,
11
        showIssuer: false,
12
        customExtensions: false,
13
        // available certificate authorities
14
        authorities: [],
15
        // data of the selected certificate authorities to be displayed in the form
16
        selectedCAData: {
17
            CN: "",
18
            C: "",
19
            L: "",
20
            ST: "",
21
            O: "",
22
            OU: "",
23
            emailAddress: ""
24
        },
25
        // Data of the new certificate to be created received from the input fields
26
        certificateData: {
27
            subject: {
28
                CN: "",
29
                C: "",
30
                L: "",
31
                ST: "",
32
                O: "",
33
                OU: "",
34
                emailAddress: ""
35
            },
36
            validityDays: 30,
37
            usage: {
38
                CA: false,
39
                authentication: false,
40
                digitalSignature: false,
41
                SSL: false
42
            },
43
            CA: null
44
        },
45
        extensions: null,
46
        key: {
47
            password: null,
48
            key_pem: null,
49
        },
50
        errorMessage: ""
51
    },
52
    // actions to be performed when the page is loaded
53
    // - initialize notBefore and notAfter with current date and current date + 1 month respectively
54
    async mounted() {
55
        this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
56
        var endDate = new Date(new Date().getTime() + (30 * 24 * 60 * 60 * 1000));
57
        this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
58

    
59
        // Initialize available CA select values
60
        try {
61
            const response = await axios.get(API_URL + "certificates", {
62
                params: {
63
                    filtering: {
64
                        usage: ["CA"],
65
                    }
66
                }
67
            });
68
            if (response.data["success"]) {
69
                createCertificateApp.authorities = response.data["data"];
70
            } else {
71
                this.showError("Error occurred while downloading list of available CAs");
72
                console.error(response.data["data"]);
73
                createCertificateApp.authorities = [];
74
            }
75
        } catch (error) {
76
            this.showError("Error occurred while downloading list of available CAs");
77
            console.log(error);
78
        }
79
    },
80
    methods: {
81
        toggleShowIssuer: function () {
82
            this.showIssuer = ~this.showIssuer;
83
        },
84
        onKeyFileChange: function (event) {
85
            var file = event.target.files[0];
86
            var reader = new FileReader();
87
            reader.readAsText(file, "UTF-8");
88
            reader.onload = function (evt) {
89
                createCertificateApp.key.key_pem = evt.target.result;
90
            }
91
            reader.onerror = function (evt) {
92
                this.showError("Error occurred while reading custom private key file.");
93
            }
94

    
95
        },
96
        showError: function (message) {
97
            document.body.scrollTop = 0;
98
            document.documentElement.scrollTop = 0;
99
            this.errorMessage = message;
100
        },
101
        // handle certificate creation request
102
        onCreateCertificate: async function () {
103
            // validate input data
104
            // - validate if is self signed or CA is selected
105
            // - validate if subject CN is filled in
106
            // - validate if C is either empty or has exactly 2 characters
107
            if (!this.isSelfSigned && this.certificateData.CA == null) {
108
                this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
109
                return;
110
            }
111
            if (this.certificateData.subject.CN === "") {
112
                this.showError("CN field must be filled in!")
113
                this.invalidCN = true;
114
                return;
115
            }
116
            if (this.certificateData.subject.C !== "" && this.certificateData.subject.C.length !== 2) {
117
                this.showError("C field must be empty or must have exactly 2 characters!");
118
                this.invalidC = true;
119
                return;
120
            }
121

    
122
            // populate optional key field in the request body
123
            delete this.certificateData.key;
124
            if (this.customKey && this.key.password != null && this.key.password !== "") {
125
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
126
                this.certificateData.key.password = this.key.password;
127
            }
128
            if (this.customKey && this.key.key_pem != null) {
129
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
130
                this.certificateData.key.key_pem = this.key.key_pem;
131
            }
132

    
133
            // populate optional extensions field in the request body
134
            delete this.certificateData.extensions;
135
            if (this.customExtensions && this.extensions !== "" && this.extensions != null)
136
            {
137
                this.certificateData.extensions = this.extensions;
138
            }
139

    
140

    
141
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
142
            try {
143
                // create a deep copy of the certificate dataa
144
                var certificateDataCopy = JSON.parse(JSON.stringify(this.certificateData));
145
                certificateDataCopy.usage = [];
146

    
147
                // convert usage dictionary to list
148
                if (this.certificateData.usage.CA) certificateDataCopy.usage.push("CA");
149
                if (this.certificateData.usage.digitalSignature) certificateDataCopy.usage.push("digitalSignature");
150
                if (this.certificateData.usage.authentication) certificateDataCopy.usage.push("authentication");
151
                if (this.certificateData.usage.SSL) certificateDataCopy.usage.push("SSL");
152

    
153
                // call RestAPI endpoint
154
                const response = await axios.post(API_URL + "certificates", certificateDataCopy);
155
                if (response.data["success"]) {
156
                    window.location.href = "/static/index.html?success=Certificate+successfully+created";
157
                }
158
                // on error display server response message
159
                else {
160
                    console.error(response.data["data"]);
161
                    createCertificateApp.showError(response.data["data"]);
162
                }
163
            } catch (error) {
164
                createCertificateApp.showError("An error occurred while creating a certificate.");
165
                console.error(error);
166
            }
167
        }
168
    },
169
    // data watches
170
    watch: {
171
        authorities: function (val, oldVal) {
172
            this.isSelfSigned = val.length === 0;
173
        },
174
        isSelfSigned: function (val, oldVal) {
175
            if (val) {
176
                this.certificateData.CA = null;
177
                this.certificateData.usage.CA = true;
178
            } else {
179
                this.certificateData.usage.CA = false;
180
            }
181
        },
182
        // if the selected CA is changed, the Issuer input fileds must be filled in
183
        'certificateData.validityDays': function (val, oldVal) {
184
            var endDate = new Date(new Date().getTime() + (val * 24 * 60 * 60 * 1000));
185
            this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
186
        },
187
        'certificateData.subject.CN': function (val, oldVal) {
188
            if (val !== '') this.invalidCN = false;
189
        },
190
        'certificateData.CA': async function (val, oldVal) {
191
            // self-signed certificate - all fields are empty
192
            if (val === "null" || val == null) {
193
                createCertificateApp.selectedCAData = {
194
                    CN: "",
195
                    C: "",
196
                    L: "",
197
                    ST: "",
198
                    O: "",
199
                    OU: "",
200
                    emailAddress: ""
201
                };
202
            }
203
            // a CA is selected - get CA's details and display them
204
            else {
205
                try {
206
                    const response = await axios.get(API_URL + "certificates/" + val + "/details");
207
                    if (response.data["success"])
208
                        createCertificateApp.selectedCAData = response.data["data"]["subject"];
209
                    else
210
                        console.log("Error occurred while fetching CA details");
211
                } catch (error) {
212
                    console.log(error);
213
                }
214
            }
215
        }
216
    }
217
});
(10-10/13)