PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Network/Http/DigestAuthentication.php

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