Projekt

Obecné

Profil

Stáhnout (7.72 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
                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
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
111
                this.certificateData.key.password = this.key.password;
112
            }
113
            if (this.customKey && this.key.key_pem != null) {
114
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
115
                this.certificateData.key.key_pem = this.key.key_pem;
116
            }
117

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

    
125

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

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

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