Skip to content

Commit 9df6cf5

Browse files
committed
Merge branch 'bug/#69-jwt-could-not-be-returned-after-user-registered' into develop
2 parents 3584291 + 135262a commit 9df6cf5

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

src/Auth.php

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

wp-graphql-jwt-authentication.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ private static function init() {
159159
// Initialize the GraphQL fields for managing tokens.
160160
ManageTokens::init();
161161

162+
162163
// Filter how WordPress determines the current user.
163164
add_filter(
164165
'determine_current_user',
@@ -179,6 +180,23 @@ private static function init() {
179180
[ '\WPGraphQL\JWT_Authentication\RefreshToken', 'register_mutation' ],
180181
10
181182
);
183+
184+
185+
/**
186+
* When the GraphQL Request is initiated, validate the token.
187+
*
188+
* If the Auth Token is not valid, prevent execution of resolvers. This will also set the
189+
* response status to 403.
190+
*/
191+
add_action( 'init_graphql_request', function() {
192+
$token = Auth::validate_token();
193+
if ( is_wp_error( $token ) ) {
194+
add_action( 'graphql_before_resolve_field', function() use ( $token ) {
195+
throw new \Exception( $token->get_error_code() . ' | ' . $token->get_error_message() );
196+
}, 1 );
197+
}
198+
} );
199+
182200
}
183201
}
184202

0 commit comments

Comments
 (0)