BasicAuthenticate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace Cake\Controller\Component\Auth;
  17. use Cake\Controller\ComponentRegistry;
  18. use Cake\Error;
  19. use Cake\Network\Request;
  20. use Cake\Network\Response;
  21. /**
  22. * Basic Authentication adapter for AuthComponent.
  23. *
  24. * Provides Basic HTTP authentication support for AuthComponent. Basic Auth will authenticate users
  25. * against the configured userModel and verify the username and passwords match. Clients using Basic Authentication
  26. * must support cookies. Since AuthComponent identifies users based on Session contents, clients using Basic
  27. * Auth must support cookies.
  28. *
  29. * ### Using Basic auth
  30. *
  31. * In your controller's components array, add auth + the required settings.
  32. * {{{
  33. * public $components = array(
  34. * 'Auth' => array(
  35. * 'authenticate' => array('Basic')
  36. * )
  37. * );
  38. * }}}
  39. *
  40. * In your login function just call `$this->Auth->login()` without any checks for POST data. This
  41. * will send the authentication headers, and trigger the login dialog in the browser/client.
  42. *
  43. * @since 2.0
  44. */
  45. class BasicAuthenticate extends BaseAuthenticate {
  46. /**
  47. * Constructor, completes configuration for basic authentication.
  48. *
  49. * @param ComponentRegistry $registry The Component registry used on this request.
  50. * @param array $settings An array of settings.
  51. */
  52. public function __construct(ComponentRegistry $registry, $settings) {
  53. parent::__construct($registry, $settings);
  54. if (empty($this->settings['realm'])) {
  55. $this->settings['realm'] = env('SERVER_NAME');
  56. }
  57. }
  58. /**
  59. * Authenticate a user using HTTP auth. Will use the configured User model and attempt a
  60. * login using HTTP auth.
  61. *
  62. * @param Cake\Network\Request $request The request to authenticate with.
  63. * @param Cake\Network\Response $response The response to add headers to.
  64. * @return mixed Either false on failure, or an array of user data on success.
  65. */
  66. public function authenticate(Request $request, Response $response) {
  67. return $this->getUser($request);
  68. }
  69. /**
  70. * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
  71. *
  72. * @param Cake\Network\Request $request Request object.
  73. * @return mixed Either false or an array of user information
  74. */
  75. public function getUser(Request $request) {
  76. $username = env('PHP_AUTH_USER');
  77. $pass = env('PHP_AUTH_PW');
  78. if (empty($username) || empty($pass)) {
  79. return false;
  80. }
  81. return $this->_findUser($username, $pass);
  82. }
  83. /**
  84. * Handles an unauthenticated access attempt by sending appropriate login headers
  85. *
  86. * @param CakeRequest $request A request object.
  87. * @param CakeResponse $response A response object.
  88. * @return void
  89. * @throws Cake\Error\UnauthorizedException
  90. */
  91. public function unauthenticated(Request $request, Response $response) {
  92. $Exception = new Error\UnauthorizedException();
  93. $Exception->responseHeader(array($this->loginHeaders()));
  94. throw $Exception;
  95. }
  96. /**
  97. * Generate the login headers
  98. *
  99. * @return string Headers for logging in.
  100. */
  101. public function loginHeaders() {
  102. return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
  103. }
  104. }