BasicAuthenticate.php 3.9 KB

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