1
|
|
2
|
// VUE instance of certificate creation page
|
3
|
var createCertificateApp = new Vue({
|
4
|
el: "#create-certificate-content",
|
5
|
data: {
|
6
|
notBefore: "",
|
7
|
notAfter: "",
|
8
|
// available certificate authorities
|
9
|
authorities: [],
|
10
|
// data of the selected certificate authorities to be displayed in the form
|
11
|
selectedCAData: {
|
12
|
CN: "",
|
13
|
C: "",
|
14
|
L: "",
|
15
|
ST: "",
|
16
|
O: "",
|
17
|
OU: "",
|
18
|
emailAddress: ""
|
19
|
},
|
20
|
// Data of the new certificate to be created received from the input fields
|
21
|
certificateData: {
|
22
|
subject: {
|
23
|
CN: "",
|
24
|
C: "",
|
25
|
L: "",
|
26
|
ST: "",
|
27
|
O: "",
|
28
|
OU: "",
|
29
|
emailAddress: ""
|
30
|
},
|
31
|
validityDays: 30,
|
32
|
usage: {
|
33
|
CA: false,
|
34
|
authentication: false,
|
35
|
digitalSignature: false,
|
36
|
SSL: false
|
37
|
},
|
38
|
CA: null
|
39
|
}
|
40
|
},
|
41
|
// actions to be performed when the page is loaded
|
42
|
// - initialize notBefore and notAfter with current date and current date + 1 month respectively
|
43
|
mounted () {
|
44
|
this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
|
45
|
var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
|
46
|
this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
|
47
|
},
|
48
|
methods: {
|
49
|
// handle certificate creation request
|
50
|
onCreateCertificate: function () {
|
51
|
// validate input data
|
52
|
// - validate if subject CN is filled in
|
53
|
if(this.certificateData.subject.CN === "") {
|
54
|
alert("CN field must be filled in!"); // TODO highlight the field
|
55
|
return;
|
56
|
}
|
57
|
this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
|
58
|
axios.post(API_URL + "certificates", this.certificateData)
|
59
|
.then(function (response) {
|
60
|
// on success return to index page
|
61
|
if(response.data["success"]) {
|
62
|
alert("Certificate was successfully created.");
|
63
|
window.location.href = "/static/index.html";
|
64
|
}
|
65
|
// on error display server response message
|
66
|
else {
|
67
|
alert(response.data["data"]);
|
68
|
}
|
69
|
})
|
70
|
.catch(function (error) {
|
71
|
console.log(error);
|
72
|
});
|
73
|
}
|
74
|
},
|
75
|
// data watches
|
76
|
watch: {
|
77
|
// if the selected CA is changed, the Issuer input fileds must be filled in
|
78
|
'certificateData.validityDays': function (val, oldVal) {
|
79
|
var endDate = new Date(new Date().getTime()+(val*24*60*60*1000));
|
80
|
this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
|
81
|
},
|
82
|
'certificateData.CA': function (val, oldVal) {
|
83
|
// self-signed certificate - all fields are empty
|
84
|
if (val === "null" || val == null) {
|
85
|
createCertificateApp.selectedCAData = {
|
86
|
CN: "",
|
87
|
C: "",
|
88
|
L: "",
|
89
|
ST: "",
|
90
|
O: "",
|
91
|
OU: "",
|
92
|
emailAddress: ""
|
93
|
};
|
94
|
}
|
95
|
// a CA is selected - get CA's details and display them
|
96
|
else {
|
97
|
axios.get(API_URL + "certificates/" + val + "/details")
|
98
|
.then(function (response) {
|
99
|
if (response.data["success"]) {
|
100
|
createCertificateApp.selectedCAData = response.data["data"]["subject"];
|
101
|
}
|
102
|
else
|
103
|
console.log("Error occurred while fetching CA details");
|
104
|
})
|
105
|
.catch(function (error) {
|
106
|
console.log(error);
|
107
|
});
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
});
|
112
|
|
113
|
// Initialize available CA select values
|
114
|
axios.get(API_URL+"certificates", {
|
115
|
params: {
|
116
|
filtering: {
|
117
|
CA: true
|
118
|
}
|
119
|
}
|
120
|
})
|
121
|
.then(function (response) {
|
122
|
if (response.data["success"]) {
|
123
|
createCertificateApp.authorities = response.data["data"];
|
124
|
}
|
125
|
else
|
126
|
console.log("Error occurred while fetching list of available certificate authorities");
|
127
|
})
|
128
|
.catch(function (error) {
|
129
|
console.log(error);
|
130
|
});
|