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() {
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 ' ] );
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments