PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Controller/Component/Auth/BasicAuthenticate.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 122 lines | 40 code | 8 blank | 74 comment | 4 complexity | b8a7af83f4403876dd5e5f82232d8293 MD5 | raw file
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright 2005-2011, 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-2011, 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. * - `realm` The realm authentication is for. Defaults the server name.
  50. *
  51. * @var array
  52. */
  53. public $settings = array(
  54. 'fields' => array(
  55. 'username' => 'username',
  56. 'password' => 'password'
  57. ),
  58. 'userModel' => 'User',
  59. 'scope' => array(),
  60. 'realm' => '',
  61. );
  62. /**
  63. * Constructor, completes configuration for basic authentication.
  64. *
  65. * @param ComponentCollection $collection The Component collection used on this request.
  66. * @param array $settings An array of settings.
  67. */
  68. public function __construct(ComponentCollection $collection, $settings) {
  69. parent::__construct($collection, $settings);
  70. if (empty($this->settings['realm'])) {
  71. $this->settings['realm'] = env('SERVER_NAME');
  72. }
  73. }
  74. /**
  75. * Authenticate a user using basic HTTP auth. Will use the configured User model and attempt a
  76. * login using basic HTTP auth.
  77. *
  78. * @param CakeRequest $request The request to authenticate with.
  79. * @param CakeResponse $response The response to add headers to.
  80. * @return mixed Either false on failure, or an array of user data on success.
  81. */
  82. public function authenticate(CakeRequest $request, CakeResponse $response) {
  83. $result = $this->getUser($request);
  84. if (empty($result)) {
  85. $response->header($this->loginHeaders());
  86. $response->statusCode(401);
  87. $response->send();
  88. return false;
  89. }
  90. return $result;
  91. }
  92. /**
  93. * Get a user based on information in the request. Used by cookie-less auth for stateless clients.
  94. *
  95. * @param CakeRequest $request Request object.
  96. * @return mixed Either false or an array of user information
  97. */
  98. public function getUser($request) {
  99. $username = env('PHP_AUTH_USER');
  100. $pass = env('PHP_AUTH_PW');
  101. if (empty($username) || empty($pass)) {
  102. return false;
  103. }
  104. return $this->_findUser($username, $pass);
  105. }
  106. /**
  107. * Generate the login headers
  108. *
  109. * @return string Headers for logging in.
  110. */
  111. public function loginHeaders() {
  112. return sprintf('WWW-Authenticate: Basic realm="%s"', $this->settings['realm']);
  113. }
  114. }