Skip to content

Commit e88f986

Browse files
committed
Report all errors to API users
1 parent 0c2945d commit e88f986

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

inc/authentication/namespace.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ function get_token_from_request() {
6565
return $token;
6666
}
6767

68-
// Please note that the following includes PHP 5.3+ code. Ryan said it would be fine, soon. ;)
69-
add_filter( 'rest_authentication_errors', function ( $error ) use ( $token ) {
70-
return null === $error ? create_invalid_token_error( $token ) : null;
71-
} );
72-
68+
// Got a token, but it's not valid.
69+
global $oauth2_error;
70+
$oauth2_error = create_invalid_token_error( $token );
7371
return null;
7472
}
7573

@@ -83,6 +81,8 @@ function get_token_from_request() {
8381
function attempt_authentication( $user = null ) {
8482
// Lock against infinite loops when querying the token itself.
8583
static $is_querying_token = false;
84+
global $oauth2_error;
85+
$oauth2_error = null;
8686

8787
if ( ! empty( $user ) || $is_querying_token ) {
8888
return $user;
@@ -101,13 +101,31 @@ function attempt_authentication( $user = null ) {
101101
$is_querying_token = false;
102102

103103
if ( empty( $token ) ) {
104-
return create_invalid_token_error( $token );
104+
$oauth2_error = create_invalid_token_error( $token_value );
105+
return $user;
105106
}
106107

107108
// Token found, authenticate as the user.
108109
return $token->get_user_id();
109110
}
110111

112+
/**
113+
* Report our errors, if we have any.
114+
*
115+
* Attached to the rest_authentication_errors filter. Passes through existing
116+
* errors registered on the filter.
117+
*
118+
* @return WP_Error|null Error if one is set, otherwise null.
119+
*/
120+
function maybe_report_errors( $error = null ) {
121+
if ( ! empty( $error ) ) {
122+
return $error;
123+
}
124+
125+
global $oauth2_error;
126+
return $oauth2_error;
127+
}
128+
111129
function create_invalid_token_error( $token ) {
112130
return new \WP_Error(
113131
'oauth2.authentication.attempt_authentication.invalid_token',

plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function bootstrap() {
1818

1919
/** @todo Implement this :) */
2020
add_filter( 'determine_current_user', __NAMESPACE__ . '\\Authentication\\attempt_authentication', 11 );
21+
add_filter( 'rest_authentication_errors', __NAMESPACE__ . '\\Authentication\\maybe_report_errors' );
2122
add_filter( 'oauth2.grant_types', __NAMESPACE__ . '\\register_grant_types', 0 );
2223
add_action( 'init', __NAMESPACE__ . '\\rest_oauth2_load_authorize_page' );
2324
add_action( 'admin_menu', array( __NAMESPACE__ . '\\admin\\Admin', 'register' ) );

0 commit comments

Comments
 (0)