oauth1.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. require_once __DIR__ . '/../3rdparty/Dropbox/autoload.php';
  28. OCP\JSON::checkAppEnabled('files_external');
  29. OCP\JSON::checkLoggedIn();
  30. OCP\JSON::callCheck();
  31. $l = \OC::$server->getL10N('files_external');
  32. // FIXME: currently hard-coded to Dropbox OAuth
  33. if (isset($_POST['app_key']) && isset($_POST['app_secret'])) {
  34. $oauth = new Dropbox_OAuth_Curl((string)$_POST['app_key'], (string)$_POST['app_secret']);
  35. if (isset($_POST['step'])) {
  36. switch ($_POST['step']) {
  37. case 1:
  38. try {
  39. if (isset($_POST['callback'])) {
  40. $callback = (string)$_POST['callback'];
  41. } else {
  42. $callback = null;
  43. }
  44. $token = $oauth->getRequestToken();
  45. OCP\JSON::success(array('data' => array('url' => $oauth->getAuthorizeUrl($callback),
  46. 'request_token' => $token['token'],
  47. 'request_token_secret' => $token['token_secret'])));
  48. } catch (Exception $exception) {
  49. OCP\JSON::error(array('data' => array('message' =>
  50. $l->t('Fetching request tokens failed. Verify that your app key and secret are correct.'))
  51. ));
  52. }
  53. break;
  54. case 2:
  55. if (isset($_POST['request_token']) && isset($_POST['request_token_secret'])) {
  56. try {
  57. $oauth->setToken((string)$_POST['request_token'], (string)$_POST['request_token_secret']);
  58. $token = $oauth->getAccessToken();
  59. OCP\JSON::success(array('access_token' => $token['token'],
  60. 'access_token_secret' => $token['token_secret']));
  61. } catch (Exception $exception) {
  62. OCP\JSON::error(array('data' => array('message' =>
  63. $l->t('Fetching access tokens failed. Verify that your app key and secret are correct.'))
  64. ));
  65. }
  66. }
  67. break;
  68. }
  69. }
  70. } else {
  71. OCP\JSON::error(array('data' => array('message' => $l->t('Please provide a valid app key and secret.'))));
  72. }