Projekt

Obecné

Profil

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

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

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

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

    
113
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
114
            try {
115
                // create a deep copy of the certificate dataa
116
                var certificateDataCopy = JSON.parse(JSON.stringify(this.certificateData));
117
                certificateDataCopy.usage = [];
118

    
119
                // convert usage dictionary to list
120
                if (this.certificateData.usage.CA) certificateDataCopy.usage.push("CA");
121
                if (this.certificateData.usage.digitalSignature) certificateDataCopy.usage.push("digitalSignature");
122
                if (this.certificateData.usage.authentication) certificateDataCopy.usage.push("authentication");
123
                if (this.certificateData.usage.SSL) certificateDataCopy.usage.push("SSL");
124

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