PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/www/system/library/Zend/Auth/Adapter/Http.php

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