v1.6.0
This commit is contained in:
parent
38520b2ba7
commit
fd2880bece
1 changed files with 1075 additions and 87 deletions
|
|
@ -310,7 +310,8 @@ class Login_Form_Manager {
|
|||
);
|
||||
}
|
||||
|
||||
$trust_days = $this->config->get_trust_device_days();
|
||||
// Trust-device duration follows the user's profile verification frequency.
|
||||
$trust_days = $pending_user instanceof WP_User ? $this->get_trust_days_for_user( $pending_user ) : 0;
|
||||
|
||||
if ( $trust_days > 0 ) {
|
||||
?>
|
||||
|
|
@ -691,10 +692,13 @@ class Login_Form_Manager {
|
|||
);
|
||||
|
||||
// Trusted devices: set cookie when user checked "Remember this browser".
|
||||
$trust_days = $this->config->get_trust_device_days();
|
||||
// Duration complies with the user's profile verification frequency.
|
||||
if ( isset( $_POST['robotstxt_2fa_trust_device'] ) && '1' === $_POST['robotstxt_2fa_trust_device'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce already verified above.
|
||||
$trust_days = $this->get_trust_days_for_user( $authenticated_user );
|
||||
|
||||
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 );
|
||||
if ( $trust_days > 0 ) {
|
||||
$this->trusted_devices->trust_current_device( $authenticated_user, $trust_days );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -931,14 +935,16 @@ class Login_Form_Manager {
|
|||
/**
|
||||
* Retrieve the verification methods that are active for the provided user.
|
||||
*
|
||||
* When 2FA is enforced by role, only methods that are both required by the
|
||||
* user's role(s) and actually set up on their profile are returned. If the
|
||||
* user has not configured any of the required methods yet, all required
|
||||
* methods that the system can deliver (e.g. email) are returned so the login
|
||||
* can still be challenged.
|
||||
* Role enforcement is treated as a floor, not a ceiling: when 2FA is
|
||||
* enforced by role, the required methods are guaranteed to be set up on the
|
||||
* profile (enforced at save time), but the user may authenticate with ANY
|
||||
* method they have configured — not only the role-required ones. If the
|
||||
* user has not configured any method yet, the role-required methods that
|
||||
* the system can deliver (e.g. email) are returned so the login can still
|
||||
* be challenged.
|
||||
*
|
||||
* @param \WP_User $user User attempting to authenticate.
|
||||
* @param array{enabled: bool, methods: array<int, string>, frequency: string}|null $user_settings Optional preloaded settings.
|
||||
* @param \WP_User $user User attempting to authenticate.
|
||||
* @param array{enabled: bool, methods: array<int, string>, frequency: string, preferred_method: string}|null $user_settings Optional preloaded settings.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
|
|
@ -968,8 +974,9 @@ class Login_Form_Manager {
|
|||
|
||||
$profile_methods = array_values( array_unique( $profile_methods ) );
|
||||
|
||||
// When forced, restrict to methods required by the user's roles.
|
||||
if ( $is_forced ) {
|
||||
if ( $is_forced && empty( $profile_methods ) ) {
|
||||
// User has not configured any method yet — fall back to the methods the
|
||||
// system can deliver from the required set so a challenge is still possible.
|
||||
$required_slugs = array();
|
||||
|
||||
foreach ( $required_methods as $method ) {
|
||||
|
|
@ -980,16 +987,9 @@ class Login_Form_Manager {
|
|||
}
|
||||
}
|
||||
|
||||
$required_slugs = array_values( array_unique( $required_slugs ) );
|
||||
|
||||
// Prefer the intersection of profile-enabled and role-required methods.
|
||||
$candidate_methods = array_values( array_intersect( $profile_methods, $required_slugs ) );
|
||||
|
||||
// Fall back to all role-required methods if none are set up yet.
|
||||
if ( empty( $candidate_methods ) ) {
|
||||
$candidate_methods = $required_slugs;
|
||||
}
|
||||
$candidate_methods = array_values( array_unique( $required_slugs ) );
|
||||
} else {
|
||||
// Offer every method the user has configured (role enforcement is a floor).
|
||||
$candidate_methods = $profile_methods;
|
||||
}
|
||||
|
||||
|
|
@ -1013,6 +1013,7 @@ class Login_Form_Manager {
|
|||
}
|
||||
|
||||
// Return methods in canonical priority order: OTP first, email second, recovery last.
|
||||
// The user's preferred method is honored separately in select_preferred_method().
|
||||
$priority = array( 'otp', 'email', 'recovery' );
|
||||
$sorted = array();
|
||||
|
||||
|
|
@ -1059,7 +1060,7 @@ class Login_Form_Manager {
|
|||
}
|
||||
}
|
||||
|
||||
return $this->select_preferred_method( $methods );
|
||||
return $this->select_preferred_method( $methods, (string) $user_settings['preferred_method'] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1102,15 +1103,29 @@ class Login_Form_Manager {
|
|||
/**
|
||||
* Determine which method should be requested when a challenge is required.
|
||||
*
|
||||
* @param array<int, string> $methods Active verification methods.
|
||||
* Honors the user's preferred method (set on their profile) when it is part
|
||||
* of the available methods; otherwise falls back to the canonical order.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param array<int, string> $methods Active verification methods (stage slugs).
|
||||
* @param string $preferred_method Optional user-chosen preferred method key.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function select_preferred_method( array $methods ): string {
|
||||
private function select_preferred_method( array $methods, string $preferred_method = '' ): string {
|
||||
if ( empty( $methods ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( '' !== $preferred_method ) {
|
||||
$preferred_slug = $this->normalize_method_slug( $preferred_method );
|
||||
|
||||
if ( '' !== $preferred_slug && in_array( $preferred_slug, $methods, true ) ) {
|
||||
return $preferred_slug;
|
||||
}
|
||||
}
|
||||
|
||||
// Prefer OTP (most secure), then email, then recovery codes (last resort).
|
||||
foreach ( array( 'otp', 'email' ) as $method ) {
|
||||
if ( in_array( $method, $methods, true ) ) {
|
||||
|
|
@ -1438,6 +1453,31 @@ class Login_Form_Manager {
|
|||
return $this->config->get_frequency();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the "Remember this browser" trust duration (in days) for a user.
|
||||
*
|
||||
* The feature must be enabled by the administrator (trust_device_days > 0)
|
||||
* and the user's effective verification frequency must define a non-zero
|
||||
* interval; the duration is taken from that frequency so the checkbox always
|
||||
* complies with the user's profile setting.
|
||||
*
|
||||
* @since 1.5.3
|
||||
*
|
||||
* @param \WP_User $user User attempting to authenticate.
|
||||
*
|
||||
* @return int Number of days, or 0 when the feature is disabled or the
|
||||
* user's frequency is per-session.
|
||||
*/
|
||||
private function get_trust_days_for_user( \WP_User $user ): int {
|
||||
if ( $this->config->get_trust_device_days() <= 0 ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$user_settings = $this->user_settings_repository->get_user_settings( $user->ID );
|
||||
|
||||
return Frequency_Options::get_interval_days( $this->get_effective_frequency( $user, $user_settings ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a hashed context key representing the current login environment.
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in a new issue