Projekt

Obecné

Profil

Stáhnout (6.48 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
                                alert("Certificate was successfully created.");
75
                                window.location.href = "/static/index.html";
76
                            }
77
                            // on error display server response message
78
                            else {
79
                                alert(response.data["data"]);
80
                            }
81
                        })
82
                        .catch(function (error) {
83
                            console.log(error);
84
                        });
85
                }
86
            },
87
            // data watches
88
            watch: {
89
                authorities: function (val, oldVal) {
90
                    this.isSelfSigned = val.length === 0;
91
                },
92
                isSelfSigned: function (val, oldVal) {
93
                    if (val) {
94
                        this.certificateData.CA = null;
95
                        this.certificateData.usage.CA = true;
96
                    } else {
97
                        this.certificateData.usage.CA = false;
98
                    }
99
                },
100
                // if the selected CA is changed, the Issuer input fileds must be filled in
101
                'certificateData.validityDays': function (val, oldVal) {
102
                    var endDate = new Date(new Date().getTime()+(val*24*60*60*1000));
103
                    this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
104
                },
105
                'certificateData.CA': function (val, oldVal) {
106
                    // self-signed certificate - all fields are empty
107
                    if (val === "null" || val == null) {
108
                        createCertificateApp.selectedCAData = {
109
                            CN: "",
110
                            C:  "",
111
                            L:  "",
112
                            ST: "",
113
                            O:  "",
114
                            OU: "",
115
                            emailAddress: ""
116
                        };
117
                    }
118
                    // a CA is selected - get CA's details and display them
119
                    else {
120
                        axios.get(API_URL + "certificates/" + val + "/details")
121
                            .then(function (response) {
122
                                if (response.data["success"]) {
123
                                    createCertificateApp.selectedCAData = response.data["data"]["subject"];
124
                                }
125
                                else
126
                                    console.log("Error occurred while fetching CA details");
127
                            })
128
                            .catch(function (error) {
129
                                console.log(error);
130
                            });
131
                    }
132
                }
133
            }
134
        });
135

    
136
    // Initialize available CA select values
137
    axios.get(API_URL+"certificates", {
138
            params: {
139
                filtering: {
140
                    CA: true
141
                }
142
            }
143
        })
144
        .then(function (response) {
145
            if (response.data["success"]) {
146
                createCertificateApp.authorities = response.data["data"];
147
            }
148
            else
149
            {
150
                createCertificateApp.authorities = []
151
            }
152
        })
153
        .catch(function (error) {
154
            console.log(error);
155
        });
(7-7/10)