Projekt

Obecné

Profil

Stáhnout (8.16 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
        customKey: false,
10
        customExtensions: false,
11
        // available certificate authorities
12
        authorities: [],
13
        // data of the selected certificate authorities to be displayed in the form
14
        selectedCAData: {
15
            CN: "",
16
            C: "",
17
            L: "",
18
            ST: "",
19
            O: "",
20
            OU: "",
21
            emailAddress: ""
22
        },
23
        // Data of the new certificate to be created received from the input fields
24
        certificateData: {
25
            subject: {
26
                CN: "",
27
                C: "",
28
                L: "",
29
                ST: "",
30
                O: "",
31
                OU: "",
32
                emailAddress: ""
33
            },
34
            validityDays: 30,
35
            usage: {
36
                CA: false,
37
                authentication: false,
38
                digitalSignature: false,
39
                SSL: false
40
            },
41
            CA: null
42
        },
43
        extensions: null,
44
        key: {
45
            password: null,
46
            key_pem: null,
47
        },
48
        errorMessage: ""
49
    },
50
    // actions to be performed when the page is loaded
51
    // - initialize notBefore and notAfter with current date and current date + 1 month respectively
52
    async mounted() {
53
        this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
54
        var endDate = new Date(new Date().getTime() + (30 * 24 * 60 * 60 * 1000));
55
        this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
56

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

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

    
112
            // populate optional key field in the request body
113
            delete this.certificateData.key;
114
            if (this.customKey && this.key.password != null && this.key.password !== "") {
115
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
116
                this.certificateData.key.password = this.key.password;
117
            }
118
            if (this.customKey && this.key.key_pem != null) {
119
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
120
                this.certificateData.key.key_pem = this.key.key_pem;
121
            }
122

    
123
            // populate optional extensions field in the request body
124
            delete this.certificateData.extensions;
125
            if (this.customExtensions && this.extensions !== "" && this.extensions != null)
126
            {
127
                this.certificateData.extensions = this.extensions;
128
            }
129

    
130

    
131
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
132
            try {
133
                // create a deep copy of the certificate dataa
134
                var certificateDataCopy = JSON.parse(JSON.stringify(this.certificateData));
135
                certificateDataCopy.usage = [];
136

    
137
                // convert usage dictionary to list
138
                if (this.certificateData.usage.CA) certificateDataCopy.usage.push("CA");
139
                if (this.certificateData.usage.digitalSignature) certificateDataCopy.usage.push("digitalSignature");
140
                if (this.certificateData.usage.authentication) certificateDataCopy.usage.push("authentication");
141
                if (this.certificateData.usage.SSL) certificateDataCopy.usage.push("SSL");
142

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