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