Projekt

Obecné

Profil

Stáhnout (5.5 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
                // available certificate authorities
7
                authorities: [],
8
                // data of the selected certificate authorities to be displayed in the form
9
                selectedCAData: {
10
                    CN: "",
11
                    C:  "",
12
                    L:  "",
13
                    ST: "",
14
                    O:  "",
15
                    OU: "",
16
                    emailAddress: ""
17
                },
18
                // Data of the new certificate to be created received from the input fields
19
                certificateData: {
20
                    subject: {
21
                        CN: "",
22
                        C: "",
23
                        L: "",
24
                        ST: "",
25
                        O: "",
26
                        OU: "",
27
                        emailAddress: ""
28
                    },
29
                    notBefore: "",
30
                    notAfter: "",
31
                    usage: {
32
                        CA: false,
33
                        authentication: false,
34
                        digitalSignature: false,
35
                        SSL: false
36
                    },
37
                    CA: null
38
                }
39
            },
40
            // actions to be performed when the page is loaded
41
            // - initialize notBefore and notAfter with current date and current date + 1 month respectively
42
            mounted () {
43
              this.certificateData.notBefore = new Date().toDateInputValue(); // init notBefore to current date
44
              var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
45
              this.certificateData.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
46
            },
47
            methods: {
48
                // handle certificate creation request
49
                onCreateCertificate: function () {
50
                    // validate input data
51
                    // - validate if subject CN is filled in
52
                    if(this.certificateData.subject.CN === "") {
53
                        alert("CN field must be filled in!"); // TODO highlight the field
54
                        return;
55
                    }
56
                    // - validate if notAfter is > notBefore
57
                    if(new Date(this.certificateData.notBefore) > new Date(this.certificateData.notAfter)) {
58
                        alert("notBefore must chronologically lower than notAfter"); // TODO highlight the field
59
                        return;
60
                    }
61
                    // send the request and handle response
62
                    axios.post(API_URL + "certificates", this.certificateData)
63
                        .then(function (response) {
64
                            // on success return to index page
65
                            if(response.data["success"]) {
66
                                alert("Certificate was successfully created.");
67
                                window.location.href = "/static/index.html";
68
                            }
69
                            // on error display server response message
70
                            else {
71
                                alert(response.data["data"]);
72
                            }
73
                        })
74
                        .catch(function (error) {
75
                            console.log(error);
76
                        });
77
                }
78
            },
79
            // data watches
80
            watch: {
81
                // if the selected CA is changed, the Issuer input fileds must be filled in
82
                'certificateData.CA': function (val, oldVal) {
83
                    // self-signed certificate - all fields are empty
84
                    if (val === "null" || val == null) {
85
                        createCertificateApp.selectedCAData = {
86
                            CN: "",
87
                            C:  "",
88
                            L:  "",
89
                            ST: "",
90
                            O:  "",
91
                            OU: "",
92
                            emailAddress: ""
93
                        };
94
                    }
95
                    // a CA is selected - get CA's details and display them
96
                    else {
97
                        axios.get(API_URL + "certificates/" + val + "/details")
98
                            .then(function (response) {
99
                                if (response.data["success"]) {
100
                                    createCertificateApp.selectedCAData = response.data["data"]["subject"];
101
                                }
102
                                else
103
                                    console.log("Error occurred while fetching CA details");
104
                            })
105
                            .catch(function (error) {
106
                                console.log(error);
107
                            });
108
                    }
109
                }
110
            }
111
        });
112

    
113
    // Initialize available CA select values
114
    axios.get(API_URL+"certificates", {
115
            params: {
116
                filtering: {
117
                    CA: true
118
                }
119
            }
120
        })
121
        .then(function (response) {
122
            if (response.data["success"]) {
123
                createCertificateApp.authorities = response.data["data"];
124
            }
125
            else
126
                console.log("Error occurred while fetching list of available certificate authorities");
127
        })
128
        .catch(function (error) {
129
            console.log(error);
130
        });
(7-7/10)