oauth2.js 3.5 KB

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