Projekt

Obecné

Profil

Stáhnout (8.57 KB) Statistiky
| Větev: | Tag: | Revize:
1 7a34cbd3 Jan Pašek
// 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 87c04047 Jan Pašek
        invalidC: false,
10 dd7fb7a3 Jan Pašek
        customKey: false,
11 376e6be2 Jan Pašek
        showIssuer: false,
12 73ab16a1 Jan Pašek
        customExtensions: false,
13 7a34cbd3 Jan Pašek
        // 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 9cac7fd4 Jan Pašek
            },
36 7a34cbd3 Jan Pašek
            validityDays: 30,
37
            usage: {
38
                CA: false,
39
                authentication: false,
40
                digitalSignature: false,
41
                SSL: false
42 71d8054e Jan Pašek
            },
43 7a34cbd3 Jan Pašek
            CA: null
44
        },
45 73ab16a1 Jan Pašek
        extensions: null,
46 dd7fb7a3 Jan Pašek
        key: {
47
            password: null,
48
            key_pem: null,
49
        },
50 7a34cbd3 Jan Pašek
        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 ac94bd92 Jan Pašek
    async mounted() {
55 7a34cbd3 Jan Pašek
        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 b556270c Jan Pašek
59 7a34cbd3 Jan Pašek
        // Initialize available CA select values
60 ac94bd92 Jan Pašek
        try {
61
            const response = await axios.get(API_URL + "certificates", {
62
                params: {
63
                    filtering: {
64 e24e5207 Jan Pašek
                        usage: ["CA"],
65 ac94bd92 Jan Pašek
                    }
66 7a34cbd3 Jan Pašek
                }
67
            });
68 ac94bd92 Jan Pašek
            if (response.data["success"]) {
69
                createCertificateApp.authorities = response.data["data"];
70
            } else {
71 dde3db30 Jan Pašek
                this.showError("Error occurred while downloading list of available CAs");
72
                console.error(response.data["data"]);
73
                createCertificateApp.authorities = [];
74 ac94bd92 Jan Pašek
            }
75
        } catch (error) {
76 dde3db30 Jan Pašek
            this.showError("Error occurred while downloading list of available CAs");
77 ac94bd92 Jan Pašek
            console.log(error);
78
        }
79 7a34cbd3 Jan Pašek
    },
80
    methods: {
81 376e6be2 Jan Pašek
        toggleShowIssuer: function () {
82
            this.showIssuer = ~this.showIssuer;
83
        },
84 dd7fb7a3 Jan Pašek
        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 7a34cbd3 Jan Pašek
        showError: function (message) {
97
            document.body.scrollTop = 0;
98
            document.documentElement.scrollTop = 0;
99
            this.errorMessage = message;
100
        },
101
        // handle certificate creation request
102 ac94bd92 Jan Pašek
        onCreateCertificate: async function () {
103 7a34cbd3 Jan Pašek
            // validate input data
104 dde3db30 Jan Pašek
            // - validate if is self signed or CA is selected
105 7a34cbd3 Jan Pašek
            // - validate if subject CN is filled in
106 dde3db30 Jan Pašek
            // - validate if C is either empty or has exactly 2 characters
107 7a34cbd3 Jan Pašek
            if (!this.isSelfSigned && this.certificateData.CA == null) {
108
                this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
109
                return;
110 a3b708c2 Jan Pašek
            }
111 7a34cbd3 Jan Pašek
            if (this.certificateData.subject.CN === "") {
112
                this.showError("CN field must be filled in!")
113
                this.invalidCN = true;
114
                return;
115 a857e1ac Jan Pašek
            }
116 87c04047 Jan Pašek
            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 dd7fb7a3 Jan Pašek
122
            // populate optional key field in the request body
123 73ab16a1 Jan Pašek
            delete this.certificateData.key;
124
            if (this.customKey && this.key.password != null && this.key.password !== "") {
125 cf1dec7f Jan Pašek
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
126 dd7fb7a3 Jan Pašek
                this.certificateData.key.password = this.key.password;
127
            }
128 73ab16a1 Jan Pašek
            if (this.customKey && this.key.key_pem != null) {
129 cf1dec7f Jan Pašek
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
130 dd7fb7a3 Jan Pašek
                this.certificateData.key.key_pem = this.key.key_pem;
131
            }
132
133 73ab16a1 Jan Pašek
            // 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 7a34cbd3 Jan Pašek
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
142 ac94bd92 Jan Pašek
            try {
143 a42ed5f4 Jan Pašek
                // 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 ac94bd92 Jan Pašek
                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 dde3db30 Jan Pašek
                    console.error(response.data["data"]);
161 ac94bd92 Jan Pašek
                    createCertificateApp.showError(response.data["data"]);
162
                }
163
            } catch (error) {
164 dde3db30 Jan Pašek
                createCertificateApp.showError("An error occurred while creating a certificate.");
165 cf1dec7f Jan Pašek
                console.error(error);
166 ac94bd92 Jan Pašek
            }
167 7a34cbd3 Jan Pašek
        }
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 ac94bd92 Jan Pašek
        'certificateData.CA': async function (val, oldVal) {
191 7a34cbd3 Jan Pašek
            // 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 ac94bd92 Jan Pašek
                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 7a34cbd3 Jan Pašek
            }
215
        }
216
    }
217
});