Projekt

Obecné

Profil

Stáhnout (6.79 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
                const response = await axios.post(API_URL + "certificates", this.certificateData);
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
            }
137
        }
138
    },
139
    // data watches
140
    watch: {
141
        authorities: function (val, oldVal) {
142
            this.isSelfSigned = val.length === 0;
143
        },
144
        isSelfSigned: function (val, oldVal) {
145
            if (val) {
146
                this.certificateData.CA = null;
147
                this.certificateData.usage.CA = true;
148
            } else {
149
                this.certificateData.usage.CA = false;
150
            }
151
        },
152
        // if the selected CA is changed, the Issuer input fileds must be filled in
153
        'certificateData.validityDays': function (val, oldVal) {
154
            var endDate = new Date(new Date().getTime() + (val * 24 * 60 * 60 * 1000));
155
            this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
156
        },
157
        'certificateData.subject.CN': function (val, oldVal) {
158
            if (val !== '') this.invalidCN = false;
159
        },
160
        'certificateData.CA': async function (val, oldVal) {
161
            // self-signed certificate - all fields are empty
162
            if (val === "null" || val == null) {
163
                createCertificateApp.selectedCAData = {
164
                    CN: "",
165
                    C: "",
166
                    L: "",
167
                    ST: "",
168
                    O: "",
169
                    OU: "",
170
                    emailAddress: ""
171
                };
172
            }
173
            // a CA is selected - get CA's details and display them
174
            else {
175
                try {
176
                    const response = await axios.get(API_URL + "certificates/" + val + "/details");
177
                    if (response.data["success"])
178
                        createCertificateApp.selectedCAData = response.data["data"]["subject"];
179
                    else
180
                        console.log("Error occurred while fetching CA details");
181
                } catch (error) {
182
                    console.log(error);
183
                }
184
            }
185
        }
186
    }
187
});
(8-8/11)