Digest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Auth
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Auth_Adapter_Interface
  24. */
  25. require_once 'Zend/Auth/Adapter/Interface.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Auth
  29. * @subpackage Adapter
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Auth_Adapter_Digest implements Zend_Auth_Adapter_Interface
  34. {
  35. /**
  36. * Filename against which authentication queries are performed
  37. *
  38. * @var string
  39. */
  40. protected $_filename;
  41. /**
  42. * Digest authentication realm
  43. *
  44. * @var string
  45. */
  46. protected $_realm;
  47. /**
  48. * Digest authentication user
  49. *
  50. * @var string
  51. */
  52. protected $_username;
  53. /**
  54. * Password for the user of the realm
  55. *
  56. * @var string
  57. */
  58. protected $_password;
  59. /**
  60. * Sets adapter options
  61. *
  62. * @param mixed $filename
  63. * @param mixed $realm
  64. * @param mixed $username
  65. * @param mixed $password
  66. */
  67. public function __construct($filename = null, $realm = null, $username = null, $password = null)
  68. {
  69. $options = array('filename', 'realm', 'username', 'password');
  70. foreach ($options as $option) {
  71. if (null !== $$option) {
  72. $methodName = 'set' . ucfirst($option);
  73. $this->$methodName($$option);
  74. }
  75. }
  76. }
  77. /**
  78. * Returns the filename option value or null if it has not yet been set
  79. *
  80. * @return string|null
  81. */
  82. public function getFilename()
  83. {
  84. return $this->_filename;
  85. }
  86. /**
  87. * Sets the filename option value
  88. *
  89. * @param mixed $filename
  90. * @return Zend_Auth_Adapter_Digest Provides a fluent interface
  91. */
  92. public function setFilename($filename)
  93. {
  94. $this->_filename = (string) $filename;
  95. return $this;
  96. }
  97. /**
  98. * Returns the realm option value or null if it has not yet been set
  99. *
  100. * @return string|null
  101. */
  102. public function getRealm()
  103. {
  104. return $this->_realm;
  105. }
  106. /**
  107. * Sets the realm option value
  108. *
  109. * @param mixed $realm
  110. * @return Zend_Auth_Adapter_Digest Provides a fluent interface
  111. */
  112. public function setRealm($realm)
  113. {
  114. $this->_realm = (string) $realm;
  115. return $this;
  116. }
  117. /**
  118. * Returns the username option value or null if it has not yet been set
  119. *
  120. * @return string|null
  121. */
  122. public function getUsername()
  123. {
  124. return $this->_username;
  125. }
  126. /**
  127. * Sets the username option value
  128. *
  129. * @param mixed $username
  130. * @return Zend_Auth_Adapter_Digest Provides a fluent interface
  131. */
  132. public function setUsername($username)
  133. {
  134. $this->_username = (string) $username;
  135. return $this;
  136. }
  137. /**
  138. * Returns the password option value or null if it has not yet been set
  139. *
  140. * @return string|null
  141. */
  142. public function getPassword()
  143. {
  144. return $this->_password;
  145. }
  146. /**
  147. * Sets the password option value
  148. *
  149. * @param mixed $password
  150. * @return Zend_Auth_Adapter_Digest Provides a fluent interface
  151. */
  152. public function setPassword($password)
  153. {
  154. $this->_password = (string) $password;
  155. return $this;
  156. }
  157. /**
  158. * Defined by Zend_Auth_Adapter_Interface
  159. *
  160. * @throws Zend_Auth_Adapter_Exception
  161. * @return Zend_Auth_Result
  162. */
  163. public function authenticate()
  164. {
  165. $optionsRequired = array('filename', 'realm', 'username', 'password');
  166. foreach ($optionsRequired as $optionRequired) {
  167. if (null === $this->{"_$optionRequired"}) {
  168. /**
  169. * @see Zend_Auth_Adapter_Exception
  170. */
  171. require_once 'Zend/Auth/Adapter/Exception.php';
  172. throw new Zend_Auth_Adapter_Exception("Option '$optionRequired' must be set before authentication");
  173. }
  174. }
  175. if (false === ($fileHandle = @fopen($this->_filename, 'r'))) {
  176. /**
  177. * @see Zend_Auth_Adapter_Exception
  178. */
  179. require_once 'Zend/Auth/Adapter/Exception.php';
  180. throw new Zend_Auth_Adapter_Exception("Cannot open '$this->_filename' for reading");
  181. }
  182. $id = "$this->_username:$this->_realm";
  183. $idLength = strlen($id);
  184. $result = array(
  185. 'code' => Zend_Auth_Result::FAILURE,
  186. 'identity' => array(
  187. 'realm' => $this->_realm,
  188. 'username' => $this->_username,
  189. ),
  190. 'messages' => array()
  191. );
  192. while ($line = trim(fgets($fileHandle))) {
  193. if (substr($line, 0, $idLength) === $id) {
  194. if ($this->_secureStringCompare(substr($line, -32), md5("$this->_username:$this->_realm:$this->_password"))) {
  195. $result['code'] = Zend_Auth_Result::SUCCESS;
  196. } else {
  197. $result['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
  198. $result['messages'][] = 'Password incorrect';
  199. }
  200. return new Zend_Auth_Result($result['code'], $result['identity'], $result['messages']);
  201. }
  202. }
  203. $result['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
  204. $result['messages'][] = "Username '$this->_username' and realm '$this->_realm' combination not found";
  205. return new Zend_Auth_Result($result['code'], $result['identity'], $result['messages']);
  206. }
  207. /**
  208. * Securely compare two strings for equality while avoided C level memcmp()
  209. * optimisations capable of leaking timing information useful to an attacker
  210. * attempting to iteratively guess the unknown string (e.g. password) being
  211. * compared against.
  212. *
  213. * @param string $a
  214. * @param string $b
  215. * @return bool
  216. */
  217. protected function _secureStringCompare($a, $b)
  218. {
  219. if (strlen($a) !== strlen($b)) {
  220. return false;
  221. }
  222. $result = 0;
  223. for ($i = 0; $i < strlen($a); $i++) {
  224. $result |= ord($a[$i]) ^ ord($b[$i]);
  225. }
  226. return $result == 0;
  227. }
  228. }