This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
jscsrgen.js
125 lines (125 loc) · 3.57 KB
/
jscsrgen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Generated by CoffeeScript 1.8.0
$(function() {
var btnDownload, btnGenerate, contentCSR, downloadCSRBundle, form, formatCountry, generate, input, pem, runnable, sanitizeCommonName, showDone, txtStatus;
form = $('#formCSR');
txtStatus = $('#status');
btnGenerate = $('#btnGenerate');
btnDownload = $('#btnDownload');
contentCSR = $('#contentCSR');
input = $(".form-control", form);
pem = {};
runnable = typeof Worker !== void 0 && Blob !== void 0;
if (runnable) {
txtStatus.attr('class', 'alert alert-info').html('<p>Ready to generate your CSR.</p>');
} else {
txtStatus.attr('class', 'alert alert-danger').html('<p>Missing support of <strong>WebWorker</strong> or <strong>Blob</strong>, unable to generate CSR.</p>');
input.attr("disabled", false);
btnGenerate.attr('disabled', true);
return;
}
formatCountry = function(country) {
if (!country.id) {
return country.text;
}
return ("<strong class=\"monospace\">" + country.id + "</strong> ") + country.text;
};
$("#countryName").select2({
formatResult: formatCountry,
formatSelection: formatCountry,
esacpeMarkup: function(m) {
return m;
}
});
sanitizeCommonName = function(cn) {
return cn.replace(/^http:\/\//, '').replace(/^https:\/\//, '').replace(/\./g, '_').replace(/^\*/, 'star').replace(/[^a-zA-Z0-9_\-]+/g, '');
};
downloadCSRBundle = function() {
var cn, content, zip;
cn = sanitizeCommonName($('#commonName').val());
zip = new JSZip();
zip.file(cn + ".key", pem["private"]);
zip.file(cn + ".csr", pem.csr);
content = zip.generate({
type: 'blob'
});
return saveAs(content, cn + ".zip");
};
showDone = function() {
contentCSR.text(pem.csr);
return $('#modalDone').on('hide.bs.modal', function() {
txtStatus.attr('class', 'alert alert-info').html('<p>Ready to generate your CSR.</p>');
input.attr("disabled", false);
pem = {};
return contentCSR.text("");
}).modal();
};
generate = function() {
var worker;
input.each(function() {
var self;
return self = $(this);
});
worker = new Worker("worker.js");
worker.onmessage = function(e) {
if (!e.data) {
return;
}
switch (e.data.type) {
case 'status':
return txtStatus.html("<p>" + e.data.message + "</p>");
case 'done':
txtStatus.html("<p>Done.</p>");
return showDone();
case 'private':
return pem["private"] = e.data.pem;
case 'csr':
return pem.csr = e.data.pem;
}
};
return worker.postMessage({
type: "start",
workload: (function() {
var ret;
ret = {};
input.each(function() {
var self;
self = $(this);
return ret[self.attr('id')] = self.val().trim();
});
return ret;
})()
});
};
btnDownload.click(function(e) {
downloadCSRBundle();
return e.preventDefault();
});
input.change(function() {
var self;
self = $(this);
if (self.val().trim() === "") {
return self.parent().addClass('has-warning');
} else {
return self.parent().removeClass('has-warning');
}
});
return form.submit(function(e) {
var pass;
e.preventDefault();
pass = true;
input.each(function() {
var self;
self = $(this);
if (self.val().trim() === "") {
self.parent().addClass('has-warning');
self.focus();
return pass = false;
}
});
if (!pass) {
return;
}
input.attr("disabled", true);
return generate();
});
});