BasicAuthenticate.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 basic HTTP auth. Will use the configured User model and attempt a
  81. * login using basic 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. $result = $this->getUser($request);
  89. if (empty($result)) {
  90. $response->header($this->loginHeaders());
  91. $response->statusCode(401);
  92. $response->send();
  93. return false;
  94. }
  95. return $result;
  96. }
  97. /**
  98. * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
  99. *
  100. * @param CakeRequest $request Request object.
  101. * @return mixed Either false or an array of user information
  102. */
  103. public function getUser(CakeRequest $request) {
  104. $username = env('PHP_AUTH_USER');
  105. $pass = env('PHP_AUTH_PW');
  106. if (empty($username) || empty($pass)) {
  107. return false;
  108. }
  109. return $this->_findUser($username, $pass);
  110. }
  111. /**
  112. * Generate the login headers
  113. *
  114. * @return string Headers for logging in.
  115. */
  116. public function loginHeaders() {
  117. return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
  118. }
  119. }