openid_test.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * @file
  4. * Dummy OpenID Provider used with SimpleTest.
  5. *
  6. * The provider simply responds positively to all authentication requests. In
  7. * addition to a Provider Endpoint (a URL used for Drupal to communicate with
  8. * the provider using the OpenID Authentication protocol) the module provides
  9. * URLs used by the various discovery mechanisms.
  10. *
  11. * When a user enters an OpenID identity, the Relying Party (in the testing
  12. * scenario, this is the OpenID module) looks up the URL of the Provider
  13. * Endpoint using one of several discovery mechanisms. The Relying Party then
  14. * redirects the user to Provider Endpoint. The provider verifies the user's
  15. * identity and redirects the user back to the Relying Party accompanied by a
  16. * signed message confirming the identity. Before redirecting to a provider for
  17. * the first time, the Relying Party fetches a secret MAC key from the provider
  18. * by doing a direct "associate" HTTP request to the Provider Endpoint. This
  19. * key is used for verifying the signed messages from the provider.
  20. */
  21. /**
  22. * Implements hook_menu().
  23. */
  24. function openid_test_menu() {
  25. $items['openid-test/yadis/xrds'] = array(
  26. 'title' => 'XRDS service document',
  27. 'page callback' => 'openid_test_yadis_xrds',
  28. 'access callback' => TRUE,
  29. 'type' => MENU_CALLBACK,
  30. );
  31. $items['openid-test/yadis/x-xrds-location'] = array(
  32. 'title' => 'Yadis discovery using X-XRDS-Location header',
  33. 'page callback' => 'openid_test_yadis_x_xrds_location',
  34. 'access callback' => TRUE,
  35. 'type' => MENU_CALLBACK,
  36. );
  37. $items['openid-test/yadis/http-equiv'] = array(
  38. 'title' => 'Yadis discovery using <meta http-equiv="X-XRDS-Location" ...>',
  39. 'page callback' => 'openid_test_yadis_http_equiv',
  40. 'access callback' => TRUE,
  41. 'type' => MENU_CALLBACK,
  42. );
  43. $items['openid-test/html/openid1'] = array(
  44. 'title' => 'HTML-based discovery using <link rel="openid.server" ...>',
  45. 'page callback' => 'openid_test_html_openid1',
  46. 'access callback' => TRUE,
  47. 'type' => MENU_CALLBACK,
  48. );
  49. $items['openid-test/html/openid2'] = array(
  50. 'title' => 'HTML-based discovery using <link rel="openid2.provider" ...>',
  51. 'page callback' => 'openid_test_html_openid2',
  52. 'access callback' => TRUE,
  53. 'type' => MENU_CALLBACK,
  54. );
  55. $items['openid-test/endpoint'] = array(
  56. 'title' => 'OpenID Provider Endpoint',
  57. 'page callback' => 'openid_test_endpoint',
  58. 'access callback' => TRUE,
  59. 'type' => MENU_CALLBACK,
  60. );
  61. $items['openid-test/redirect'] = array(
  62. 'title' => 'OpenID Provider Redirection Point',
  63. 'page callback' => 'openid_test_redirect',
  64. 'access callback' => TRUE,
  65. 'type' => MENU_CALLBACK,
  66. );
  67. $items['openid-test/redirected/%/%'] = array(
  68. 'title' => 'OpenID Provider Final URL',
  69. 'page callback' => 'openid_test_redirected_method',
  70. 'page arguments' => array(2, 3),
  71. 'access callback' => TRUE,
  72. 'type' => MENU_CALLBACK,
  73. );
  74. return $items;
  75. }
  76. /**
  77. * Implements hook_menu_site_status_alter().
  78. */
  79. function openid_test_menu_site_status_alter(&$menu_site_status, $path) {
  80. // Allow access to openid endpoint and identity even in offline mode.
  81. if ($menu_site_status == MENU_SITE_OFFLINE && user_is_anonymous() && in_array($path, array('openid-test/yadis/xrds', 'openid-test/endpoint'))) {
  82. $menu_site_status = MENU_SITE_ONLINE;
  83. }
  84. }
  85. /**
  86. * Menu callback; XRDS document that references the OP Endpoint URL.
  87. */
  88. function openid_test_yadis_xrds() {
  89. if ($_SERVER['HTTP_ACCEPT'] == 'application/xrds+xml') {
  90. // Only respond to XRI requests for one specific XRI. The is used to verify
  91. // that the XRI has been properly encoded. The "+" sign in the _xrd_r query
  92. // parameter is decoded to a space by PHP.
  93. if (arg(3) == 'xri') {
  94. if (variable_get('clean_url', 0)) {
  95. if (arg(4) != '@example*résumé;%25' || $_GET['_xrd_r'] != 'application/xrds xml') {
  96. drupal_not_found();
  97. }
  98. }
  99. else {
  100. // Drupal cannot properly emulate an XRI proxy resolver using unclean
  101. // URLs, so the arguments gets messed up.
  102. if (arg(4) . '/' . arg(5) != '@example*résumé;%25?_xrd_r=application/xrds xml') {
  103. drupal_not_found();
  104. }
  105. }
  106. }
  107. drupal_add_http_header('Content-Type', 'application/xrds+xml');
  108. print '<?xml version="1.0" encoding="UTF-8"?>
  109. <xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)" xmlns:openid="http://openid.net/xmlns/1.0">
  110. <XRD>
  111. <Status cid="' . check_plain(variable_get('openid_test_canonical_id_status', 'verified')) . '"/>
  112. <ProviderID>xri://@</ProviderID>
  113. <CanonicalID>http://example.com/user</CanonicalID>
  114. <Service>
  115. <Type>http://example.com/this-is-ignored</Type>
  116. </Service>
  117. <Service priority="5">
  118. <Type>http://openid.net/signon/1.0</Type>
  119. <URI>http://example.com/this-is-only-openid-1.0</URI>
  120. </Service>
  121. <Service priority="10">
  122. <Type>http://specs.openid.net/auth/2.0/signon</Type>
  123. <Type>http://openid.net/srv/ax/1.0</Type>
  124. <URI>' . url('openid-test/endpoint', array('absolute' => TRUE)) . '</URI>
  125. <LocalID>http://example.com/xrds</LocalID>
  126. </Service>
  127. <Service priority="15">
  128. <Type>http://specs.openid.net/auth/2.0/signon</Type>
  129. <URI>http://example.com/this-has-too-low-priority</URI>
  130. </Service>
  131. <Service>
  132. <Type>http://specs.openid.net/auth/2.0/signon</Type>
  133. <URI>http://example.com/this-has-too-low-priority</URI>
  134. </Service>
  135. ';
  136. if (arg(3) == 'server') {
  137. print '
  138. <Service>
  139. <Type>http://specs.openid.net/auth/2.0/server</Type>
  140. <URI>http://example.com/this-has-too-low-priority</URI>
  141. </Service>
  142. <Service priority="20">
  143. <Type>http://specs.openid.net/auth/2.0/server</Type>
  144. <URI>' . url('openid-test/endpoint', array('absolute' => TRUE)) . '</URI>
  145. </Service>';
  146. }
  147. elseif (arg(3) == 'delegate') {
  148. print '
  149. <Service priority="0">
  150. <Type>http://specs.openid.net/auth/2.0/signon</Type>
  151. <Type>http://openid.net/srv/ax/1.0</Type>
  152. <URI>' . url('openid-test/endpoint', array('absolute' => TRUE)) . '</URI>
  153. <openid:Delegate>http://example.com/xrds-delegate</openid:Delegate>
  154. </Service>';
  155. }
  156. print '
  157. </XRD>
  158. </xrds:XRDS>';
  159. }
  160. else {
  161. return t('This is a regular HTML page. If the client sends an Accept: application/xrds+xml header when requesting this URL, an XRDS document is returned.');
  162. }
  163. }
  164. /**
  165. * Menu callback; regular HTML page with an X-XRDS-Location HTTP header.
  166. */
  167. function openid_test_yadis_x_xrds_location() {
  168. drupal_add_http_header('X-XRDS-Location', url('openid-test/yadis/xrds', array('absolute' => TRUE)));
  169. return t('This page includes an X-RDS-Location HTTP header containing the URL of an XRDS document.');
  170. }
  171. /**
  172. * Menu callback; regular HTML page with <meta> element.
  173. */
  174. function openid_test_yadis_http_equiv() {
  175. $element = array(
  176. '#tag' => 'meta',
  177. '#attributes' => array(
  178. 'http-equiv' => 'X-XRDS-Location',
  179. 'content' => url('openid-test/yadis/xrds', array('absolute' => TRUE)),
  180. ),
  181. );
  182. drupal_add_html_head($element, 'openid_test_yadis_http_equiv');
  183. return t('This page includes a &lt;meta equiv=...&gt; element containing the URL of an XRDS document.');
  184. }
  185. /**
  186. * Menu callback; regular HTML page with OpenID 1.0 <link> element.
  187. */
  188. function openid_test_html_openid1() {
  189. drupal_add_html_head_link(array('rel' => 'openid.server', 'href' => url('openid-test/endpoint', array('absolute' => TRUE))));
  190. drupal_add_html_head_link(array('rel' => 'openid.delegate', 'href' => 'http://example.com/html-openid1'));
  191. return t('This page includes a &lt;link rel=...&gt; element containing the URL of an OpenID Provider Endpoint.');
  192. }
  193. /**
  194. * Menu callback; regular HTML page with OpenID 2.0 <link> element.
  195. */
  196. function openid_test_html_openid2() {
  197. drupal_add_html_head_link(array('rel' => 'openid2.provider', 'href' => url('openid-test/endpoint', array('absolute' => TRUE))));
  198. drupal_add_html_head_link(array('rel' => 'openid2.local_id', 'href' => 'http://example.com/html-openid2'));
  199. return t('This page includes a &lt;link rel=...&gt; element containing the URL of an OpenID Provider Endpoint.');
  200. }
  201. /**
  202. * Menu callback; OpenID Provider Endpoint.
  203. *
  204. * It accepts "associate" requests directly from the Relying Party, and
  205. * "checkid_setup" requests made by the user's browser based on HTTP redirects
  206. * (in OpenID 1) or HTML forms (in OpenID 2) generated by the Relying Party.
  207. */
  208. function openid_test_endpoint() {
  209. switch ($_REQUEST['openid_mode']) {
  210. case 'associate':
  211. _openid_test_endpoint_associate();
  212. break;
  213. case 'checkid_setup':
  214. _openid_test_endpoint_authenticate();
  215. break;
  216. }
  217. }
  218. /**
  219. * Menu callback; redirect during Normalization/Discovery.
  220. */
  221. function openid_test_redirect($count = 0) {
  222. if ($count == 0) {
  223. $url = variable_get('openid_test_redirect_url', '');
  224. }
  225. else {
  226. $url = url('openid-test/redirect/' . --$count, array('absolute' => TRUE));
  227. }
  228. $http_response_code = variable_get('openid_test_redirect_http_reponse_code', 301);
  229. header('Location: ' . $url, TRUE, $http_response_code);
  230. exit();
  231. }
  232. /**
  233. * Menu callback; respond with appropriate callback.
  234. */
  235. function openid_test_redirected_method($method1, $method2) {
  236. return call_user_func('openid_test_' . $method1 . '_' . $method2);
  237. }
  238. /**
  239. * OpenID endpoint; handle "associate" requests (see OpenID Authentication 2.0,
  240. * section 8).
  241. *
  242. * The purpose of association is to send the secret MAC key to the Relying Party
  243. * using Diffie-Hellman key exchange. The MAC key is used in subsequent
  244. * "authenticate" requests. The "associate" request is made by the Relying Party
  245. * (in the testing scenario, this is the OpenID module that communicates with
  246. * the endpoint using drupal_http_request()).
  247. */
  248. function _openid_test_endpoint_associate() {
  249. module_load_include('inc', 'openid');
  250. // Use default parameters for Diffie-Helmann key exchange.
  251. $mod = OPENID_DH_DEFAULT_MOD;
  252. $gen = OPENID_DH_DEFAULT_GEN;
  253. // Generate private Diffie-Helmann key.
  254. $r = _openid_dh_rand($mod);
  255. $private = _openid_math_add($r, 1);
  256. // Calculate public Diffie-Helmann key.
  257. $public = _openid_math_powmod($gen, $private, $mod);
  258. // Calculate shared secret based on Relying Party's public key.
  259. $cpub = _openid_dh_base64_to_long($_REQUEST['openid_dh_consumer_public']);
  260. $shared = _openid_math_powmod($cpub, $private, $mod);
  261. // Encrypt the MAC key using the shared secret.
  262. $enc_mac_key = base64_encode(_openid_dh_xorsecret($shared, base64_decode(variable_get('mac_key'))));
  263. // Generate response including our public key and the MAC key. Using our
  264. // public key and its own private key, the Relying Party can calculate the
  265. // shared secret, and with this it can decrypt the encrypted MAC key.
  266. $response = array(
  267. 'ns' => 'http://specs.openid.net/auth/2.0',
  268. 'assoc_handle' => 'openid-test',
  269. 'session_type' => $_REQUEST['openid_session_type'],
  270. 'assoc_type' => $_REQUEST['openid_assoc_type'],
  271. 'expires_in' => '3600',
  272. 'dh_server_public' => _openid_dh_long_to_base64($public),
  273. 'enc_mac_key' => $enc_mac_key,
  274. );
  275. // Respond to Relying Party in the special Key-Value Form Encoding (see OpenID
  276. // Authentication 1.0, section 4.1.1).
  277. drupal_add_http_header('Content-Type', 'text/plain');
  278. print _openid_create_message($response);
  279. }
  280. /**
  281. * OpenID endpoint; handle "authenticate" requests.
  282. *
  283. * All requests result in a successful response. The request is a GET or POST
  284. * made by the user's browser based on an HTML form or HTTP redirect generated
  285. * by the Relying Party. The user is redirected back to the Relying Party using
  286. * a URL containing a signed message in the query string confirming the user's
  287. * identity.
  288. */
  289. function _openid_test_endpoint_authenticate() {
  290. module_load_include('inc', 'openid');
  291. $expected_identity = variable_get('openid_test_identity');
  292. if ($expected_identity && $_REQUEST['openid_identity'] != $expected_identity) {
  293. $response = variable_get('openid_test_response', array()) + array(
  294. 'openid.ns' => OPENID_NS_2_0,
  295. 'openid.mode' => 'error',
  296. 'openid.error' => 'Unexpted identity',
  297. );
  298. drupal_add_http_header('Content-Type', 'text/plain');
  299. header('Location: ' . url($_REQUEST['openid_return_to'], array('query' => $response, 'external' => TRUE)));
  300. return;
  301. }
  302. // Generate unique identifier for this authentication.
  303. $nonce = _openid_nonce();
  304. // Generate response containing the user's identity.
  305. $response = variable_get('openid_test_response', array()) + array(
  306. 'openid.ns' => OPENID_NS_2_0,
  307. 'openid.mode' => 'id_res',
  308. 'openid.op_endpoint' => url('openid-test/endpoint', array('absolute' => TRUE)),
  309. 'openid.claimed_id' => !empty($_REQUEST['openid_claimed_id']) ? $_REQUEST['openid_claimed_id'] : '',
  310. 'openid.identity' => $_REQUEST['openid_identity'],
  311. 'openid.return_to' => $_REQUEST['openid_return_to'],
  312. 'openid.response_nonce' => $nonce,
  313. 'openid.assoc_handle' => 'openid-test',
  314. );
  315. if (isset($response['openid.signed'])) {
  316. $keys_to_sign = explode(',', $response['openid.signed']);
  317. }
  318. else {
  319. // Unless openid.signed is explicitly defined, all keys are signed.
  320. $keys_to_sign = array();
  321. foreach ($response as $key => $value) {
  322. // Strip off the "openid." prefix.
  323. $keys_to_sign[] = substr($key, 7);
  324. }
  325. $response['openid.signed'] = implode(',', $keys_to_sign);
  326. }
  327. // Sign the message using the MAC key that was exchanged during association.
  328. $association = new stdClass();
  329. $association->mac_key = variable_get('mac_key');
  330. if (!isset($response['openid.sig'])) {
  331. $response['openid.sig'] = _openid_signature($association, $response, $keys_to_sign);
  332. }
  333. // Put the signed message into the query string of a URL supplied by the
  334. // Relying Party, and redirect the user.
  335. drupal_add_http_header('Content-Type', 'text/plain');
  336. header('Location: ' . url($_REQUEST['openid_return_to'], array('query' => $response, 'external' => TRUE)));
  337. }