Projekt

Obecné

Profil

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