public_key.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. $(document).ready(function() {
  2. OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
  3. if (scheme === 'publickey') {
  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. $(config).append($(document.createElement('input'))
  23. .addClass('button auth-param')
  24. .attr('type', 'button')
  25. .attr('value', t('files_external', 'Generate keys'))
  26. .attr('name', 'public_key_generate')
  27. );
  28. }
  29. function generateKeys(tr) {
  30. var config = $(tr).find('.configuration');
  31. $.post(OC.filePath('files_external', 'ajax', 'public_key.php'), {}, function(result) {
  32. if (result && result.status === 'success') {
  33. $(config).find('[data-parameter="public_key"]').val(result.data.public_key).keyup();
  34. $(config).find('[data-parameter="private_key"]').val(result.data.private_key);
  35. OCA.External.Settings.mountConfig.saveStorageConfig(tr, function() {
  36. // Nothing to do
  37. });
  38. } else {
  39. OC.dialogs.alert(result.data.message, t('files_external', 'Error generating key pair') );
  40. }
  41. });
  42. }
  43. });