Skip to content

Commit 16493c1

Browse files
committed
Ensure password change revokes all tokens
1 parent 7ccfc84 commit 16493c1

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

tests/wp-admin/includes/class-test-wp-key-pair-list-table.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@
1616
class Test_WP_Key_Pair_List_Table extends WP_UnitTestCase {
1717

1818
/**
19+
* List Table.
20+
*
1921
* @var WP_Key_Pair_List_Table
2022
*/
2123
protected $table;
2224

23-
function setUp() {
25+
/**
26+
* Setup.
27+
*
28+
* @inheritdoc
29+
*/
30+
public function setUp() {
2431
parent::setUp();
2532
$this->table = new WP_Key_Pair_List_Table( array( 'screen' => 'profile' ) );
2633
}

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,85 @@ public function test_show_user_profile() {
151151
$this->assertTrue( wp_script_is( 'key-pair-js', 'enqueued' ) );
152152
}
153153

154+
/**
155+
* Test after_password_reset().
156+
*
157+
* @covers ::after_password_reset()
158+
* @since 0.1
159+
*/
160+
public function test_after_password_reset() {
161+
$user_data = array(
162+
'role' => 'editor',
163+
'user_login' => 'testeditor',
164+
'user_pass' => 'testpassword',
165+
);
166+
167+
$user_id = $this->factory->user->create( $user_data );
168+
169+
$this->assertEquals( array(), $this->key_pair->get_user_key_pairs( $user_id ) );
170+
171+
$keypairs = array(
172+
array(
173+
'api_key' => 12345,
174+
'api_secret' => 54321,
175+
),
176+
);
177+
update_user_meta( $user_id, WP_REST_Key_Pair::_USERMETA_KEY_, $keypairs );
178+
$this->assertEquals( $keypairs, $this->key_pair->get_user_key_pairs( $user_id ) );
179+
180+
$this->key_pair->after_password_reset( get_user_by( 'ID', $user_id ) );
181+
$this->assertEquals( $keypairs, $this->key_pair->get_user_key_pairs( $user_id ) );
182+
183+
reset_password( get_user_by( 'ID', $user_id ), 'testpassword1' );
184+
$this->assertEquals( array(), $this->key_pair->get_user_key_pairs( $user_id ) );
185+
}
186+
187+
/**
188+
* Test profile_update().
189+
*
190+
* @covers ::profile_update()
191+
* @since 0.1
192+
*/
193+
public function test_profile_update() {
194+
global $wp_current_filter;
195+
196+
$tmp = $wp_current_filter;
197+
198+
$user_data = array(
199+
'role' => 'editor',
200+
'user_login' => 'testeditor',
201+
'user_pass' => 'testpassword',
202+
);
203+
204+
$user_id = $this->factory->user->create( $user_data );
205+
206+
$this->assertEquals( array(), $this->key_pair->get_user_key_pairs( $user_id ) );
207+
208+
$keypairs = array(
209+
array(
210+
'api_key' => 12345,
211+
'api_secret' => 54321,
212+
),
213+
);
214+
update_user_meta( $user_id, WP_REST_Key_Pair::_USERMETA_KEY_, $keypairs );
215+
$this->assertEquals( $keypairs, $this->key_pair->get_user_key_pairs( $user_id ) );
216+
217+
$this->key_pair->profile_update( $user_id );
218+
$this->assertEquals( $keypairs, $this->key_pair->get_user_key_pairs( $user_id ) );
219+
220+
$wp_current_filter = array(
221+
'profile_update',
222+
);
223+
$this->key_pair->profile_update( $user_id );
224+
$this->assertEquals( $keypairs, $this->key_pair->get_user_key_pairs( $user_id ) );
225+
226+
$_POST['pass1'] = 'changed';
227+
$this->key_pair->profile_update( $user_id );
228+
$this->assertEquals( array(), $this->key_pair->get_user_key_pairs( $user_id ) );
229+
230+
$wp_current_filter = $tmp;
231+
}
232+
154233
/**
155234
* Test require_token().
156235
*

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function init() {
5656
add_action( 'rest_api_init', array( $this, 'register_routes' ), 99 );
5757
add_action( 'show_user_profile', array( $this, 'show_user_profile' ) );
5858
add_action( 'edit_user_profile', array( $this, 'show_user_profile' ) );
59+
add_action( 'after_password_reset', array( $this, 'after_password_reset' ) );
60+
add_action( 'profile_update', array( $this, 'profile_update' ) );
5961

6062
add_filter( 'rest_authentication_require_token', array( $this, 'require_token' ), 10, 3 );
6163
add_filter( 'rest_authentication_user', array( $this, 'authenticate' ), 10, 2 );
@@ -258,6 +260,40 @@ public function show_user_profile( WP_User $user ) {
258260
$this->template_key_pair_row();
259261
}
260262

263+
/**
264+
* Fires after the user's password is reset.
265+
*
266+
* @param WP_User $user The user.
267+
*/
268+
public function after_password_reset( WP_User $user ) {
269+
if ( 'after_password_reset' !== current_filter() ) {
270+
return;
271+
}
272+
273+
$keypairs = $this->get_user_key_pairs( $user->ID );
274+
if ( ! empty( $keypairs ) ) {
275+
$this->set_user_key_pairs( $user->ID, array() );
276+
}
277+
}
278+
279+
/**
280+
* Fires after the user's password is reset.
281+
*
282+
* @param int $user_id The user ID.
283+
*/
284+
public function profile_update( $user_id ) {
285+
if ( 'profile_update' !== current_filter() ) {
286+
return;
287+
}
288+
289+
if ( isset( $_POST['pass1'] ) && ! empty( $_POST['pass1'] ) ) { // phpcs:ignore
290+
$keypairs = $this->get_user_key_pairs( $user_id );
291+
if ( ! empty( $keypairs ) ) {
292+
$this->set_user_key_pairs( $user_id, array() );
293+
}
294+
}
295+
}
296+
261297
/**
262298
* Filters `rest_authentication_require_token` to exclude the key-pair endpoint,
263299
*

0 commit comments

Comments
 (0)