v1.2.0
This commit is contained in:
parent
d405d8587d
commit
a115f08319
1 changed files with 1817 additions and 141 deletions
|
|
@ -8,8 +8,10 @@
|
|||
namespace Robotstxt\TwoFA\Login;
|
||||
|
||||
use Robotstxt\TwoFA\Frequency_Options;
|
||||
use Robotstxt\TwoFA\User\Grace_Period;
|
||||
use Robotstxt\TwoFA\User\OTP_Manager;
|
||||
use Robotstxt\TwoFA\User\Recovery_Codes;
|
||||
use Robotstxt\TwoFA\User\Trusted_Devices;
|
||||
use Robotstxt\TwoFA\User\Two_Factor_Config;
|
||||
use Robotstxt\TwoFA\User\User_Settings_Repository;
|
||||
use WP_User;
|
||||
|
|
@ -132,6 +134,20 @@ class Login_Form_Manager {
|
|||
*/
|
||||
private Two_Factor_Config $config;
|
||||
|
||||
/**
|
||||
* Trusted devices manager.
|
||||
*
|
||||
* @var Trusted_Devices
|
||||
*/
|
||||
private Trusted_Devices $trusted_devices;
|
||||
|
||||
/**
|
||||
* Grace period manager.
|
||||
*
|
||||
* @var Grace_Period
|
||||
*/
|
||||
private Grace_Period $grace_period;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
|
|
@ -141,6 +157,8 @@ class Login_Form_Manager {
|
|||
$this->recovery_codes = new Recovery_Codes();
|
||||
$this->user_settings_repository = new User_Settings_Repository();
|
||||
$this->config = new Two_Factor_Config();
|
||||
$this->trusted_devices = new Trusted_Devices();
|
||||
$this->grace_period = new Grace_Period();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -287,12 +305,45 @@ class Login_Form_Manager {
|
|||
);
|
||||
}
|
||||
|
||||
$trust_days = $this->config->get_trust_device_days();
|
||||
|
||||
if ( $trust_days > 0 ) {
|
||||
?>
|
||||
<p class="robotstxt-2fa-trust-field robotstxt-2fa-stage-field">
|
||||
<label>
|
||||
<input type="checkbox" name="robotstxt_2fa_trust_device" value="1" />
|
||||
<?php
|
||||
/* translators: %d: number of days the device trust lasts. */
|
||||
$trust_label = _n(
|
||||
'Remember this browser for %d day',
|
||||
'Remember this browser for %d days',
|
||||
$trust_days,
|
||||
'robotstxt-2fa'
|
||||
);
|
||||
echo esc_html( sprintf( $trust_label, absint( $trust_days ) ) );
|
||||
?>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
$back_url = wp_login_url( $this->get_requested_redirect() );
|
||||
?>
|
||||
<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 ) ) ); ?>">
|
||||
<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>
|
||||
|
|
@ -538,6 +589,28 @@ class Login_Form_Manager {
|
|||
return new \WP_Error( 'robotstxt_2fa_unknown_user', __( 'We could not load the account requesting verification.', 'robotstxt-2fa' ) );
|
||||
}
|
||||
|
||||
// Brute-force: check if the account is currently locked out.
|
||||
$lock_key = 'robotstxt_2fa_locked_' . $authenticated_user->ID;
|
||||
$locked = get_transient( $lock_key );
|
||||
|
||||
if ( false !== $locked ) {
|
||||
$remaining_seconds = max( 0, ( is_numeric( $locked ) ? (int) $locked : 0 ) - time() );
|
||||
$remaining_minutes = (int) ceil( $remaining_seconds / 60 );
|
||||
return new \WP_Error(
|
||||
'robotstxt_2fa_locked',
|
||||
sprintf(
|
||||
/* translators: %d: minutes remaining. */
|
||||
_n(
|
||||
'Too many failed attempts. Please try again in %d minute.',
|
||||
'Too many failed attempts. Please try again in %d minutes.',
|
||||
$remaining_minutes,
|
||||
'robotstxt-2fa'
|
||||
),
|
||||
$remaining_minutes
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$stage_token = $this->get_requested_stage_token();
|
||||
|
||||
if ( '' === $stage_token || ! $this->validate_stage_token( $authenticated_user, $stage_token ) ) {
|
||||
|
|
@ -582,9 +655,38 @@ class Login_Form_Manager {
|
|||
}
|
||||
|
||||
if ( ! $is_valid ) {
|
||||
/**
|
||||
* Fires when a 2FA verification attempt fails.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_User $user User who failed the challenge.
|
||||
* @param string $method Verification method slug that was attempted.
|
||||
*/
|
||||
do_action( 'robotstxt_2fa_verification_failed', $authenticated_user, $method );
|
||||
|
||||
// Brute-force: increment attempt counter and lock if threshold reached.
|
||||
$max_attempts = $this->config->get_max_attempts();
|
||||
|
||||
if ( $max_attempts > 0 ) {
|
||||
$attempts_key = 'robotstxt_2fa_attempts_' . $authenticated_user->ID;
|
||||
$raw_attempts = get_transient( $attempts_key );
|
||||
$attempts = ( is_numeric( $raw_attempts ) ? (int) $raw_attempts : 0 ) + 1;
|
||||
set_transient( $attempts_key, $attempts, 30 * MINUTE_IN_SECONDS );
|
||||
|
||||
if ( $attempts >= $max_attempts ) {
|
||||
delete_transient( $attempts_key );
|
||||
$expires_at = time() + $this->config->get_lockout_duration() * MINUTE_IN_SECONDS;
|
||||
set_transient( $lock_key, $expires_at, $this->config->get_lockout_duration() * MINUTE_IN_SECONDS );
|
||||
}
|
||||
}
|
||||
|
||||
return new \WP_Error( 'robotstxt_2fa_invalid_code', __( 'The verification code is incorrect or has expired.', 'robotstxt-2fa' ) );
|
||||
}
|
||||
|
||||
// Brute-force: clear the attempt counter on success.
|
||||
delete_transient( 'robotstxt_2fa_attempts_' . $authenticated_user->ID );
|
||||
|
||||
$this->user_settings_repository->record_last_verification(
|
||||
$authenticated_user->ID,
|
||||
$method,
|
||||
|
|
@ -592,6 +694,23 @@ class Login_Form_Manager {
|
|||
time()
|
||||
);
|
||||
|
||||
// Trusted devices: set cookie when user checked "Remember this browser".
|
||||
$trust_days = $this->config->get_trust_device_days();
|
||||
|
||||
if ( $trust_days > 0 && isset( $_POST['robotstxt_2fa_trust_device'] ) && '1' === $_POST['robotstxt_2fa_trust_device'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce already verified above.
|
||||
$this->trusted_devices->trust_current_device( $authenticated_user, $trust_days );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires after a successful 2FA verification.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param \WP_User $user Authenticated user.
|
||||
* @param string $method Verification method slug that succeeded.
|
||||
*/
|
||||
do_action( 'robotstxt_2fa_verification_success', $authenticated_user, $method );
|
||||
|
||||
return $authenticated_user;
|
||||
}
|
||||
|
||||
|
|
@ -615,6 +734,63 @@ class Login_Form_Manager {
|
|||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter whether to skip the 2FA challenge for the current request.
|
||||
*
|
||||
* Return true to bypass the challenge entirely. Use this for REST API
|
||||
* requests, WP-CLI, Application Passwords, trusted IPs, etc.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param bool $skip Whether to skip the challenge. Default false.
|
||||
* @param \WP_User $user Authenticated user about to be challenged.
|
||||
*/
|
||||
if ( (bool) apply_filters( 'robotstxt_2fa_skip_challenge', false, $user ) ) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
// Trusted devices: skip challenge when the browser carries a valid trust cookie.
|
||||
if ( $this->config->get_trust_device_days() > 0 && $this->trusted_devices->is_trusted( $user ) ) {
|
||||
return $user;
|
||||
}
|
||||
|
||||
$required_methods = $this->config->get_required_methods_for_user( $user );
|
||||
|
||||
// Grace period: if forced but no methods configured yet, check whether the user is within their grace window.
|
||||
if ( ! empty( $required_methods ) ) {
|
||||
$user_settings = $this->user_settings_repository->get_user_settings( $user->ID );
|
||||
$has_any_method = ! empty( $user_settings['methods'] );
|
||||
|
||||
if ( ! $has_any_method ) {
|
||||
$grace_days = $this->config->get_grace_period_days();
|
||||
|
||||
if ( $grace_days > 0 && $this->grace_period->is_active( $user, $grace_days ) ) {
|
||||
// Still within grace period — let the user through.
|
||||
$remaining = $this->grace_period->get_days_remaining( $user, $grace_days );
|
||||
set_transient(
|
||||
'robotstxt_2fa_grace_notice_' . $user->ID,
|
||||
$remaining,
|
||||
MINUTE_IN_SECONDS
|
||||
);
|
||||
return $user;
|
||||
}
|
||||
|
||||
if ( $grace_days > 0 ) {
|
||||
// Grace period has expired.
|
||||
if ( 'wizard' === $this->config->get_grace_period_action() ) {
|
||||
$this->grace_period->set_pending_setup( $user->ID );
|
||||
return $user;
|
||||
}
|
||||
|
||||
// Block mode.
|
||||
return new \WP_Error(
|
||||
'robotstxt_2fa_setup_required',
|
||||
__( 'Your account requires two-factor authentication. Please contact your administrator to complete the setup.', 'robotstxt-2fa' )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$method = $this->determine_required_verification_method( $user );
|
||||
|
||||
if ( '' === $method ) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue