Skip to content
This repository was archived by the owner on May 29, 2020. It is now read-only.

Commit 4533102

Browse files
committed
Merge pull request #5 from Phally/2.4-token
CakePHP 2.4 compatibilty for TokenAuthenticate.
2 parents e2481ac + b572bb7 commit 4533102

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

Controller/Component/Auth/TokenAuthenticate.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
* {{{
88
* $this->Auth->authenticate = array(
99
* 'Authenticate.Token' => array(
10+
* 'parameter' => '_token',
11+
* 'header' => 'X-MyApiTokenHeader',
12+
* 'userModel' => 'User',
13+
* 'scope' => array('User.active' => 1)
1014
* 'fields' => array(
1115
* 'username' => 'username',
1216
* 'password' => 'password',
1317
* 'token' => 'public_key',
1418
* ),
15-
* 'parameter' => '_token',
16-
* 'header' => 'X-MyApiTokenHeader',
17-
* 'userModel' => 'User',
18-
* 'scope' => array('User.active' => 1)
19+
* 'continue' => true
1920
* )
2021
* )
2122
* }}}
@@ -26,29 +27,35 @@ class TokenAuthenticate extends BaseAuthenticate {
2627
/**
2728
* Settings for this object.
2829
*
29-
* - `fields` The fields to use to identify a user by. Make sure `'token'` has been added to the array
3030
* - `parameter` The url parameter name of the token.
3131
* - `header` The token header value.
3232
* - `userModel` The model name of the User, defaults to User.
33+
* - `fields` The fields to use to identify a user by. Make sure `'token'` has been added to the array
3334
* - `scope` Additional conditions to use when looking up and authenticating users,
3435
* i.e. `array('User.is_active' => 1).`
3536
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
3637
* - `contain` Extra models to contain and store in session.
38+
* - `continue` Continue after trying token authentication or just throw the `unauthorized` exception.
39+
* - `unauthorized` Exception name to throw or a status code as an integer.
3740
*
3841
* @var array
3942
*/
4043
public $settings = array(
44+
'parameter' => '_token',
45+
'header' => 'X-ApiToken',
46+
47+
'userModel' => 'User',
4148
'fields' => array(
4249
'username' => 'username',
4350
'password' => 'password',
4451
'token' => 'token',
4552
),
46-
'parameter' => '_token',
47-
'header' => 'X-ApiToken',
48-
'userModel' => 'User',
4953
'scope' => array(),
5054
'recursive' => 0,
5155
'contain' => null,
56+
57+
'continue' => false,
58+
'unauthorized' => 'BadRequestException'
5259
);
5360

5461
/**
@@ -65,18 +72,33 @@ public function __construct(ComponentCollection $collection, $settings) {
6572
}
6673

6774
/**
75+
* Implemented because CakePHP forces you to.
6876
*
69-
* @param CakeRequest $request The request object
77+
* @param CakeRequest $request The request object.
7078
* @param CakeResponse $response response object.
71-
* @return mixed. False on login failure. An array of User data on success.
79+
* @return boolean Always false.
7280
*/
7381
public function authenticate(CakeRequest $request, CakeResponse $response) {
74-
$user = $this->getUser($request);
75-
if (!$user) {
76-
$response->statusCode(401);
77-
$response->send();
82+
return false;
83+
}
84+
85+
/**
86+
* If unauthenticated, try to authenticate and respond.
87+
*
88+
* @param CakeRequest $request The request object.
89+
* @param CakeResponse $response The response object.
90+
* @return boolean False on failure, user on success.
91+
* @throws HttpException
92+
*/
93+
public function unauthenticated(CakeRequest $request, CakeResponse $response) {
94+
if ($this->settings['continue']) {
95+
return false;
96+
}
97+
if (is_string($this->settings['unauthorized'])) {
98+
throw new $this->settings['unauthorized'];
7899
}
79-
return $user;
100+
$message = __d('authenticate', 'You are not authenticated.');
101+
throw new HttpException($message, $this->settings['unauthorized']);
80102
}
81103

82104
/**

0 commit comments

Comments
 (0)