auth.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * Original:
  6. * @author Frank Karlitschek
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  8. *
  9. * Adapted:
  10. * @author Michiel de Jong, 2012
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  14. * License as published by the Free Software Foundation; either
  15. * version 3 of the License, or any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public
  23. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. header("X-Frame-Options: Sameorigin");
  27. OCP\App::checkAppEnabled('remoteStorage');
  28. require_once('Sabre/autoload.php');
  29. require_once('lib_remoteStorage.php');
  30. require_once('oauth_ro_auth.php');
  31. ini_set('default_charset', 'UTF-8');
  32. #ini_set('error_reporting', '');
  33. @ob_clean();
  34. foreach($_GET as $k => $v) {
  35. if($k=='userid'){
  36. $userId=$v;
  37. } else if($k=='redirect_uri'){
  38. $appUrlParts=explode('/', $v);
  39. $appUrl = htmlentities($appUrlParts[2]);//TODO: check if this is equal to client_id
  40. } else if($k=='scope'){
  41. $categories=htmlentities($v);
  42. }
  43. }
  44. $currUser = OCP\USER::getUser();
  45. if($userId && $appUrl && $categories) {
  46. if($currUser == $userId) {
  47. if(isset($_POST['allow'])) {
  48. //TODO: check if this can be faked by editing the cookie in firebug!
  49. $token=OC_remoteStorage::createCategories($appUrl, $categories);
  50. header('Location: '.$_GET['redirect_uri'].'#access_token='.$token.'&token_type=bearer');
  51. } else if($existingToken = OC_remoteStorage::getTokenFor($appUrl, $categories)) {
  52. header('Location: '.$_GET['redirect_uri'].'#access_token='.$existingToken.'&token_type=bearer');
  53. } else {
  54. //params ok, logged in ok, but need to click Allow still:
  55. $appUrlParts = explode('/', $_GET['redirect_uri']);
  56. $host = $appUrlParts[2];
  57. $categories = explode(',', $_GET['scope']);
  58. OCP\Util::addStyle('', 'auth');
  59. OCP\Template::printGuestPage('remoteStorage', 'auth', array(
  60. 'host' => $host,
  61. 'categories' => $categories,
  62. ));
  63. }//end 'need to click Allow still'
  64. } else {//login not ok
  65. if($currUser) {
  66. die('You are logged in as '.$currUser.' instead of '.htmlentities($userId));
  67. } else {
  68. // this will display the login page for us
  69. OCP\Util::checkLoggedIn();
  70. }
  71. }
  72. } else {//params not ok
  73. die('please use e.g. '.OCP\Util::linkTo('remoteStorage', 'auth.php').'?userid=admin&redirect_uri=http://host/path&scope=...');
  74. }