Projekt

Obecné

Profil

Stáhnout (6.06 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
                    }
90
                },
91 6c9cb54a Jan Pašek
                // if the selected CA is changed, the Issuer input fileds must be filled in
92 1b9fc014 Jan Pašek
                'certificateData.validityDays': function (val, oldVal) {
93
                    var endDate = new Date(new Date().getTime()+(val*24*60*60*1000));
94
                    this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
95
                },
96 9cac7fd4 Jan Pašek
                'certificateData.CA': function (val, oldVal) {
97 6c9cb54a Jan Pašek
                    // self-signed certificate - all fields are empty
98 b556270c Jan Pašek
                    if (val === "null" || val == null) {
99
                        createCertificateApp.selectedCAData = {
100
                            CN: "",
101
                            C:  "",
102
                            L:  "",
103
                            ST: "",
104
                            O:  "",
105
                            OU: "",
106
                            emailAddress: ""
107
                        };
108
                    }
109 6c9cb54a Jan Pašek
                    // a CA is selected - get CA's details and display them
110 b556270c Jan Pašek
                    else {
111
                        axios.get(API_URL + "certificates/" + val + "/details")
112
                            .then(function (response) {
113
                                if (response.data["success"]) {
114
                                    createCertificateApp.selectedCAData = response.data["data"]["subject"];
115
                                }
116
                                else
117
                                    console.log("Error occurred while fetching CA details");
118
                            })
119
                            .catch(function (error) {
120
                                console.log(error);
121
                            });
122
                    }
123
                }
124
            }
125
        });
126
127 6c9cb54a Jan Pašek
    // Initialize available CA select values
128 b556270c Jan Pašek
    axios.get(API_URL+"certificates", {
129
            params: {
130
                filtering: {
131
                    CA: true
132
                }
133
            }
134
        })
135
        .then(function (response) {
136
            if (response.data["success"]) {
137
                createCertificateApp.authorities = response.data["data"];
138 a3b708c2 Jan Pašek
            }
139 b556270c Jan Pašek
            else
140 a857e1ac Jan Pašek
            {
141
                createCertificateApp.authorities = []
142
            }
143 b556270c Jan Pašek
        })
144
        .catch(function (error) {
145
            console.log(error);
146 a3b708c2 Jan Pašek
        });