Projekt

Obecné

Profil

Stáhnout (5.61 KB) Statistiky
| Větev: | Tag: | Revize:
1 7a34cbd3 Jan Pašek
// VUE instance of certificate creation page
2
var createCertificateApp = new Vue({
3
    el: "#create-certificate-content",
4
    data: {
5
        notBefore: "",
6
        notAfter: "",
7
        isSelfSigned: false,
8
        invalidCN: 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 9cac7fd4 Jan Pašek
            },
32 7a34cbd3 Jan Pašek
            validityDays: 30,
33
            usage: {
34
                CA: false,
35
                authentication: false,
36
                digitalSignature: false,
37
                SSL: false
38 71d8054e Jan Pašek
            },
39 7a34cbd3 Jan Pašek
            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 b556270c Jan Pašek
50 7a34cbd3 Jan Pašek
        // Initialize available CA select values
51
        axios.get(API_URL + "certificates", {
52 b556270c Jan Pašek
            params: {
53
                filtering: {
54
                    CA: true
55
                }
56
            }
57
        })
58 7a34cbd3 Jan Pašek
            .then(function (response) {
59
                if (response.data["success"]) {
60
                    createCertificateApp.authorities = response.data["data"];
61
                } else {
62
                    createCertificateApp.authorities = []
63
                }
64
            })
65
            .catch(function (error) {
66
                console.log(error);
67
            });
68
    },
69
    methods: {
70
        showError: function (message) {
71
            document.body.scrollTop = 0;
72
            document.documentElement.scrollTop = 0;
73
            this.errorMessage = message;
74
        },
75
        // handle certificate creation request
76
        onCreateCertificate: function () {
77
            // validate input data
78
            // - validate if subject CN is filled in
79
            if (!this.isSelfSigned && this.certificateData.CA == null) {
80
                this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
81
                return;
82 a3b708c2 Jan Pašek
            }
83 7a34cbd3 Jan Pašek
            if (this.certificateData.subject.CN === "") {
84
                this.showError("CN field must be filled in!")
85
                this.invalidCN = true;
86
                return;
87 a857e1ac Jan Pašek
            }
88 7a34cbd3 Jan Pašek
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
89
            axios.post(API_URL + "certificates", this.certificateData)
90
                .then(function (response) {
91
                    // on success return to index page
92
                    if (response.data["success"]) {
93
                        window.location.href = "/static/index.html?success=Certificate+successfully+created";
94
                    }
95
                    // on error display server response message
96
                    else {
97
                        createCertificateApp.showError(response.data["data"]);
98
                    }
99
                })
100
                .catch(function (error) {
101
                    console.log(error);
102
                });
103
        }
104
    },
105
    // data watches
106
    watch: {
107
        authorities: function (val, oldVal) {
108
            this.isSelfSigned = val.length === 0;
109
        },
110
        isSelfSigned: function (val, oldVal) {
111
            if (val) {
112
                this.certificateData.CA = null;
113
                this.certificateData.usage.CA = true;
114
            } else {
115
                this.certificateData.usage.CA = false;
116
            }
117
        },
118
        // if the selected CA is changed, the Issuer input fileds must be filled in
119
        'certificateData.validityDays': function (val, oldVal) {
120
            var endDate = new Date(new Date().getTime() + (val * 24 * 60 * 60 * 1000));
121
            this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
122
        },
123
        'certificateData.subject.CN': function (val, oldVal) {
124
            if (val !== '') this.invalidCN = false;
125
        },
126
        'certificateData.CA': function (val, oldVal) {
127
            // self-signed certificate - all fields are empty
128
            if (val === "null" || val == null) {
129
                createCertificateApp.selectedCAData = {
130
                    CN: "",
131
                    C: "",
132
                    L: "",
133
                    ST: "",
134
                    O: "",
135
                    OU: "",
136
                    emailAddress: ""
137
                };
138
            }
139
            // a CA is selected - get CA's details and display them
140
            else {
141
                axios.get(API_URL + "certificates/" + val + "/details")
142
                    .then(function (response) {
143
                        if (response.data["success"]) {
144
                            createCertificateApp.selectedCAData = response.data["data"]["subject"];
145
                        } else
146
                            console.log("Error occurred while fetching CA details");
147
                    })
148
                    .catch(function (error) {
149
                        console.log(error);
150
                    });
151
            }
152
        }
153
    }
154
});