BasicAuthentication.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Basic 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 http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. /**
  21. * Basic authentication
  22. *
  23. * @package Cake.Network.Http
  24. */
  25. class BasicAuthentication {
  26. /**
  27. * Authentication
  28. *
  29. * @param HttpSocket $http
  30. * @param array $authInfo
  31. * @return void
  32. * @see http://www.ietf.org/rfc/rfc2617.txt
  33. */
  34. public static function authentication(HttpSocket $http, &$authInfo) {
  35. if (isset($authInfo['user'], $authInfo['pass'])) {
  36. $http->request['header']['Authorization'] = self::_generateHeader($authInfo['user'], $authInfo['pass']);
  37. }
  38. }
  39. /**
  40. * Proxy Authentication
  41. *
  42. * @param HttpSocket $http
  43. * @param array $proxyInfo
  44. * @return void
  45. * @see http://www.ietf.org/rfc/rfc2617.txt
  46. */
  47. public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
  48. if (isset($proxyInfo['user'], $proxyInfo['pass'])) {
  49. $http->request['header']['Proxy-Authorization'] = self::_generateHeader($proxyInfo['user'], $proxyInfo['pass']);
  50. }
  51. }
  52. /**
  53. * Generate basic [proxy] authentication header
  54. *
  55. * @param string $user
  56. * @param string $pass
  57. * @return string
  58. */
  59. protected static function _generateHeader($user, $pass) {
  60. return 'Basic ' . base64_encode($user . ':' . $pass);
  61. }
  62. }