Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
94.74% covered (success)
94.74%
18 / 19
WPGraphQL\JWT_Authentication\RefreshToken
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
13.02
94.74% covered (success)
94.74%
18 / 19
 root_mutation_fields
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 mutation
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
8 / 8
 anonymousFunction:46#251
0.00% covered (danger)
0.00%
0 / 1
5.05
87.50% covered (warning)
87.50%
7 / 8
<?php
namespace WPGraphQL\JWT_Authentication;
use GraphQL\Error\UserError;
use GraphQLRelay\Relay;
use WPGraphQL\Types;
class RefreshToken {
    private static $mutation;
    /**
     * Takes an array of fields from the RootMutation and returns the fields
     * with the "login" mutation field added
     *
     * @param array $fields The fields in the RootMutation of the Schema
     *
     * @return array $fields
     */
    public static function root_mutation_fields( $fields ) {
        $fields['refreshJwtAuthToken'] = self::mutation();
        return $fields;
    }
    public static function mutation() {
        if ( empty( self::$mutation ) ) {
            self::$mutation = Relay::mutationWithClientMutationId([
                'name' => 'RefreshJwtAuthToken',
                'isPrivate' => false,
                'description' => __( 'Use a valid JWT Refresh token to retrieve a new JWT Auth Token', 'wp-graphql-jwt-authentication' ),
                'inputFields' => [
                    'jwtRefreshToken' => [
                        'type' => Types::non_null( Types::string() ),
                        'description' => __( 'A valid, previously issued JWT refresh token. If valid a new Auth token will be provided. If invalid, expired, revoked or otherwise invalid, a new AuthToken will not be provided.', 'wp-graphql-jwt-authentication' ),
                    ],
                ],
                'outputFields' => [
                    'authToken' => [
                        'type' => Types::string(),
                        'description' => __( 'JWT Token that can be used in future requests for Authentication', 'wp-graphql-jwt-authentication' ),
                    ],
                ],
                'mutateAndGetPayload' => function( $input ) {
                    $refresh_token = ! empty( $input['jwtRefreshToken'] ) ? Auth::validate_token( $input['jwtRefreshToken'] ) : null;
                    $id = isset( $refresh_token->data->user->id ) || 0 === $refresh_token->data->user->id ? absint( $refresh_token->data->user->id ) : 0;
                    if ( empty( $id ) ) {
                        throw new UserError( __( 'The provided refresh token is invalid', 'wp-graphql-jwt-authentication' ) );
                    }
                    $user = new \WP_User( $id );
                    $auth_token = Auth::get_token( $user, false );
                    return [
                        'authToken' => $auth_token,
                    ];
                },
            ]);
        }
        return ( ! empty( self::$mutation ) ) ? self::$mutation : null;
    }
}