-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Login: 2FA: Add a post-login nag to setup backup codes when either no…
…ne are saved or the user is running really low on them. This will hopefully reduce the number of users who become locked out of their account after losing their authentication key / device / etc. Merges #358 Fixes WordPress/wporg-two-factor#279 See WordPress/wporg-two-factor#300, WordPress/wporg-two-factor#275 git-svn-id: https://meta.svn.wordpress.org/sites/trunk@13982 74240141-8908-4e6f-9713-ba540dce6ec7
- Loading branch information
Showing
3 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
wordpress.org/public_html/wp-content/themes/pub/wporg-login/backup-codes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
use function WordPressdotorg\Two_Factor\get_edit_account_url; | ||
/** | ||
* The 'Backup Codes' post-login screen. | ||
* | ||
* This template is used for two primary purposes: | ||
* 1. The user has logged in with a backup code, we need to push them to verify their 2FA settings. | ||
* 2. The user is running low on backup codes (or has none!), we need to remind them to generate new ones. | ||
* | ||
* @package wporg-login | ||
*/ | ||
|
||
$account_settings_url = get_edit_account_url(); | ||
$redirect_to = wporg_login_wordpress_url(); | ||
$user = wp_get_current_user(); | ||
$session = WP_Session_Tokens::get_instance( $user->ID )->get( wp_get_session_token() ); | ||
$used_backup_code = str_contains( $session['two-factor-provider'] ?? '', 'Backup_Codes' ); | ||
$codes_available = Two_Factor_Backup_Codes::codes_remaining_for_user( $user ); | ||
$can_ignore = ! $used_backup_code || ( $used_backup_code && $codes_available > 1 ); | ||
|
||
if ( isset( $_REQUEST['redirect_to'] ) ) { | ||
$redirect_to = wp_validate_redirect( wp_unslash( $_REQUEST['redirect_to'] ), $redirect_to ); | ||
} | ||
|
||
// If the user is here in error, redirect off. | ||
if ( ! is_user_logged_in() || ! Two_Factor_Core::is_user_using_two_factor( $user->ID ) ) { | ||
wp_safe_redirect( $redirect_to ); | ||
exit; | ||
} | ||
|
||
/** | ||
* Record the last time we nagged the user about backup codes, as we only want to do this once per code-use. | ||
*/ | ||
update_user_meta( $user->ID, 'last_2fa_backup_codes_nag', $codes_available ); | ||
|
||
get_header(); | ||
?> | ||
|
||
<h2 class="center"><?php | ||
if ( $used_backup_code ) { | ||
_e( 'Backup Code used', 'wporg-login' ); | ||
} else { | ||
_e( 'Account Backup Codes', 'wporg-login' ); | ||
} | ||
?></h2> | ||
|
||
<p> </p> | ||
|
||
<p><?php | ||
if ( $used_backup_code ) { | ||
_e( "You've logged in with a backup code.<br>These codes are intended to be used when you lose access to your authentication device.<br>Please take a moment to review your account settings and ensure your two-factor settings are up-to-date.", 'wporg-login' ); | ||
} else { | ||
if ( ! $codes_available ) { | ||
_e( 'You do not have any backup codes remaining.', 'wporg-login' ); | ||
} else { | ||
printf( | ||
_n( | ||
'You have %s backup code remaining.', | ||
'You have %s backup codes remaining.', | ||
$codes_available, | ||
'wporg-login' | ||
), | ||
'<code>' . number_format_i18n( $codes_available ) . '</code>' | ||
); | ||
} | ||
|
||
// Direct to the backup codes screen. | ||
$account_settings_url = add_query_arg( 'screen', 'backup-codes', $account_settings_url ); | ||
} | ||
?></p> | ||
|
||
<p> </p> | ||
|
||
<p><?php | ||
_e( 'If you run out of backup codes and no longer have access to your authentication device, you are at risk of being locked out of your WordPress.org account if we are unable to verify account ownership.', 'wporg-login' ); | ||
?></p> | ||
|
||
<p> </p> | ||
|
||
<p><a href="<?php echo esc_url( $account_settings_url ); ?>"><button class="button-primary"><?php _e( 'View my account settings', 'wporg-login' ); ?></button></a></p> | ||
|
||
<?php if ( $can_ignore ) { ?> | ||
<p id="nav"> | ||
<a href="<?php echo esc_url( $redirect_to ); ?>" style="font-style: italic;"><?php _e( "I'll do this later", 'wporg-login' ); ?></a> | ||
</p> | ||
<?php } ?> | ||
|
||
<?php get_footer(); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters