DigestAuthentication.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Digest authentication
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Network.Http
  17. * @since CakePHP(tm) v 2.0.0
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. /**
  21. * Digest authentication
  22. *
  23. * @package Cake.Network.Http
  24. */
  25. class DigestAuthentication {
  26. /**
  27. * Authentication
  28. *
  29. * @param HttpSocket $http
  30. * @param array $authInfo
  31. * @return void
  32. * @link http://www.ietf.org/rfc/rfc2617.txt
  33. */
  34. public static function authentication(HttpSocket $http, &$authInfo) {
  35. if (isset($authInfo['user'], $authInfo['pass'])) {
  36. if (!isset($authInfo['realm']) && !self::_getServerInformation($http, $authInfo)) {
  37. return;
  38. }
  39. $http->request['header']['Authorization'] = self::_generateHeader($http, $authInfo);
  40. }
  41. }
  42. /**
  43. * Retrieve information about the authentication
  44. *
  45. * @param HttpSocket $http
  46. * @param array $authInfo
  47. * @return boolean
  48. */
  49. protected static function _getServerInformation(HttpSocket $http, &$authInfo) {
  50. $originalRequest = $http->request;
  51. $http->configAuth(false);
  52. $http->request($http->request);
  53. $http->request = $originalRequest;
  54. $http->configAuth('Digest', $authInfo);
  55. if (empty($http->response['header']['WWW-Authenticate'])) {
  56. return false;
  57. }
  58. preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $http->response['header']['WWW-Authenticate'], $matches, PREG_SET_ORDER);
  59. foreach ($matches as $match) {
  60. $authInfo[$match[1]] = $match[2];
  61. }
  62. if (!empty($authInfo['qop']) && empty($authInfo['nc'])) {
  63. $authInfo['nc'] = 1;
  64. }
  65. return true;
  66. }
  67. /**
  68. * Generate the header Authorization
  69. *
  70. * @param HttpSocket $http
  71. * @param array $authInfo
  72. * @return string
  73. */
  74. protected static function _generateHeader(HttpSocket $http, &$authInfo) {
  75. $a1 = md5($authInfo['user'] . ':' . $authInfo['realm'] . ':' . $authInfo['pass']);
  76. $a2 = md5($http->request['method'] . ':' . $http->request['uri']['path']);
  77. if (empty($authInfo['qop'])) {
  78. $response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $a2);
  79. } else {
  80. $authInfo['cnonce'] = uniqid();
  81. $nc = sprintf('%08x', $authInfo['nc']++);
  82. $response = md5($a1 . ':' . $authInfo['nonce'] . ':' . $nc . ':' . $authInfo['cnonce'] . ':auth:' . $a2);
  83. }
  84. $authHeader = 'Digest ';
  85. $authHeader .= 'username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $authInfo['user']) . '", ';
  86. $authHeader .= 'realm="' . $authInfo['realm'] . '", ';
  87. $authHeader .= 'nonce="' . $authInfo['nonce'] . '", ';
  88. $authHeader .= 'uri="' . $http->request['uri']['path'] . '", ';
  89. $authHeader .= 'response="' . $response . '"';
  90. if (!empty($authInfo['opaque'])) {
  91. $authHeader .= ', opaque="' . $authInfo['opaque'] . '"';
  92. }
  93. if (!empty($authInfo['qop'])) {
  94. $authHeader .= ', qop="auth", nc=' . $nc . ', cnonce="' . $authInfo['cnonce'] . '"';
  95. }
  96. return $authHeader;
  97. }
  98. }