Skip to content

Commit 1dcc3e9

Browse files
committed
Merge branch 'fix-get-requests' into develop
2 parents 9f68e99 + 85094e5 commit 1dcc3e9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

tests/wp-includes/rest-api/auth/class-test-wp-rest-token.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ public function test_require_token() {
370370
$_SERVER['REQUEST_URI'] = $token_uri;
371371
$this->assertFalse( $this->token->require_token() );
372372

373+
// Some GET requests require authentication to work correctly (i.e. – fetching draft posts)
374+
// If a token is present, treat it as though it's required.
375+
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer: Test';
376+
$this->assertTrue( $this->token->require_token() );
377+
unset( $_SERVER['HTTP_AUTHORIZATION'] );
378+
373379
// Don't require authentication to generate a token.
374380
$_SERVER['REQUEST_METHOD'] = 'POST';
375381
$this->assertFalse( $this->token->require_token() );
@@ -382,7 +388,7 @@ public function test_require_token() {
382388
$_SERVER['REQUEST_METHOD'] = 'GET';
383389
add_filter( 'rest_authentication_require_token', '__return_true' );
384390
$this->assertTrue( $this->token->require_token() );
385-
add_filter( 'rest_authentication_require_token', '__return_true' );
391+
remove_filter( 'rest_authentication_require_token', '__return_true' );
386392

387393
unset( $_SERVER['REQUEST_METHOD'] );
388394
unset( $_SERVER['REQUEST_URI'] );

wp-includes/rest-api/auth/class-wp-rest-token.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,14 @@ public function require_token() {
374374
$require_token = false;
375375
}
376376

377-
// GET requests do not need to be authenticated.
378-
if ( 'GET' === $request_method ) {
377+
/**
378+
* GET requests do not typically require authentication, but if the
379+
* Authorization header is provided, we will use it. WHat's happening
380+
* here is that `WP_REST_Token::get_auth_header` returns the bearer
381+
* token or a `WP_Error`. So if we have an error then we can safely skip
382+
* the GET request.
383+
*/
384+
if ( 'GET' === $request_method && is_wp_error( $this->get_auth_header() ) ) {
379385
$require_token = false;
380386
}
381387

0 commit comments

Comments
 (0)