PageRenderTime 73ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Authentication/Adapter/Http.php

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