DigestAuthentication.php 3.4 KB

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