oauth2.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Adam Williamson <awilliam@redhat.com>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. * @author Volkan Gezer <volkangezer@gmail.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. set_include_path(get_include_path().PATH_SEPARATOR.
  31. \OC_App::getAppPath('files_external').'/3rdparty/google-api-php-client/src');
  32. require_once 'Google/autoload.php';
  33. OCP\JSON::checkAppEnabled('files_external');
  34. OCP\JSON::checkLoggedIn();
  35. OCP\JSON::callCheck();
  36. $l = \OC::$server->getL10N('files_external');
  37. // FIXME: currently hard-coded to Google Drive
  38. if (isset($_POST['client_id']) && isset($_POST['client_secret']) && isset($_POST['redirect'])) {
  39. $client = new Google_Client();
  40. $client->setClientId((string)$_POST['client_id']);
  41. $client->setClientSecret((string)$_POST['client_secret']);
  42. $client->setRedirectUri((string)$_POST['redirect']);
  43. $client->setScopes(array('https://www.googleapis.com/auth/drive'));
  44. $client->setApprovalPrompt('force');
  45. $client->setAccessType('offline');
  46. if (isset($_POST['step'])) {
  47. $step = $_POST['step'];
  48. if ($step == 1) {
  49. try {
  50. $authUrl = $client->createAuthUrl();
  51. OCP\JSON::success(array('data' => array(
  52. 'url' => $authUrl
  53. )));
  54. } catch (Exception $exception) {
  55. OCP\JSON::error(array('data' => array(
  56. 'message' => $l->t('Step 1 failed. Exception: %s', array($exception->getMessage()))
  57. )));
  58. }
  59. } else if ($step == 2 && isset($_POST['code'])) {
  60. try {
  61. $token = $client->authenticate((string)$_POST['code']);
  62. OCP\JSON::success(array('data' => array(
  63. 'token' => $token
  64. )));
  65. } catch (Exception $exception) {
  66. OCP\JSON::error(array('data' => array(
  67. 'message' => $l->t('Step 2 failed. Exception: %s', array($exception->getMessage()))
  68. )));
  69. }
  70. }
  71. }
  72. }