This commit is contained in:
Javier Casares 2026-11-04 14:36:03 +00:00
commit 1ff471299e
3 changed files with 230 additions and 24 deletions

View file

@ -7,6 +7,7 @@
namespace Robotstxt\TwoFA;
use Robotstxt\TwoFA\Login\Email_Code_Sender;
use Robotstxt\TwoFA\User\Two_Factor_Config;
if ( ! defined( 'ABSPATH' ) ) {
@ -61,6 +62,42 @@ class Email_Notifications {
add_action( self::DIGEST_CRON_HOOK, array( $this, 'send_digest' ) );
add_filter( 'cron_schedules', array( $this, 'add_cron_schedules' ) );
add_action( 'init', array( $this, 'maybe_schedule_digest' ) );
add_action( 'admin_notices', array( $this, 'render_delivery_failure_notice' ) );
add_action( 'network_admin_notices', array( $this, 'render_delivery_failure_notice' ) );
}
/**
* Warn administrators while verification-code delivery is failing.
*
* When a role forces email verification and wp_mail() fails, users are
* locked out at the login challenge with an error that points them at
* their administrator — while the real fault is the site's mail
* configuration. The failure marker is written by
* {@see Email_Code_Sender} on failed delivery and removed automatically
* once a code is delivered successfully.
*
* @since 1.6.8
*
* @return void
*/
public function render_delivery_failure_notice(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$failure = get_option( Email_Code_Sender::MAIL_FAILURE_OPTION, array() );
if ( ! is_array( $failure ) || empty( $failure['at'] ) ) {
return;
}
?>
<div class="notice notice-error robotstxt-2fa-mail-failure">
<p>
<strong><?php esc_html_e( '2FA (by ROBOTSTXT): email delivery is failing.', 'robotstxt-2fa' ); ?></strong>
<?php esc_html_e( 'Verification code emails cannot be sent, so users required to sign in with an email code cannot access the site. Check the site mail or SMTP configuration — this notice disappears automatically once a code is delivered successfully.', 'robotstxt-2fa' ); ?>
</p>
</div>
<?php
}
/**

View file

@ -17,6 +17,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* Handles generation and delivery of email-based verification codes.
*/
class Email_Code_Sender {
/**
* Option key recording the last failed delivery attempt. Kept while mail
* delivery fails so administrators can see why forced-email users cannot
* sign in; cleared automatically once a code is delivered successfully.
*/
public const MAIL_FAILURE_OPTION = 'robotstxt_2fa_last_mail_failure';
/**
* Default length of the generated numeric code.
*/
@ -187,14 +194,57 @@ class Email_Code_Sender {
$headers = array( 'Content-Type: text/plain; charset=UTF-8' );
if ( ! wp_mail( $user->user_email, $email_subject, $message, $headers ) ) {
$this->record_mail_failure( $user->user_email );
return false;
}
$this->clear_mail_failure();
$this->user_settings_repository->store_email_challenge( $user->ID, $code, $expires );
return true;
}
/**
* Record a failed verification-code delivery for administrator visibility.
*
* When a role forces email verification, a failing wp_mail() locks users
* out of their accounts with no site-side signal. Store the failure so an
* admin notice can explain the root cause.
*
* @since 1.6.8
*
* @param string $to Intended recipient.
*
* @return void
*/
private function record_mail_failure( string $to ): void {
$existing = get_option( self::MAIL_FAILURE_OPTION, array() );
$record = is_array( $existing ) ? $existing : array();
update_option(
self::MAIL_FAILURE_OPTION,
array(
'at' => time(),
'to' => $to,
'count' => ( isset( $record['count'] ) && is_numeric( $record['count'] ) ? (int) $record['count'] : 0 ) + 1,
),
false
);
}
/**
* Clear the delivery failure marker after a successful delivery.
*
* @since 1.6.8
*
* @return void
*/
private function clear_mail_failure(): void {
delete_option( self::MAIL_FAILURE_OPTION );
}
/**
* Verify a submitted code against the stored challenge.
*

View file

@ -85,9 +85,21 @@ class Login_Form_Manager {
private const FIELD_METHOD = 'robotstxt_2fa_method';
/**
* Transient key prefix for pending-stage tokens.
* Option key storing the pending-stage token map.
*
* Maps token => array( user ID, expiry ). Persistent storage instead of a
* transient: external object caches can evict transients at any time,
* which would kill live verification stages and lock users out with
* "We could not determine which account you are trying to access."
*/
private const PENDING_TRANSIENT_PREFIX = 'robotstxt_2fa_stage_';
private const PENDING_OPTION = 'robotstxt_2fa_pending_stages';
/**
* Maximum concurrent pending-stage tokens kept in the map.
*
* The map is pruned on every write; the cap bounds its size.
*/
private const PENDING_MAX_TOKENS = 20;
/**
* Pending-stage token TTL in seconds — matches WP nonce lifetime (24 h).
@ -649,7 +661,7 @@ class Login_Form_Manager {
$authenticated_user = $this->resolve_pending_user();
if ( null === $authenticated_user ) {
return new \WP_Error( 'robotstxt_2fa_unknown_user', __( 'We could not determine which account you are trying to access.', 'robotstxt-2fa' ) );
return new \WP_Error( 'robotstxt_2fa_unknown_user', __( 'Your verification session expired or could not be found. Please sign in again.', 'robotstxt-2fa' ) );
}
// Brute-force: check if the account is currently locked out.
@ -753,7 +765,7 @@ class Login_Form_Manager {
// Consume the pending token so it cannot be replayed.
$pending_token = $this->get_requested_pending_token();
if ( '' !== $pending_token ) {
delete_transient( self::PENDING_TRANSIENT_PREFIX . $pending_token );
$this->consume_pending_token( $pending_token );
}
$this->user_settings_repository->record_last_verification(
@ -1333,21 +1345,99 @@ class Login_Form_Manager {
/**
* Issue an opaque pending-stage token that maps to a user ID.
*
* Stores the user ID in a short-lived transient so the login username
* is never transmitted in the URL query string.
* Stores the token in a persistent option map (see PENDING_OPTION) so the
* login username is never transmitted in the URL query string and object
* cache eviction cannot kill a live stage.
*
* @since 1.3.0
* @since 1.6.8 Stored in a persistent option map instead of a transient.
*
* @param int $user_id User identifier.
*
* @return string 32-character alphanumeric token.
*/
private function issue_pending_token( int $user_id ): string {
$tokens = $this->get_pending_token_map();
$now = time();
foreach ( $tokens as $existing_token => $data ) {
if ( $data['e'] <= $now ) {
unset( $tokens[ $existing_token ] );
}
}
$token = wp_generate_password( 32, false, false );
set_transient( self::PENDING_TRANSIENT_PREFIX . $token, $user_id, self::PENDING_TTL );
$tokens[ $token ] = array(
'u' => $user_id,
'e' => $now + self::PENDING_TTL,
);
if ( count( $tokens ) > self::PENDING_MAX_TOKENS ) {
uasort(
$tokens,
static function ( array $a, array $b ): int {
return (int) $a['e'] <=> (int) $b['e'];
}
);
$tokens = array_slice( $tokens, -self::PENDING_MAX_TOKENS, null, true );
}
update_option( self::PENDING_OPTION, $tokens, false );
return $token;
}
/**
* Retrieve the raw pending-stage token map stored in the database.
*
* @since 1.6.8
*
* @return array<string, array{u: int, e: int}> Token => user ID + expiry.
*/
private function get_pending_token_map(): array {
$raw = get_option( self::PENDING_OPTION, array() );
if ( ! is_array( $raw ) ) {
return array();
}
$tokens = array();
foreach ( $raw as $token => $data ) {
if ( is_string( $token ) && is_array( $data ) && isset( $data['u'], $data['e'] ) && is_numeric( $data['u'] ) && is_numeric( $data['e'] ) ) {
$tokens[ $token ] = array(
'u' => (int) $data['u'],
'e' => (int) $data['e'],
);
}
}
return $tokens;
}
/**
* Remove a consumed pending-stage token from the map.
*
* @since 1.6.8
*
* @param string $token Token to consume.
*
* @return void
*/
private function consume_pending_token( string $token ): void {
$tokens = $this->get_pending_token_map();
if ( ! isset( $tokens[ $token ] ) ) {
return;
}
unset( $tokens[ $token ] );
update_option( self::PENDING_OPTION, $tokens, false );
}
/**
* Retrieve the raw pending-stage token from the current request.
*
@ -1373,6 +1463,7 @@ class Login_Form_Manager {
* Resolve the pending-stage token to its associated WP_User.
*
* @since 1.3.0
* @since 1.6.8 Reads the persistent token map instead of a transient.
*
* @return \WP_User|null Null when no valid token is present.
*/
@ -1383,13 +1474,13 @@ class Login_Form_Manager {
return null;
}
$user_id_raw = get_transient( self::PENDING_TRANSIENT_PREFIX . $token );
$data = $this->get_pending_token_map()[ $token ] ?? null;
if ( ! is_numeric( $user_id_raw ) ) {
if ( null === $data || $data['e'] <= time() ) {
return null;
}
$user = get_user_by( 'ID', (int) $user_id_raw );
$user = get_user_by( 'ID', $data['u'] );
return $user instanceof \WP_User ? $user : null;
}