oauth1.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. $(document).ready(function() {
  2. function displayGranted($tr) {
  3. $tr.find('.configuration input.auth-param').attr('disabled', 'disabled').addClass('disabled-success');
  4. }
  5. OCA.External.Settings.mountConfig.whenSelectAuthMechanism(function($tr, authMechanism, scheme, onCompletion) {
  6. if (authMechanism === 'oauth1::oauth1') {
  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', 'oauth1_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 app_key = $tr.find('.configuration [data-parameter="app_key"]').val();
  20. var app_secret = $tr.find('.configuration [data-parameter="app_secret"]').val();
  21. if (app_key != '' && app_secret != '') {
  22. var pos = window.location.search.indexOf('oauth_token') + 12;
  23. var token = $tr.find('.configuration [data-parameter="token"]');
  24. if (pos != -1 && window.location.search.substr(pos, $(token).val().length) == $(token).val()) {
  25. var token_secret = $tr.find('.configuration [data-parameter="token_secret"]');
  26. var statusSpan = $tr.find('.status span');
  27. statusSpan.removeClass();
  28. statusSpan.addClass('waiting');
  29. $.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) {
  30. if (result && result.status == 'success') {
  31. $(token).val(result.access_token);
  32. $(token_secret).val(result.access_token_secret);
  33. $(configured).val('true');
  34. OCA.External.Settings.mountConfig.saveStorageConfig($tr, function(status) {
  35. if (status) {
  36. displayGranted($tr);
  37. }
  38. });
  39. } else {
  40. OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring OAuth1'));
  41. }
  42. });
  43. }
  44. }
  45. }
  46. });
  47. }
  48. });
  49. $('#externalStorage').on('click', '[name="oauth1_grant"]', function(event) {
  50. event.preventDefault();
  51. var tr = $(this).parent().parent();
  52. var app_key = $(this).parent().find('[data-parameter="app_key"]').val();
  53. var app_secret = $(this).parent().find('[data-parameter="app_secret"]').val();
  54. if (app_key != '' && app_secret != '') {
  55. var configured = $(this).parent().find('[data-parameter="configured"]');
  56. var token = $(this).parent().find('[data-parameter="token"]');
  57. var token_secret = $(this).parent().find('[data-parameter="token_secret"]');
  58. $.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) {
  59. if (result && result.status == 'success') {
  60. $(configured).val('false');
  61. $(token).val(result.data.request_token);
  62. $(token_secret).val(result.data.request_token_secret);
  63. OCA.External.Settings.mountConfig.saveStorageConfig(tr, function() {
  64. window.location = result.data.url;
  65. });
  66. } else {
  67. OC.dialogs.alert(result.data.message, t('files_external', 'Error configuring OAuth1'));
  68. }
  69. });
  70. } else {
  71. OC.dialogs.alert(
  72. t('files_external', 'Please provide a valid app key and secret.'),
  73. t('files_external', 'Error configuring OAuth1')
  74. );
  75. }
  76. });
  77. });