Skip to content

Commit f1170be

Browse files
authored
Merge pull request #62 from kidunot89/revert-56-revert-55-feature/wp-graphql-v0.6.0-support
JWT field registration updated.
2 parents da65553 + b4d3990 commit f1170be

File tree

1 file changed

+91
-84
lines changed

1 file changed

+91
-84
lines changed

src/ManageTokens.php

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ class ManageTokens {
2121
* Initialize the funcionality for managing tokens
2222
*/
2323
public static function init() {
24-
25-
// Filter the User type to have a jwtUserSecret and jwtAuthToken field.
26-
add_filter( 'graphql_user_fields', [ __CLASS__, 'add_user_fields' ], 10, 3 );
24+
// Register JWT fields.
25+
add_action( 'graphql_register_types', [ __CLASS__, 'add_jwt_fields' ], 10 );
2726

2827
// Add fields to the input for user mutations.
2928
add_filter( 'graphql_user_mutation_input_fields', [ __CLASS__, 'add_user_mutation_input_fields' ] );
@@ -52,91 +51,99 @@ public static function init() {
5251
}
5352

5453
/**
55-
* Filters the User type in the GraphQL Schema to provide fields for querying for user's
56-
* jwtAuthToken and jwtUserSecret
54+
* Registers JWT fields to the GraphQL schema on all targeted types.
55+
*
56+
* Type must provided an root object with an User ID assigned to the "ID" field.
57+
* Ex. $source->ID
58+
*/
59+
public static function add_jwt_fields() {
60+
$types = apply_filters( 'graphql_jwt_user_types', [ 'User' ] );
61+
foreach ( $types as $type ) {
62+
self::register_jwt_fields_to( $type );
63+
}
64+
}
65+
66+
/**
67+
* Adds the JWT fields to the provided type.
5768
*
58-
* @param array $fields The fields for the User type in the GraphQL Schema.
59-
* @param \WPGraphQL\Type\WPObjectType $object The WPObjectType the fields are be added to.
60-
* @param \WPGraphQL\Registry\TypeRegistry $type_registry TypeRegistry instance.
69+
* @param string $type Type for the fields to be registered to.
6170
*
6271
* @throws UserError Invalid token/Token not found.
63-
* @return array $fields
6472
*/
65-
public static function add_user_fields( $fields, $object, $type_registry ) {
66-
$fields['jwtAuthToken'] = [
67-
'type' => $type_registry->get_type( 'String' ),
68-
'description' => __( 'A JWT token that can be used in future requests for authentication/authorization', 'wp-graphql-jwt-authentication' ),
69-
'resolve' => function ( User $user ) {
70-
$user = get_user_by( 'id', $user->ID );
71-
72-
// Get the token for the user.
73-
$token = Auth::get_token( $user );
74-
75-
// If the token cannot be returned, throw an error.
76-
if ( empty( $token ) || is_wp_error( $token ) ) {
77-
throw new UserError( __( 'The JWT token could not be returned', 'wp-graphql-jwt-authentication' ) );
78-
}
79-
80-
return ! empty( $token ) ? $token : null;
81-
},
82-
];
83-
84-
$fields['jwtRefreshToken'] = [
85-
'type' => $type_registry->get_type( 'String' ),
86-
'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' ),
87-
'resolve' => function ( User $user ) {
88-
$user = get_user_by( 'id', $user->ID );
89-
90-
// Get the token for the user.
91-
$token = Auth::get_refresh_token( $user );
92-
93-
// If the token cannot be returned, throw an error.
94-
if ( empty( $token ) || is_wp_error( $token ) ) {
95-
throw new UserError( __( 'The JWT token could not be returned', 'wp-graphql-jwt-authentication' ) );
96-
}
97-
98-
return ! empty( $token ) ? $token : null;
99-
},
100-
];
101-
102-
$fields['jwtUserSecret'] = [
103-
'type' => $type_registry->get_type( 'String' ),
104-
'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' ),
105-
'resolve' => function ( User $user ) {
106-
// Get the user's JWT Secret.
107-
$secret = Auth::get_user_jwt_secret( $user->ID );
108-
109-
// If the secret cannot be returned, throw an error.
110-
if ( is_wp_error( $secret ) ) {
111-
throw new UserError( __( 'The user secret could not be returned', 'wp-graphql-jwt-authentication' ) );
112-
}
113-
114-
// Return the secret.
115-
return ! empty( $secret ) ? $secret : null;
116-
},
117-
];
118-
119-
$fields['jwtAuthExpiration'] = [
120-
'type' => $type_registry->get_type( 'String' ),
121-
'description' => __( 'The expiration for the JWT Token for the user. If not set custom for the user, it will use the default sitewide expiration setting', 'wp-graphql-jwt-authentication' ),
122-
'resolve' => function () {
123-
$expiration = Auth::get_token_expiration();
124-
125-
return ! empty( $expiration ) ? $expiration : null;
126-
},
127-
];
128-
129-
$fields['isJwtAuthSecretRevoked'] = [
130-
'type' => $type_registry->non_null( $type_registry->get_type( 'Boolean' ) ),
131-
'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' ),
132-
'resolve' => function ( User $user ) {
133-
$revoked = Auth::is_jwt_secret_revoked( $user->ID );
134-
135-
return true === $revoked ? true : false;
136-
},
137-
];
138-
139-
return $fields;
73+
public static function register_jwt_fields_to( $type ) {
74+
register_graphql_fields(
75+
$type,
76+
[
77+
'jwtAuthToken' => [
78+
'type' => 'String',
79+
'description' => __( 'A JWT token that can be used in future requests for authentication/authorization', 'wp-graphql-jwt-authentication' ),
80+
'resolve' => function ( $user ) {
81+
$user = get_user_by( 'id', $user->ID );
82+
83+
// Get the token for the user.
84+
$token = Auth::get_token( $user );
85+
86+
// If the token cannot be returned, throw an error.
87+
if ( empty( $token ) || is_wp_error( $token ) ) {
88+
throw new UserError( __( 'The JWT token could not be returned', 'wp-graphql-jwt-authentication' ) );
89+
}
90+
91+
return ! empty( $token ) ? $token : null;
92+
},
93+
],
94+
'jwtRefreshToken' => [
95+
'type' => 'String',
96+
'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' ),
97+
'resolve' => function ( $user ) {
98+
$user = get_user_by( 'id', $user->ID );
99+
100+
// Get the token for the user.
101+
$token = Auth::get_refresh_token( $user );
102+
103+
// If the token cannot be returned, throw an error.
104+
if ( empty( $token ) || is_wp_error( $token ) ) {
105+
throw new UserError( __( 'The JWT token could not be returned', 'wp-graphql-jwt-authentication' ) );
106+
}
107+
108+
return ! empty( $token ) ? $token : null;
109+
},
110+
],
111+
'jwtUserSecret' => [
112+
'type' => 'String',
113+
'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' ),
114+
'resolve' => function ( $user ) {
115+
// Get the user's JWT Secret.
116+
$secret = Auth::get_user_jwt_secret( $user->ID );
117+
118+
// If the secret cannot be returned, throw an error.
119+
if ( is_wp_error( $secret ) ) {
120+
throw new UserError( __( 'The user secret could not be returned', 'wp-graphql-jwt-authentication' ) );
121+
}
122+
123+
// Return the secret.
124+
return ! empty( $secret ) ? $secret : null;
125+
},
126+
],
127+
'jwtAuthExpiration' => [
128+
'type' => 'String',
129+
'description' => __( 'The expiration for the JWT Token for the user. If not set custom for the user, it will use the default sitewide expiration setting', 'wp-graphql-jwt-authentication' ),
130+
'resolve' => function () {
131+
$expiration = Auth::get_token_expiration();
132+
133+
return ! empty( $expiration ) ? $expiration : null;
134+
},
135+
],
136+
'isJwtAuthSecretRevoked' => [
137+
'type' => [ 'non_null' => 'Boolean' ],
138+
'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' ),
139+
'resolve' => function ( $user ) {
140+
$revoked = Auth::is_jwt_secret_revoked( $user->ID );
141+
142+
return true === $revoked ? true : false;
143+
},
144+
],
145+
]
146+
);
140147

141148
}
142149

0 commit comments

Comments
 (0)