PageRenderTime 60ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/webservice/oauthv1.php

https://gitlab.com/ImaPotato/Mahara
PHP | 202 lines | 136 code | 17 blank | 49 comment | 16 complexity | 7e904f18c03542eb2099300c8884da46 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, BSD-2-Clause, MIT, GPL-3.0, Apache-2.0
  1. <?php
  2. /**
  3. * OAuth v1 Identity Provider component
  4. *
  5. * @package mahara
  6. * @subpackage auth-webservice
  7. * @author Catalyst IT Ltd
  8. * @author Arjan Scherpenisse <arjan@scherpenisse.net>
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
  10. * @copyright For copyright information on Mahara, please see the README file distributed with this software.
  11. *
  12. */
  13. /**
  14. * @author Arjan Scherpenisse <arjan@scherpenisse.net>
  15. *
  16. * The MIT License
  17. *
  18. * Copyright (c) 2007-2008 Mediamatic Lab
  19. *
  20. * Permission is hereby granted, free of charge, to any person obtaining a copy
  21. * of this software and associated documentation files (the "Software"), to deal
  22. * in the Software without restriction, including without limitation the rights
  23. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  24. * copies of the Software, and to permit persons to whom the Software is
  25. * furnished to do so, subject to the following conditions:
  26. *
  27. * The above copyright notice and this permission notice shall be included in
  28. * all copies or substantial portions of the Software.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  31. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  33. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  35. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  36. * THE SOFTWARE.
  37. */
  38. define('INTERNAL', 1);
  39. define('PUBLIC', 1);
  40. define('XMLRPC', 1);
  41. define('TITLE', '');
  42. global $SESSION, $USER;
  43. // Catch anything that goes wrong in init.php
  44. ob_start();
  45. require(dirname(dirname(__FILE__)) . '/init.php');
  46. $errors = trim(ob_get_contents());
  47. ob_end_clean();
  48. require_once(dirname(__FILE__) . '/lib.php');
  49. if (!webservice_protocol_is_enabled('oauth')) {
  50. header("HTTP/1.0 404 Not Found");
  51. die;
  52. }
  53. // you must use HTTPS as token based auth is a hazzard without it
  54. if (!is_https()) {
  55. header("HTTP/1.0 403 Forbidden - HTTPS must be used");
  56. die;
  57. }
  58. /*
  59. * Always announce XRDS OAuth discovery
  60. */
  61. header('X-XRDS-Location: ' . get_config('wwwroot') . 'webservice/oauthv1/services.xrds');
  62. /*
  63. * Initialize OAuth store
  64. */
  65. require_once(get_config('docroot') . 'webservice/libs/oauth-php/OAuthServer.php');
  66. require_once(get_config('docroot') . 'webservice/libs/oauth-php/OAuthStore.php');
  67. OAuthStore::instance('Mahara');
  68. global $server;
  69. $server = new OAuthServer();
  70. !isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] = null;
  71. // Now - what kind of OAuth interaction are we handling?
  72. if ($_SERVER['PATH_INFO'] == '/request_token') {
  73. $server->requestToken();
  74. exit;
  75. }
  76. else if ($_SERVER['PATH_INFO'] == '/access_token') {
  77. $server->accessToken();
  78. exit;
  79. }
  80. else if ($_SERVER['PATH_INFO'] == '/authorize') {
  81. # logon
  82. require_once('pieforms/pieform.php');
  83. if (!$USER->is_logged_in()) {
  84. $form = new Pieform(auth_get_login_form());
  85. auth_draw_login_page(null, $form);
  86. exit;
  87. }
  88. $rs = null;
  89. try {
  90. $rs = $server->authorizeVerify();
  91. }
  92. catch (OAuthException2 $e) {
  93. header('HTTP/1.1 400 Bad Request');
  94. header('Content-Type: text/plain');
  95. echo "Failed OAuth Request: " . $e->getMessage();
  96. exit();
  97. }
  98. // XXX user must be logged in
  99. // display what is accessing and ask the user to confirm
  100. $form = array(
  101. 'renderer' => 'div',
  102. 'type' => 'div',
  103. 'id' => 'maintable',
  104. 'name' => 'authorise',
  105. 'jsform' => false,
  106. 'successcallback' => 'oauth_authorise_submit',
  107. 'elements' => array(
  108. 'application_uri' => array(
  109. 'title' => get_string('application_title', 'auth.webservice'),
  110. 'value' => '<a href="' . $rs['application_uri'] . '" target="_blank">' . $rs['application_title'] . '</a>',
  111. 'type' => 'html',
  112. ),
  113. 'application_access' => array(
  114. 'value' => get_string('oauth_access', 'auth.webservice'),
  115. 'type' => 'html',
  116. ),
  117. 'instructions' => array(
  118. 'value' => get_string('oauth_instructions', 'auth.webservice') . "<br/><br/>",
  119. 'type' => 'html',
  120. ),
  121. 'submit' => array(
  122. 'type' => 'submitcancel',
  123. 'value' => array(get_string('authorise', 'auth.webservice'), get_string('cancel')),
  124. 'goto' => get_config('wwwroot'),
  125. ),
  126. ),
  127. );
  128. $form = pieform($form);
  129. $smarty = smarty(array(), array('<link rel="stylesheet" type="text/css" href="' . $THEME->get_url('style/webservice.css', false, 'auth/webservice') . '">',));
  130. $smarty->assign('form', $form);
  131. $smarty->assign('PAGEHEADING', get_string('authorise', 'auth.webservice'));
  132. $smarty->display('form.tpl');
  133. exit;
  134. }
  135. else if ($_SERVER['PATH_INFO'] == '/oob') {
  136. // display the verifier token
  137. $verifier = $SESSION->get('oauh_verifier');
  138. $SESSION->set('oauh_verifier', null);
  139. $form = array(
  140. 'renderer' => 'div',
  141. 'type' => 'div',
  142. 'id' => 'maintable',
  143. 'name' => 'authorise',
  144. 'jsform' => false,
  145. 'successcallback' => 'oauth_authorise_submit',
  146. 'elements' => array(
  147. 'instructions' => array(
  148. 'title' => get_string('instructions', 'auth.webservice'),
  149. 'value' => get_string('oobinfo', 'auth.webservice'),
  150. 'type' => 'html',
  151. ),
  152. 'verifier' => array(
  153. 'title' => get_string('verifier', 'auth.webservice'),
  154. 'value' => '<div id="verifier">' . $verifier . '</div>',
  155. 'type' => 'html',
  156. ),
  157. ),
  158. );
  159. $form = pieform($form);
  160. $smarty = smarty(array(), array('<link rel="stylesheet" type="text/css" href="' . $THEME->get_url('style/webservice.css', false, 'auth/webservice') . '">',));
  161. $smarty->assign('form', $form);
  162. $smarty->assign('PAGEHEADING', get_string('oob', 'auth.webservice'));
  163. $smarty->display('form.tpl');
  164. exit;
  165. }
  166. else {
  167. header('HTTP/1.1 500 Internal Server Error');
  168. header('Content-Type: text/plain');
  169. echo "Unknown request";
  170. }
  171. function oauth_authorise_submit(Pieform $form, $values) {
  172. global $server, $USER, $SESSION;
  173. try {
  174. $server->authorizeVerify();
  175. $verifier = $server->authorizeFinish(true, $USER->get('id'));
  176. $SESSION->set('oauh_verifier', $verifier);
  177. redirect('/webservice/oauthv1.php/oob');
  178. }
  179. catch (OAuthException2 $e) {
  180. header('HTTP/1.1 400 Bad Request');
  181. header('Content-Type: text/plain');
  182. echo "Failed OAuth Request: " . $e->getMessage();
  183. }
  184. exit;
  185. }