oauth.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright (c) 2012, Tom Needham <tom@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or later.
  5. * See the COPYING-README file.
  6. */
  7. require_once('../lib/base.php');
  8. // Logic
  9. $operation = isset($_GET['operation']) ? $_GET['operation'] : '';
  10. $server = OC_OAuth_server::init();
  11. switch($operation){
  12. case 'register':
  13. // Here external apps can register with an ownCloud
  14. if(empty($_GET['name']) || empty($_GET['url'])) {
  15. // Invalid request
  16. echo 401;
  17. } else {
  18. $callbacksuccess = empty($_GET['callback_success']) ? null : $_GET['callback_success'];
  19. $callbackfail = empty($_GET['callback_fail']) ? null : $_GET['callback_fail'];
  20. $consumer = OC_OAuth_Server::register_consumer($_GET['name'], $_GET['url'], $callbacksuccess, $callbackfail);
  21. echo 'Registered consumer successfully! </br></br>Key: ' . $consumer->key . '</br>Secret: ' . $consumer->secret;
  22. }
  23. break;
  24. case 'request_token':
  25. try {
  26. $request = OAuthRequest::from_request();
  27. $token = $server->get_request_token($request);
  28. echo $token;
  29. } catch (OAuthException $exception) {
  30. OC_Log::write('OC_OAuth_Server', $exception->getMessage(), OC_LOG::ERROR);
  31. echo $exception->getMessage();
  32. }
  33. break;
  34. case 'authorise';
  35. OC_API::checkLoggedIn();
  36. // Example
  37. $consumer = array(
  38. 'name' => 'Firefox Bookmark Sync',
  39. 'scopes' => array('ookmarks'),
  40. );
  41. // Check that the scopes are real and installed
  42. $apps = OC_App::getEnabledApps();
  43. $notfound = array();
  44. foreach($consumer['scopes'] as $requiredapp){
  45. // App scopes are in this format: app_$appname
  46. $requiredapp = end(explode('_', $requiredapp));
  47. if(!in_array($requiredapp, $apps)) {
  48. $notfound[] = $requiredapp;
  49. }
  50. }
  51. if(!empty($notfound)) {
  52. // We need more apps :( Show error
  53. if(count($notfound)==1) {
  54. $message = 'requires that you have an extra app installed on your ownCloud. Please contact your ownCloud administrator and ask them to install the app below.';
  55. } else {
  56. $message = 'requires that you have some extra apps installed on your ownCloud. Please contract your ownCloud administrator and ask them to install the apps below.';
  57. }
  58. $t = new OC_Template('settings', 'oauth-required-apps', 'guest');
  59. OC_Util::addStyle('settings', 'oauth');
  60. $t->assign('requiredapps', $notfound);
  61. $t->assign('consumer', $consumer);
  62. $t->assign('message', $message);
  63. $t->printPage();
  64. } else {
  65. $t = new OC_Template('settings', 'oauth', 'guest');
  66. OC_Util::addStyle('settings', 'oauth');
  67. $t->assign('consumer', $consumer);
  68. $t->printPage();
  69. }
  70. break;
  71. case 'access_token';
  72. try {
  73. $request = OAuthRequest::from_request();
  74. $token = $server->fetch_access_token($request);
  75. echo $token;
  76. } catch (OAuthException $exception) {
  77. OC_Log::write('OC_OAuth_Server', $exception->getMessage(), OC_LOG::ERROR);
  78. echo $exception->getMessage();
  79. }
  80. break;
  81. default:
  82. // Something went wrong, we need an operation!
  83. OC_Response::setStatus(400);
  84. break;
  85. }