@@ -68,8 +68,8 @@ public static function login_and_get_token( $username, $password ) {
68
68
* The token is signed, now create the object with basic user data to send to the client
69
69
*/
70
70
$ response = [
71
- 'authToken ' => self ::get_signed_token ( $ user ),
72
- 'refreshToken ' => self ::get_refresh_token ( $ user ),
71
+ 'authToken ' => self ::get_signed_token ( wp_get_current_user () ),
72
+ 'refreshToken ' => self ::get_refresh_token ( wp_get_current_user () ),
73
73
'user ' => DataSource::resolve_user ( $ user ->data ->ID , \WPGraphQL::get_app_context () ),
74
74
'id ' => $ user ->data ->ID ,
75
75
];
@@ -123,7 +123,8 @@ public static function get_token_expiration() {
123
123
/**
124
124
* Retrieves validates user and retrieve signed token
125
125
*
126
- * @param User|WP_User $user Owner of the token.
126
+ * @param \WP_User $user Owner of the token.
127
+ * @param bool $cap_check Whether to check capabilities when getting the token
127
128
*
128
129
* @return null|string
129
130
*/
@@ -200,11 +201,13 @@ protected static function get_signed_token( $user, $cap_check = true ) {
200
201
*/
201
202
public static function get_user_jwt_secret ( $ user_id ) {
202
203
204
+ $ is_revoked = Auth::is_jwt_secret_revoked ( $ user_id );
205
+
203
206
/**
204
207
* If the secret has been revoked, throw an error
205
208
*/
206
- if ( true === Auth:: is_jwt_secret_revoked ( $ user_id ) ) {
207
- return new \ WP_Error ( ' graphql-jwt-revoked-secret ' , __ ( ' The JWT Auth secret cannot be returned ' , ' wp-graphql-jwt-authentication ' ) ) ;
209
+ if ( true === ( bool ) $ is_revoked ) {
210
+ return null ;
208
211
}
209
212
210
213
/**
@@ -216,11 +219,11 @@ public static function get_user_jwt_secret( $user_id ) {
216
219
$ capability = apply_filters ( 'graphql_jwt_auth_edit_users_capability ' , 'edit_users ' , $ user_id );
217
220
218
221
/**
219
- * If the request is not from the current_user AND the current_user doesn't have the proper capabilities, don't return the secret
222
+ * If the request is not from the current_user or the current_user doesn't have the proper capabilities, don't return the secret
220
223
*/
221
224
$ is_current_user = ( $ user_id === get_current_user_id () ) ? true : false ;
222
225
if ( ! $ is_current_user && ! current_user_can ( $ capability ) ) {
223
- return new \ WP_Error ( ' graphql-jwt-improper-capabilities ' , __ ( ' The JWT Auth secret for this user cannot be returned ' , ' wp-graphql-jwt-authentication ' ) ) ;
226
+ return null ;
224
227
}
225
228
226
229
/**
@@ -232,7 +235,7 @@ public static function get_user_jwt_secret( $user_id ) {
232
235
* If there is no stored secret, or it's not a string
233
236
*/
234
237
if ( empty ( $ secret ) || ! is_string ( $ secret ) ) {
235
- Auth::issue_new_user_secret ( $ user_id );
238
+ $ secret = Auth::issue_new_user_secret ( $ user_id );
236
239
}
237
240
238
241
/**
@@ -291,13 +294,21 @@ public static function is_jwt_secret_revoked( $user_id ) {
291
294
* Public method for getting an Auth token for a given user
292
295
*
293
296
* @param \WP_USer $user The user to get the token for
297
+ * @param boolean $cap_check Whether to check capabilities. Default is true.
294
298
*
295
299
* @return null|string
296
300
*/
297
301
public static function get_token ( $ user , $ cap_check = true ) {
298
302
return self ::get_signed_token ( $ user , $ cap_check );
299
303
}
300
304
305
+ /**
306
+ * Given a WP_User, this returns a refresh token for the user
307
+ * @param \WP_User $user A WP_User object
308
+ * @param bool $cap_check
309
+ *
310
+ * @return null|string
311
+ */
301
312
public static function get_refresh_token ( $ user , $ cap_check = true ) {
302
313
303
314
self ::$ is_refresh_token = true ;
@@ -309,6 +320,7 @@ public static function get_refresh_token( $user, $cap_check = true ) {
309
320
*/
310
321
add_filter ( 'graphql_jwt_auth_token_before_sign ' , function ( $ token , \WP_User $ user ) {
311
322
$ secret = Auth::get_user_jwt_secret ( $ user ->ID );
323
+
312
324
if ( ! empty ( $ secret ) && ! is_wp_error ( $ secret ) && true === self ::is_refresh_token () ) {
313
325
314
326
/**
@@ -415,7 +427,7 @@ public static function filter_determine_current_user( $user ) {
415
427
*
416
428
* @return mixed|boolean|\WP_Error
417
429
*/
418
- public static function revoke_user_secret ( int $ user_id ) {
430
+ public static function revoke_user_secret ( $ user_id ) {
419
431
420
432
/**
421
433
* Filter the capability that is tied to editing/viewing user JWT Auth info
@@ -530,7 +542,7 @@ public static function validate_token( $token = null, $refresh = false ) {
530
542
* @since 0.0.1
531
543
*/
532
544
if ( empty ( $ auth_header ) ) {
533
- return false ;
545
+ return $ token ;
534
546
} else {
535
547
/**
536
548
* The HTTP_AUTHORIZATION is present verify the format
@@ -545,52 +557,59 @@ public static function validate_token( $token = null, $refresh = false ) {
545
557
* If there's no secret key, throw an error as there needs to be a secret key for Auth to work properly
546
558
*/
547
559
if ( ! self ::get_secret_key () ) {
548
- throw new \Exception ( __ ( 'JWT is not configured properly ' , 'wp-graphql-jwt-authentication ' ) );
560
+ self ::set_status ( 403 );
561
+ return new \WP_Error ( 'invalid-secret-key ' , __ ( 'JWT is not configured properly ' , 'wp-graphql-jwt-authentication ' ) );
549
562
}
550
563
564
+
565
+
551
566
/**
552
- * Try to decode the token
567
+ * Decode the Token
553
568
*/
554
- try {
569
+ JWT :: $ leeway = 60 ;
555
570
556
- /**
557
- * Decode the Token
558
- */
559
- JWT ::$ leeway = 60 ;
571
+ $ secret = self ::get_secret_key ();
560
572
561
- $ secret = self :: get_secret_key ();
573
+ try {
562
574
$ token = ! empty ( $ token ) ? JWT ::decode ( $ token , $ secret , [ 'HS256 ' ] ) : null ;
575
+ } catch ( \Exception $ exception ) {
576
+ $ token = new \WP_Error ( 'invalid-secret-key ' , $ exception ->getMessage () );
577
+ }
563
578
564
- /**
565
- * The Token is decoded now validate the iss
566
- */
567
- if ( ! isset ( $ token ->iss ) || get_bloginfo ( 'url ' ) !== $ token ->iss ) {
568
- throw new \Exception ( __ ( 'The iss do not match with this server ' , 'wp-graphql-jwt-authentication ' ) );
569
- }
579
+ /**
580
+ * If there's no token listed, just bail now before validating an empty token.
581
+ * This will treat the request as a public request
582
+ */
583
+ if ( empty ( $ token ) ) {
584
+ return $ token ;
585
+ }
570
586
571
- /**
572
- * So far so good, validate the user id in the token
573
- */
574
- if ( ! isset ( $ token ->data -> user -> id ) ) {
575
- throw new \Exception ( __ ( 'User ID not found in the token ' , 'wp-graphql-jwt-authentication ' ) );
576
- }
587
+ /**
588
+ * The Token is decoded now validate the iss
589
+ */
590
+ if ( ! isset ( $ token ->iss ) || get_bloginfo ( ' url ' ) !== $ token -> iss ) {
591
+ return new \WP_Error ( ' invalid-jwt ' , __ ( 'The iss do not match with this server ' , 'wp-graphql-jwt-authentication ' ) );
592
+ }
577
593
578
- /**
579
- * If there is a user_secret in the token (refresh tokens) make sure it matches what
580
- */
581
- if ( isset ( $ token ->data ->user ->user_secret ) ) {
594
+ /**
595
+ * So far so good, validate the user id in the token
596
+ */
597
+ if ( ! isset ( $ token ->data ->user ->id ) ) {
598
+ return new \WP_Error ( 'invalid-jwt ' , __ ( 'User ID not found in the token ' , 'wp-graphql-jwt-authentication ' ) );
599
+ }
600
+
601
+ /**
602
+ * If there is a user_secret in the token (refresh tokens) make sure it matches what
603
+ */
604
+ if ( isset ( $ token ->data ->user ->user_secret ) ) {
582
605
583
- if ( Auth::is_jwt_secret_revoked ( $ token ->data ->user ->id ) ) {
584
- throw new \Exception ( __ ( 'The User Secret does not match or has been revoked for this user ' , 'wp-graphql-jwt-authentication ' ) );
585
- }
606
+ if ( Auth::is_jwt_secret_revoked ( $ token ->data ->user ->id ) ) {
607
+ return new \WP_Error ( 'invalid-jwt ' , __ ( 'The User Secret does not match or has been revoked for this user ' , 'wp-graphql-jwt-authentication ' ) );
586
608
}
609
+ }
587
610
588
- /**
589
- * If any exceptions are caught
590
- */
591
- } catch ( \Exception $ error ) {
611
+ if ( is_wp_error ( $ token ) ) {
592
612
self ::set_status ( 403 );
593
- return new \WP_Error ( 'invalid_token ' , __ ( 'The JWT Token is invalid ' , 'wp-graphql-jwt-authentication ' ) );
594
613
}
595
614
596
615
self ::$ is_refresh_token = false ;
0 commit comments