PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/app/sitesearch/lib/Zend/Auth/Adapter/Http.php

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