BasicAuthentication.php 1.9 KB

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