Projekt

Obecné

Profil

Stáhnout (5.5 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 6c9cb54a Jan Pašek
                // available certificate authorities
7
                authorities: [],
8
                // data of the selected certificate authorities to be displayed in the form
9 b556270c Jan Pašek
                selectedCAData: {
10 a3b708c2 Jan Pašek
                    CN: "",
11
                    C:  "",
12
                    L:  "",
13
                    ST: "",
14
                    O:  "",
15
                    OU: "",
16
                    emailAddress: ""
17 9cac7fd4 Jan Pašek
                },
18 6c9cb54a Jan Pašek
                // Data of the new certificate to be created received from the input fields
19 9cac7fd4 Jan Pašek
                certificateData: {
20
                    subject: {
21
                        CN: "",
22
                        C: "",
23
                        L: "",
24
                        ST: "",
25
                        O: "",
26
                        OU: "",
27
                        emailAddress: ""
28
                    },
29
                    notBefore: "",
30
                    notAfter: "",
31
                    usage: {
32
                        CA: false,
33
                        authentication: false,
34
                        digitalSignature: false,
35
                        SSL: false
36
                    },
37
                    CA: null
38 a3b708c2 Jan Pašek
                }
39 b556270c Jan Pašek
            },
40 6c9cb54a Jan Pašek
            // actions to be performed when the page is loaded
41
            // - initialize notBefore and notAfter with current date and current date + 1 month respectively
42 9cac7fd4 Jan Pašek
            mounted () {
43 6c9cb54a Jan Pašek
              this.certificateData.notBefore = new Date().toDateInputValue(); // init notBefore to current date
44 9cac7fd4 Jan Pašek
              var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
45 6c9cb54a Jan Pašek
              this.certificateData.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
46 9cac7fd4 Jan Pašek
            },
47 71d8054e Jan Pašek
            methods: {
48 6c9cb54a Jan Pašek
                // handle certificate creation request
49 71d8054e Jan Pašek
                onCreateCertificate: function () {
50 6c9cb54a Jan Pašek
                    // validate input data
51
                    // - validate if subject CN is filled in
52
                    if(this.certificateData.subject.CN === "") {
53
                        alert("CN field must be filled in!"); // TODO highlight the field
54
                        return;
55
                    }
56
                    // - validate if notAfter is > notBefore
57
                    if(new Date(this.certificateData.notBefore) > new Date(this.certificateData.notAfter)) {
58
                        alert("notBefore must chronologically lower than notAfter"); // TODO highlight the field
59
                        return;
60
                    }
61
                    // send the request and handle response
62 71d8054e Jan Pašek
                    axios.post(API_URL + "certificates", this.certificateData)
63
                        .then(function (response) {
64 6c9cb54a Jan Pašek
                            // on success return to index page
65 71d8054e Jan Pašek
                            if(response.data["success"]) {
66
                                alert("Certificate was successfully created.");
67
                                window.location.href = "/static/index.html";
68
                            }
69 6c9cb54a Jan Pašek
                            // on error display server response message
70 71d8054e Jan Pašek
                            else {
71
                                alert(response.data["data"]);
72
                            }
73
                        })
74
                        .catch(function (error) {
75
                            console.log(error);
76
                        });
77
                }
78
            },
79 6c9cb54a Jan Pašek
            // data watches
80 b556270c Jan Pašek
            watch: {
81 6c9cb54a Jan Pašek
                // if the selected CA is changed, the Issuer input fileds must be filled in
82 9cac7fd4 Jan Pašek
                'certificateData.CA': function (val, oldVal) {
83 6c9cb54a Jan Pašek
                    // self-signed certificate - all fields are empty
84 b556270c Jan Pašek
                    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 6c9cb54a Jan Pašek
                    // a CA is selected - get CA's details and display them
96 b556270c Jan Pašek
                    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 6c9cb54a Jan Pašek
    // Initialize available CA select values
114 b556270c Jan Pašek
    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 a3b708c2 Jan Pašek
            }
125 b556270c Jan Pašek
            else
126
                console.log("Error occurred while fetching list of available certificate authorities");
127
        })
128
        .catch(function (error) {
129
            console.log(error);
130 a3b708c2 Jan Pašek
        });