@@ -88,15 +88,21 @@ class JwtAuthenticate extends BaseAuthenticate
8888 */
8989 public function __construct (ComponentRegistry $ registry , $ config )
9090 {
91- $ this -> setConfig ( [
91+ $ defaultConfig = [
9292 'header ' => 'authorization ' ,
9393 'prefix ' => 'bearer ' ,
9494 'parameter ' => 'token ' ,
9595 'queryDatasource ' => true ,
9696 'fields ' => ['username ' => 'id ' ],
9797 'unauthenticatedException ' => UnauthorizedException::class,
9898 'key ' => null ,
99- ]);
99+ ];
100+
101+ if (!class_exists (UnauthorizedException::class, false )) {
102+ $ defaultConfig ['unauthenticatedException ' ] = 'Cake\Network\Exception\UnauthorizedException ' ;
103+ }
104+
105+ $ this ->setConfig ($ defaultConfig );
100106
101107 if (empty ($ config ['allowedAlgs ' ])) {
102108 $ config ['allowedAlgs ' ] = ['HS256 ' ];
@@ -177,15 +183,15 @@ public function getPayload($request = null)
177183 /**
178184 * Get token from header or query string.
179185 *
180- * @param \Cake\Network\Request |null $request Request object.
186+ * @param \Cake\Http\ServerRequest |null $request Request object.
181187 *
182188 * @return string|null Token string if found else null.
183189 */
184190 public function getToken ($ request = null )
185191 {
186192 $ config = $ this ->_config ;
187193
188- if (! $ request ) {
194+ if ($ request === null ) {
189195 return $ this ->_token ;
190196 }
191197
@@ -195,7 +201,10 @@ public function getToken($request = null)
195201 }
196202
197203 if (!empty ($ this ->_config ['parameter ' ])) {
198- $ this ->_token = $ request ->getQuery ($ this ->_config ['parameter ' ]);
204+ $ token = $ request ->getQuery ($ this ->_config ['parameter ' ]);
205+ if ($ token !== null ) {
206+ $ token = (string )$ token ;
207+ }
199208 }
200209
201210 return $ this ->_token ;
@@ -246,7 +255,9 @@ public function unauthenticated(ServerRequest $request, Response $response)
246255 return ;
247256 }
248257
249- $ message = $ this ->_error ? $ this ->_error ->getMessage () : $ this ->_registry ->Auth ->_config ['authError ' ];
258+ $ message = $ this ->_error
259+ ? $ this ->_error ->getMessage ()
260+ : $ this ->_registry ->get ('Auth ' )->getConfig ('authError ' );
250261
251262 $ exception = new $ this ->_config ['unauthenticatedException ' ]($ message );
252263 throw $ exception ;
0 commit comments