Projekt

Obecné

Profil

Stáhnout (6.66 KB) Statistiky
| Větev: | Tag: | Revize:
1

    
2
    // VUE instance of certificate creation page
3
    var createCertificateApp = new Vue({
4
            el: "#create-certificate-content",
5
            data: {
6
                notBefore: "",
7
                notAfter: "",
8
                isSelfSigned: false,
9
                invalidCN: false,
10
                // available certificate authorities
11
                authorities: [],
12
                // data of the selected certificate authorities to be displayed in the form
13
                selectedCAData: {
14
                    CN: "",
15
                    C:  "",
16
                    L:  "",
17
                    ST: "",
18
                    O:  "",
19
                    OU: "",
20
                    emailAddress: ""
21
                },
22
                // Data of the new certificate to be created received from the input fields
23
                certificateData: {
24
                    subject: {
25
                        CN: "",
26
                        C: "",
27
                        L: "",
28
                        ST: "",
29
                        O: "",
30
                        OU: "",
31
                        emailAddress: ""
32
                    },
33
                    validityDays: 30,
34
                    usage: {
35
                        CA: false,
36
                        authentication: false,
37
                        digitalSignature: false,
38
                        SSL: false
39
                    },
40
                    CA: null
41
                },
42
                errorMessage: ""
43
            },
44
            // actions to be performed when the page is loaded
45
            // - initialize notBefore and notAfter with current date and current date + 1 month respectively
46
            mounted () {
47
              this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
48
              var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
49
              this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
50
            },
51
            methods: {
52
                showError: function (message) {
53
                    document.body.scrollTop = 0;
54
                    document.documentElement.scrollTop = 0;
55
                    this.errorMessage = message;
56
                },
57
                // handle certificate creation request
58
                onCreateCertificate: function () {
59
                    // validate input data
60
                    // - validate if subject CN is filled in
61
                    if(!this.isSelfSigned && this.certificateData.CA == null)
62
                    {
63
                        this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
64
                        return;
65
                    }
66
                    if(this.certificateData.subject.CN === "") {
67
                        this.showError("CN field must be filled in!")
68
                        this.invalidCN = true;
69
                        return;
70
                    }
71
                    this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
72
                    axios.post(API_URL + "certificates", this.certificateData)
73
                        .then(function (response) {
74
                            // on success return to index page
75
                            if(response.data["success"]) {
76
                                window.location.href = "/static/index.html?success=Certificate+successfully+created";
77
                            }
78
                            // on error display server response message
79
                            else {
80
                                createCertificateApp.showError(response.data["data"]);
81
                            }
82
                        })
83
                        .catch(function (error) {
84
                            console.log(error);
85
                        });
86
                }
87
            },
88
            // data watches
89
            watch: {
90
                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
                        this.certificateData.usage.CA = true;
97
                    } else {
98
                        this.certificateData.usage.CA = false;
99
                    }
100
                },
101
                // if the selected CA is changed, the Issuer input fileds must be filled in
102
                '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
                'certificateData.subject.CN': function (val, oldVal) {
107
                    if (val !== '') this.invalidCN = false;
108
                },
109
                'certificateData.CA': function (val, oldVal) {
110
                    // self-signed certificate - all fields are empty
111
                    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
                    // a CA is selected - get CA's details and display them
123
                    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
    // Initialize available CA select values
141
    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
            }
152
            else
153
            {
154
                createCertificateApp.authorities = []
155
            }
156
        })
157
        .catch(function (error) {
158
            console.log(error);
159
        });
(8-8/11)