Skip to content

Commit b25d117

Browse files
committed
Allow GET requests to use tokens, if provided
1 parent e3fda4d commit b25d117

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ public function test_require_token() {
231231
$_SERVER['REQUEST_URI'] = $token_uri;
232232
$this->assertFalse( $this->token->require_token() );
233233

234+
// Some GET requests require authentication to work correctly (i.e. – fetching draft posts)
235+
// If a token is present, treat it as though it's required.
236+
$mock = $this->getMockBuilder( get_class( $this->token ) )
237+
->setMethods(
238+
array(
239+
'validate_token',
240+
)
241+
)
242+
->getMock();
243+
$mock->method( 'validate_token' )->willReturn( true );
244+
$this->assertTrue( $mock->require_token() );
245+
234246
// Don't require authentication to generate a token.
235247
$_SERVER['REQUEST_METHOD'] = 'POST';
236248
$this->assertFalse( $this->token->require_token() );

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ public function require_token() {
272272
$require_token = false;
273273
}
274274

275-
// GET requests do not need to be authenticated.
276-
if ( 'GET' === $request_method ) {
275+
// GET requests do not require authentication, but if a valid token is provided, requests should
276+
// be performed as the user corresponding to that token.
277+
if ( 'GET' === $request_method && is_wp_error( $this->validate_token() ) ) {
277278
$require_token = false;
278279
}
279280

0 commit comments

Comments
 (0)