BasicAuthentication.php 1.7 KB

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