PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

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