BasicAuthenticate.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 http://www.opensource.org/licenses/mit-license.php MIT License
  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. * Constructor, completes configuration for basic authentication.
  45. *
  46. * @param ComponentCollection $collection The Component collection used on this request.
  47. * @param array $settings An array of settings.
  48. */
  49. public function __construct(ComponentCollection $collection, $settings) {
  50. parent::__construct($collection, $settings);
  51. if (empty($this->settings['realm'])) {
  52. $this->settings['realm'] = env('SERVER_NAME');
  53. }
  54. }
  55. /**
  56. * Authenticate a user using HTTP auth. Will use the configured User model and attempt a
  57. * login using HTTP auth.
  58. *
  59. * @param CakeRequest $request The request to authenticate with.
  60. * @param CakeResponse $response The response to add headers to.
  61. * @return mixed Either false on failure, or an array of user data on success.
  62. */
  63. public function authenticate(CakeRequest $request, CakeResponse $response) {
  64. return $this->getUser($request);
  65. }
  66. /**
  67. * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
  68. *
  69. * @param CakeRequest $request Request object.
  70. * @return mixed Either false or an array of user information
  71. */
  72. public function getUser(CakeRequest $request) {
  73. $username = env('PHP_AUTH_USER');
  74. $pass = env('PHP_AUTH_PW');
  75. if (empty($username) || empty($pass)) {
  76. return false;
  77. }
  78. return $this->_findUser($username, $pass);
  79. }
  80. /**
  81. * Handles an unauthenticated access attempt by sending appropriate login headers
  82. *
  83. * @param CakeRequest $request A request object.
  84. * @param CakeResponse $response A response object.
  85. * @return void
  86. * @throws UnauthorizedException
  87. */
  88. public function unauthenticated(CakeRequest $request, CakeResponse $response) {
  89. $Exception = new UnauthorizedException();
  90. $Exception->responseHeader(array($this->loginHeaders()));
  91. throw $Exception;
  92. }
  93. /**
  94. * Generate the login headers
  95. *
  96. * @return string Headers for logging in.
  97. */
  98. public function loginHeaders() {
  99. return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
  100. }
  101. }