1 | <?php |
||
39 | class JwtAuthenticate extends BaseAuthenticate |
||
40 | { |
||
41 | /** |
||
42 | * Parsed token. |
||
43 | * |
||
44 | * @var string|null |
||
45 | */ |
||
46 | protected $_token; |
||
47 | |||
48 | /** |
||
49 | * Payload data. |
||
50 | * |
||
51 | * @var object|null |
||
52 | */ |
||
53 | protected $_payload; |
||
54 | |||
55 | /** |
||
56 | * Exception. |
||
57 | * |
||
58 | * @var \Exception |
||
59 | */ |
||
60 | protected $_error; |
||
61 | |||
62 | /** |
||
63 | * Constructor. |
||
64 | * |
||
65 | * Settings for this object. |
||
66 | * |
||
67 | * - `header` - Header name to check. Defaults to `'authorization'`. |
||
68 | * - `prefix` - Token prefix. Defaults to `'bearer'`. |
||
69 | * - `parameter` - The url parameter name of the token. Defaults to `token`. |
||
70 | * First $_SERVER['HTTP_AUTHORIZATION'] is checked for token value. |
||
71 | * Its value should be of form "Bearer <token>". If empty this query string |
||
72 | * paramater is checked. |
||
73 | * - `allowedAlgs` - List of supported verification algorithms. |
||
74 | * Defaults to ['HS256']. See API of JWT::decode() for more info. |
||
75 | * - `queryDatasource` - Boolean indicating whether the `sub` claim of JWT |
||
76 | * token should be used to query the user model and get user record. If |
||
77 | * set to `false` JWT's payload is directly retured. Defaults to `true`. |
||
78 | * - `userModel` - The model name of users, defaults to `Users`. |
||
79 | * - `fields` - Key `username` denotes the identifier field for fetching user |
||
80 | * record. The `sub` claim of JWT must contain identifier value. |
||
81 | * Defaults to ['username' => 'id']. |
||
82 | * - `finder` - Finder method. |
||
83 | * - `unauthenticatedException` - Fully namespaced exception name. Exception to |
||
84 | * throw if authentication fails. Set to false to do nothing. |
||
85 | * Defaults to '\Cake\Htttp\Exception\UnauthorizedException'. |
||
86 | * - `key` - The key, or map of keys used to decode JWT. If not set, value |
||
87 | * of Security::salt() will be used. |
||
88 | * |
||
89 | * @param Action $action AbstractClass with implementations |
||
90 | * used on this request. |
||
91 | * @param array $config Array of config to use. |
||
92 | */ |
||
93 | public function __construct(Action $action, array $config = []) |
||
117 | |||
118 | /** |
||
119 | * Get user record based on info available in JWT. |
||
120 | * |
||
121 | * @param \Cake\Http\ServerRequest $request The request object. |
||
122 | * @param \Cake\Http\Response $response Response object. |
||
123 | * |
||
124 | * @throws Exception |
||
125 | * |
||
126 | * @return bool|array User record array or false on failure. |
||
127 | */ |
||
128 | public function authenticate(ServerRequest $request, Response $response) |
||
132 | |||
133 | /** |
||
134 | * Get user record based on info available in JWT. |
||
135 | * |
||
136 | * @param \Cake\Http\ServerRequest $request Request object. |
||
137 | * |
||
138 | * @throws Exception |
||
139 | * |
||
140 | * @return bool|array User record array or false on failure. |
||
141 | */ |
||
142 | public function getUser(ServerRequest $request) |
||
167 | |||
168 | /** |
||
169 | * Get payload data. |
||
170 | * |
||
171 | * @param \Cake\Http\ServerRequest|null $request Request instance or null |
||
172 | * |
||
173 | * @throws Exception |
||
174 | * |
||
175 | * @return object|null Payload object on success, null on failurec |
||
176 | */ |
||
177 | public function getPayload($request = null) |
||
192 | |||
193 | /** |
||
194 | * Get token from header or query string. |
||
195 | * |
||
196 | * @param \Cake\Http\ServerRequest|null $request Request object. |
||
197 | * |
||
198 | * @return string|null Token string if found else null. |
||
199 | */ |
||
200 | public function getToken($request = null) |
||
224 | |||
225 | /** |
||
226 | * Decode JWT token. |
||
227 | * |
||
228 | * @param string $token JWT token to decode. |
||
229 | * |
||
230 | * @throws Exception |
||
231 | * |
||
232 | * @return object|null The JWT's payload as a PHP object, null on failure. |
||
233 | */ |
||
234 | protected function _decode($token) |
||
252 | |||
253 | /** |
||
254 | * Handles an unauthenticated access attempt. Depending on value of config |
||
255 | * `unauthenticatedException` either throws the specified exception or returns |
||
256 | * null. |
||
257 | * |
||
258 | * @param \Cake\Http\ServerRequest $request A request object. |
||
259 | * @param \Cake\Http\Response $response A response object. |
||
260 | * |
||
261 | * @throws \Cake\Http\Exception\UnauthorizedException Or any other |
||
262 | * configured exception. |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | public function unauthenticated(ServerRequest $request, Response $response) |
||
279 | } |
||
280 |