1
0

oauth1.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. window.addEventListener('DOMContentLoaded', function() {
  7. function displayGranted($tr) {
  8. $tr.find('.configuration input.auth-param').attr('disabled', 'disabled').addClass('disabled-success');
  9. }
  10. OCA.Files_External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
  11. if (authMechanism === 'oauth1::oauth1') {
  12. var config = $tr.find('.configuration');
  13. config.append($(document.createElement('input'))
  14. .addClass('button auth-param')
  15. .attr('type', 'button')
  16. .attr('value', t('files_external', 'Grant access'))
  17. .attr('name', 'oauth1_grant')
  18. );
  19. onCompletion.then(function() {
  20. var configured = $tr.find('[data-parameter="configured"]');
  21. if ($(configured).val() == 'true') {
  22. displayGranted($tr);
  23. } else {
  24. var app_key = $tr.find('.configuration [data-parameter="app_key"]').val();
  25. var app_secret = $tr.find('.configuration [data-parameter="app_secret"]').val();
  26. if (app_key != '' && app_secret != '') {
  27. var pos = window.location.search.indexOf('oauth_token') + 12;
  28. var token = $tr.find('.configuration [data-parameter="token"]');
  29. if (pos != -1 && window.location.search.substr(pos, $(token).val().length) == $(token).val()) {
  30. var token_secret = $tr.find('.configuration [data-parameter="token_secret"]');
  31. var statusSpan = $tr.find('.status span');
  32. statusSpan.removeClass();
  33. statusSpan.addClass('waiting');
  34. $.post(OC.filePath('files_external', 'ajax', 'oauth1.php'), { step: 2, app_key: app_key, app_secret: app_secret, request_token: $(token).val(), request_token_secret: $(token_secret).val() }, function(result) {
  35. if (result && result.status == 'success') {
  36. $(token).val(result.access_token);
  37. $(token_secret).val(result.access_token_secret);
  38. $(configured).val('true');
  39. OCA.Files_External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
  40. if (status) {
  41. displayGranted($tr);
  42. }
  43. });
  44. } else {
  45. OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring OAuth1'));
  46. }
  47. });
  48. }
  49. }
  50. }
  51. });
  52. }
  53. });
  54. $('#externalStorage').on('click', '[name="oauth1_grant"]', function(event) {
  55. event.preventDefault();
  56. var tr = $(this).parent().parent();
  57. var app_key = $(this).parent().find('[data-parameter="app_key"]').val();
  58. var app_secret = $(this).parent().find('[data-parameter="app_secret"]').val();
  59. if (app_key != '' && app_secret != '') {
  60. var configured = $(this).parent().find('[data-parameter="configured"]');
  61. var token = $(this).parent().find('[data-parameter="token"]');
  62. var token_secret = $(this).parent().find('[data-parameter="token_secret"]');
  63. $.post(OC.filePath('files_external', 'ajax', 'oauth1.php'), { step: 1, app_key: app_key, app_secret: app_secret, callback: location.protocol + '//' + location.host + location.pathname }, function(result) {
  64. if (result && result.status == 'success') {
  65. $(configured).val('false');
  66. $(token).val(result.data.request_token);
  67. $(token_secret).val(result.data.request_token_secret);
  68. OCA.Files_External.Settings.mountConfig.saveStorageConfig(tr, function() {
  69. window.location = result.data.url;
  70. });
  71. } else {
  72. OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring OAuth1'));
  73. }
  74. });
  75. } else {
  76. OC.dialogs.alert(
  77. t('files_external', 'Please provide a valid app key and secret.'),
  78. t('files_external', 'Error configuring OAuth1')
  79. );
  80. }
  81. });
  82. });