Skip to content

Commit c32beda

Browse files
committed
Add a new filter when an invalid token is used
1 parent ca76a9c commit c32beda

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,21 @@ public function authenticate( $result ) {
241241
// Validate the bearer token.
242242
$token = $this->validate_token();
243243
if ( is_wp_error( $token ) ) {
244-
return $token;
244+
/**
245+
* Filter the response when a token is invalid.
246+
*
247+
* By default an authentication error will be returned. This filter
248+
* allows us to modify that response ignoring an invalid token,
249+
* allowing the REST API response to continue, making JWT auth
250+
* optional.
251+
*
252+
* @param object|WP_Error $token Return the JSON Web Token object,
253+
* or WP_Error on failure.
254+
* @param mixed $result Result of any other
255+
* authentication errors.
256+
* @return mixed
257+
*/
258+
return apply_filters( 'rest_authentication_invalid_token', $token, $result );
245259
}
246260

247261
// If it's a wp_user based token, set the current user.
@@ -382,7 +396,7 @@ public function require_token() {
382396

383397
/**
384398
* GET requests do not typically require authentication, but if the
385-
* Authorization header is provided, we will use it. WHat's happening
399+
* Authorization header is provided, we will use it. What's happening
386400
* here is that `WP_REST_Token::get_auth_header` returns the bearer
387401
* token or a `WP_Error`. So if we have an error then we can safely skip
388402
* the GET request.

0 commit comments

Comments
 (0)