You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.9 KiB
JavaScript

8 years ago
$(function () {
8 years ago
var error = $('<div class="alert alert-danger" role="alert"></div>');
8 years ago
$('.magic-csr').click(function () {
8 years ago
var name = $('#name').val();
8 years ago
if (name.length === 0) {
$('h2').after(error.text("Name can't be empty"));
return;
}
8 years ago
if (!/^[A-Za-z0-9\-]+$/.test(name)) {
$('h2').after(error.text("Only alphanumeric and - allowed in name"));
8 years ago
return;
}
8 years ago
var user = $('.user').text();
8 years ago
var keys = forge.pki.rsa.generateKeyPair(1024);
var csr = forge.pki.createCertificationRequest();
8 years ago
var commonName = name + '.' + user;
8 years ago
csr.publicKey = keys.publicKey;
csr.setSubject([{
name: 'commonName',
8 years ago
value: commonName
8 years ago
}]);
csr.sign(keys.privateKey);
8 years ago
var csrPem = forge.pki.certificationRequestToPem(csr);
8 years ago
8 years ago
var newCertParams = {
csr: csrPem,
8 years ago
name: name
8 years ago
};
var keyPem = "";
if ($('#wantsPassword').prop('checked')) {
keyPem = forge.pki.encryptRsaPrivateKey(keys.privateKey, $('#password').val());
newCertParams.key = keyPem;
} else {
keyPem = forge.pki.privateKeyToPem(keys.privateKey);
}
$.post('/panel/certificates/new', newCertParams, function (data) {
8 years ago
if (data.success) {
var zip = new JSZip();
8 years ago
zip.file(commonName + '.key', keyPem);
8 years ago
for(var file in data.zip) {
zip.file(file, data.zip[file]);
}
8 years ago
8 years ago
var content = zip.generate({type:"blob"});
saveAs(content, commonName + '-vpn.zip');
location.href = '/panel';
} else {
$('h2').after(error.text(data.error));
}
8 years ago
}, 'json');
8 years ago
});
8 years ago
$("#wantsPassword").change(function () {
$('#password, #saveOnline').prop('disabled', !this.checked);
});
8 years ago
});