PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

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