Projekt

Obecné

Profil

Stáhnout (6.22 KB) Statistiky
| Větev: | Tag: | Revize:
1
// 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
            },
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
    async 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
        // Initialize available CA select values
51
        try {
52
            const response = await axios.get(API_URL + "certificates", {
53
                params: {
54
                    filtering: {
55
                        CA: true
56
                    }
57
                }
58
            });
59
            if (response.data["success"]) {
60
                createCertificateApp.authorities = response.data["data"];
61
            } else {
62
                createCertificateApp.authorities = []
63
            }
64
        } catch (error) {
65
            console.log(error);
66
        }
67
    },
68
    methods: {
69
        showError: function (message) {
70
            document.body.scrollTop = 0;
71
            document.documentElement.scrollTop = 0;
72
            this.errorMessage = message;
73
        },
74
        // handle certificate creation request
75
        onCreateCertificate: async function () {
76
            // validate input data
77
            // - validate if subject CN is filled in
78
            if (!this.isSelfSigned && this.certificateData.CA == null) {
79
                this.showError("Issuer must be selected or 'Self-signed' option must be checked!")
80
                return;
81
            }
82
            if (this.certificateData.subject.CN === "") {
83
                this.showError("CN field must be filled in!")
84
                this.invalidCN = true;
85
                return;
86
            }
87
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
88
            try {
89
                // create a deep copy of the certificate dataa
90
                var certificateDataCopy = JSON.parse(JSON.stringify(this.certificateData));
91
                certificateDataCopy.usage = [];
92

    
93
                // convert usage dictionary to list
94
                if (this.certificateData.usage.CA) certificateDataCopy.usage.push("CA");
95
                if (this.certificateData.usage.digitalSignature) certificateDataCopy.usage.push("digitalSignature");
96
                if (this.certificateData.usage.authentication) certificateDataCopy.usage.push("authentication");
97
                if (this.certificateData.usage.SSL) certificateDataCopy.usage.push("SSL");
98

    
99
                // call RestAPI endpoint
100
                const response = await axios.post(API_URL + "certificates", certificateDataCopy);
101
                if (response.data["success"]) {
102
                    window.location.href = "/static/index.html?success=Certificate+successfully+created";
103
                }
104
                // on error display server response message
105
                else {
106
                    createCertificateApp.showError(response.data["data"]);
107
                }
108
            } catch (error) {
109
                this.showError("An error occurred while creating a certificate. For more details, see console.")
110
                console.log(error);
111
            }
112
        }
113
    },
114
    // data watches
115
    watch: {
116
        authorities: function (val, oldVal) {
117
            this.isSelfSigned = val.length === 0;
118
        },
119
        isSelfSigned: function (val, oldVal) {
120
            if (val) {
121
                this.certificateData.CA = null;
122
                this.certificateData.usage.CA = true;
123
            } else {
124
                this.certificateData.usage.CA = false;
125
            }
126
        },
127
        // if the selected CA is changed, the Issuer input fileds must be filled in
128
        'certificateData.validityDays': function (val, oldVal) {
129
            var endDate = new Date(new Date().getTime() + (val * 24 * 60 * 60 * 1000));
130
            this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
131
        },
132
        'certificateData.subject.CN': function (val, oldVal) {
133
            if (val !== '') this.invalidCN = false;
134
        },
135
        'certificateData.CA': async function (val, oldVal) {
136
            // self-signed certificate - all fields are empty
137
            if (val === "null" || val == null) {
138
                createCertificateApp.selectedCAData = {
139
                    CN: "",
140
                    C: "",
141
                    L: "",
142
                    ST: "",
143
                    O: "",
144
                    OU: "",
145
                    emailAddress: ""
146
                };
147
            }
148
            // a CA is selected - get CA's details and display them
149
            else {
150
                try {
151
                    const response = await axios.get(API_URL + "certificates/" + val + "/details");
152
                    if (response.data["success"])
153
                        createCertificateApp.selectedCAData = response.data["data"]["subject"];
154
                    else
155
                        console.log("Error occurred while fetching CA details");
156
                } catch (error) {
157
                    console.log(error);
158
                }
159
            }
160
        }
161
    }
162
});
(8-8/11)