Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 6c9cb54a

Přidáno uživatelem Jan Pašek před asi 4 roky(ů)

Re #8475 - Added code comments

Zobrazit rozdíly:

static/js/components.js
1

  
2
// Component representing an instance in certificate listing
1 3
Vue.component("certificate-item",{
4
    // certificate = certificate to be displayed by the list item
2 5
    props: ["certificate"],
3 6
    template: "<tr>"+
4 7
                "<td class='align-middle'>{{ certificate.CN }}</td>"+
......
15 18
                "<td class='align-middle'><button v-on:click='onCertificateRootDownload()'>Download chain root</button></td>"+
16 19
            "</tr>",
17 20
    methods: {
21
        // Get the certificate PEM data from the server and downloads it to users computer
18 22
        onCertificateDownload: function () {
19 23
            var id = this.certificate.id;
20 24
            axios.get(API_URL + "certificates/" + id)
......
29 33
                    console.log(error);
30 34
                });
31 35
        },
36
        // Get the certificate's chain of trust in PEM format from the server and downloads it to users computer
32 37
        onCertificateChainDownload: function () {
33 38
            var id = this.certificate.id;
34 39
            axios.get(API_URL + "certificates/" + id + "/chain")
......
43 48
                    console.log(error);
44 49
                });
45 50
        },
51
        // Get the certificate's root CA as PEM data from the server and downloads it to users computer
46 52
        onCertificateRootDownload: function () {
47 53
            var id = this.certificate.id;
48 54
            axios.get(API_URL + "certificates/" + id + "/root")
......
60 66
    }
61 67
});
62 68

  
69
// Component representing an item in select of available CAs
63 70
Vue.component("ca-select-item", {
71
    // Certificate authority to be represented by the option
64 72
    props: ["ca"],
65 73
    template: "<option v-bind:value='ca.id'>{{ ca.CN }}</option>"
66 74
});
static/js/constants.js
1
// API base url - in production it shall be only '/api/'
1 2
const API_URL = "https://virtserver.swaggerhub.com/janpasek97/X509_management/1.0.0/api/";
static/js/create_certificate.js
1

  
2
    // VUE instance of certificate creation page
1 3
    var createCertificateApp = new Vue({
2 4
            el: "#create-certificate-content",
3 5
            data: {
4
                authorities: [{id: 5, CN: "Test"}],
6
                // available certificate authorities
7
                authorities: [],
8
                // data of the selected certificate authorities to be displayed in the form
5 9
                selectedCAData: {
6 10
                    CN: "",
7 11
                    C:  "",
......
11 15
                    OU: "",
12 16
                    emailAddress: ""
13 17
                },
18
                // Data of the new certificate to be created received from the input fields
14 19
                certificateData: {
15 20
                    subject: {
16 21
                        CN: "",
......
32 37
                    CA: null
33 38
                }
34 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
35 42
            mounted () {
36
              this.certificateData.notBefore = new Date().toDateInputValue();
43
              this.certificateData.notBefore = new Date().toDateInputValue(); // init notBefore to current date
37 44
              var endDate = new Date(new Date().getTime()+(30*24*60*60*1000));
38
              this.certificateData.notAfter = endDate.toDateInputValue();
45
              this.certificateData.notAfter = endDate.toDateInputValue(); // init notAfter to notBefore + 30 days
39 46
            },
40 47
            methods: {
48
                // handle certificate creation request
41 49
                onCreateCertificate: function () {
42

  
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
43 62
                    axios.post(API_URL + "certificates", this.certificateData)
44 63
                        .then(function (response) {
64
                            // on success return to index page
45 65
                            if(response.data["success"]) {
46 66
                                alert("Certificate was successfully created.");
47 67
                                window.location.href = "/static/index.html";
48 68
                            }
69
                            // on error display server response message
49 70
                            else {
50 71
                                alert(response.data["data"]);
51 72
                            }
......
55 76
                        });
56 77
                }
57 78
            },
79
            // data watches
58 80
            watch: {
81
                // if the selected CA is changed, the Issuer input fileds must be filled in
59 82
                'certificateData.CA': function (val, oldVal) {
83
                    // self-signed certificate - all fields are empty
60 84
                    if (val === "null" || val == null) {
61 85
                        createCertificateApp.selectedCAData = {
62 86
                            CN: "",
......
68 92
                            emailAddress: ""
69 93
                        };
70 94
                    }
95
                    // a CA is selected - get CA's details and display them
71 96
                    else {
72 97
                        axios.get(API_URL + "certificates/" + val + "/details")
73 98
                            .then(function (response) {
......
85 110
            }
86 111
        });
87 112

  
113
    // Initialize available CA select values
88 114
    axios.get(API_URL+"certificates", {
89 115
            params: {
90 116
                filtering: {
static/js/index.js
1 1

  
2
    // certificate listing app VUE instance
2 3
    var certificateListingApp = new Vue({
3 4
        el: "#certificateListingPage",
4 5
        data: {
6
            // list of all certificates to be displayed in the list
5 7
            certificates: []
6 8
        }
7 9
    });
8 10

  
11
    // download a list of all available certificates and display them in the table
9 12
    axios.get(API_URL + "certificates")
10 13
        .then(function (response) {
11 14
            if (response.data["success"])
......
15 18
        })
16 19
        .catch(function (error) {
17 20
            console.log(error);
18
        });
19

  
20
    function downloadCertificate(id) {
21

  
22
    }
21
        });
static/js/utilities.js
1
/**
2
 * Generate a file to be downloaded by the browser
3
 * @param filename name of the file to be downloaded
4
 * @param text content of the downloaded file
5
 */
1 6
function download(filename, text) {
2
  var element = document.createElement('a');
3
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
4
  element.setAttribute('download', filename);
7
    // first a hidden <a> element is created and download content is assigned
8
    var element = document.createElement('a');
9
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
10
    element.setAttribute('download', filename);
5 11

  
6
  element.style.display = 'none';
7
  document.body.appendChild(element);
12
    element.style.display = 'none';
13
    document.body.appendChild(element);
8 14

  
9
  element.click();
15
    // generate artificial click event to download the file
16
    element.click();
10 17

  
11
  document.body.removeChild(element);
18
    // remove the download link from the webpage
19
    document.body.removeChild(element);
12 20
}
13 21

  
22
// Adds toDateInputValue() method to Date object
23
// toDateInputValue() produces date in YYYY-MM-DD format
14 24
Date.prototype.toDateInputValue = (function() {
15 25
    var local = new Date(this);
16 26
    local.setMinutes(this.getMinutes() - this.getTimezoneOffset());

Také k dispozici: Unified diff