Skip to content

Commit ed078c8

Browse files
authored
Merge pull request #676 from WordPress/483-mark-recommended
Label recommended methods to simplify the configuration
2 parents f924767 + caff650 commit ed078c8

5 files changed

Lines changed: 72 additions & 23 deletions

File tree

class-two-factor-core.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ public static function manage_users_custom_column( $output, $column_name, $user_
18371837
* @param WP_User $user WP_User object of the logged-in user.
18381838
*/
18391839
public static function user_two_factor_options( $user ) {
1840-
$notices = [];
1840+
$notices = array();
18411841

18421842
$providers = self::get_supported_providers_for_user( $user );
18431843

@@ -1901,13 +1901,46 @@ public static function user_two_factor_options( $user ) {
19011901
do_action( 'show_user_security_settings', $user, $providers );
19021902
}
19031903

1904+
/**
1905+
* Get the recommended providers for a user.
1906+
*
1907+
* @param WP_User $user User instance.
1908+
*
1909+
* @return array List of provider keys.
1910+
*/
1911+
private static function get_recommended_providers( $user ) {
1912+
$providers = array(
1913+
'Two_Factor_Totp',
1914+
'Two_Factor_Backup_Codes',
1915+
);
1916+
1917+
/**
1918+
* Set the keys of the recommended (secure) methods.
1919+
*
1920+
* @param array $recommended_providers The recommended providers.
1921+
* @param WP_User $user The user.
1922+
*/
1923+
return (array) apply_filters( 'two_factor_recommended_providers', $providers, $user );
1924+
}
1925+
1926+
/**
1927+
* Render the user settings.
1928+
*
1929+
* @param WP_User $user User instance.
1930+
* @param array $providers List of available providers.
1931+
*/
19041932
private static function render_user_providers_form( $user, $providers ) {
1905-
$primary_provider_key = self::get_primary_provider_key_selected_for_user( $user );
1906-
$enabled_providers = self::get_enabled_providers_for_user( $user );
1933+
$primary_provider_key = self::get_primary_provider_key_selected_for_user( $user );
1934+
$enabled_providers = self::get_enabled_providers_for_user( $user );
1935+
$recommended_provider_keys = self::get_recommended_providers( $user );
1936+
1937+
// Move the recommended providers first.
1938+
$recommended_providers = array_intersect_key( $providers, array_flip( $recommended_provider_keys ) );
1939+
$providers = array_merge( $recommended_providers, $providers );
19071940

19081941
?>
19091942
<p>
1910-
<?php esc_html_e( 'Configure a primary two-factor method along with a backup method, such as Recovery Codes, to avoid being locked out if you lose access to your primary method.', 'two-factor' ); ?>
1943+
<?php esc_html_e( 'Configure a primary two-factor method along with a backup method, such as Recovery Codes, to avoid being locked out if you lose access to your primary method. Methods marked as recommended are more secure and easier to use.', 'two-factor' ); ?>
19111944
</p>
19121945

19131946
<?php wp_nonce_field( 'user_two_factor_options', '_nonce_user_two_factor_options', false ); ?>
@@ -1921,7 +1954,10 @@ private static function render_user_providers_form( $user, $providers ) {
19211954
<td>
19221955
<label class="two-factor-method-label">
19231956
<input id="enabled-<?php echo esc_attr( $provider_key ); ?>" type="checkbox" name="<?php echo esc_attr( self::ENABLED_PROVIDERS_USER_META_KEY ); ?>[]" value="<?php echo esc_attr( $provider_key ); ?>" <?php checked( in_array( $provider_key, $enabled_providers, true ) ); ?> />
1924-
<?php echo esc_html( sprintf( __( 'Enable %s', 'two-factor' ), $object->get_label() ) ); ?>
1957+
<strong><?php echo esc_html( sprintf( __( 'Enable %s', 'two-factor' ), $object->get_label() ) ); ?></strong>
1958+
<?php if ( in_array( $provider_key, $recommended_provider_keys, true ) ) : ?>
1959+
<abbr title="<?php esc_attr_e( 'This method is more secure and easy to use', 'two-factor' ); ?>" class="two-factor-method-recommended"><?php esc_html_e( 'Recommended', 'two-factor' ); ?></abbr>
1960+
<?php endif; ?>
19251961
</label>
19261962
<?php
19271963
/**

providers/class-two-factor-backup-codes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ public function user_options( $user ) {
175175
<button type="button" class="button button-two-factor-backup-codes-generate button-secondary hide-if-no-js">
176176
<?php esc_html_e( 'Generate new recovery codes', 'two-factor' ); ?>
177177
</button>
178+
179+
<em><?php esc_html_e( 'This invalidates all currently stored codes.' ); ?></em>
178180
</p>
179181
</p>
180182
<div class="two-factor-backup-codes-wrapper" style="display:none;">

providers/class-two-factor-email.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public function is_available_for_user( $user ) {
368368
public function user_options( $user ) {
369369
$email = $user->user_email;
370370
?>
371-
<div>
371+
<p>
372372
<?php
373373
echo esc_html(
374374
sprintf(
@@ -378,7 +378,7 @@ public function user_options( $user ) {
378378
)
379379
);
380380
?>
381-
</div>
381+
</p>
382382
<?php
383383
}
384384

providers/class-two-factor-totp.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function register_rest_routes() {
6464
array(
6565
'methods' => WP_REST_Server::DELETABLE,
6666
'callback' => array( $this, 'rest_delete_totp' ),
67-
'permission_callback' => function( $request ) {
67+
'permission_callback' => function ( $request ) {
6868
return Two_Factor_Core::rest_api_can_edit_user_and_update_two_factor_options( $request['user_id'] );
6969
},
7070
'args' => array(
@@ -77,20 +77,20 @@ public function register_rest_routes() {
7777
array(
7878
'methods' => WP_REST_Server::CREATABLE,
7979
'callback' => array( $this, 'rest_setup_totp' ),
80-
'permission_callback' => function( $request ) {
80+
'permission_callback' => function ( $request ) {
8181
return Two_Factor_Core::rest_api_can_edit_user_and_update_two_factor_options( $request['user_id'] );
8282
},
8383
'args' => array(
84-
'user_id' => array(
84+
'user_id' => array(
8585
'required' => true,
8686
'type' => 'integer',
8787
),
88-
'key' => array(
88+
'key' => array(
8989
'type' => 'string',
9090
'default' => '',
9191
'validate_callback' => null, // Note: validation handled in ::rest_setup_totp().
9292
),
93-
'code' => array(
93+
'code' => array(
9494
'type' => 'string',
9595
'default' => '',
9696
'validate_callback' => null, // Note: validation handled in ::rest_setup_totp().
@@ -159,10 +159,10 @@ public function rest_delete_totp( $request ) {
159159
$this->user_two_factor_options( $user );
160160
$html = ob_get_clean();
161161

162-
return [
162+
return array(
163163
'success' => true,
164164
'html' => $html,
165-
];
165+
);
166166
}
167167

168168
/**
@@ -198,10 +198,10 @@ public function rest_setup_totp( $request ) {
198198
$this->user_two_factor_options( $user );
199199
$html = ob_get_clean();
200200

201-
return [
201+
return array(
202202
'success' => true,
203203
'html' => $html,
204-
];
204+
);
205205
}
206206

207207
/**
@@ -283,22 +283,19 @@ public function user_two_factor_options( $user ) {
283283
<div id="two-factor-totp-options">
284284
<?php
285285
if ( empty( $key ) ) :
286-
287286
$key = $this->generate_key();
288287
$totp_url = $this->generate_qr_code_url( $user, $key );
289288

290289
?>
291-
292290
<p>
293-
<?php esc_html_e( 'Please scan the QR code or manually enter the key, then enter an authentication code from your app in order to complete setup.', 'two-factor' ); ?>
291+
<?php esc_html_e( 'Please scan the QR code or manually copy the shared secret key from below to your Authenticator app:', 'two-factor' ); ?>
294292
</p>
295293
<p id="two-factor-qr-code">
296-
<a href="<?php echo $totp_url; ?>">
297-
Loading...
294+
<a href="<?php echo esc_url( $totp_url ); ?>">
295+
<?php esc_html_e( 'Loading', 'two-factor' ); ?>
298296
<img src="<?php echo esc_url( admin_url( 'images/spinner.gif' ) ); ?>" alt="" />
299297
</a>
300298
</p>
301-
302299
<style>
303300
#two-factor-qr-code {
304301
/* The size of the image will change based on the length of the URL inside it. */
@@ -343,7 +340,11 @@ public function user_two_factor_options( $user ) {
343340
</script>
344341

345342
<p>
346-
<code><?php echo esc_html( $key ); ?></code>
343+
<?php esc_html_e( 'Shared secret key:', 'two-factor' ); ?> <code><?php echo esc_html( $key ); ?></code>
344+
</p>
345+
<hr />
346+
<p>
347+
<?php esc_html_e( 'Enter the code generated by the Authenticator app to complete the setup:', 'two-factor' ); ?>
347348
</p>
348349
<p>
349350
<input type="hidden" id="two-factor-totp-key" name="two-factor-totp-key" value="<?php echo esc_attr( $key ); ?>" />

user-edit.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@
77
display: block;
88
font-weight: 700;
99
}
10+
11+
.two-factor-methods-table .two-factor-method-recommended {
12+
font-size: 0.8rem;
13+
line-height: 1;
14+
font-weight: 400;
15+
border: 1px dotted;
16+
border-radius: 0.15rem;
17+
padding: 0.1rem 0.25rem;
18+
margin: 0 0.15rem;
19+
}

0 commit comments

Comments
 (0)