nginx-proxy-manager/frontend/js/app/nginx/certificates/form.js

211 lines
8.6 KiB
JavaScript
Raw Normal View History

const _ = require('underscore');
const Mn = require('backbone.marionette');
const App = require('../../main');
const CertificateModel = require('../../../models/certificate');
const template = require('./form.ejs');
require('jquery-serializejson');
require('selectize');
module.exports = Mn.View.extend({
template: template,
className: 'modal-dialog',
2019-01-15 19:12:10 -05:00
max_file_size: 102400,
ui: {
form: 'form',
domain_names: 'input[name="domain_names"]',
buttons: '.modal-footer button',
cancel: 'button.cancel',
save: 'button.save',
other_certificate: '#other_certificate',
other_certificate_label: '#other_certificate_label',
other_certificate_key: '#other_certificate_key',
cloudflare_switch: 'input[name="meta[cloudflare_use]"]',
cloudflare_token: 'input[name="meta[cloudflare_token]"',
cloudflare: '.cloudflare',
other_certificate_key_label: '#other_certificate_key_label',
other_intermediate_certificate: '#other_intermediate_certificate',
2020-08-29 23:24:51 -04:00
other_intermediate_certificate_label: '#other_intermediate_certificate_label'
},
2020-08-29 23:24:51 -04:00
events: {
'change @ui.cloudflare_switch': function() {
let checked = this.ui.cloudflare_switch.prop('checked');
if (checked) {
this.ui.cloudflare_token.prop('required', 'required');
this.ui.cloudflare.show();
} else {
this.ui.cloudflare_token.prop('required', false);
this.ui.cloudflare.hide();
}
},
'click @ui.save': function (e) {
e.preventDefault();
if (!this.ui.form[0].checkValidity()) {
$('<input type="submit">').hide().appendTo(this.ui.form).click().remove();
$(this).removeClass('btn-loading');
return;
}
2018-08-08 02:58:21 -04:00
let view = this;
let data = this.ui.form.serializeJSON();
2018-08-07 06:27:20 -04:00
data.provider = this.model.get('provider');
let domain_err = false;
if (!data.meta.cloudflare_use) {
data.domain_names.split(',').map(function (name) {
if (name.match(/\*/im)) {
domain_err = true;
}
});
}
if (domain_err) {
alert('Cannot request Let\'s Encrypt Certificate for wildcard domains when not using CloudFlare DNS');
return;
}
// Manipulate
2018-08-07 06:27:20 -04:00
if (typeof data.meta !== 'undefined' && typeof data.meta.letsencrypt_agree !== 'undefined') {
data.meta.letsencrypt_agree = !!data.meta.letsencrypt_agree;
}
2020-08-23 08:50:41 -04:00
if (typeof data.meta !== 'undefined' && typeof data.meta.cloudflare_use !== 'undefined') {
data.meta.cloudflare_use = !!data.meta.cloudflare_use;
}
if (typeof data.domain_names === 'string' && data.domain_names) {
data.domain_names = data.domain_names.split(',');
}
2018-08-07 06:27:20 -04:00
let ssl_files = [];
// check files are attached
2018-08-07 06:27:20 -04:00
if (this.model.get('provider') === 'other' && !this.model.hasSslFiles()) {
2018-08-08 02:58:21 -04:00
if (!this.ui.other_certificate[0].files.length || !this.ui.other_certificate[0].files[0].size) {
alert('Certificate file is not attached');
2018-08-07 06:27:20 -04:00
return;
} else {
2018-08-08 02:58:21 -04:00
if (this.ui.other_certificate[0].files[0].size > this.max_file_size) {
2019-01-15 19:11:51 -05:00
alert('Certificate file is too large (> 100kb)');
return;
}
2018-08-08 02:58:21 -04:00
ssl_files.push({name: 'certificate', file: this.ui.other_certificate[0].files[0]});
}
2018-08-08 02:58:21 -04:00
if (!this.ui.other_certificate_key[0].files.length || !this.ui.other_certificate_key[0].files[0].size) {
alert('Certificate key file is not attached');
2018-08-07 06:27:20 -04:00
return;
} else {
2018-08-08 02:58:21 -04:00
if (this.ui.other_certificate_key[0].files[0].size > this.max_file_size) {
2019-01-15 19:11:51 -05:00
alert('Certificate key file is too large (> 100kb)');
2018-08-08 02:58:21 -04:00
return;
}
ssl_files.push({name: 'certificate_key', file: this.ui.other_certificate_key[0].files[0]});
}
if (this.ui.other_intermediate_certificate[0].files.length && this.ui.other_intermediate_certificate[0].files[0].size) {
if (this.ui.other_intermediate_certificate[0].files[0].size > this.max_file_size) {
2019-01-15 19:11:51 -05:00
alert('Intermediate Certificate file is too large (> 100kb)');
return;
}
2018-08-08 02:58:21 -04:00
ssl_files.push({name: 'intermediate_certificate', file: this.ui.other_intermediate_certificate[0].files[0]});
}
}
this.ui.buttons.prop('disabled', true).addClass('btn-disabled');
2020-08-23 09:24:20 -04:00
this.ui.save.addClass('btn-loading');
2018-08-08 02:58:21 -04:00
// compile file data
let form_data = new FormData();
if (view.model.get('provider') && ssl_files.length) {
ssl_files.map(function (file) {
form_data.append(file.name, file.file);
});
}
new Promise(resolve => {
if (view.model.get('provider') === 'other') {
resolve(App.Api.Nginx.Certificates.validate(form_data));
} else {
resolve();
}
})
.then(() => {
return App.Api.Nginx.Certificates.create(data);
})
.then(result => {
view.model.set(result);
// Now upload the certs if we need to
2018-08-08 02:58:21 -04:00
if (view.model.get('provider') === 'other') {
2018-08-07 06:27:20 -04:00
return App.Api.Nginx.Certificates.upload(view.model.get('id'), form_data)
.then(result => {
view.model.set('meta', _.assign({}, view.model.get('meta'), result));
});
}
})
.then(() => {
App.UI.closeModal(function () {
2018-08-08 02:58:21 -04:00
App.Controller.showNginxCertificates();
});
})
.catch(err => {
alert(err.message);
this.ui.buttons.prop('disabled', false).removeClass('btn-disabled');
2020-08-23 09:24:20 -04:00
this.ui.save.removeClass('btn-loading');
});
2020-08-29 23:24:51 -04:00
},
'change @ui.other_certificate_key': function(e){
this.setFileName("other_certificate_key_label", e)
},
'change @ui.other_certificate': function(e){
this.setFileName("other_certificate_label", e)
},
'change @ui.other_intermediate_certificate': function(e){
this.setFileName("other_intermediate_certificate_label", e)
}
},
2020-08-29 23:24:51 -04:00
setFileName(ui, e){
this.getUI(ui).text(e.target.files[0].name)
},
templateContext: {
getLetsencryptEmail: function () {
return typeof this.meta.letsencrypt_email !== 'undefined' ? this.meta.letsencrypt_email : App.Cache.User.get('email');
},
getLetsencryptAgree: function () {
return typeof this.meta.letsencrypt_agree !== 'undefined' ? this.meta.letsencrypt_agree : false;
2020-08-23 08:50:41 -04:00
},
getCloudflareUse: function () {
return typeof this.meta.cloudflare_use !== 'undefined' ? this.meta.cloudflare_use : false;
}
},
onRender: function () {
this.ui.domain_names.selectize({
delimiter: ',',
persist: false,
maxOptions: 15,
create: function (input) {
return {
value: input,
text: input
};
},
2020-08-23 09:24:20 -04:00
createFilter: /^(?:[^.]+\.?)+[^.]$/
});
this.ui.cloudflare.hide();
},
initialize: function (options) {
if (typeof options.model === 'undefined' || !options.model) {
2018-08-07 06:27:20 -04:00
this.model = new CertificateModel.Model({provider: 'letsencrypt'});
}
}
});