public_key.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. $(document).ready(function() {
  2. OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
  3. if (scheme === 'publickey' && authMechanism === 'publickey::rsa') {
  4. var config = $tr.find('.configuration');
  5. if ($(config).find('[name="public_key_generate"]').length === 0) {
  6. setupTableRow($tr, config);
  7. onCompletion.then(function() {
  8. // If there's no private key, build one
  9. if (0 === $(config).find('[data-parameter="private_key"]').val().length) {
  10. generateKeys($tr);
  11. }
  12. });
  13. }
  14. }
  15. });
  16. $('#externalStorage').on('click', '[name="public_key_generate"]', function(event) {
  17. event.preventDefault();
  18. var tr = $(this).parent().parent();
  19. generateKeys(tr);
  20. });
  21. function setupTableRow(tr, config) {
  22. var selectList = document.createElement('select');
  23. selectList.id = 'keyLength';
  24. var options = [1024, 2048, 4096];
  25. for (var i = 0; i < options.length; i++) {
  26. var option = document.createElement('option');
  27. option.value = options[i];
  28. option.text = options[i];
  29. selectList.appendChild(option);
  30. }
  31. $(config).append(selectList);
  32. $(config).append($(document.createElement('input'))
  33. .addClass('button auth-param')
  34. .attr('type', 'button')
  35. .attr('value', t('files_external', 'Generate keys'))
  36. .attr('name', 'public_key_generate')
  37. );
  38. }
  39. function generateKeys(tr) {
  40. var config = $(tr).find('.configuration');
  41. var keyLength = config.find('#keyLength').val();
  42. $.post(OC.filePath('files_external', 'ajax', 'public_key.php'), {
  43. keyLength: keyLength
  44. }, function(result) {
  45. if (result && result.status === 'success') {
  46. $(config).find('[data-parameter="public_key"]').val(result.data.public_key).keyup();
  47. $(config).find('[data-parameter="private_key"]').val(result.data.private_key);
  48. OCA.External.Settings.mountConfig.saveStorageConfig(tr, function() {
  49. // Nothing to do
  50. });
  51. } else {
  52. OC.dialogs.alert(result.data.message, t('files_external', 'Error generating key pair') );
  53. }
  54. });
  55. }
  56. });