PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pgfouine-1.2/tests/simpletest/authentication.php

#
PHP | 219 lines | 87 code | 16 blank | 116 comment | 11 complexity | 8f11e50a509aa7500c9a568c72ddf50a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * Base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id: authentication.php,v 1.1 2005/11/09 23:41:18 gsmet Exp $
  7. */
  8. /**
  9. * include http class
  10. */
  11. require_once(dirname(__FILE__) . '/http.php');
  12. /**
  13. * Represents a single security realm's identity.
  14. * @package SimpleTest
  15. * @subpackage WebTester
  16. */
  17. class SimpleRealm {
  18. var $_type;
  19. var $_root;
  20. var $_username;
  21. var $_password;
  22. /**
  23. * Starts with the initial entry directory.
  24. * @param string $type Authentication type for this
  25. * realm. Only Basic authentication
  26. * is currently supported.
  27. * @param SimpleUrl $url Somewhere in realm.
  28. * @access public
  29. */
  30. function SimpleRealm($type, $url) {
  31. $this->_type = $type;
  32. $this->_root = $url->getBasePath();
  33. $this->_username = false;
  34. $this->_password = false;
  35. }
  36. /**
  37. * Adds another location to the realm.
  38. * @param SimpleUrl $url Somewhere in realm.
  39. * @access public
  40. */
  41. function stretch($url) {
  42. $this->_root = $this->_getCommonPath($this->_root, $url->getPath());
  43. }
  44. /**
  45. * Finds the common starting path.
  46. * @param string $first Path to compare.
  47. * @param string $second Path to compare.
  48. * @return string Common directories.
  49. * @access private
  50. */
  51. function _getCommonPath($first, $second) {
  52. $first = explode('/', $first);
  53. $second = explode('/', $second);
  54. for ($i = 0; $i < min(count($first), count($second)); $i++) {
  55. if ($first[$i] != $second[$i]) {
  56. return implode('/', array_slice($first, 0, $i)) . '/';
  57. }
  58. }
  59. return implode('/', $first) . '/';
  60. }
  61. /**
  62. * Sets the identity to try within this realm.
  63. * @param string $username Username in authentication dialog.
  64. * @param string $username Password in authentication dialog.
  65. * @access public
  66. */
  67. function setIdentity($username, $password) {
  68. $this->_username = $username;
  69. $this->_password = $password;
  70. }
  71. /**
  72. * Accessor for current identity.
  73. * @return string Last succesful username.
  74. * @access public
  75. */
  76. function getUsername() {
  77. return $this->_username;
  78. }
  79. /**
  80. * Accessor for current identity.
  81. * @return string Last succesful password.
  82. * @access public
  83. */
  84. function getPassword() {
  85. return $this->_password;
  86. }
  87. /**
  88. * Test to see if the URL is within the directory
  89. * tree of the realm.
  90. * @param SimpleUrl $url URL to test.
  91. * @return boolean True if subpath.
  92. * @access public
  93. */
  94. function isWithin($url) {
  95. return (strpos($url->getBasePath(), $this->_root) === 0);
  96. }
  97. }
  98. /**
  99. * Manages security realms.
  100. * @package SimpleTest
  101. * @subpackage WebTester
  102. */
  103. class SimpleAuthenticator {
  104. var $_realms;
  105. /**
  106. * Clears the realms.
  107. * @access public
  108. */
  109. function SimpleAuthenticator() {
  110. $this->restartSession();
  111. }
  112. /**
  113. * Starts with no realms set up.
  114. * @access public
  115. */
  116. function restartSession() {
  117. $this->_realms = array();
  118. }
  119. /**
  120. * Adds a new realm centered the current URL.
  121. * Browsers vary wildly on their behaviour in this
  122. * regard. Mozilla ignores the realm and presents
  123. * only when challenged, wasting bandwidth. IE
  124. * just carries on presenting until a new challenge
  125. * occours. SimpleTest tries to follow the spirit of
  126. * the original standards committee and treats the
  127. * base URL as the root of a file tree shaped realm.
  128. * @param SimpleUrl $url Base of realm.
  129. * @param string $type Authentication type for this
  130. * realm. Only Basic authentication
  131. * is currently supported.
  132. * @param string $realm Name of realm.
  133. * @access public
  134. */
  135. function addRealm($url, $type, $realm) {
  136. $this->_realms[$url->getHost()][$realm] = new SimpleRealm($type, $url);
  137. }
  138. /**
  139. * Sets the current identity to be presented
  140. * against that realm.
  141. * @param string $host Server hosting realm.
  142. * @param string $realm Name of realm.
  143. * @param string $username Username for realm.
  144. * @param string $password Password for realm.
  145. * @access public
  146. */
  147. function setIdentityForRealm($host, $realm, $username, $password) {
  148. if (isset($this->_realms[$host][$realm])) {
  149. $this->_realms[$host][$realm]->setIdentity($username, $password);
  150. }
  151. }
  152. /**
  153. * Finds the name of the realm by comparing URLs.
  154. * @param SimpleUrl $url URL to test.
  155. * @return SimpleRealm Name of realm.
  156. * @access private
  157. */
  158. function _findRealmFromUrl($url) {
  159. if (! isset($this->_realms[$url->getHost()])) {
  160. return false;
  161. }
  162. foreach ($this->_realms[$url->getHost()] as $name => $realm) {
  163. if ($realm->isWithin($url)) {
  164. return $realm;
  165. }
  166. }
  167. return false;
  168. }
  169. /**
  170. * Presents the appropriate headers for this location.
  171. * @param SimpleHttpRequest $request Request to modify.
  172. * @param SimpleUrl $url Base of realm.
  173. * @access public
  174. */
  175. function addHeaders(&$request, $url) {
  176. if ($url->getUsername() && $url->getPassword()) {
  177. $username = $url->getUsername();
  178. $password = $url->getPassword();
  179. } elseif ($realm = $this->_findRealmFromUrl($url)) {
  180. $username = $realm->getUsername();
  181. $password = $realm->getPassword();
  182. } else {
  183. return;
  184. }
  185. $this->addBasicHeaders($request, $username, $password);
  186. }
  187. /**
  188. * Presents the appropriate headers for this
  189. * location for basic authentication.
  190. * @param SimpleHttpRequest $request Request to modify.
  191. * @param string $username Username for realm.
  192. * @param string $password Password for realm.
  193. * @access public
  194. * @static
  195. */
  196. function addBasicHeaders(&$request, $username, $password) {
  197. if ($username && $password) {
  198. $request->addHeaderLine(
  199. 'Authorization: Basic ' . base64_encode("$username:$password"));
  200. }
  201. }
  202. }
  203. ?>