Projekt

Obecné

Profil

Stáhnout (7.72 KB) Statistiky
| Větev: | Tag: | Revize:
1 7a34cbd3 Jan Pašek
// 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 dd7fb7a3 Jan Pašek
        customKey: false,
10 73ab16a1 Jan Pašek
        customExtensions: false,
11 7a34cbd3 Jan Pašek
        // 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 9cac7fd4 Jan Pašek
            },
34 7a34cbd3 Jan Pašek
            validityDays: 30,
35
            usage: {
36
                CA: false,
37
                authentication: false,
38
                digitalSignature: false,
39
                SSL: false
40 71d8054e Jan Pašek
            },
41 7a34cbd3 Jan Pašek
            CA: null
42
        },
43 73ab16a1 Jan Pašek
        extensions: null,
44 dd7fb7a3 Jan Pašek
        key: {
45
            password: null,
46
            key_pem: null,
47
        },
48 7a34cbd3 Jan Pašek
        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 ac94bd92 Jan Pašek
    async mounted() {
53 7a34cbd3 Jan Pašek
        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 b556270c Jan Pašek
57 7a34cbd3 Jan Pašek
        // Initialize available CA select values
58 ac94bd92 Jan Pašek
        try {
59
            const response = await axios.get(API_URL + "certificates", {
60
                params: {
61
                    filtering: {
62 e24e5207 Jan Pašek
                        usage: ["CA"],
63 ac94bd92 Jan Pašek
                    }
64 7a34cbd3 Jan Pašek
                }
65
            });
66 ac94bd92 Jan Pašek
            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 7a34cbd3 Jan Pašek
    },
75
    methods: {
76 dd7fb7a3 Jan Pašek
        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 7a34cbd3 Jan Pašek
        showError: function (message) {
89
            document.body.scrollTop = 0;
90
            document.documentElement.scrollTop = 0;
91
            this.errorMessage = message;
92
        },
93
        // handle certificate creation request
94 ac94bd92 Jan Pašek
        onCreateCertificate: async function () {
95 7a34cbd3 Jan Pašek
            // 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 a3b708c2 Jan Pašek
            }
101 7a34cbd3 Jan Pašek
            if (this.certificateData.subject.CN === "") {
102
                this.showError("CN field must be filled in!")
103
                this.invalidCN = true;
104
                return;
105 a857e1ac Jan Pašek
            }
106 dd7fb7a3 Jan Pašek
107
            // populate optional key field in the request body
108 73ab16a1 Jan Pašek
            delete this.certificateData.key;
109
            if (this.customKey && this.key.password != null && this.key.password !== "") {
110 cf1dec7f Jan Pašek
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
111 dd7fb7a3 Jan Pašek
                this.certificateData.key.password = this.key.password;
112
            }
113 73ab16a1 Jan Pašek
            if (this.customKey && this.key.key_pem != null) {
114 cf1dec7f Jan Pašek
                if (!this.certificateData.hasOwnProperty("key")) this.certificateData.key = {};
115 dd7fb7a3 Jan Pašek
                this.certificateData.key.key_pem = this.key.key_pem;
116
            }
117
118 73ab16a1 Jan Pašek
            // 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 7a34cbd3 Jan Pašek
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
127 ac94bd92 Jan Pašek
            try {
128 a42ed5f4 Jan Pašek
                // 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 ac94bd92 Jan Pašek
                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 cf1dec7f Jan Pašek
                createCertificateApp.showError(error.response.data["data"]);
149
                console.error(error);
150 ac94bd92 Jan Pašek
            }
151 7a34cbd3 Jan Pašek
        }
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 ac94bd92 Jan Pašek
        'certificateData.CA': async function (val, oldVal) {
175 7a34cbd3 Jan Pašek
            // 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 ac94bd92 Jan Pašek
                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 7a34cbd3 Jan Pašek
            }
199
        }
200
    }
201
});