File tree Expand file tree Collapse file tree 2 files changed +15
-3
lines changed
tests/wp-includes/rest-api/auth
wp-includes/rest-api/auth Expand file tree Collapse file tree 2 files changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -370,6 +370,12 @@ public function test_require_token() {
370
370
$ _SERVER ['REQUEST_URI ' ] = $ token_uri ;
371
371
$ this ->assertFalse ( $ this ->token ->require_token () );
372
372
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
+
373
379
// Don't require authentication to generate a token.
374
380
$ _SERVER ['REQUEST_METHOD ' ] = 'POST ' ;
375
381
$ this ->assertFalse ( $ this ->token ->require_token () );
@@ -382,7 +388,7 @@ public function test_require_token() {
382
388
$ _SERVER ['REQUEST_METHOD ' ] = 'GET ' ;
383
389
add_filter ( 'rest_authentication_require_token ' , '__return_true ' );
384
390
$ this ->assertTrue ( $ this ->token ->require_token () );
385
- add_filter ( 'rest_authentication_require_token ' , '__return_true ' );
391
+ remove_filter ( 'rest_authentication_require_token ' , '__return_true ' );
386
392
387
393
unset( $ _SERVER ['REQUEST_METHOD ' ] );
388
394
unset( $ _SERVER ['REQUEST_URI ' ] );
Original file line number Diff line number Diff line change @@ -374,8 +374,14 @@ public function require_token() {
374
374
$ require_token = false ;
375
375
}
376
376
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 () ) ) {
379
385
$ require_token = false ;
380
386
}
381
387
You can’t perform that action at this time.
0 commit comments