public_key.js 2.2 KB

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