Projekt

Obecné

Profil

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