v1.0.0
This commit is contained in:
parent
968211baad
commit
d405d8587d
3 changed files with 232 additions and 223 deletions
|
|
@ -52,7 +52,16 @@ class Email_Code_Sender {
|
|||
* @return int
|
||||
*/
|
||||
public function get_code_length(): int {
|
||||
return self::CODE_LENGTH;
|
||||
return self::CODE_LENGTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the minimum seconds that must elapse between resends.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_resend_interval(): int {
|
||||
return self::RESEND_INTERVAL;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -287,44 +287,23 @@ class Login_Form_Manager {
|
|||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $alternates ) ) {
|
||||
$link_markup = array();
|
||||
|
||||
foreach ( $alternates as $alternate ) {
|
||||
$link_markup[] = sprintf(
|
||||
'<a href="%1$s">%2$s</a>',
|
||||
esc_url( $alternate['url'] ),
|
||||
esc_html( $alternate['label'] )
|
||||
);
|
||||
}
|
||||
|
||||
printf(
|
||||
'<p class="robotstxt-2fa-alternate description robotstxt-2fa-stage-field">%1$s %2$s</p>',
|
||||
esc_html__( 'Prefer a different verification method?', 'robotstxt-2fa' ),
|
||||
wp_kses_post( implode( ' <span class="robotstxt-2fa-links-separator" aria-hidden="true">•</span> ', $link_markup ) )
|
||||
);
|
||||
}
|
||||
|
||||
$back_url = wp_login_url( $this->get_requested_redirect() );
|
||||
?>
|
||||
<p class="robotstxt-2fa-login-links robotstxt-2fa-stage-field">
|
||||
<a class="robotstxt-2fa-back-link" href="<?php echo esc_url( $back_url ); ?>"><?php esc_html_e( 'Back to login', 'robotstxt-2fa' ); ?></a>
|
||||
<?php if ( 'email' === $method && '' !== $stage_token ) : ?>
|
||||
<span class="robotstxt-2fa-links-separator" aria-hidden="true">•</span>
|
||||
<a class="robotstxt-2fa-resend-link" href="
|
||||
<?php
|
||||
echo esc_url(
|
||||
$this->get_stage_url(
|
||||
array(
|
||||
self::QUERY_RESEND => '1',
|
||||
self::QUERY_NOTICE => null,
|
||||
)
|
||||
)
|
||||
);
|
||||
?>
|
||||
"><?php esc_html_e( 'Send the code again', 'robotstxt-2fa' ); ?></a>
|
||||
<ul class="robotstxt-2fa-login-links robotstxt-2fa-stage-field">
|
||||
<?php if ( 'email' === $method && '' !== $stage_token ) : ?>
|
||||
<li>
|
||||
<a class="robotstxt-2fa-resend-link" href="<?php echo esc_url( $this->get_stage_url( array( self::QUERY_RESEND => '1', self::QUERY_NOTICE => null ) ) ); ?>">
|
||||
<?php esc_html_e( 'Send the code again', 'robotstxt-2fa' ); ?>
|
||||
</a>
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
<?php foreach ( $alternates as $alternate ) : ?>
|
||||
<li>
|
||||
<a href="<?php echo esc_url( $alternate['url'] ); ?>"><?php echo esc_html( $alternate['label'] ); ?></a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
<li><a class="robotstxt-2fa-back-link" href="<?php echo esc_url( $back_url ); ?>"><?php esc_html_e( 'Back to login', 'robotstxt-2fa' ); ?></a></li>
|
||||
</ul>
|
||||
<?php
|
||||
}
|
||||
|
||||
|
|
@ -348,6 +327,14 @@ class Login_Form_Manager {
|
|||
#loginform .wp-hide-pw {
|
||||
display: none !important;
|
||||
}
|
||||
.robotstxt-2fa-login-links {
|
||||
list-style: none;
|
||||
margin: 0.5em 0 0;
|
||||
padding: 0;
|
||||
}
|
||||
.robotstxt-2fa-login-links li {
|
||||
margin: 0.4em 0;
|
||||
}
|
||||
</style>
|
||||
<?php
|
||||
}
|
||||
|
|
@ -412,7 +399,18 @@ class Login_Form_Manager {
|
|||
return;
|
||||
}
|
||||
|
||||
$login = $this->get_requested_login();
|
||||
$login = $this->get_requested_login();
|
||||
$method = $this->get_requested_method();
|
||||
$sent_at = 0;
|
||||
|
||||
if ( 'email' === $method && '' !== $login ) {
|
||||
$user = get_user_by( 'login', $login );
|
||||
|
||||
if ( $user instanceof \WP_User ) {
|
||||
$challenge = $this->user_settings_repository->get_email_challenge( $user->ID );
|
||||
$sent_at = $challenge['sent_at'];
|
||||
}
|
||||
}
|
||||
?>
|
||||
<script>
|
||||
(function() {
|
||||
|
|
@ -467,6 +465,32 @@ class Login_Form_Manager {
|
|||
if ( tokenField ) {
|
||||
tokenField.focus();
|
||||
}
|
||||
|
||||
/* Resend link cooldown */
|
||||
var resendLink = document.querySelector('.robotstxt-2fa-resend-link');
|
||||
if ( resendLink ) {
|
||||
var sentAt = <?php echo absint( $sent_at ); ?>;
|
||||
var cooldown = <?php echo absint( $this->email_code_sender->get_resend_interval() ); ?>;
|
||||
var label = resendLink.textContent.trim();
|
||||
|
||||
function updateResend() {
|
||||
var remaining = cooldown - ( Math.floor( Date.now() / 1000 ) - sentAt );
|
||||
if ( remaining <= 0 ) {
|
||||
resendLink.removeAttribute('aria-disabled');
|
||||
resendLink.style.opacity = '';
|
||||
resendLink.style.pointerEvents = '';
|
||||
resendLink.textContent = label;
|
||||
} else {
|
||||
resendLink.setAttribute('aria-disabled', 'true');
|
||||
resendLink.style.opacity = '0.45';
|
||||
resendLink.style.pointerEvents = 'none';
|
||||
resendLink.textContent = label + ' (' + remaining + 's)';
|
||||
setTimeout( updateResend, 1000 );
|
||||
}
|
||||
}
|
||||
|
||||
updateResend();
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
<?php
|
||||
|
|
@ -811,7 +835,17 @@ class Login_Form_Manager {
|
|||
}
|
||||
}
|
||||
|
||||
return $available;
|
||||
// Return methods in canonical priority order: OTP first, email second, recovery last.
|
||||
$priority = array( 'otp', 'email', 'recovery' );
|
||||
$sorted = array();
|
||||
|
||||
foreach ( $priority as $p ) {
|
||||
if ( in_array( $p, $available, true ) ) {
|
||||
$sorted[] = $p;
|
||||
}
|
||||
}
|
||||
|
||||
return $sorted;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -45,11 +45,6 @@ class Profile_Settings {
|
|||
*/
|
||||
private const OTP_CODE_FIELD = 'robotstxt_2fa_otp_code';
|
||||
|
||||
/**
|
||||
* Form field used to request a new OTP secret.
|
||||
*/
|
||||
private const OTP_REGENERATE_FIELD = 'robotstxt_2fa_regenerate_otp';
|
||||
|
||||
/**
|
||||
* Form field used to confirm recovery codes.
|
||||
*/
|
||||
|
|
@ -354,7 +349,7 @@ class Profile_Settings {
|
|||
<?php esc_html_e( 'Scan the QR code or enter the setup key below in your authenticator app.', 'robotstxt-2fa' ); ?>
|
||||
</p>
|
||||
<?php if ( '' !== $otp_qr_data_uri ) : ?>
|
||||
<img src="<?php echo esc_url( $otp_qr_data_uri ); ?>" alt="<?php esc_attr_e( 'Authenticator app QR code', 'robotstxt-2fa' ); ?>" class="robotstxt-2fa-otp-qr" />
|
||||
<img src="<?php echo esc_url( $otp_qr_data_uri, array_merge( wp_allowed_protocols(), array( 'data' ) ) ); ?>" alt="<?php esc_attr_e( 'Authenticator app QR code', 'robotstxt-2fa' ); ?>" class="robotstxt-2fa-otp-qr" />
|
||||
<?php else : ?>
|
||||
<p class="description error">
|
||||
<?php esc_html_e( 'Install the QR code library and enable the XMLWriter extension to display the authenticator code image. Run the following command inside this plugin directory:', 'robotstxt-2fa' ); ?>
|
||||
|
|
@ -374,13 +369,7 @@ class Profile_Settings {
|
|||
<?php esc_html_e( 'Authenticator code', 'robotstxt-2fa' ); ?>
|
||||
</label>
|
||||
<input type="text" name="<?php echo esc_attr( self::OTP_CODE_FIELD ); ?>" id="robotstxt-2fa-otp-code" pattern="\\d{<?php echo absint( $otp_digits ); ?>}" inputmode="numeric" autocomplete="one-time-code" />
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Need a different pairing? Use the button below to generate a new QR code.', 'robotstxt-2fa' ); ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
<button type="submit" class="button button-secondary" name="<?php echo esc_attr( self::OTP_REGENERATE_FIELD ); ?>" value="1">
|
||||
<?php esc_html_e( 'Generate new secret', 'robotstxt-2fa' ); ?>
|
||||
</button>
|
||||
</div>
|
||||
<?php $recovery_required = in_array( 'recovery_codes', $required_methods, true ); ?>
|
||||
<?php if ( $recovery_required ) : ?>
|
||||
|
|
@ -426,42 +415,33 @@ class Profile_Settings {
|
|||
</p>
|
||||
<?php endif; ?>
|
||||
<?php if ( $has_preview ) : ?>
|
||||
<div class="notice notice-success inline robotstxt-2fa-recovery-preview" data-robotstxt-2fa-codes-scope="1">
|
||||
<div class="robotstxt-2fa-recovery-preview" data-robotstxt-2fa-codes-scope="1">
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Copy these recovery codes and store them somewhere safe. Each code works only once.', 'robotstxt-2fa' ); ?>
|
||||
</p>
|
||||
<div class="robotstxt-2fa-recovery-code-grid" data-robotstxt-2fa-codes-list>
|
||||
<?php
|
||||
$code_pairs = array_chunk( $preview_codes, 2 );
|
||||
foreach ( $code_pairs as $pair ) :
|
||||
?>
|
||||
<div class="robotstxt-2fa-recovery-code-row">
|
||||
<?php foreach ( $pair as $preview_code ) : ?>
|
||||
<div class="robotstxt-2fa-recovery-code-item">
|
||||
<code data-robotstxt-2fa-code><?php echo esc_html( $preview_code ); ?></code>
|
||||
<?php endforeach; ?>
|
||||
<?php if ( 1 === count( $pair ) ) : ?>
|
||||
<div class="robotstxt-2fa-recovery-code-item"> </div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<ul class="robotstxt-2fa-recovery-code-list" data-robotstxt-2fa-codes-list>
|
||||
<?php foreach ( $preview_codes as $preview_code ) : ?>
|
||||
<li><code data-robotstxt-2fa-code><?php echo esc_html( $preview_code ); ?></code></li>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<button type="button" class="button button-secondary robotstxt-2fa-recovery-copy" data-robotstxt-2fa-copy-button="1">
|
||||
<?php esc_html_e( 'Copy codes', 'robotstxt-2fa' ); ?>
|
||||
</button>
|
||||
<p class="description robotstxt-2fa-recovery-copy-feedback" data-robotstxt-2fa-copy-feedback="1" hidden>
|
||||
<?php esc_html_e( 'The recovery codes have been copied to your clipboard.', 'robotstxt-2fa' ); ?>
|
||||
</ul>
|
||||
<p>
|
||||
<button type="button" class="button button-secondary robotstxt-2fa-recovery-copy" data-robotstxt-2fa-copy-button="1">
|
||||
<?php esc_html_e( 'Copy codes', 'robotstxt-2fa' ); ?>
|
||||
</button>
|
||||
<span class="robotstxt-2fa-recovery-copy-feedback" data-robotstxt-2fa-copy-feedback="1" hidden>
|
||||
<?php esc_html_e( '✓ Copied to clipboard', 'robotstxt-2fa' ); ?>
|
||||
</span>
|
||||
</p>
|
||||
<?php if ( ! $recovery_enabled ) : ?>
|
||||
<div class="robotstxt-2fa-recovery-confirm">
|
||||
<label class="robotstxt-2fa-recovery-confirm-label" for="robotstxt-2fa-recovery-confirm">
|
||||
<?php esc_html_e( 'Enter one of the codes above to confirm you saved them.', 'robotstxt-2fa' ); ?>
|
||||
<label for="robotstxt-2fa-recovery-confirm">
|
||||
<?php esc_html_e( 'Enter one of the codes above to confirm you saved them:', 'robotstxt-2fa' ); ?>
|
||||
</label>
|
||||
<input type="text" name="<?php echo esc_attr( self::RECOVERY_CONFIRM_FIELD ); ?>" id="robotstxt-2fa-recovery-confirm" pattern="\\d{<?php echo absint( $recovery_code_length ); ?>}" inputmode="numeric" autocomplete="one-time-code" />
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Once confirmed, the codes disappear from this page but remain valid until you use them.', 'robotstxt-2fa' ); ?>
|
||||
</p>
|
||||
</div>
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Once confirmed, the codes disappear from this page but remain valid until you use them.', 'robotstxt-2fa' ); ?>
|
||||
</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php elseif ( ! $recovery_enabled ) : ?>
|
||||
|
|
@ -540,7 +520,16 @@ class Profile_Settings {
|
|||
}
|
||||
}
|
||||
|
||||
toggle.addEventListener( 'change', applyState );
|
||||
toggle.addEventListener( 'change', function () {
|
||||
applyState();
|
||||
// Pre-check email when enabling 2FA for the first time.
|
||||
if ( toggle.checked ) {
|
||||
var emailCheckbox = document.getElementById( 'robotstxt-2fa-method-email' );
|
||||
if ( emailCheckbox && ! emailCheckbox.checked ) {
|
||||
emailCheckbox.checked = true;
|
||||
}
|
||||
}
|
||||
} );
|
||||
applyState();
|
||||
}());
|
||||
</script>
|
||||
|
|
@ -717,12 +706,6 @@ class Profile_Settings {
|
|||
$otp_was_enabled = in_array( 'otp', $previous_methods, true );
|
||||
$recovery_was_enabled = in_array( 'recovery_codes', $previous_methods, true );
|
||||
|
||||
$otp_regenerate_requested = isset( $_POST[ self::OTP_REGENERATE_FIELD ] );
|
||||
|
||||
if ( $otp_regenerate_requested ) {
|
||||
$this->focus_section = true;
|
||||
}
|
||||
|
||||
$otp_code = '';
|
||||
|
||||
if ( isset( $_POST[ self::OTP_CODE_FIELD ] ) && is_string( $_POST[ self::OTP_CODE_FIELD ] ) ) {
|
||||
|
|
@ -746,21 +729,8 @@ class Profile_Settings {
|
|||
$otp_requested = in_array( 'otp', $requested_methods, true );
|
||||
$otp_should_remain_enabled = false;
|
||||
|
||||
if ( $otp_regenerate_requested ) {
|
||||
$this->otp_manager->delete_secret( $user_id );
|
||||
$this->otp_manager->ensure_secret( $user );
|
||||
$otp_was_enabled = false;
|
||||
$otp_requested = true;
|
||||
add_settings_error(
|
||||
'robotstxt-2fa',
|
||||
'robotstxt-2fa-otp-reset',
|
||||
__( 'A new authenticator secret is ready. Scan the QR code and confirm it with a fresh code to finish reconnecting.', 'robotstxt-2fa' ),
|
||||
'updated'
|
||||
);
|
||||
}
|
||||
|
||||
if ( $otp_was_enabled ) {
|
||||
if ( $otp_requested && ! $otp_regenerate_requested ) {
|
||||
if ( $otp_requested ) {
|
||||
$otp_should_remain_enabled = true;
|
||||
} else {
|
||||
$this->otp_manager->delete_secret( $user_id );
|
||||
|
|
@ -865,6 +835,11 @@ class Profile_Settings {
|
|||
|
||||
$selected_methods = array_values( array_unique( $selected_methods ) );
|
||||
|
||||
// When enabling for the first time without selecting a method, default to email.
|
||||
if ( $enabled && empty( $selected_methods ) && in_array( 'email', $available_keys, true ) ) {
|
||||
$selected_methods[] = 'email';
|
||||
}
|
||||
|
||||
if ( ! $is_forced && empty( $selected_methods ) ) {
|
||||
$enabled = false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue