forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenAuthenticate.php
More file actions
138 lines (130 loc) · 4.06 KB
/
Copy pathTokenAuthenticate.php
File metadata and controls
138 lines (130 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
/**
* An authentication adapter for AuthComponent. Provides the ability to authenticate using Token
*
* {{{
* $this->Auth->authenticate = array(
* 'Authenticate.Token' => array(
* 'fields' => array(
* 'username' => 'username',
* 'password' => 'password',
* 'token' => 'public_key',
* ),
* 'parameter' => '_token',
* 'header' => 'X-MyApiTokenHeader',
* 'userModel' => 'User',
* 'scope' => array('User.active' => 1)
* )
* )
* }}}
*
* @author Ceeram
* @copyright Ceeram
* @license MIT
* @link https://github.com/ceeram/Authenticate
*/
class TokenAuthenticate extends BaseAuthenticate {
/**
* Settings for this object.
*
* - `fields` The fields to use to identify a user by. Make sure `'token'` has been added to the array
* - `parameter` The url parameter name of the token.
* - `header` The token header value.
* - `userModel` The model name of the User, defaults to User.
* - `scope` Additional conditions to use when looking up and authenticating users,
* i.e. `array('User.is_active' => 1).`
* - `recursive` The value of the recursive key passed to find(). Defaults to 0.
* - `contain` Extra models to contain and store in session.
*
* @var array
*/
public $settings = array(
'fields' => array(
'username' => 'username',
'password' => 'password',
'token' => 'token',
),
'parameter' => '_token',
'header' => 'X-ApiToken',
'userModel' => 'User',
'scope' => array(),
'recursive' => 0,
'contain' => null,
);
/**
* Constructor
*
* @param ComponentCollection $collection The Component collection used on this request.
* @param array $settings Array of settings to use.
*/
public function __construct(ComponentCollection $collection, $settings) {
parent::__construct($collection, $settings);
if (empty($this->settings['parameter']) && empty($this->settings['header'])) {
throw new CakeException(__d('users', 'You need to specify token parameter and/or header'));
}
}
/**
*
* @param CakeRequest $request The request object
* @param CakeResponse $response response object.
* @return mixed. False on login failure. An array of User data on success.
*/
public function authenticate(CakeRequest $request, CakeResponse $response) {
$user = $this->getUser($request);
if (!$user) {
$response->statusCode(401);
$response->send();
}
return $user;
}
/**
* Get token information from the request.
*
* @param CakeRequest $request Request object.
* @return mixed Either false or an array of user information
*/
public function getUser(CakeRequest $request) {
if (!empty($this->settings['header'])) {
$token = $request->header($this->settings['header']);
if ($token) {
return $this->_findUser($token, null);
}
}
if (!empty($this->settings['parameter']) && !empty($request->query[$this->settings['parameter']])) {
$token = $request->query[$this->settings['parameter']];
return $this->_findUser($token);
}
return false;
}
/**
* Find a user record.
*
* @param string $username The token identifier.
* @param string $password Unused password.
* @return Mixed Either false on failure, or an array of user data.
*/
public function _findUser($username, $password = null) {
$userModel = $this->settings['userModel'];
list($plugin, $model) = pluginSplit($userModel);
$fields = $this->settings['fields'];
$conditions = array(
$model . '.' . $fields['token'] => $username,
);
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
}
$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => (int)$this->settings['recursive'],
'contain' => $this->settings['contain'],
));
if (empty($result) || empty($result[$model])) {
return false;
}
$user = $result[$model];
unset($user[$fields['password']]);
unset($result[$model]);
return array_merge($user, $result);
}
}