1
0

oauth2.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. window.addEventListener('DOMContentLoaded', function() {
  2. function displayGranted($tr) {
  3. $tr.find('.configuration input.auth-param').attr('disabled', 'disabled').addClass('disabled-success');
  4. }
  5. OCA.Files_External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
  6. if (authMechanism === 'oauth2::oauth2') {
  7. var config = $tr.find('.configuration');
  8. config.append($(document.createElement('input'))
  9. .addClass('button auth-param')
  10. .attr('type', 'button')
  11. .attr('value', t('files_external', 'Grant access'))
  12. .attr('name', 'oauth2_grant')
  13. );
  14. onCompletion.then(function() {
  15. var configured = $tr.find('[data-parameter="configured"]');
  16. if ($(configured).val() == 'true') {
  17. displayGranted($tr);
  18. } else {
  19. var client_id = $tr.find('.configuration [data-parameter="client_id"]').val();
  20. var client_secret = $tr.find('.configuration [data-parameter="client_secret"]')
  21. .val();
  22. if (client_id != '' && client_secret != '') {
  23. var params = {};
  24. window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
  25. params[key] = value;
  26. });
  27. if (params['code'] !== undefined) {
  28. var token = $tr.find('.configuration [data-parameter="token"]');
  29. var statusSpan = $tr.find('.status span');
  30. statusSpan.removeClass();
  31. statusSpan.addClass('waiting');
  32. $.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
  33. {
  34. step: 2,
  35. client_id: client_id,
  36. client_secret: client_secret,
  37. redirect: location.protocol + '//' + location.host + location.pathname,
  38. code: params['code'],
  39. }, function(result) {
  40. if (result && result.status == 'success') {
  41. $(token).val(result.data.token);
  42. $(configured).val('true');
  43. OCA.Files_External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
  44. if (status) {
  45. displayGranted($tr);
  46. }
  47. });
  48. } else {
  49. OC.dialogs.alert(result.data.message,
  50. t('files_external', 'Error configuring OAuth2')
  51. );
  52. }
  53. }
  54. );
  55. }
  56. }
  57. }
  58. });
  59. }
  60. });
  61. $('#externalStorage').on('click', '[name="oauth2_grant"]', function(event) {
  62. event.preventDefault();
  63. var tr = $(this).parent().parent();
  64. var configured = $(this).parent().find('[data-parameter="configured"]');
  65. var client_id = $(this).parent().find('[data-parameter="client_id"]').val();
  66. var client_secret = $(this).parent().find('[data-parameter="client_secret"]').val();
  67. if (client_id != '' && client_secret != '') {
  68. var token = $(this).parent().find('[data-parameter="token"]');
  69. $.post(OC.filePath('files_external', 'ajax', 'oauth2.php'),
  70. {
  71. step: 1,
  72. client_id: client_id,
  73. client_secret: client_secret,
  74. redirect: location.protocol + '//' + location.host + location.pathname,
  75. }, function(result) {
  76. if (result && result.status == 'success') {
  77. $(configured).val('false');
  78. $(token).val('false');
  79. OCA.Files_External.Settings.mountConfig.saveStorageConfig(tr, function(status) {
  80. window.location = result.data.url;
  81. });
  82. } else {
  83. OC.dialogs.alert(result.data.message,
  84. t('files_external', 'Error configuring OAuth2')
  85. );
  86. }
  87. }
  88. );
  89. }
  90. });
  91. });