Projekt

Obecné

Profil

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

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

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

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

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

    
129

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

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

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