BasicAuthenticate.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('BaseAuthenticate', 'Controller/Component/Auth');
  15. /**
  16. * Basic Authentication adapter for AuthComponent.
  17. *
  18. * Provides Basic HTTP authentication support for AuthComponent. Basic Auth will
  19. * authenticate users against the configured userModel and verify the username
  20. * and passwords match.
  21. *
  22. * ### Using Basic auth
  23. *
  24. * In your controller's components array, add auth + the required settings.
  25. * {{{
  26. * public $components = array(
  27. * 'Auth' => array(
  28. * 'authenticate' => array('Basic')
  29. * )
  30. * );
  31. * }}}
  32. *
  33. * You should also set `AuthComponent::$sessionKey = false;` in your AppController's
  34. * beforeFilter() to prevent CakePHP from sending a session cookie to the client.
  35. *
  36. * Since HTTP Basic Authentication is stateless you don't need a login() action
  37. * in your controller. The user credentials will be checked on each request. If
  38. * valid credentials are not provided, required authentication headers will be sent
  39. * by this authentication provider which triggers the login dialog in the browser/client.
  40. *
  41. * You may also want to use `$this->Auth->unauthorizedRedirect = false;`.
  42. * By default, unauthorized users are redirected to the referrer URL,
  43. * `AuthComponent::$loginAction`, or '/'. If unauthorizedRedirect is set to
  44. * false, a ForbiddenException exception is thrown instead of redirecting.
  45. *
  46. * @package Cake.Controller.Component.Auth
  47. * @since 2.0
  48. */
  49. class BasicAuthenticate extends BaseAuthenticate {
  50. /**
  51. * Constructor, completes configuration for basic authentication.
  52. *
  53. * @param ComponentCollection $collection The Component collection used on this request.
  54. * @param array $settings An array of settings.
  55. */
  56. public function __construct(ComponentCollection $collection, $settings) {
  57. parent::__construct($collection, $settings);
  58. if (empty($this->settings['realm'])) {
  59. $this->settings['realm'] = env('SERVER_NAME');
  60. }
  61. }
  62. /**
  63. * Authenticate a user using HTTP auth. Will use the configured User model and attempt a
  64. * login using HTTP auth.
  65. *
  66. * @param CakeRequest $request The request to authenticate with.
  67. * @param CakeResponse $response The response to add headers to.
  68. * @return mixed Either false on failure, or an array of user data on success.
  69. */
  70. public function authenticate(CakeRequest $request, CakeResponse $response) {
  71. return $this->getUser($request);
  72. }
  73. /**
  74. * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
  75. *
  76. * @param CakeRequest $request Request object.
  77. * @return mixed Either false or an array of user information
  78. */
  79. public function getUser(CakeRequest $request) {
  80. $username = env('PHP_AUTH_USER');
  81. $pass = env('PHP_AUTH_PW');
  82. if (!is_string($username) || $username === '' || !is_string($pass) || $pass === '') {
  83. return false;
  84. }
  85. return $this->_findUser($username, $pass);
  86. }
  87. /**
  88. * Handles an unauthenticated access attempt by sending appropriate login headers
  89. *
  90. * @param CakeRequest $request A request object.
  91. * @param CakeResponse $response A response object.
  92. * @return void
  93. * @throws UnauthorizedException
  94. */
  95. public function unauthenticated(CakeRequest $request, CakeResponse $response) {
  96. $Exception = new UnauthorizedException();
  97. $Exception->responseHeader(array($this->loginHeaders()));
  98. throw $Exception;
  99. }
  100. /**
  101. * Generate the login headers
  102. *
  103. * @return string Headers for logging in.
  104. */
  105. public function loginHeaders() {
  106. return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
  107. }
  108. }