Projekt

Obecné

Profil

Stáhnout (6.06 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
            },
42
            // actions to be performed when the page is loaded
43
            // - initialize notBefore and notAfter with current date and current date + 1 month respectively
44
            mounted () {
45
              this.notBefore = new Date().toDateInputValue(); // init notBefore to current date
46
              var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
47
              this.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
48
            },
49
            methods: {
50
                // handle certificate creation request
51
                onCreateCertificate: function () {
52
                    // validate input data
53
                    // - validate if subject CN is filled in
54
                    if(!this.isSelfSigned && this.certificateData.CA == null)
55
                    {
56
                        alert("Issuer must be selected or 'Self-signed' option must be checked!");
57
                        return;
58
                    }
59
                    if(this.certificateData.subject.CN === "") {
60
                        alert("CN field must be filled in!"); // TODO highlight the field
61
                        return;
62
                    }
63
                    this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
64
                    axios.post(API_URL + "certificates", this.certificateData)
65
                        .then(function (response) {
66
                            // on success return to index page
67
                            if(response.data["success"]) {
68
                                alert("Certificate was successfully created.");
69
                                window.location.href = "/static/index.html";
70
                            }
71
                            // on error display server response message
72
                            else {
73
                                alert(response.data["data"]);
74
                            }
75
                        })
76
                        .catch(function (error) {
77
                            console.log(error);
78
                        });
79
                }
80
            },
81
            // data watches
82
            watch: {
83
                authorities: function (val, oldVal) {
84
                    this.isSelfSigned = val.length === 0;
85
                },
86
                isSelfSigned: function (val, oldVal) {
87
                    if (val) {
88
                        this.certificateData.CA = null;
89
                    }
90
                },
91
                // if the selected CA is changed, the Issuer input fileds must be filled in
92
                'certificateData.validityDays': function (val, oldVal) {
93
                    var endDate = new Date(new Date().getTime()+(val*24*60*60*1000));
94
                    this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
95
                },
96
                'certificateData.CA': function (val, oldVal) {
97
                    // self-signed certificate - all fields are empty
98
                    if (val === "null" || val == null) {
99
                        createCertificateApp.selectedCAData = {
100
                            CN: "",
101
                            C:  "",
102
                            L:  "",
103
                            ST: "",
104
                            O:  "",
105
                            OU: "",
106
                            emailAddress: ""
107
                        };
108
                    }
109
                    // a CA is selected - get CA's details and display them
110
                    else {
111
                        axios.get(API_URL + "certificates/" + val + "/details")
112
                            .then(function (response) {
113
                                if (response.data["success"]) {
114
                                    createCertificateApp.selectedCAData = response.data["data"]["subject"];
115
                                }
116
                                else
117
                                    console.log("Error occurred while fetching CA details");
118
                            })
119
                            .catch(function (error) {
120
                                console.log(error);
121
                            });
122
                    }
123
                }
124
            }
125
        });
126

    
127
    // Initialize available CA select values
128
    axios.get(API_URL+"certificates", {
129
            params: {
130
                filtering: {
131
                    CA: true
132
                }
133
            }
134
        })
135
        .then(function (response) {
136
            if (response.data["success"]) {
137
                createCertificateApp.authorities = response.data["data"];
138
            }
139
            else
140
            {
141
                createCertificateApp.authorities = []
142
            }
143
        })
144
        .catch(function (error) {
145
            console.log(error);
146
        });
(7-7/10)