Projekt

Obecné

Profil

Stáhnout (7.52 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
                        CA: true
63
                    }
64
                }
65
            });
66
            if (response.data["success"]) {
67
                createCertificateApp.authorities = response.data["data"];
68
            } else {
69
                createCertificateApp.authorities = []
70
            }
71
        } catch (error) {
72
            console.log(error);
73
        }
74
    },
75
    methods: {
76
        onKeyFileChange: function (event) {
77
            var file = event.target.files[0];
78
            var reader = new FileReader();
79
            reader.readAsText(file, "UTF-8");
80
            reader.onload = function (evt) {
81
                createCertificateApp.key.key_pem = evt.target.result;
82
            }
83
            reader.onerror = function (evt) {
84
                this.showError("Error occurred while reading custom private key file.");
85
            }
86

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

    
107
            // populate optional key field in the request body
108
            delete this.certificateData.key;
109
            if (this.customKey && this.key.password != null && this.key.password !== "") {
110
                this.certificateData.key.password = this.key.password;
111
            }
112
            if (this.customKey && this.key.key_pem != null) {
113
                this.certificateData.key.key_pem = this.key.key_pem;
114
            }
115

    
116
            // populate optional extensions field in the request body
117
            delete this.certificateData.extensions;
118
            if (this.customExtensions && this.extensions !== "" && this.extensions != null)
119
            {
120
                this.certificateData.extensions = this.extensions;
121
            }
122

    
123

    
124
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
125
            try {
126
                // create a deep copy of the certificate dataa
127
                var certificateDataCopy = JSON.parse(JSON.stringify(this.certificateData));
128
                certificateDataCopy.usage = [];
129

    
130
                // convert usage dictionary to list
131
                if (this.certificateData.usage.CA) certificateDataCopy.usage.push("CA");
132
                if (this.certificateData.usage.digitalSignature) certificateDataCopy.usage.push("digitalSignature");
133
                if (this.certificateData.usage.authentication) certificateDataCopy.usage.push("authentication");
134
                if (this.certificateData.usage.SSL) certificateDataCopy.usage.push("SSL");
135

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