oauth2.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 === 'oauth2::oauth2') {
  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', 'oauth2_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 client_id = $tr.find('.configuration [data-parameter="client_id"]').val();
  25. var client_secret = $tr.find('.configuration [data-parameter="client_secret"]')
  26. .val();
  27. if (client_id != '' && client_secret != '') {
  28. var params = {};
  29. window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
  30. params[key] = value;
  31. });
  32. if (params['code'] !== undefined) {
  33. var token = $tr.find('.configuration [data-parameter="token"]');
  34. var statusSpan = $tr.find('.status span');
  35. statusSpan.removeClass();
  36. statusSpan.addClass('waiting');
  37. $.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
  38. {
  39. step: 2,
  40. client_id: client_id,
  41. client_secret: client_secret,
  42. redirect: location.protocol + '//' + location.host + location.pathname,
  43. code: params['code'],
  44. }, function(result) {
  45. if (result && result.status == 'success') {
  46. $(token).val(result.data.token);
  47. $(configured).val('true');
  48. OCA.Files_External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
  49. if (status) {
  50. displayGranted($tr);
  51. }
  52. });
  53. } else {
  54. OC.dialogs.alert(result.data.message,
  55. t('files_external', 'Error configuring OAuth2')
  56. );
  57. }
  58. }
  59. );
  60. }
  61. }
  62. }
  63. });
  64. }
  65. });
  66. $('#externalStorage').on('click', '[name="oauth2_grant"]', function(event) {
  67. event.preventDefault();
  68. var tr = $(this).parent().parent();
  69. var configured = $(this).parent().find('[data-parameter="configured"]');
  70. var client_id = $(this).parent().find('[data-parameter="client_id"]').val();
  71. var client_secret = $(this).parent().find('[data-parameter="client_secret"]').val();
  72. if (client_id != '' && client_secret != '') {
  73. var token = $(this).parent().find('[data-parameter="token"]');
  74. $.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
  75. {
  76. step: 1,
  77. client_id: client_id,
  78. client_secret: client_secret,
  79. redirect: location.protocol + '//' + location.host + location.pathname,
  80. }, function(result) {
  81. if (result && result.status == 'success') {
  82. $(configured).val('false');
  83. $(token).val('false');
  84. OCA.Files_External.Settings.mountConfig.saveStorageConfig(tr, function(status) {
  85. window.location = result.data.url;
  86. });
  87. } else {
  88. OC.dialogs.alert(result.data.message,
  89. t('files_external', 'Error configuring OAuth2')
  90. );
  91. }
  92. }
  93. );
  94. }
  95. });
  96. });