Skip to content

Commit 654dd6b

Browse files
committed
Merge branch 'fix/privileged-get-requests-not-working' of https://github.com/jkmassel/jwt-auth into fix-get-requests
2 parents 9f68e99 + 9259df0 commit 654dd6b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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

Lines changed: 6 additions & 0 deletions
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() );

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,10 @@ 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+
// GET requests do not require authentication, but if
378+
// the Authorization header is provided, requests should
379+
// be performed as the user corresponding to that token.
380+
if ( 'GET' === $request_method && is_wp_error( $this->get_auth_header() ) ) {
379381
$require_token = false;
380382
}
381383

0 commit comments

Comments
 (0)