Skip to content

Commit

Permalink
#41 - JWT fields cannot be retrieved via viewer query
Browse files Browse the repository at this point in the history
- When the Model Layer was introduced it changed the objects that are passed down to fields on the Viewer. This adjusts the field resolvers to expect a User (model) instead of WP_User to be passed down
  • Loading branch information
jasonbahl committed Jul 18, 2019
1 parent f18e999 commit ce4f5b2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/ManageTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WPGraphQL\JWT_Authentication;

use GraphQL\Error\UserError;
use WPGraphQL\Model\User;
use WPGraphQL\Types;

class ManageTokens {
Expand Down Expand Up @@ -85,7 +86,9 @@ public static function add_user_fields( $fields ) {
$fields['jwtAuthToken'] = [
'type' => Types::string(),
'description' => __( 'A JWT token that can be used in future requests for authentication/authorization', 'wp-graphql-jwt-authentication' ),
'resolve' => function ( \WP_User $user ) {
'resolve' => function ( User $user ) {

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

/**
* Get the token for the user
Expand All @@ -106,7 +109,9 @@ public static function add_user_fields( $fields ) {
$fields['jwtRefreshToken'] = [
'type' => Types::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 ( \WP_User $user ) {
'resolve' => function ( User $user ) {

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

/**
* Get the token for the user
Expand All @@ -127,12 +132,12 @@ public static function add_user_fields( $fields ) {
$fields['jwtUserSecret'] = [
'type' => Types::string(),
'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 ( \WP_User $user ) {
'resolve' => function ( User $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
Expand Down Expand Up @@ -319,11 +324,11 @@ public static function add_tokens_to_graphql_response_headers( $headers ) {
* @throws \Exception
*/
public static function add_auth_headers_to_rest_response( $response, $handler, $request ) {

if( ! $response instanceof \WP_HTTP_Response ) {
return $response;
}

/**
* If the request _is_ SSL, or GRAPHQL_DEBUG is defined, return the tokens
* otherwise do not return them.
Expand Down

0 comments on commit ce4f5b2

Please sign in to comment.