oauth2.php 2.7 KB

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