Credential store

This commit is contained in:
fbsanchez 2019-06-25 18:21:23 +02:00
parent 22d936d4f3
commit e1bf12a1fb
2 changed files with 99 additions and 65 deletions

View File

@ -49,16 +49,21 @@ if (! check_acl($config['id_user'], 0, 'PM')) {
// Required files. // Required files.
ui_require_css_file('credential_store'); ui_require_css_file('credential_store');
require_once $config['homedir'].'/include/functions_credential_store.php'; require_once $config['homedir'].'/include/functions_credential_store.php';
require_once $config['homedir'].'/include/functions_io.php';
if (is_ajax()) { if (is_ajax()) {
$draw = get_parameter('draw', 0); $draw = get_parameter('draw', 0);
$filter = get_parameter('filter', []); $filter = get_parameter('filter', []);
$get_key = get_parameter('get_key', 0); $get_key = get_parameter('get_key', 0);
$new_form = get_parameter('new_form', 0);
$new_key = get_parameter('new_key', 0); $new_key = get_parameter('new_key', 0);
$update_key = get_parameter('update_key', 0); $update_key = get_parameter('update_key', 0);
$delete_key = get_parameter('delete_key', 0); $delete_key = get_parameter('delete_key', 0);
if ($new_form) {
echo print_inputs();
exit;
}
if ($delete_key) { if ($delete_key) {
$identifier = get_parameter('identifier', null); $identifier = get_parameter('identifier', null);
@ -130,6 +135,9 @@ if (is_ajax()) {
$values = []; $values = [];
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
$values[$key] = base64_decode($value); $values[$key] = base64_decode($value);
if ($key == 'identifier') {
$values[$key] = preg_replace('/\s+/', '-', trim($values[$key]));
}
} }
$identifier = $values['identifier']; $identifier = $values['identifier'];
@ -318,8 +326,7 @@ try {
// Auxiliar div. // Auxiliar div.
$new = '<div id="new_key" style="display: none"><form id="form_new">'; $new = '<div id="new_key" style="display: none"><form id="form_new">';
$new .= print_inputs(); $new .= '</form></div>';
$new .= '</form> </div>';
$details = '<div id="info_key" style="display: none"><form id="form_update">'; $details = '<div id="info_key" style="display: none"><form id="form_update">';
$details .= '</form></div>'; $details .= '</form></div>';
$aux = '<div id="aux" style="display: none"></div>'; $aux = '<div id="aux" style="display: none"></div>';
@ -386,7 +393,7 @@ echo '</div>';
if (!failed) { if (!failed) {
dt_<?php echo $table_id; ?>.draw(0); dt_<?php echo $table_id; ?>.draw(0);
$(".ui-dialog-content").dialog("close"); $(".ui-dialog-content").dialog("close");
$('.ui-dialog-content').remove(); cleanupDOM();
} else { } else {
$(this).dialog('close'); $(this).dialog('close');
} }
@ -407,7 +414,8 @@ echo '</div>';
text: '<?php echo __('Cancel'); ?>', text: '<?php echo __('Cancel'); ?>',
click: function(e) { click: function(e) {
$(this).dialog('close'); $(this).dialog('close');
$(this).remove(); cleanupDOM();
} }
}, },
{ {
@ -464,7 +472,7 @@ echo '</div>';
text: '<?php echo __('Cancel'); ?>', text: '<?php echo __('Cancel'); ?>',
click: function(e) { click: function(e) {
$(this).dialog('close'); $(this).dialog('close');
$(this).remove(); cleanupDOM();
} }
}, },
{ {
@ -503,6 +511,15 @@ echo '</div>';
}) })
} }
function cleanupDOM() {
$('#div-identifier').empty();
$('#div-product').empty();
$('#div-username').empty();
$('#div-password').empty();
$('#div-extra_1').empty();
$('#div-extra_2').empty();
}
function calculate_inputs() { function calculate_inputs() {
if ($('#product :selected').val() == "CUSTOM") { if ($('#product :selected').val() == "CUSTOM") {
$('#div-username label').text('<?php echo __('Username'); ?>'); $('#div-username label').text('<?php echo __('Username'); ?>');
@ -528,66 +545,82 @@ echo '</div>';
function add_key() { function add_key() {
// Clear form. // Clear form.
$('#form_new :input').each(function() { $('#form_update').empty();
$(this).val('') $('#form_update').html('Loading...');
}); $.ajax({
$('#id_group').val(0); method: 'post',
$('#product').val('CUSTOM'); url: '<?php echo ui_get_full_url('ajax.php', false, false, false); ?>',
data: {
$('#product').on('change', function() { page: 'godmode/groups/credential_store',
calculate_inputs() new_form: 1
});
// Show form.
$('#new_key').dialog({
width: 580,
height: 400,
position: {
my: 'center',
at: 'center',
of: window,
collision: 'fit'
}, },
title: "<?php echo __('Register new key into keystore'); ?>", success: function(data) {
buttons: [ $('#form_new').html(data);
{ $('#id_group').val(0);
class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub upd submit-cancel', // By default AWS.
text: "<?php echo __('Cancel'); ?>", $('#product').val('AWS');
click: function(e) { calculate_inputs();
$(this).dialog('close');
$(this).remove();
}
},
{
class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub ok submit-next',
text: 'OK',
click: function(e) {
var values = {};
$('#form_new :input').each(function() { $('#product').on('change', function() {
values[this.name] = btoa($(this).val()); calculate_inputs()
}); });
$.ajax({ // Show form.
method: 'post', $('#new_key').dialog({
url: '<?php echo ui_get_full_url('ajax.php', false, false, false); ?>', width: 580,
data: { height: 400,
page: 'godmode/groups/credential_store', position: {
new_key: 1, my: 'center',
values: values at: 'center',
}, of: window,
datatype: "json", collision: 'fit'
success: function (data) { },
handle_response(data); title: "<?php echo __('Register new key into keystore'); ?>",
}, buttons: [
error: function(e) { {
handle_response(e); class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub upd submit-cancel',
text: "<?php echo __('Cancel'); ?>",
click: function(e) {
$(this).dialog('close');
cleanupDOM();
} }
}); },
} {
}, class: 'ui-widget ui-state-default ui-corner-all ui-button-text-only sub ok submit-next',
] text: 'OK',
}); click: function(e) {
var values = {};
console.log($('#form_new'));
$('#form_new :input').each(function() {
values[this.name] = btoa($(this).val());
});
$.ajax({
method: 'post',
url: '<?php echo ui_get_full_url('ajax.php', false, false, false); ?>',
data: {
page: 'godmode/groups/credential_store',
new_key: 1,
values: values
},
datatype: "json",
success: function (data) {
handle_response(data);
},
error: function(e) {
handle_response(e);
}
});
}
},
]
});
}
})
} }
$(document).ready(function(){ $(document).ready(function(){

View File

@ -268,6 +268,7 @@ function print_inputs($values=null)
'value' => $values['identifier'], 'value' => $values['identifier'],
'disabled' => (bool) $values['identifier'], 'disabled' => (bool) $values['identifier'],
'return' => true, 'return' => true,
'script' => 'alert(\'puta\')',
] ]
); );
$return .= html_print_input( $return .= html_print_input(
@ -290,9 +291,9 @@ function print_inputs($values=null)
'type' => 'select', 'type' => 'select',
'script' => 'calculate_inputs()', 'script' => 'calculate_inputs()',
'fields' => [ 'fields' => [
'CUSTOM' => __('Custom'), // 'CUSTOM' => __('Custom'),
'AWS' => __('Aws'), 'AWS' => __('Aws'),
'AZURE' => __('Azure'), // 'AZURE' => __('Azure'),
// 'GOOGLE' => __('Google'), // 'GOOGLE' => __('Google'),
], ],
'selected' => $values['product'], 'selected' => $values['product'],