BasicAuthenticate.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * PHP 5
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. App::uses('BaseAuthenticate', 'Controller/Component/Auth');
  17. /**
  18. * Basic Authentication adapter for AuthComponent.
  19. *
  20. * Provides Basic HTTP authentication support for AuthComponent. Basic Auth will authenticate users
  21. * against the configured userModel and verify the username and passwords match. Clients using Basic Authentication
  22. * must support cookies. Since AuthComponent identifies users based on Session contents, clients using Basic
  23. * Auth must support cookies.
  24. *
  25. * ### Using Basic auth
  26. *
  27. * In your controller's components array, add auth + the required settings.
  28. * {{{
  29. * public $components = array(
  30. * 'Auth' => array(
  31. * 'authenticate' => array('Basic')
  32. * )
  33. * );
  34. * }}}
  35. *
  36. * In your login function just call `$this->Auth->login()` without any checks for POST data. This
  37. * will send the authentication headers, and trigger the login dialog in the browser/client.
  38. *
  39. * @package Cake.Controller.Component.Auth
  40. * @since 2.0
  41. */
  42. class BasicAuthenticate extends BaseAuthenticate {
  43. /**
  44. * Settings for this object.
  45. *
  46. * - `fields` The fields to use to identify a user by.
  47. * - `userModel` The model name of the User, defaults to User.
  48. * - `scope` Additional conditions to use when looking up and authenticating users,
  49. * i.e. `array('User.is_active' => 1).`
  50. * - `recursive` The value of the recursive key passed to find(). Defaults to 0.
  51. * - `contain` Extra models to contain and store in session.
  52. * - `realm` The realm authentication is for. Defaults the server name.
  53. *
  54. * @var array
  55. */
  56. public $settings = array(
  57. 'fields' => array(
  58. 'username' => 'username',
  59. 'password' => 'password'
  60. ),
  61. 'userModel' => 'User',
  62. 'scope' => array(),
  63. 'recursive' => 0,
  64. 'contain' => null,
  65. 'realm' => '',
  66. );
  67. /**
  68. * Constructor, completes configuration for basic authentication.
  69. *
  70. * @param ComponentCollection $collection The Component collection used on this request.
  71. * @param array $settings An array of settings.
  72. */
  73. public function __construct(ComponentCollection $collection, $settings) {
  74. parent::__construct($collection, $settings);
  75. if (empty($this->settings['realm'])) {
  76. $this->settings['realm'] = env('SERVER_NAME');
  77. }
  78. }
  79. /**
  80. * Authenticate a user using HTTP auth. Will use the configured User model and attempt a
  81. * login using HTTP auth.
  82. *
  83. * @param CakeRequest $request The request to authenticate with.
  84. * @param CakeResponse $response The response to add headers to.
  85. * @return mixed Either false on failure, or an array of user data on success.
  86. */
  87. public function authenticate(CakeRequest $request, CakeResponse $response) {
  88. return $this->getUser($request);
  89. }
  90. /**
  91. * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
  92. *
  93. * @param CakeRequest $request Request object.
  94. * @return mixed Either false or an array of user information
  95. */
  96. public function getUser(CakeRequest $request) {
  97. $username = env('PHP_AUTH_USER');
  98. $pass = env('PHP_AUTH_PW');
  99. if (empty($username) || empty($pass)) {
  100. return false;
  101. }
  102. return $this->_findUser($username, $pass);
  103. }
  104. /**
  105. * Handles an unauthenticated access attempt by sending appropriate login headers
  106. *
  107. * @param CakeRequest $request A request object.
  108. * @param CakeResponse $response A response object.
  109. * @return void
  110. * @throws UnauthorizedException
  111. */
  112. public function unauthenticated(CakeRequest $request, CakeResponse $response) {
  113. $Exception = new UnauthorizedException();
  114. $Exception->responseHeader(array($this->loginHeaders()));
  115. throw $Exception;
  116. }
  117. /**
  118. * Generate the login headers
  119. *
  120. * @return string Headers for logging in.
  121. */
  122. public function loginHeaders() {
  123. return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
  124. }
  125. }