@@ -427,7 +427,7 @@ public static function filter_determine_current_user( $user ) {
427427 *
428428 * @return mixed|boolean|\WP_Error
429429 */
430- public static function revoke_user_secret ( int $ user_id ) {
430+ public static function revoke_user_secret ( $ user_id ) {
431431
432432 /**
433433 * Filter the capability that is tied to editing/viewing user JWT Auth info
@@ -542,7 +542,7 @@ public static function validate_token( $token = null, $refresh = false ) {
542542 * @since 0.0.1
543543 */
544544 if ( empty ( $ auth_header ) ) {
545- return false ;
545+ return $ token ;
546546 } else {
547547 /**
548548 * The HTTP_AUTHORIZATION is present verify the format
@@ -557,52 +557,60 @@ public static function validate_token( $token = null, $refresh = false ) {
557557 * If there's no secret key, throw an error as there needs to be a secret key for Auth to work properly
558558 */
559559 if ( ! self ::get_secret_key () ) {
560- 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 ' ) );
561562 }
562563
564+
565+
563566 /**
564- * Try to decode the token
567+ * Decode the Token
565568 */
566- try {
569+ JWT :: $ leeway = 60 ;
567570
568- /**
569- * Decode the Token
570- */
571- JWT ::$ leeway = 60 ;
571+ $ secret = self ::get_secret_key ();
572572
573- $ secret = self :: get_secret_key ();
573+ try {
574574 $ token = ! empty ( $ token ) ? JWT ::decode ( $ token , $ secret , [ 'HS256 ' ] ) : null ;
575+ } catch ( \Exception $ exception ) {
576+ $ token = new \WP_Error ( 'invalid-secret-key ' , $ exception ->getMessage () );
577+ }
575578
576- /**
577- * The Token is decoded now validate the iss
578- */
579- if ( ! isset ( $ token ->iss ) || get_bloginfo ( 'url ' ) !== $ token ->iss ) {
580- throw new \Exception ( __ ( 'The iss do not match with this server ' , 'wp-graphql-jwt-authentication ' ) );
581- }
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+ }
582586
583- /**
584- * So far so good, validate the user id in the token
585- */
586- if ( ! isset ( $ token ->data -> user -> id ) ) {
587- throw new \Exception ( __ ( 'User ID not found in the token ' , 'wp-graphql-jwt-authentication ' ) );
588- }
587+ /**
588+ * The Token is decoded now validate the iss
589+ */
590+ if ( ! isset ( $ token ->iss ) || get_bloginfo ( ' url ' ) !== $ token -> iss ) {
591+ $ token = new \WP_Error ( ' invalid-jwt ' , __ ( 'The iss do not match with this server ' , 'wp-graphql-jwt-authentication ' ) );
592+ }
589593
590- /**
591- * If there is a user_secret in the token (refresh tokens) make sure it matches what
592- */
593- if ( isset ( $ token ->data ->user ->user_secret ) ) {
594594
595- if ( Auth::is_jwt_secret_revoked ( $ token ->data ->user ->id ) ) {
596- throw new \Exception ( __ ( 'The User Secret does not match or has been revoked for this user ' , 'wp-graphql-jwt-authentication ' ) );
597- }
595+ /**
596+ * So far so good, validate the user id in the token
597+ */
598+ if ( ! isset ( $ token ->data ->user ->id ) ) {
599+ $ token = new \WP_Error ( 'invalid-jwt ' , __ ( 'User ID not found in the token ' , 'wp-graphql-jwt-authentication ' ) );
600+ }
601+
602+ /**
603+ * If there is a user_secret in the token (refresh tokens) make sure it matches what
604+ */
605+ if ( isset ( $ token ->data ->user ->user_secret ) ) {
606+
607+ if ( Auth::is_jwt_secret_revoked ( $ token ->data ->user ->id ) ) {
608+ $ token = new \WP_Error ( 'invalid-jwt ' , __ ( 'The User Secret does not match or has been revoked for this user ' , 'wp-graphql-jwt-authentication ' ) );
598609 }
610+ }
599611
600- /**
601- * If any exceptions are caught
602- */
603- } catch ( \Exception $ error ) {
612+ if ( is_wp_error ( $ token ) ) {
604613 self ::set_status ( 403 );
605- return new \WP_Error ( 'invalid_token ' , __ ( 'The JWT Token is invalid ' , 'wp-graphql-jwt-authentication ' ) );
606614 }
607615
608616 self ::$ is_refresh_token = false ;
0 commit comments