Skip to content

Commit 2e8436b

Browse files
committed
Allow regenerating secret
1 parent 9093493 commit 2e8436b

File tree

2 files changed

+76
-25
lines changed

2 files changed

+76
-25
lines changed

lib/class-wp-rest-oauth1-admin.php

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public static function load() {
6565
case 'delete':
6666
return self::handle_delete();
6767

68+
case 'regenerate':
69+
return self::handle_regenerate();
70+
6871
default:
6972
global $wp_list_table;
7073

@@ -238,6 +241,7 @@ public static function render_edit_page() {
238241
}
239242

240243
$form_action = self::get_url( array( 'action' => 'edit', 'id' => $id ) );
244+
$regenerate_action = self::get_url( array( 'action' => 'regenerate', 'id' => $id ) );
241245
}
242246

243247
// Handle form submission
@@ -246,10 +250,18 @@ public static function render_edit_page() {
246250
$messages = self::handle_edit_submit( $consumer );
247251
}
248252
if ( ! empty( $_GET['did_action'] ) ) {
249-
if ( $_GET['did_action'] === 'edit' ) {
250-
$messages[] = __( 'Updated application.', 'rest_oauth1' );
251-
} else {
252-
$messages[] = __( 'Successfully created application.', 'rest_oauth1' );
253+
switch ( $_GET['did_action'] ) {
254+
case 'edit':
255+
$messages[] = __( 'Updated application.', 'rest_oauth1' );
256+
break;
257+
258+
case 'regenerate':
259+
$messages[] = __( 'Regenerated secret.', 'rest_oauth1' );
260+
break;
261+
262+
default:
263+
$messages[] = __( 'Successfully created application.', 'rest_oauth1' );
264+
break;
253265
}
254266
}
255267

@@ -318,25 +330,6 @@ public static function render_edit_page() {
318330
<p class="description"><?php echo esc_html( "Your application's callback URL. The callback passed with the request token must match the scheme, host, port, and path of this URL." ) ?></p>
319331
</td>
320332
</tr>
321-
322-
<?php if ( ! empty( $consumer ) ): ?>
323-
<tr>
324-
<th scope="row">
325-
<?php echo esc_html__( 'Client Key' ) ?>
326-
</th>
327-
<td>
328-
<code><?php echo esc_html( $consumer->key ) ?></code>
329-
</td>
330-
</tr>
331-
<tr>
332-
<th scope="row">
333-
<?php echo esc_html__( 'Client Secret' ) ?>
334-
</th>
335-
<td>
336-
<code><?php echo esc_html( $consumer->secret ) ?></code>
337-
</td>
338-
</tr>
339-
<?php endif ?>
340333
</table>
341334

342335
<?php
@@ -353,11 +346,39 @@ public static function render_edit_page() {
353346

354347
?>
355348
</form>
349+
350+
<?php if ( ! empty( $consumer ) ): ?>
351+
<form method="post" action="<?php echo esc_url( $regenerate_action ) ?>">
352+
<h3><?php esc_html_e( 'OAuth Credentials', 'rest_oauth1' ) ?></h3>
353+
354+
<table class="form-table">
355+
<tr>
356+
<th scope="row">
357+
<?php echo esc_html__( 'Client Key', 'rest_oauth1' ) ?>
358+
</th>
359+
<td>
360+
<code><?php echo esc_html( $consumer->key ) ?></code>
361+
</td>
362+
</tr>
363+
<tr>
364+
<th scope="row">
365+
<?php echo esc_html__( 'Client Secret', 'rest_oauth1' ) ?>
366+
</th>
367+
<td>
368+
<code><?php echo esc_html( $consumer->secret ) ?></code>
369+
</td>
370+
</tr>
371+
</table>
372+
373+
<?php
374+
wp_nonce_field( 'rest-oauth1-regenerate:' . $consumer->ID );
375+
submit_button( __( 'Regenerate Secret', 'rest_oauth1' ), 'delete' );
376+
?>
377+
</form>
378+
<?php endif ?>
356379
</div>
357380

358381
<?php
359-
360-
include(ABSPATH . 'wp-admin/admin-footer.php');
361382
}
362383

363384
public static function handle_delete() {
@@ -383,4 +404,19 @@ public static function handle_delete() {
383404
wp_redirect( self::get_url( 'deleted=1' ) );
384405
exit;
385406
}
407+
408+
public static function handle_regenerate() {
409+
if ( empty( $_GET['id'] ) ) {
410+
return;
411+
}
412+
413+
$id = $_GET['id'];
414+
check_admin_referer( 'rest-oauth1-regenerate:' . $id );
415+
416+
$client = WP_REST_OAuth1_Client::get( $id );
417+
$client->regenerate_secret();
418+
419+
wp_redirect( self::get_url( array( 'action' => 'edit', 'id' => $id, 'did_action' => 'regenerate' ) ) );
420+
exit;
421+
}
386422
}

lib/class-wp-rest-oauth1-client.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ class WP_REST_OAuth1_Client extends WP_REST_Client {
44
const CONSUMER_KEY_LENGTH = 12;
55
const CONSUMER_SECRET_LENGTH = 48;
66

7+
/**
8+
* Regenerate the secret for the client.
9+
*
10+
* @return bool|WP_Error True on success, error otherwise.
11+
*/
12+
public function regenerate_secret() {
13+
$params = array(
14+
'meta' => array(
15+
'secret' => wp_generate_password( self::CONSUMER_SECRET_LENGTH, false ),
16+
),
17+
);
18+
19+
return $this->update( $params );
20+
}
21+
722
/**
823
* Get the client type.
924
*

0 commit comments

Comments
 (0)