Projekt

Obecné

Profil

Stáhnout (5.42 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 ac94bd92 Jan Pašek
    async mounted() {
46 7a34cbd3 Jan Pašek
        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 ac94bd92 Jan Pašek
        try {
52
            const response = await axios.get(API_URL + "certificates", {
53
                params: {
54
                    filtering: {
55
                        CA: true
56
                    }
57 7a34cbd3 Jan Pašek
                }
58
            });
59 ac94bd92 Jan Pašek
            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 7a34cbd3 Jan Pašek
    },
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 ac94bd92 Jan Pašek
        onCreateCertificate: async function () {
76 7a34cbd3 Jan Pašek
            // 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 a3b708c2 Jan Pašek
            }
82 7a34cbd3 Jan Pašek
            if (this.certificateData.subject.CN === "") {
83
                this.showError("CN field must be filled in!")
84
                this.invalidCN = true;
85
                return;
86 a857e1ac Jan Pašek
            }
87 7a34cbd3 Jan Pašek
            this.certificateData.validityDays = parseInt(this.certificateData.validityDays);
88 ac94bd92 Jan Pašek
            try {
89
                const response = await axios.post(API_URL + "certificates", this.certificateData);
90
                if (response.data["success"]) {
91
                    window.location.href = "/static/index.html?success=Certificate+successfully+created";
92
                }
93
                // on error display server response message
94
                else {
95
                    createCertificateApp.showError(response.data["data"]);
96
                }
97
            } catch (error) {
98
                console.log(error);
99
            }
100 7a34cbd3 Jan Pašek
        }
101
    },
102
    // data watches
103
    watch: {
104
        authorities: function (val, oldVal) {
105
            this.isSelfSigned = val.length === 0;
106
        },
107
        isSelfSigned: function (val, oldVal) {
108
            if (val) {
109
                this.certificateData.CA = null;
110
                this.certificateData.usage.CA = true;
111
            } else {
112
                this.certificateData.usage.CA = false;
113
            }
114
        },
115
        // if the selected CA is changed, the Issuer input fileds must be filled in
116
        'certificateData.validityDays': function (val, oldVal) {
117
            var endDate = new Date(new Date().getTime() + (val * 24 * 60 * 60 * 1000));
118
            this.notAfter = endDate.toDateInputValue(); // init notAfter to today + validityDays
119
        },
120
        'certificateData.subject.CN': function (val, oldVal) {
121
            if (val !== '') this.invalidCN = false;
122
        },
123 ac94bd92 Jan Pašek
        'certificateData.CA': async function (val, oldVal) {
124 7a34cbd3 Jan Pašek
            // self-signed certificate - all fields are empty
125
            if (val === "null" || val == null) {
126
                createCertificateApp.selectedCAData = {
127
                    CN: "",
128
                    C: "",
129
                    L: "",
130
                    ST: "",
131
                    O: "",
132
                    OU: "",
133
                    emailAddress: ""
134
                };
135
            }
136
            // a CA is selected - get CA's details and display them
137
            else {
138 ac94bd92 Jan Pašek
                try {
139
                    const response = await axios.get(API_URL + "certificates/" + val + "/details");
140
                    if (response.data["success"])
141
                        createCertificateApp.selectedCAData = response.data["data"]["subject"];
142
                    else
143
                        console.log("Error occurred while fetching CA details");
144
                } catch (error) {
145
                    console.log(error);
146
                }
147 7a34cbd3 Jan Pašek
            }
148
        }
149
    }
150
});