Skip to content

Commit a220921

Browse files
committed
#26 - Expose Refresh Token in Authenticated REST API requests
1 parent 6495640 commit a220921

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/ManageTokens.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ public static function init() {
5959
'add_auth_headers_to_response'
6060
] );
6161

62+
/**
63+
* Add Auth Headers to REST REQUEST responses
64+
*
65+
* This allows clients to use WPGraphQL JWT Authentication
66+
* tokens with WPGraphQL _and_ with REST API requests, and
67+
* this exposes refresh tokens in the REST API response
68+
* so folks can refresh their tokens after each REST API
69+
* request.
70+
*/
71+
add_filter( 'rest_request_after_callbacks', [
72+
'\WPGraphQL\JWT_Authentication\ManageTokens',
73+
'add_auth_headers_to_rest_response'
74+
], 10, 3 );
75+
6276
add_filter( 'graphql_access_control_allow_headers', [
6377
'\WPGraphQL\JWT_Authentication\ManageTokens',
6478
'add_jwt_allowed_headers'
@@ -72,6 +86,7 @@ public static function init() {
7286
* @param array $fields The fields for the User type in the GraphQL Schema
7387
*
7488
* @return array $fields
89+
* @throws \Exception
7590
*/
7691
public static function add_user_fields( $fields ) {
7792

@@ -318,6 +333,52 @@ public static function add_tokens_to_graphql_response_headers( $headers ) {
318333

319334
}
320335

336+
/**
337+
* Expose X-JWT-Refresh tokens in the response headers for REST requests.
338+
*
339+
* This allows clients the ability to Authenticate with WPGraphQL, use the token
340+
* with REST API Requests, but get new refresh tokens from the REST API Headers
341+
*
342+
* @return \WP_HTTP_Response
343+
* @throws \Exception
344+
*/
345+
public static function add_auth_headers_to_rest_response( \WP_HTTP_Response $response, $handler, $request ) {
346+
347+
/**
348+
* Note: The Access-Control-Expose-Headers aren't directly filterable
349+
* for REST API responses, so this overrides them altogether.
350+
*
351+
* This isn't ideal, as any other plugin could override as well.
352+
*
353+
* Might need a patch to core to allow for individual filtering.
354+
*/
355+
$response->set_headers( [
356+
'Access-Control-Expose-Headers' => 'X-WP-Total, X-WP-TotalPages, X-JWT-Refresh',
357+
] );
358+
359+
$refresh_token = null;
360+
361+
$validate_auth_header = Auth::validate_token( str_ireplace( 'Bearer ', '', Auth::get_auth_header() ), false );
362+
363+
if ( ! is_wp_error( $validate_auth_header ) && ! empty( $validate_auth_header->data->user->id ) ) {
364+
365+
$refresh_token = Auth::get_refresh_token( new \WP_User( $validate_auth_header->data->user->id ), false );
366+
367+
if ( ! empty( $refresh_token ) && ! is_wp_error( $refresh_token ) ) {
368+
$headers['X-JWT-Refresh'] = $refresh_token;
369+
}
370+
371+
}
372+
373+
if ( $refresh_token ) {
374+
$response->set_headers( [
375+
'X-JWT-Refresh' => $refresh_token,
376+
] );
377+
}
378+
379+
return $response;
380+
}
381+
321382
/**
322383
* Expose the X-JWT-Refresh tokens in the response headers. This allows
323384
* folks to grab new refresh tokens from authenticated requests for subsequent use.

0 commit comments

Comments
 (0)