| 16591 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* DigestAuthenticateTest file
|
|
|
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 |
* @package Cake.Test.Case.Controller.Component.Auth
|
|
|
15 |
* @since CakePHP(tm) v 2.0
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('DigestAuthenticate', 'Controller/Component/Auth');
|
|
|
20 |
App::uses('AppModel', 'Model');
|
|
|
21 |
App::uses('CakeRequest', 'Network');
|
|
|
22 |
App::uses('CakeResponse', 'Network');
|
|
|
23 |
|
|
|
24 |
require_once CAKE . 'Test' . DS . 'Case' . DS . 'Model' . DS . 'models.php';
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Test case for DigestAuthentication
|
|
|
28 |
*
|
|
|
29 |
* @package Cake.Test.Case.Controller.Component.Auth
|
|
|
30 |
*/
|
|
|
31 |
class DigestAuthenticateTest extends CakeTestCase {
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Fixtures
|
|
|
35 |
*
|
|
|
36 |
* @var array
|
|
|
37 |
*/
|
|
|
38 |
public $fixtures = array('core.user', 'core.auth_user');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* setup
|
|
|
42 |
*
|
|
|
43 |
* @return void
|
|
|
44 |
*/
|
|
|
45 |
public function setUp() {
|
|
|
46 |
parent::setUp();
|
|
|
47 |
$this->Collection = $this->getMock('ComponentCollection');
|
|
|
48 |
$this->server = $_SERVER;
|
|
|
49 |
$this->auth = new DigestAuthenticate($this->Collection, array(
|
|
|
50 |
'fields' => array('username' => 'user', 'password' => 'password'),
|
|
|
51 |
'userModel' => 'User',
|
|
|
52 |
'realm' => 'localhost',
|
|
|
53 |
'nonce' => 123,
|
|
|
54 |
'opaque' => '123abc'
|
|
|
55 |
));
|
|
|
56 |
|
|
|
57 |
$password = DigestAuthenticate::password('mariano', 'cake', 'localhost');
|
|
|
58 |
$User = ClassRegistry::init('User');
|
|
|
59 |
$User->updateAll(array('password' => $User->getDataSource()->value($password)));
|
|
|
60 |
|
|
|
61 |
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
62 |
$this->response = $this->getMock('CakeResponse');
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* tearDown
|
|
|
67 |
*
|
|
|
68 |
* @return void
|
|
|
69 |
*/
|
|
|
70 |
public function tearDown() {
|
|
|
71 |
parent::tearDown();
|
|
|
72 |
$_SERVER = $this->server;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* test applying settings in the constructor
|
|
|
77 |
*
|
|
|
78 |
* @return void
|
|
|
79 |
*/
|
|
|
80 |
public function testConstructor() {
|
|
|
81 |
$object = new DigestAuthenticate($this->Collection, array(
|
|
|
82 |
'userModel' => 'AuthUser',
|
|
|
83 |
'fields' => array('username' => 'user', 'password' => 'password'),
|
|
|
84 |
'nonce' => 123456
|
|
|
85 |
));
|
|
|
86 |
$this->assertEquals('AuthUser', $object->settings['userModel']);
|
|
|
87 |
$this->assertEquals(array('username' => 'user', 'password' => 'password'), $object->settings['fields']);
|
|
|
88 |
$this->assertEquals(123456, $object->settings['nonce']);
|
|
|
89 |
$this->assertEquals(env('SERVER_NAME'), $object->settings['realm']);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* test the authenticate method
|
|
|
94 |
*
|
|
|
95 |
* @return void
|
|
|
96 |
*/
|
|
|
97 |
public function testAuthenticateNoData() {
|
|
|
98 |
$request = new CakeRequest('posts/index', false);
|
|
|
99 |
|
|
|
100 |
$this->response->expects($this->never())
|
|
|
101 |
->method('header');
|
|
|
102 |
|
|
|
103 |
$this->assertFalse($this->auth->getUser($request, $this->response));
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* test the authenticate method
|
|
|
108 |
*
|
|
|
109 |
* @expectedException UnauthorizedException
|
|
|
110 |
* @expectedExceptionCode 401
|
|
|
111 |
* @return void
|
|
|
112 |
*/
|
|
|
113 |
public function testAuthenticateWrongUsername() {
|
|
|
114 |
$request = new CakeRequest('posts/index', false);
|
|
|
115 |
$request->addParams(array('pass' => array(), 'named' => array()));
|
|
|
116 |
|
|
|
117 |
$_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
|
|
|
118 |
Digest username="incorrect_user",
|
|
|
119 |
realm="localhost",
|
|
|
120 |
nonce="123456",
|
|
|
121 |
uri="/dir/index.html",
|
|
|
122 |
qop=auth,
|
|
|
123 |
nc=00000001,
|
|
|
124 |
cnonce="0a4f113b",
|
|
|
125 |
response="6629fae49393a05397450978507c4ef1",
|
|
|
126 |
opaque="123abc"
|
|
|
127 |
DIGEST;
|
|
|
128 |
|
|
|
129 |
$this->auth->unauthenticated($request, $this->response);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* test that challenge headers are sent when no credentials are found.
|
|
|
134 |
*
|
|
|
135 |
* @return void
|
|
|
136 |
*/
|
|
|
137 |
public function testAuthenticateChallenge() {
|
|
|
138 |
$request = new CakeRequest('posts/index', false);
|
|
|
139 |
$request->addParams(array('pass' => array(), 'named' => array()));
|
|
|
140 |
|
|
|
141 |
try {
|
|
|
142 |
$this->auth->unauthenticated($request, $this->response);
|
|
|
143 |
} catch (UnauthorizedException $e) {
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
$this->assertNotEmpty($e);
|
|
|
147 |
|
|
|
148 |
$expected = array('WWW-Authenticate: Digest realm="localhost",qop="auth",nonce="123",opaque="123abc"');
|
|
|
149 |
$this->assertEquals($expected, $e->responseHeader());
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* test authenticate success
|
|
|
154 |
*
|
|
|
155 |
* @return void
|
|
|
156 |
*/
|
|
|
157 |
public function testAuthenticateSuccess() {
|
|
|
158 |
$request = new CakeRequest('posts/index', false);
|
|
|
159 |
$request->addParams(array('pass' => array(), 'named' => array()));
|
|
|
160 |
|
|
|
161 |
$_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
|
|
|
162 |
Digest username="mariano",
|
|
|
163 |
realm="localhost",
|
|
|
164 |
nonce="123",
|
|
|
165 |
uri="/dir/index.html",
|
|
|
166 |
qop=auth,
|
|
|
167 |
nc=1,
|
|
|
168 |
cnonce="123",
|
|
|
169 |
response="06b257a54befa2ddfb9bfa134224aa29",
|
|
|
170 |
opaque="123abc"
|
|
|
171 |
DIGEST;
|
|
|
172 |
|
|
|
173 |
$result = $this->auth->authenticate($request, $this->response);
|
|
|
174 |
$expected = array(
|
|
|
175 |
'id' => 1,
|
|
|
176 |
'user' => 'mariano',
|
|
|
177 |
'created' => '2007-03-17 01:16:23',
|
|
|
178 |
'updated' => '2007-03-17 01:18:31'
|
|
|
179 |
);
|
|
|
180 |
$this->assertEquals($expected, $result);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* test scope failure.
|
|
|
185 |
*
|
|
|
186 |
* @expectedException UnauthorizedException
|
|
|
187 |
* @expectedExceptionCode 401
|
|
|
188 |
* @return void
|
|
|
189 |
*/
|
|
|
190 |
public function testAuthenticateFailReChallenge() {
|
|
|
191 |
$this->auth->settings['scope'] = array('user' => 'nate');
|
|
|
192 |
$request = new CakeRequest('posts/index', false);
|
|
|
193 |
$request->addParams(array('pass' => array(), 'named' => array()));
|
|
|
194 |
|
|
|
195 |
$_SERVER['PHP_AUTH_DIGEST'] = <<<DIGEST
|
|
|
196 |
Digest username="mariano",
|
|
|
197 |
realm="localhost",
|
|
|
198 |
nonce="123",
|
|
|
199 |
uri="/dir/index.html",
|
|
|
200 |
qop=auth,
|
|
|
201 |
nc=1,
|
|
|
202 |
cnonce="123",
|
|
|
203 |
response="6629fae49393a05397450978507c4ef1",
|
|
|
204 |
opaque="123abc"
|
|
|
205 |
DIGEST;
|
|
|
206 |
|
|
|
207 |
$this->auth->unauthenticated($request, $this->response);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* testParseDigestAuthData method
|
|
|
212 |
*
|
|
|
213 |
* @return void
|
|
|
214 |
*/
|
|
|
215 |
public function testParseAuthData() {
|
|
|
216 |
$digest = <<<DIGEST
|
|
|
217 |
Digest username="Mufasa",
|
|
|
218 |
realm="testrealm@host.com",
|
|
|
219 |
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
|
220 |
uri="/dir/index.html?query=string&value=some%20value",
|
|
|
221 |
qop=auth,
|
|
|
222 |
nc=00000001,
|
|
|
223 |
cnonce="0a4f113b",
|
|
|
224 |
response="6629fae49393a05397450978507c4ef1",
|
|
|
225 |
opaque="5ccc069c403ebaf9f0171e9517f40e41"
|
|
|
226 |
DIGEST;
|
|
|
227 |
$expected = array(
|
|
|
228 |
'username' => 'Mufasa',
|
|
|
229 |
'realm' => 'testrealm@host.com',
|
|
|
230 |
'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
|
|
|
231 |
'uri' => '/dir/index.html?query=string&value=some%20value',
|
|
|
232 |
'qop' => 'auth',
|
|
|
233 |
'nc' => '00000001',
|
|
|
234 |
'cnonce' => '0a4f113b',
|
|
|
235 |
'response' => '6629fae49393a05397450978507c4ef1',
|
|
|
236 |
'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
|
|
|
237 |
);
|
|
|
238 |
$result = $this->auth->parseAuthData($digest);
|
|
|
239 |
$this->assertSame($expected, $result);
|
|
|
240 |
|
|
|
241 |
$result = $this->auth->parseAuthData('');
|
|
|
242 |
$this->assertNull($result);
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Test parsing a full URI. While not part of the spec some mobile clients will do it wrong.
|
|
|
247 |
*
|
|
|
248 |
* @return void
|
|
|
249 |
*/
|
|
|
250 |
public function testParseAuthDataFullUri() {
|
|
|
251 |
$digest = <<<DIGEST
|
|
|
252 |
Digest username="admin",
|
|
|
253 |
realm="192.168.0.2",
|
|
|
254 |
nonce="53a7f9b83f61b",
|
|
|
255 |
uri="http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment",
|
|
|
256 |
qop=auth,
|
|
|
257 |
nc=00000001,
|
|
|
258 |
cnonce="b85ff144e496e6e18d1c73020566ea3b",
|
|
|
259 |
response="5894f5d9cd41d012bac09eeb89d2ddf2",
|
|
|
260 |
opaque="6f65e91667cf98dd13464deaf2739fde"
|
|
|
261 |
DIGEST;
|
|
|
262 |
|
|
|
263 |
$expected = 'http://192.168.0.2/pvcollection/sites/pull/HFD%200001.json#fragment';
|
|
|
264 |
$result = $this->auth->parseAuthData($digest);
|
|
|
265 |
$this->assertSame($expected, $result['uri']);
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
/**
|
|
|
269 |
* test parsing digest information with email addresses
|
|
|
270 |
*
|
|
|
271 |
* @return void
|
|
|
272 |
*/
|
|
|
273 |
public function testParseAuthEmailAddress() {
|
|
|
274 |
$digest = <<<DIGEST
|
|
|
275 |
Digest username="mark@example.com",
|
|
|
276 |
realm="testrealm@host.com",
|
|
|
277 |
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
|
278 |
uri="/dir/index.html",
|
|
|
279 |
qop=auth,
|
|
|
280 |
nc=00000001,
|
|
|
281 |
cnonce="0a4f113b",
|
|
|
282 |
response="6629fae49393a05397450978507c4ef1",
|
|
|
283 |
opaque="5ccc069c403ebaf9f0171e9517f40e41"
|
|
|
284 |
DIGEST;
|
|
|
285 |
$expected = array(
|
|
|
286 |
'username' => 'mark@example.com',
|
|
|
287 |
'realm' => 'testrealm@host.com',
|
|
|
288 |
'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
|
|
|
289 |
'uri' => '/dir/index.html',
|
|
|
290 |
'qop' => 'auth',
|
|
|
291 |
'nc' => '00000001',
|
|
|
292 |
'cnonce' => '0a4f113b',
|
|
|
293 |
'response' => '6629fae49393a05397450978507c4ef1',
|
|
|
294 |
'opaque' => '5ccc069c403ebaf9f0171e9517f40e41'
|
|
|
295 |
);
|
|
|
296 |
$result = $this->auth->parseAuthData($digest);
|
|
|
297 |
$this->assertSame($expected, $result);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* test password hashing
|
|
|
302 |
*
|
|
|
303 |
* @return void
|
|
|
304 |
*/
|
|
|
305 |
public function testPassword() {
|
|
|
306 |
$result = DigestAuthenticate::password('mark', 'password', 'localhost');
|
|
|
307 |
$expected = md5('mark:localhost:password');
|
|
|
308 |
$this->assertEquals($expected, $result);
|
|
|
309 |
}
|
|
|
310 |
}
|