PHP OpenID consumer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

urlbuilder.inc.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?PHP
  2. /* Poidsy 0.4 - http://chris.smith.name/projects/poidsy
  3. * Copyright (c) 2008 Chris Smith
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. require_once(dirname(__FILE__) . '/keymanager.inc.php');
  24. class URLBuilder {
  25. const NAMESPACE = 'http://openid.net/signon/1.1';
  26. public static function addArguments($base, $arguments) {
  27. $first = true;
  28. $res = $base === false ? '' : $base;
  29. if ($base !== false && strrpos($base, '?', -2) === false) {
  30. if ($base[strlen($base) - 1] != '?') {
  31. $res .= '?';
  32. }
  33. } else if ($base !== false) {
  34. $res .= '&';
  35. }
  36. foreach ($arguments as $key => $value) {
  37. if ($first) {
  38. $first = false;
  39. } else {
  40. $res .= '&';
  41. }
  42. $res .= urlencode($key) . '=' . urlencode($value);
  43. }
  44. return $res;
  45. }
  46. public static function buildRequest($type, $base, $delegate, $identity, $returnURL, $handle) {
  47. $args = array(
  48. 'openid.ns' => self::NAMESPACE,
  49. 'openid.mode' => 'checkid_' . $type,
  50. 'openid.identity' => $delegate,
  51. 'openid.claimed_id' => $identity,
  52. 'openid.trust_root' => self::getTrustRoot($returnURL),
  53. 'openid.return_to' => self::addArguments($returnURL,
  54. array('openid.nonce' => $_SESSION['openid']['nonce']))
  55. );
  56. if ($handle !== null) {
  57. $args['openid.assoc_handle'] = $handle;
  58. }
  59. self::addSRegArgs($args);
  60. return self::addArguments($base, $args);
  61. }
  62. private static function getTrustRoot($base = null) {
  63. if (defined('OPENID_TRUSTROOT')) {
  64. return OPENID_TRUSTROOT;
  65. }
  66. $curr = self::getCurrentURL();
  67. $root = $base == null ? $curr : $base;
  68. while (substr($curr, 0, strlen($root)) != $root) {
  69. $root = dirname($root) . '/';
  70. }
  71. return $root;
  72. }
  73. private static function addSRegArgs(&$args) {
  74. if (defined('OPENID_SREG_REQUEST')) {
  75. $args['openid.sreg.required'] = OPENID_SREG_REQUEST;
  76. }
  77. if (defined('OPENID_SREG_OPTIONAL')) {
  78. $args['openid.sreg.optional'] = OPENID_SREG_OPTIONAL;
  79. }
  80. if (defined('OPENID_SREG_POLICY')) {
  81. $args['openid.sreg.policy_url'] = OPENID_SREG_POLICY;
  82. }
  83. }
  84. public static function buildAssociate($server) {
  85. $args = array(
  86. 'openid.ns' => self::NAMESPACE,
  87. 'openid.mode' => 'associate',
  88. 'openid.assoc_type' => 'HMAC-SHA1',
  89. );
  90. if (KeyManager::supportsDH()) {
  91. $args['openid.session_type'] = 'DH-SHA1';
  92. $args['openid.dh_modulus'] = KeyManager::getDhModulus();
  93. $args['openid.dh_gen'] = KeyManager::getDhGen();
  94. $args['openid.dh_consumer_public'] = KeyManager::getDhPublicKey($server);
  95. } else {
  96. $args['openid.session_type'] = '';
  97. }
  98. return self::addArguments(false, $args);
  99. }
  100. public static function buildAuth($params) {
  101. $args = array(
  102. 'openid.ns' => self::NAMESPACE,
  103. 'openid.mode' => 'check_authentication'
  104. );
  105. $toadd = array('assoc_handle', 'sig', 'signed');
  106. $toadd = array_merge($toadd, explode(',', $params['openid_signed']));
  107. foreach ($toadd as $arg) {
  108. if (!isset($args['openid.' . $arg])) {
  109. $args['openid.' . $arg] = $params['openid_' . $arg];
  110. }
  111. }
  112. return self::addArguments(false, $args);
  113. }
  114. public static function getCurrentURL() {
  115. $res = 'http';
  116. if (isset($_SERVER['HTTPS'])) {
  117. $res = 'https';
  118. }
  119. $res .= '://' . $_SERVER['SERVER_NAME'];
  120. if ($_SERVER['SERVER_PORT'] != 80) {
  121. $res .= ':' . $_SERVER['SERVER_PORT'];
  122. }
  123. $url = $_SERVER['REQUEST_URI'];
  124. while (preg_match('/([\?&])openid[\._](.*?)=(.*?)(&|$)/', $url, $m)) {
  125. $url = str_replace($m[0], $m[1], $url);
  126. }
  127. $url = preg_replace('/\??&*$/', '', $url);
  128. return $res . $url;
  129. }
  130. /**
  131. * Redirects the user back to their original page.
  132. */
  133. public static function redirect() {
  134. if (defined('OPENID_REDIRECTURL')) {
  135. $url = OPENID_REDIRECTURL;
  136. } else if (isset($_SESSION['openid']['redirect'])) {
  137. $url = $_SESSION['openid']['redirect'];
  138. } else {
  139. $url = self::getCurrentURL();
  140. }
  141. self::doRedirect($url);
  142. }
  143. /**
  144. * Redirects the user to the specified URL.
  145. *
  146. * @param $url The URL to redirect the user to
  147. */
  148. public static function doRedirect($url) {
  149. header('Location: ' . $url);
  150. echo '<html><head><title>Redirecting</title></head><body>';
  151. echo '<p>Redirecting to <a href="', htmlentities($url), '">';
  152. echo htmlentities($url), '</a></p></body></html>';
  153. exit();
  154. }
  155. }
  156. ?>