7
7
* {{{
8
8
* $this->Auth->authenticate = array(
9
9
* 'Authenticate.Token' => array(
10
+ * 'parameter' => '_token',
11
+ * 'header' => 'X-MyApiTokenHeader',
12
+ * 'userModel' => 'User',
13
+ * 'scope' => array('User.active' => 1)
10
14
* 'fields' => array(
11
15
* 'username' => 'username',
12
16
* 'password' => 'password',
13
17
* 'token' => 'public_key',
14
18
* ),
15
- * 'parameter' => '_token',
16
- * 'header' => 'X-MyApiTokenHeader',
17
- * 'userModel' => 'User',
18
- * 'scope' => array('User.active' => 1)
19
+ * 'continue' => true
19
20
* )
20
21
* )
21
22
* }}}
@@ -26,29 +27,35 @@ class TokenAuthenticate extends BaseAuthenticate {
26
27
/**
27
28
* Settings for this object.
28
29
*
29
- * - `fields` The fields to use to identify a user by. Make sure `'token'` has been added to the array
30
30
* - `parameter` The url parameter name of the token.
31
31
* - `header` The token header value.
32
32
* - `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
33
34
* - `scope` Additional conditions to use when looking up and authenticating users,
34
35
* i.e. `array('User.is_active' => 1).`
35
36
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
36
37
* - `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.
37
40
*
38
41
* @var array
39
42
*/
40
43
public $ settings = array (
44
+ 'parameter ' => '_token ' ,
45
+ 'header ' => 'X-ApiToken ' ,
46
+
47
+ 'userModel ' => 'User ' ,
41
48
'fields ' => array (
42
49
'username ' => 'username ' ,
43
50
'password ' => 'password ' ,
44
51
'token ' => 'token ' ,
45
52
),
46
- 'parameter ' => '_token ' ,
47
- 'header ' => 'X-ApiToken ' ,
48
- 'userModel ' => 'User ' ,
49
53
'scope ' => array (),
50
54
'recursive ' => 0 ,
51
55
'contain ' => null ,
56
+
57
+ 'continue ' => false ,
58
+ 'unauthorized ' => 'BadRequestException '
52
59
);
53
60
54
61
/**
@@ -65,18 +72,33 @@ public function __construct(ComponentCollection $collection, $settings) {
65
72
}
66
73
67
74
/**
75
+ * Implemented because CakePHP forces you to.
68
76
*
69
- * @param CakeRequest $request The request object
77
+ * @param CakeRequest $request The request object.
70
78
* @param CakeResponse $response response object.
71
- * @return mixed. False on login failure. An array of User data on success .
79
+ * @return boolean Always false .
72
80
*/
73
81
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 ' ];
78
99
}
79
- return $ user ;
100
+ $ message = __d ('authenticate ' , 'You are not authenticated. ' );
101
+ throw new HttpException ($ message , $ this ->settings ['unauthorized ' ]);
80
102
}
81
103
82
104
/**
0 commit comments