Projekt

Obecné

Profil

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