Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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