Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#69 - JWT Tokens could not be returned after regiserUser mutation #72

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ protected static function get_signed_token( $user, $cap_check = true ) {
*/
public static function get_user_jwt_secret( $user_id ) {

$is_revoked = Auth::is_jwt_secret_revoked( $user_id );

/**
* If the secret has been revoked, throw an error
*/
if ( true === Auth::is_jwt_secret_revoked( $user_id ) ) {
return new \WP_Error( 'graphql-jwt-revoked-secret', __( 'The JWT Auth secret cannot be returned', 'wp-graphql-jwt-authentication' ) );
if ( true === (bool) $is_revoked ) {
return null;
}

/**
Expand All @@ -220,7 +222,7 @@ public static function get_user_jwt_secret( $user_id ) {
*/
$is_current_user = ( $user_id === get_current_user_id() ) ? true : false;
if ( ! $is_current_user || ! current_user_can( $capability ) ) {
return new \WP_Error( 'graphql-jwt-improper-capabilities', __( 'The JWT Auth secret for this user cannot be returned', 'wp-graphql-jwt-authentication' ) );
return null;
}

/**
Expand Down
25 changes: 17 additions & 8 deletions src/ManageTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,42 @@ public static function register_jwt_fields_to( $type ) {
'type' => 'String',
'description' => __( 'A JWT token that can be used in future requests for authentication/authorization', 'wp-graphql-jwt-authentication' ),
'resolve' => function ( $user ) {
$user = get_user_by( 'id', $user->ID );
$user = get_user_by( 'id', $user->userId );

// Get the token for the user.
$token = Auth::get_token( $user );

// If the token cannot be returned, throw an error.
if ( empty( $token ) || is_wp_error( $token ) ) {
if ( empty( $token ) ) {
throw new UserError( __( 'The JWT token could not be returned', 'wp-graphql-jwt-authentication' ) );
}

if ( $token instanceof \WP_Error ) {
throw new UserError( $token->get_error_message() );
}

return ! empty( $token ) ? $token : null;
},
],
'jwtRefreshToken' => [
'type' => 'String',
'description' => __( 'A JWT token that can be used in future requests to get a refreshed jwtAuthToken. If the refresh token used in a request is revoked or otherwise invalid, a valid Auth token will NOT be issued in the response headers.', 'wp-graphql-jwt-authentication' ),
'resolve' => function ( $user ) {
$user = get_user_by( 'id', $user->ID );

$user = get_user_by( 'id', $user->userId );

// Get the token for the user.
$token = Auth::get_refresh_token( $user );

// If the token cannot be returned, throw an error.
if ( empty( $token ) || is_wp_error( $token ) ) {
if ( empty( $token ) ) {
throw new UserError( __( 'The JWT token could not be returned', 'wp-graphql-jwt-authentication' ) );
}

if ( $token instanceof \WP_Error ) {
throw new UserError( $token->get_error_message() );
}

return ! empty( $token ) ? $token : null;
},
],
Expand All @@ -110,11 +119,11 @@ public static function register_jwt_fields_to( $type ) {
'description' => __( 'A unique secret tied to the users JWT token that can be revoked or refreshed. Revoking the secret prevents JWT tokens from being issued to the user. Refreshing the token invalidates previously issued tokens, but allows new tokens to be issued.', 'wp-graphql' ),
'resolve' => function ( $user ) {
// Get the user's JWT Secret.
$secret = Auth::get_user_jwt_secret( $user->ID );
$secret = Auth::get_user_jwt_secret( $user->userId );

// If the secret cannot be returned, throw an error.
if ( is_wp_error( $secret ) ) {
throw new UserError( __( 'The user secret could not be returned', 'wp-graphql-jwt-authentication' ) );
if ( $secret instanceof \WP_Error ) {
throw new UserError( $secret->get_error_message() );
}

// Return the secret.
Expand All @@ -134,7 +143,7 @@ public static function register_jwt_fields_to( $type ) {
'type' => [ 'non_null' => 'Boolean' ],
'description' => __( 'Whether the JWT User secret has been revoked. If the secret has been revoked, auth tokens will not be issued until an admin, or user with proper capabilities re-issues a secret for the user.', 'wp-graphql-jwt-authentication' ),
'resolve' => function ( $user ) {
$revoked = Auth::is_jwt_secret_revoked( $user->ID );
$revoked = Auth::is_jwt_secret_revoked( $user->userId );

return true === $revoked ? true : false;
},
Expand Down