Projekt

Obecné

Profil

Stáhnout (6.46 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 0e7fa934 Jan Pašek
                },
41
                errorMessage: ""
42 b556270c Jan Pašek
            },
43 6c9cb54a Jan Pašek
            // actions to be performed when the page is loaded
44
            // - initialize notBefore and notAfter with current date and current date + 1 month respectively
45 9cac7fd4 Jan Pašek
            mounted () {
46 1b9fc014 Jan Pašek
              this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
47 9cac7fd4 Jan Pašek
              var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
48 1b9fc014 Jan Pašek
              this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
49 9cac7fd4 Jan Pašek
            },
50 71d8054e Jan Pašek
            methods: {
51 0e7fa934 Jan Pašek
                showError: function (message) {
52
                    document.body.scrollTop = 0;
53
                    document.documentElement.scrollTop = 0;
54
                    this.errorMessage = message;
55
                },
56 6c9cb54a Jan Pašek
                // handle certificate creation request
57 71d8054e Jan Pašek
                onCreateCertificate: function () {
58 6c9cb54a Jan Pašek
                    // validate input data
59
                    // - validate if subject CN is filled in
60 a857e1ac Jan Pašek
                    if(!this.isSelfSigned && this.certificateData.CA == null)
61
                    {
62 0e7fa934 Jan Pašek
                        this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
63 a857e1ac Jan Pašek
                        return;
64
                    }
65 6c9cb54a Jan Pašek
                    if(this.certificateData.subject.CN === "") {
66 0e7fa934 Jan Pašek
                        this.showError("CN field must be filled in!") // TODO highlight the field
67 6c9cb54a Jan Pašek
                        return;
68
                    }
69 493022a0 Jan Pašek
                    this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
70 71d8054e Jan Pašek
                    axios.post(API_URL + "certificates", this.certificateData)
71
                        .then(function (response) {
72 6c9cb54a Jan Pašek
                            // on success return to index page
73 71d8054e Jan Pašek
                            if(response.data["success"]) {
74 6d850d19 Jan Pašek
                                window.location.href = "/static/index.html?success=Certificate+successfully+created";
75 71d8054e Jan Pašek
                            }
76 6c9cb54a Jan Pašek
                            // on error display server response message
77 71d8054e Jan Pašek
                            else {
78 6d850d19 Jan Pašek
                                createCertificateApp.showError(response.data["data"]);
79 71d8054e Jan Pašek
                            }
80
                        })
81
                        .catch(function (error) {
82
                            console.log(error);
83
                        });
84
                }
85
            },
86 6c9cb54a Jan Pašek
            // data watches
87 b556270c Jan Pašek
            watch: {
88 a857e1ac Jan Pašek
                authorities: function (val, oldVal) {
89
                    this.isSelfSigned = val.length === 0;
90
                },
91
                isSelfSigned: function (val, oldVal) {
92
                    if (val) {
93
                        this.certificateData.CA = null;
94 d621fe05 Jan Pašek
                        this.certificateData.usage.CA = true;
95 0e7fa934 Jan Pašek
                    } else {
96
                        this.certificateData.usage.CA = false;
97 a857e1ac Jan Pašek
                    }
98
                },
99 6c9cb54a Jan Pašek
                // if the selected CA is changed, the Issuer input fileds must be filled in
100 1b9fc014 Jan Pašek
                'certificateData.validityDays': function (val, oldVal) {
101
                    var endDate = new Date(new Date().getTime()+(val*24*60*60*1000));
102
                    this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
103
                },
104 9cac7fd4 Jan Pašek
                'certificateData.CA': function (val, oldVal) {
105 6c9cb54a Jan Pašek
                    // self-signed certificate - all fields are empty
106 b556270c Jan Pašek
                    if (val === "null" || val == null) {
107
                        createCertificateApp.selectedCAData = {
108
                            CN: "",
109
                            C:  "",
110
                            L:  "",
111
                            ST: "",
112
                            O:  "",
113
                            OU: "",
114
                            emailAddress: ""
115
                        };
116
                    }
117 6c9cb54a Jan Pašek
                    // a CA is selected - get CA's details and display them
118 b556270c Jan Pašek
                    else {
119
                        axios.get(API_URL + "certificates/" + val + "/details")
120
                            .then(function (response) {
121
                                if (response.data["success"]) {
122
                                    createCertificateApp.selectedCAData = response.data["data"]["subject"];
123
                                }
124
                                else
125
                                    console.log("Error occurred while fetching CA details");
126
                            })
127
                            .catch(function (error) {
128
                                console.log(error);
129
                            });
130
                    }
131
                }
132
            }
133
        });
134
135 6c9cb54a Jan Pašek
    // Initialize available CA select values
136 b556270c Jan Pašek
    axios.get(API_URL+"certificates", {
137
            params: {
138
                filtering: {
139
                    CA: true
140
                }
141
            }
142
        })
143
        .then(function (response) {
144
            if (response.data["success"]) {
145
                createCertificateApp.authorities = response.data["data"];
146 a3b708c2 Jan Pašek
            }
147 b556270c Jan Pašek
            else
148 a857e1ac Jan Pašek
            {
149
                createCertificateApp.authorities = []
150
            }
151 b556270c Jan Pašek
        })
152
        .catch(function (error) {
153
            console.log(error);
154 a3b708c2 Jan Pašek
        });