Skip to content

Commit d393e4a

Browse files
committed
Add more docs & clean array args
1 parent f4bfeb6 commit d393e4a

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

wp-includes/rest-api/auth/class-wp-rest-key-pair.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ public function register_routes() {
103103
'validate_callback' => 'rest_validate_request_arg',
104104
),
105105
),
106-
'schema' => array(
107-
$this,
108-
'get_item_schema',
109-
),
106+
'schema' => array( $this, 'get_item_schema' ),
110107
);
111108
register_rest_route( self::_NAMESPACE_, '/' . self::_REST_BASE_ . '/(?P<user_id>[\d]+)', $args );
112109

@@ -306,7 +303,9 @@ public function require_token( $require_token, $request_uri, $request_method ) {
306303
* Authenticate the key-pair if API key and API secret is provided and return the user.
307304
*
308305
* If not authenticated, send back the original $user value to allow other authentication
309-
* methods to attempt authentication.
306+
* methods to attempt authentication. If the initial value of `$user` is false this method
307+
* will return a `WP_User` object on success or a `WP_Error` object on failure. However,
308+
* if the value is not `false` it will return that value, which could be any type of object.
310309
*
311310
* @filter rest_authentication_user
312311
*
@@ -383,6 +382,11 @@ public function authenticate( $user, WP_REST_Request $request ) {
383382
/**
384383
* Filters the JWT Payload.
385384
*
385+
* Due to the fact that `$user` could have been filtered the object type is technically
386+
* unknown. However, likely a `WP_User` object if auth has not been filtered. In any
387+
* case, the object must have the `$user->data->api_key` property in order to connect
388+
* the API key to the JWT payload and allow for token invalidation.
389+
*
386390
* @filter rest_authentication_token_private_claims
387391
*
388392
* @param array $payload The payload used to generate the token.

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

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,43 +89,35 @@ public static function get_rest_uri() {
8989
*/
9090
public function register_routes() {
9191
$args = array(
92-
array(
93-
'methods' => WP_REST_Server::CREATABLE,
94-
'callback' => array(
95-
$this,
96-
'generate_token',
92+
'methods' => WP_REST_Server::CREATABLE,
93+
'callback' => array( $this, 'generate_token' ),
94+
'args' => array(
95+
'username' => array(
96+
'description' => __( 'The username of the user; requires also setting the password argument.', 'jwt-auth' ),
97+
'type' => 'string',
98+
'sanitize_callback' => 'sanitize_user',
99+
'validate_callback' => 'rest_validate_request_arg',
97100
),
98-
'args' => array(
99-
'username' => array(
100-
'description' => __( 'The username of the user; requires also setting the password argument.', 'jwt-auth' ),
101-
'type' => 'string',
102-
'sanitize_callback' => 'sanitize_user',
103-
'validate_callback' => 'rest_validate_request_arg',
104-
),
105-
'password' => array(
106-
'description' => __( 'The password of the user; requires also setting the username argument.', 'jwt-auth' ),
107-
'type' => 'string',
108-
'sanitize_callback' => 'sanitize_text_field',
109-
'validate_callback' => 'rest_validate_request_arg',
110-
),
111-
'api_key' => array(
112-
'description' => __( 'The API key of the user; requires also setting the api_secret.', 'jwt-auth' ),
113-
'type' => 'string',
114-
'sanitize_callback' => 'sanitize_text_field',
115-
'validate_callback' => 'rest_validate_request_arg',
116-
),
117-
'api_secret' => array(
118-
'description' => __( 'The API secret of the user; requires also setting the api_key.', 'jwt-auth' ),
119-
'type' => 'string',
120-
'sanitize_callback' => 'sanitize_text_field',
121-
'validate_callback' => 'rest_validate_request_arg',
122-
),
101+
'password' => array(
102+
'description' => __( 'The password of the user; requires also setting the username argument.', 'jwt-auth' ),
103+
'type' => 'string',
104+
'sanitize_callback' => 'sanitize_text_field',
105+
'validate_callback' => 'rest_validate_request_arg',
106+
),
107+
'api_key' => array(
108+
'description' => __( 'The API key of the user; requires also setting the api_secret.', 'jwt-auth' ),
109+
'type' => 'string',
110+
'sanitize_callback' => 'sanitize_text_field',
111+
'validate_callback' => 'rest_validate_request_arg',
112+
),
113+
'api_secret' => array(
114+
'description' => __( 'The API secret of the user; requires also setting the api_key.', 'jwt-auth' ),
115+
'type' => 'string',
116+
'sanitize_callback' => 'sanitize_text_field',
117+
'validate_callback' => 'rest_validate_request_arg',
123118
),
124119
),
125-
'schema' => array(
126-
$this,
127-
'get_item_schema',
128-
),
120+
'schema' => array( $this, 'get_item_schema' ),
129121
);
130122
register_rest_route( self::_NAMESPACE_, '/' . self::_REST_BASE_, $args );
131123
}

0 commit comments

Comments
 (0)