PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/src/lib/Zend/Auth/Adapter/Http.php

https://bitbucket.org/mkrasuski/magento-ce
PHP | 868 lines | 408 code | 86 blank | 374 comment | 114 complexity | 0cfb159e07de35d89ab6754c9e409b1e MD5 | raw file
  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 Zend_Auth_Adapter_Http
  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. * HTTP Authentication Adapter
  28. *
  29. * Implements a pretty good chunk of RFC 2617.
  30. *
  31. * @category Zend
  32. * @package Zend_Auth
  33. * @subpackage Zend_Auth_Adapter_Http
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @todo Support auth-int
  37. * @todo Track nonces, nonce-count, opaque for replay protection and stale support
  38. * @todo Support Authentication-Info header
  39. */
  40. class Zend_Auth_Adapter_Http implements Zend_Auth_Adapter_Interface
  41. {
  42. /**
  43. * Reference to the HTTP Request object
  44. *
  45. * @var Zend_Controller_Request_Http
  46. */
  47. protected $_request;
  48. /**
  49. * Reference to the HTTP Response object
  50. *
  51. * @var Zend_Controller_Response_Http
  52. */
  53. protected $_response;
  54. /**
  55. * Object that looks up user credentials for the Basic scheme
  56. *
  57. * @var Zend_Auth_Adapter_Http_Resolver_Interface
  58. */
  59. protected $_basicResolver;
  60. /**
  61. * Object that looks up user credentials for the Digest scheme
  62. *
  63. * @var Zend_Auth_Adapter_Http_Resolver_Interface
  64. */
  65. protected $_digestResolver;
  66. /**
  67. * List of authentication schemes supported by this class
  68. *
  69. * @var array
  70. */
  71. protected $_supportedSchemes = array('basic', 'digest');
  72. /**
  73. * List of schemes this class will accept from the client
  74. *
  75. * @var array
  76. */
  77. protected $_acceptSchemes;
  78. /**
  79. * Space-delimited list of protected domains for Digest Auth
  80. *
  81. * @var string
  82. */
  83. protected $_domains;
  84. /**
  85. * The protection realm to use
  86. *
  87. * @var string
  88. */
  89. protected $_realm;
  90. /**
  91. * Nonce timeout period
  92. *
  93. * @var integer
  94. */
  95. protected $_nonceTimeout;
  96. /**
  97. * Whether to send the opaque value in the header. True by default
  98. *
  99. * @var boolean
  100. */
  101. protected $_useOpaque;
  102. /**
  103. * List of the supported digest algorithms. I want to support both MD5 and
  104. * MD5-sess, but MD5-sess won't make it into the first version.
  105. *
  106. * @var array
  107. */
  108. protected $_supportedAlgos = array('MD5');
  109. /**
  110. * The actual algorithm to use. Defaults to MD5
  111. *
  112. * @var string
  113. */
  114. protected $_algo;
  115. /**
  116. * List of supported qop options. My intetion is to support both 'auth' and
  117. * 'auth-int', but 'auth-int' won't make it into the first version.
  118. *
  119. * @var array
  120. */
  121. protected $_supportedQops = array('auth');
  122. /**
  123. * Whether or not to do Proxy Authentication instead of origin server
  124. * authentication (send 407's instead of 401's). Off by default.
  125. *
  126. * @var boolean
  127. */
  128. protected $_imaProxy;
  129. /**
  130. * Flag indicating the client is IE and didn't bother to return the opaque string
  131. *
  132. * @var boolean
  133. */
  134. protected $_ieNoOpaque;
  135. /**
  136. * Constructor
  137. *
  138. * @param array $config Configuration settings:
  139. * 'accept_schemes' => 'basic'|'digest'|'basic digest'
  140. * 'realm' => <string>
  141. * 'digest_domains' => <string> Space-delimited list of URIs
  142. * 'nonce_timeout' => <int>
  143. * 'use_opaque' => <bool> Whether to send the opaque value in the header
  144. * 'alogrithm' => <string> See $_supportedAlgos. Default: MD5
  145. * 'proxy_auth' => <bool> Whether to do authentication as a Proxy
  146. * @throws Zend_Auth_Adapter_Exception
  147. */
  148. public function __construct(array $config)
  149. {
  150. if (!extension_loaded('hash')) {
  151. /**
  152. * @see Zend_Auth_Adapter_Exception
  153. */
  154. #require_once 'Zend/Auth/Adapter/Exception.php';
  155. throw new Zend_Auth_Adapter_Exception(__CLASS__ . ' requires the \'hash\' extension');
  156. }
  157. $this->_request = null;
  158. $this->_response = null;
  159. $this->_ieNoOpaque = false;
  160. if (empty($config['accept_schemes'])) {
  161. /**
  162. * @see Zend_Auth_Adapter_Exception
  163. */
  164. #require_once 'Zend/Auth/Adapter/Exception.php';
  165. throw new Zend_Auth_Adapter_Exception('Config key \'accept_schemes\' is required');
  166. }
  167. $schemes = explode(' ', $config['accept_schemes']);
  168. $this->_acceptSchemes = array_intersect($schemes, $this->_supportedSchemes);
  169. if (empty($this->_acceptSchemes)) {
  170. /**
  171. * @see Zend_Auth_Adapter_Exception
  172. */
  173. #require_once 'Zend/Auth/Adapter/Exception.php';
  174. throw new Zend_Auth_Adapter_Exception('No supported schemes given in \'accept_schemes\'. Valid values: '
  175. . implode(', ', $this->_supportedSchemes));
  176. }
  177. // Double-quotes are used to delimit the realm string in the HTTP header,
  178. // and colons are field delimiters in the password file.
  179. if (empty($config['realm']) ||
  180. !ctype_print($config['realm']) ||
  181. strpos($config['realm'], ':') !== false ||
  182. strpos($config['realm'], '"') !== false) {
  183. /**
  184. * @see Zend_Auth_Adapter_Exception
  185. */
  186. #require_once 'Zend/Auth/Adapter/Exception.php';
  187. throw new Zend_Auth_Adapter_Exception('Config key \'realm\' is required, and must contain only printable '
  188. . 'characters, excluding quotation marks and colons');
  189. } else {
  190. $this->_realm = $config['realm'];
  191. }
  192. if (in_array('digest', $this->_acceptSchemes)) {
  193. if (empty($config['digest_domains']) ||
  194. !ctype_print($config['digest_domains']) ||
  195. strpos($config['digest_domains'], '"') !== false) {
  196. /**
  197. * @see Zend_Auth_Adapter_Exception
  198. */
  199. #require_once 'Zend/Auth/Adapter/Exception.php';
  200. throw new Zend_Auth_Adapter_Exception('Config key \'digest_domains\' is required, and must contain '
  201. . 'only printable characters, excluding quotation marks');
  202. } else {
  203. $this->_domains = $config['digest_domains'];
  204. }
  205. if (empty($config['nonce_timeout']) ||
  206. !is_numeric($config['nonce_timeout'])) {
  207. /**
  208. * @see Zend_Auth_Adapter_Exception
  209. */
  210. #require_once 'Zend/Auth/Adapter/Exception.php';
  211. throw new Zend_Auth_Adapter_Exception('Config key \'nonce_timeout\' is required, and must be an '
  212. . 'integer');
  213. } else {
  214. $this->_nonceTimeout = (int) $config['nonce_timeout'];
  215. }
  216. // We use the opaque value unless explicitly told not to
  217. if (isset($config['use_opaque']) && false == (bool) $config['use_opaque']) {
  218. $this->_useOpaque = false;
  219. } else {
  220. $this->_useOpaque = true;
  221. }
  222. if (isset($config['algorithm']) && in_array($config['algorithm'], $this->_supportedAlgos)) {
  223. $this->_algo = $config['algorithm'];
  224. } else {
  225. $this->_algo = 'MD5';
  226. }
  227. }
  228. // Don't be a proxy unless explicitly told to do so
  229. if (isset($config['proxy_auth']) && true == (bool) $config['proxy_auth']) {
  230. $this->_imaProxy = true; // I'm a Proxy
  231. } else {
  232. $this->_imaProxy = false;
  233. }
  234. }
  235. /**
  236. * Setter for the _basicResolver property
  237. *
  238. * @param Zend_Auth_Adapter_Http_Resolver_Interface $resolver
  239. * @return Zend_Auth_Adapter_Http Provides a fluent interface
  240. */
  241. public function setBasicResolver(Zend_Auth_Adapter_Http_Resolver_Interface $resolver)
  242. {
  243. $this->_basicResolver = $resolver;
  244. return $this;
  245. }
  246. /**
  247. * Getter for the _basicResolver property
  248. *
  249. * @return Zend_Auth_Adapter_Http_Resolver_Interface
  250. */
  251. public function getBasicResolver()
  252. {
  253. return $this->_basicResolver;
  254. }
  255. /**
  256. * Setter for the _digestResolver property
  257. *
  258. * @param Zend_Auth_Adapter_Http_Resolver_Interface $resolver
  259. * @return Zend_Auth_Adapter_Http Provides a fluent interface
  260. */
  261. public function setDigestResolver(Zend_Auth_Adapter_Http_Resolver_Interface $resolver)
  262. {
  263. $this->_digestResolver = $resolver;
  264. return $this;
  265. }
  266. /**
  267. * Getter for the _digestResolver property
  268. *
  269. * @return Zend_Auth_Adapter_Http_Resolver_Interface
  270. */
  271. public function getDigestResolver()
  272. {
  273. return $this->_digestResolver;
  274. }
  275. /**
  276. * Setter for the Request object
  277. *
  278. * @param Zend_Controller_Request_Http $request
  279. * @return Zend_Auth_Adapter_Http Provides a fluent interface
  280. */
  281. public function setRequest(Zend_Controller_Request_Http $request)
  282. {
  283. $this->_request = $request;
  284. return $this;
  285. }
  286. /**
  287. * Getter for the Request object
  288. *
  289. * @return Zend_Controller_Request_Http
  290. */
  291. public function getRequest()
  292. {
  293. return $this->_request;
  294. }
  295. /**
  296. * Setter for the Response object
  297. *
  298. * @param Zend_Controller_Response_Http $response
  299. * @return Zend_Auth_Adapter_Http Provides a fluent interface
  300. */
  301. public function setResponse(Zend_Controller_Response_Http $response)
  302. {
  303. $this->_response = $response;
  304. return $this;
  305. }
  306. /**
  307. * Getter for the Response object
  308. *
  309. * @return Zend_Controller_Response_Http
  310. */
  311. public function getResponse()
  312. {
  313. return $this->_response;
  314. }
  315. /**
  316. * Authenticate
  317. *
  318. * @throws Zend_Auth_Adapter_Exception
  319. * @return Zend_Auth_Result
  320. */
  321. public function authenticate()
  322. {
  323. if (empty($this->_request) ||
  324. empty($this->_response)) {
  325. /**
  326. * @see Zend_Auth_Adapter_Exception
  327. */
  328. #require_once 'Zend/Auth/Adapter/Exception.php';
  329. throw new Zend_Auth_Adapter_Exception('Request and Response objects must be set before calling '
  330. . 'authenticate()');
  331. }
  332. if ($this->_imaProxy) {
  333. $getHeader = 'Proxy-Authorization';
  334. } else {
  335. $getHeader = 'Authorization';
  336. }
  337. $authHeader = $this->_request->getHeader($getHeader);
  338. if (!$authHeader) {
  339. return $this->_challengeClient();
  340. }
  341. list($clientScheme) = explode(' ', $authHeader);
  342. $clientScheme = strtolower($clientScheme);
  343. // The server can issue multiple challenges, but the client should
  344. // answer with only the selected auth scheme.
  345. if (!in_array($clientScheme, $this->_supportedSchemes)) {
  346. $this->_response->setHttpResponseCode(400);
  347. return new Zend_Auth_Result(
  348. Zend_Auth_Result::FAILURE_UNCATEGORIZED,
  349. array(),
  350. array('Client requested an incorrect or unsupported authentication scheme')
  351. );
  352. }
  353. // client sent a scheme that is not the one required
  354. if (!in_array($clientScheme, $this->_acceptSchemes)) {
  355. // challenge again the client
  356. return $this->_challengeClient();
  357. }
  358. switch ($clientScheme) {
  359. case 'basic':
  360. $result = $this->_basicAuth($authHeader);
  361. break;
  362. case 'digest':
  363. $result = $this->_digestAuth($authHeader);
  364. break;
  365. default:
  366. /**
  367. * @see Zend_Auth_Adapter_Exception
  368. */
  369. #require_once 'Zend/Auth/Adapter/Exception.php';
  370. throw new Zend_Auth_Adapter_Exception('Unsupported authentication scheme');
  371. }
  372. return $result;
  373. }
  374. /**
  375. * Challenge Client
  376. *
  377. * Sets a 401 or 407 Unauthorized response code, and creates the
  378. * appropriate Authenticate header(s) to prompt for credentials.
  379. *
  380. * @return Zend_Auth_Result Always returns a non-identity Auth result
  381. */
  382. protected function _challengeClient()
  383. {
  384. if ($this->_imaProxy) {
  385. $statusCode = 407;
  386. $headerName = 'Proxy-Authenticate';
  387. } else {
  388. $statusCode = 401;
  389. $headerName = 'WWW-Authenticate';
  390. }
  391. $this->_response->setHttpResponseCode($statusCode);
  392. // Send a challenge in each acceptable authentication scheme
  393. if (in_array('basic', $this->_acceptSchemes)) {
  394. $this->_response->setHeader($headerName, $this->_basicHeader());
  395. }
  396. if (in_array('digest', $this->_acceptSchemes)) {
  397. $this->_response->setHeader($headerName, $this->_digestHeader());
  398. }
  399. return new Zend_Auth_Result(
  400. Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
  401. array(),
  402. array('Invalid or absent credentials; challenging client')
  403. );
  404. }
  405. /**
  406. * Basic Header
  407. *
  408. * Generates a Proxy- or WWW-Authenticate header value in the Basic
  409. * authentication scheme.
  410. *
  411. * @return string Authenticate header value
  412. */
  413. protected function _basicHeader()
  414. {
  415. return 'Basic realm="' . $this->_realm . '"';
  416. }
  417. /**
  418. * Digest Header
  419. *
  420. * Generates a Proxy- or WWW-Authenticate header value in the Digest
  421. * authentication scheme.
  422. *
  423. * @return string Authenticate header value
  424. */
  425. protected function _digestHeader()
  426. {
  427. $wwwauth = 'Digest realm="' . $this->_realm . '", '
  428. . 'domain="' . $this->_domains . '", '
  429. . 'nonce="' . $this->_calcNonce() . '", '
  430. . ($this->_useOpaque ? 'opaque="' . $this->_calcOpaque() . '", ' : '')
  431. . 'algorithm="' . $this->_algo . '", '
  432. . 'qop="' . implode(',', $this->_supportedQops) . '"';
  433. return $wwwauth;
  434. }
  435. /**
  436. * Basic Authentication
  437. *
  438. * @param string $header Client's Authorization header
  439. * @throws Zend_Auth_Adapter_Exception
  440. * @return Zend_Auth_Result
  441. */
  442. protected function _basicAuth($header)
  443. {
  444. if (empty($header)) {
  445. /**
  446. * @see Zend_Auth_Adapter_Exception
  447. */
  448. #require_once 'Zend/Auth/Adapter/Exception.php';
  449. throw new Zend_Auth_Adapter_Exception('The value of the client Authorization header is required');
  450. }
  451. if (empty($this->_basicResolver)) {
  452. /**
  453. * @see Zend_Auth_Adapter_Exception
  454. */
  455. #require_once 'Zend/Auth/Adapter/Exception.php';
  456. throw new Zend_Auth_Adapter_Exception('A basicResolver object must be set before doing Basic '
  457. . 'authentication');
  458. }
  459. // Decode the Authorization header
  460. $auth = substr($header, strlen('Basic '));
  461. $auth = base64_decode($auth);
  462. if (!$auth) {
  463. /**
  464. * @see Zend_Auth_Adapter_Exception
  465. */
  466. #require_once 'Zend/Auth/Adapter/Exception.php';
  467. throw new Zend_Auth_Adapter_Exception('Unable to base64_decode Authorization header value');
  468. }
  469. // See ZF-1253. Validate the credentials the same way the digest
  470. // implementation does. If invalid credentials are detected,
  471. // re-challenge the client.
  472. if (!ctype_print($auth)) {
  473. return $this->_challengeClient();
  474. }
  475. // Fix for ZF-1515: Now re-challenges on empty username or password
  476. $creds = array_filter(explode(':', $auth));
  477. if (count($creds) != 2) {
  478. return $this->_challengeClient();
  479. }
  480. $password = $this->_basicResolver->resolve($creds[0], $this->_realm);
  481. if ($password && $this->_secureStringCompare($password, $creds[1])) {
  482. $identity = array('username'=>$creds[0], 'realm'=>$this->_realm);
  483. return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
  484. } else {
  485. return $this->_challengeClient();
  486. }
  487. }
  488. /**
  489. * Digest Authentication
  490. *
  491. * @param string $header Client's Authorization header
  492. * @throws Zend_Auth_Adapter_Exception
  493. * @return Zend_Auth_Result Valid auth result only on successful auth
  494. */
  495. protected function _digestAuth($header)
  496. {
  497. if (empty($header)) {
  498. /**
  499. * @see Zend_Auth_Adapter_Exception
  500. */
  501. #require_once 'Zend/Auth/Adapter/Exception.php';
  502. throw new Zend_Auth_Adapter_Exception('The value of the client Authorization header is required');
  503. }
  504. if (empty($this->_digestResolver)) {
  505. /**
  506. * @see Zend_Auth_Adapter_Exception
  507. */
  508. #require_once 'Zend/Auth/Adapter/Exception.php';
  509. throw new Zend_Auth_Adapter_Exception('A digestResolver object must be set before doing Digest authentication');
  510. }
  511. $data = $this->_parseDigestAuth($header);
  512. if ($data === false) {
  513. $this->_response->setHttpResponseCode(400);
  514. return new Zend_Auth_Result(
  515. Zend_Auth_Result::FAILURE_UNCATEGORIZED,
  516. array(),
  517. array('Invalid Authorization header format')
  518. );
  519. }
  520. // See ZF-1052. This code was a bit too unforgiving of invalid
  521. // usernames. Now, if the username is bad, we re-challenge the client.
  522. if ('::invalid::' == $data['username']) {
  523. return $this->_challengeClient();
  524. }
  525. // Verify that the client sent back the same nonce
  526. if ($this->_calcNonce() != $data['nonce']) {
  527. return $this->_challengeClient();
  528. }
  529. // The opaque value is also required to match, but of course IE doesn't
  530. // play ball.
  531. if (!$this->_ieNoOpaque && $this->_calcOpaque() != $data['opaque']) {
  532. return $this->_challengeClient();
  533. }
  534. // Look up the user's password hash. If not found, deny access.
  535. // This makes no assumptions about how the password hash was
  536. // constructed beyond that it must have been built in such a way as
  537. // to be recreatable with the current settings of this object.
  538. $ha1 = $this->_digestResolver->resolve($data['username'], $data['realm']);
  539. if ($ha1 === false) {
  540. return $this->_challengeClient();
  541. }
  542. // If MD5-sess is used, a1 value is made of the user's password
  543. // hash with the server and client nonce appended, separated by
  544. // colons.
  545. if ($this->_algo == 'MD5-sess') {
  546. $ha1 = hash('md5', $ha1 . ':' . $data['nonce'] . ':' . $data['cnonce']);
  547. }
  548. // Calculate h(a2). The value of this hash depends on the qop
  549. // option selected by the client and the supported hash functions
  550. switch ($data['qop']) {
  551. case 'auth':
  552. $a2 = $this->_request->getMethod() . ':' . $data['uri'];
  553. break;
  554. case 'auth-int':
  555. // Should be REQUEST_METHOD . ':' . uri . ':' . hash(entity-body),
  556. // but this isn't supported yet, so fall through to default case
  557. default:
  558. /**
  559. * @see Zend_Auth_Adapter_Exception
  560. */
  561. #require_once 'Zend/Auth/Adapter/Exception.php';
  562. throw new Zend_Auth_Adapter_Exception('Client requested an unsupported qop option');
  563. }
  564. // Using hash() should make parameterizing the hash algorithm
  565. // easier
  566. $ha2 = hash('md5', $a2);
  567. // Calculate the server's version of the request-digest. This must
  568. // match $data['response']. See RFC 2617, section 3.2.2.1
  569. $message = $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' . $ha2;
  570. $digest = hash('md5', $ha1 . ':' . $message);
  571. // If our digest matches the client's let them in, otherwise return
  572. // a 401 code and exit to prevent access to the protected resource.
  573. if ($this->_secureStringCompare($digest, $data['response'])) {
  574. $identity = array('username'=>$data['username'], 'realm'=>$data['realm']);
  575. return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
  576. } else {
  577. return $this->_challengeClient();
  578. }
  579. }
  580. /**
  581. * Calculate Nonce
  582. *
  583. * @return string The nonce value
  584. */
  585. protected function _calcNonce()
  586. {
  587. // Once subtle consequence of this timeout calculation is that it
  588. // actually divides all of time into _nonceTimeout-sized sections, such
  589. // that the value of timeout is the point in time of the next
  590. // approaching "boundary" of a section. This allows the server to
  591. // consistently generate the same timeout (and hence the same nonce
  592. // value) across requests, but only as long as one of those
  593. // "boundaries" is not crossed between requests. If that happens, the
  594. // nonce will change on its own, and effectively log the user out. This
  595. // would be surprising if the user just logged in.
  596. $timeout = ceil(time() / $this->_nonceTimeout) * $this->_nonceTimeout;
  597. $nonce = hash('md5', $timeout . ':' . $this->_request->getServer('HTTP_USER_AGENT') . ':' . __CLASS__);
  598. return $nonce;
  599. }
  600. /**
  601. * Calculate Opaque
  602. *
  603. * The opaque string can be anything; the client must return it exactly as
  604. * it was sent. It may be useful to store data in this string in some
  605. * applications. Ideally, a new value for this would be generated each time
  606. * a WWW-Authenticate header is sent (in order to reduce predictability),
  607. * but we would have to be able to create the same exact value across at
  608. * least two separate requests from the same client.
  609. *
  610. * @return string The opaque value
  611. */
  612. protected function _calcOpaque()
  613. {
  614. return hash('md5', 'Opaque Data:' . __CLASS__);
  615. }
  616. /**
  617. * Parse Digest Authorization header
  618. *
  619. * @param string $header Client's Authorization: HTTP header
  620. * @return array|false Data elements from header, or false if any part of
  621. * the header is invalid
  622. */
  623. protected function _parseDigestAuth($header)
  624. {
  625. $temp = null;
  626. $data = array();
  627. // See ZF-1052. Detect invalid usernames instead of just returning a
  628. // 400 code.
  629. $ret = preg_match('/username="([^"]+)"/', $header, $temp);
  630. if (!$ret || empty($temp[1])
  631. || !ctype_print($temp[1])
  632. || strpos($temp[1], ':') !== false) {
  633. $data['username'] = '::invalid::';
  634. } else {
  635. $data['username'] = $temp[1];
  636. }
  637. $temp = null;
  638. $ret = preg_match('/realm="([^"]+)"/', $header, $temp);
  639. if (!$ret || empty($temp[1])) {
  640. return false;
  641. }
  642. if (!ctype_print($temp[1]) || strpos($temp[1], ':') !== false) {
  643. return false;
  644. } else {
  645. $data['realm'] = $temp[1];
  646. }
  647. $temp = null;
  648. $ret = preg_match('/nonce="([^"]+)"/', $header, $temp);
  649. if (!$ret || empty($temp[1])) {
  650. return false;
  651. }
  652. if (!ctype_xdigit($temp[1])) {
  653. return false;
  654. } else {
  655. $data['nonce'] = $temp[1];
  656. }
  657. $temp = null;
  658. $ret = preg_match('/uri="([^"]+)"/', $header, $temp);
  659. if (!$ret || empty($temp[1])) {
  660. return false;
  661. }
  662. // Section 3.2.2.5 in RFC 2617 says the authenticating server must
  663. // verify that the URI field in the Authorization header is for the
  664. // same resource requested in the Request Line.
  665. $rUri = @parse_url($this->_request->getRequestUri());
  666. $cUri = @parse_url($temp[1]);
  667. if (false === $rUri || false === $cUri) {
  668. return false;
  669. } else {
  670. // Make sure the path portion of both URIs is the same
  671. if ($rUri['path'] != $cUri['path']) {
  672. return false;
  673. }
  674. // Section 3.2.2.5 seems to suggest that the value of the URI
  675. // Authorization field should be made into an absolute URI if the
  676. // Request URI is absolute, but it's vague, and that's a bunch of
  677. // code I don't want to write right now.
  678. $data['uri'] = $temp[1];
  679. }
  680. $temp = null;
  681. $ret = preg_match('/response="([^"]+)"/', $header, $temp);
  682. if (!$ret || empty($temp[1])) {
  683. return false;
  684. }
  685. if (32 != strlen($temp[1]) || !ctype_xdigit($temp[1])) {
  686. return false;
  687. } else {
  688. $data['response'] = $temp[1];
  689. }
  690. $temp = null;
  691. // The spec says this should default to MD5 if omitted. OK, so how does
  692. // that square with the algo we send out in the WWW-Authenticate header,
  693. // if it can easily be overridden by the client?
  694. $ret = preg_match('/algorithm="?(' . $this->_algo . ')"?/', $header, $temp);
  695. if ($ret && !empty($temp[1])
  696. && in_array($temp[1], $this->_supportedAlgos)) {
  697. $data['algorithm'] = $temp[1];
  698. } else {
  699. $data['algorithm'] = 'MD5'; // = $this->_algo; ?
  700. }
  701. $temp = null;
  702. // Not optional in this implementation
  703. $ret = preg_match('/cnonce="([^"]+)"/', $header, $temp);
  704. if (!$ret || empty($temp[1])) {
  705. return false;
  706. }
  707. if (!ctype_print($temp[1])) {
  708. return false;
  709. } else {
  710. $data['cnonce'] = $temp[1];
  711. }
  712. $temp = null;
  713. // If the server sent an opaque value, the client must send it back
  714. if ($this->_useOpaque) {
  715. $ret = preg_match('/opaque="([^"]+)"/', $header, $temp);
  716. if (!$ret || empty($temp[1])) {
  717. // Big surprise: IE isn't RFC 2617-compliant.
  718. if (false !== strpos($this->_request->getHeader('User-Agent'), 'MSIE')) {
  719. $temp[1] = '';
  720. $this->_ieNoOpaque = true;
  721. } else {
  722. return false;
  723. }
  724. }
  725. // This implementation only sends MD5 hex strings in the opaque value
  726. if (!$this->_ieNoOpaque &&
  727. (32 != strlen($temp[1]) || !ctype_xdigit($temp[1]))) {
  728. return false;
  729. } else {
  730. $data['opaque'] = $temp[1];
  731. }
  732. $temp = null;
  733. }
  734. // Not optional in this implementation, but must be one of the supported
  735. // qop types
  736. $ret = preg_match('/qop="?(' . implode('|', $this->_supportedQops) . ')"?/', $header, $temp);
  737. if (!$ret || empty($temp[1])) {
  738. return false;
  739. }
  740. if (!in_array($temp[1], $this->_supportedQops)) {
  741. return false;
  742. } else {
  743. $data['qop'] = $temp[1];
  744. }
  745. $temp = null;
  746. // Not optional in this implementation. The spec says this value
  747. // shouldn't be a quoted string, but apparently some implementations
  748. // quote it anyway. See ZF-1544.
  749. $ret = preg_match('/nc="?([0-9A-Fa-f]{8})"?/', $header, $temp);
  750. if (!$ret || empty($temp[1])) {
  751. return false;
  752. }
  753. if (8 != strlen($temp[1]) || !ctype_xdigit($temp[1])) {
  754. return false;
  755. } else {
  756. $data['nc'] = $temp[1];
  757. }
  758. $temp = null;
  759. return $data;
  760. }
  761. /**
  762. * Securely compare two strings for equality while avoided C level memcmp()
  763. * optimisations capable of leaking timing information useful to an attacker
  764. * attempting to iteratively guess the unknown string (e.g. password) being
  765. * compared against.
  766. *
  767. * @param string $a
  768. * @param string $b
  769. * @return bool
  770. */
  771. protected function _secureStringCompare($a, $b)
  772. {
  773. if (strlen($a) !== strlen($b)) {
  774. return false;
  775. }
  776. $result = 0;
  777. for ($i = 0; $i < strlen($a); $i++) {
  778. $result |= ord($a[$i]) ^ ord($b[$i]);
  779. }
  780. return $result == 0;
  781. }
  782. }