228 lines
5.8 KiB
PHP
228 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* Resolve two-factor configuration from global settings.
|
|
*
|
|
* @package Robotstxt_2FA
|
|
*/
|
|
|
|
namespace Robotstxt\TwoFA\User;
|
|
|
|
use Robotstxt\TwoFA\Admin\Settings_Page;
|
|
use Robotstxt\TwoFA\Frequency_Options;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Exposes helpers to read plugin-level two-factor configuration.
|
|
*/
|
|
class Two_Factor_Config {
|
|
/**
|
|
* Retrieve global two-factor settings with multisite support.
|
|
*
|
|
* @return array{role_methods: array<string, array<int, string>>, frequency: string, force_frequency: bool, delete_on_uninstall: bool, max_attempts: int, lockout_duration: int, trust_device_days: int, grace_period_days: int, grace_period_action: string, app_passwords_exempt: bool, ip_allow: string, ip_deny: string, notifications: array{on_enable: bool, on_new_ip: bool, on_recovery_used: bool, on_recovery_used_admin: bool, digest_enabled: bool, digest_frequency: string}}
|
|
*/
|
|
public function get_settings(): array {
|
|
$settings_page = new Settings_Page();
|
|
return $settings_page->get_settings();
|
|
}
|
|
|
|
/**
|
|
* Retrieve the per-role method map.
|
|
*
|
|
* @return array<string, array<int, string>>
|
|
*/
|
|
public function get_role_methods(): array {
|
|
return $this->get_settings()['role_methods'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the list of roles that have at least one required method configured.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function get_forced_roles(): array {
|
|
return array_keys( $this->get_role_methods() );
|
|
}
|
|
|
|
/**
|
|
* Retrieve the required verification methods for a specific role.
|
|
*
|
|
* @param string $role Role slug.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function get_methods_for_role( string $role ): array {
|
|
$role_methods = $this->get_role_methods();
|
|
return $role_methods[ $role ] ?? array();
|
|
}
|
|
|
|
/**
|
|
* Retrieve the union of required methods across all of a user's roles.
|
|
*
|
|
* Returns an empty array when none of the user's roles have required methods.
|
|
*
|
|
* @param \WP_User $user User object currently being authenticated.
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public function get_required_methods_for_user( \WP_User $user ): array {
|
|
$role_methods = $this->get_role_methods();
|
|
$required = array();
|
|
|
|
foreach ( $user->roles as $role ) {
|
|
if ( isset( $role_methods[ $role ] ) ) {
|
|
foreach ( $role_methods[ $role ] as $method ) {
|
|
$required[] = $method;
|
|
}
|
|
}
|
|
}
|
|
|
|
$required = array_values( array_unique( $required ) );
|
|
|
|
/**
|
|
* Filter the required 2FA methods for a specific user.
|
|
*
|
|
* Return a modified array of method slugs to override or extend the
|
|
* role-based configuration for this user.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @param array<int, string> $required Method slugs required by the user's roles.
|
|
* @param \WP_User $user User being authenticated.
|
|
*/
|
|
// @phpstan-var array<int, string> $filtered — apply_filters returns mixed; sanitized below.
|
|
$filtered = apply_filters( 'robotstxt_2fa_required_methods_for_user', $required, $user );
|
|
|
|
return array_values( array_filter( $filtered ) );
|
|
}
|
|
|
|
/**
|
|
* Determine if two-factor authentication is enforced for the provided user.
|
|
*
|
|
* @param \WP_User $user User object currently being authenticated.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function is_two_factor_forced_for_user( \WP_User $user ): bool {
|
|
return ! empty( $this->get_required_methods_for_user( $user ) );
|
|
}
|
|
|
|
/**
|
|
* Retrieve the globally configured verification frequency.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_frequency(): string {
|
|
return $this->get_settings()['frequency'];
|
|
}
|
|
|
|
/**
|
|
* Determine whether the global frequency must be enforced for all users.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function is_frequency_forced(): bool {
|
|
return $this->get_settings()['force_frequency'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the maximum failed 2FA attempts before lockout (0 = unlimited).
|
|
*
|
|
* @since 1.2.0
|
|
*
|
|
* @return int
|
|
*/
|
|
public function get_max_attempts(): int {
|
|
return $this->get_settings()['max_attempts'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the lockout duration in minutes.
|
|
*
|
|
* @since 1.2.0
|
|
*
|
|
* @return int
|
|
*/
|
|
public function get_lockout_duration(): int {
|
|
return $this->get_settings()['lockout_duration'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve how many days a trusted browser cookie is valid (0 = disabled).
|
|
*
|
|
* @since 1.2.0
|
|
*
|
|
* @return int
|
|
*/
|
|
public function get_trust_device_days(): int {
|
|
return $this->get_settings()['trust_device_days'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the grace period in days for users who have not yet configured 2FA (0 = disabled).
|
|
*
|
|
* @since 1.2.0
|
|
*
|
|
* @return int
|
|
*/
|
|
public function get_grace_period_days(): int {
|
|
return $this->get_settings()['grace_period_days'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the action taken after the grace period expires: 'block' or 'wizard'.
|
|
*
|
|
* @since 1.2.0
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_grace_period_action(): string {
|
|
return $this->get_settings()['grace_period_action'];
|
|
}
|
|
|
|
/**
|
|
* Determine whether Application Password requests are exempt from the 2FA challenge.
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function is_app_passwords_exempt(): bool {
|
|
return $this->get_settings()['app_passwords_exempt'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the IP allow list string (newline-separated IPs/CIDRs).
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_ip_allow(): string {
|
|
return $this->get_settings()['ip_allow'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the IP deny list string (newline-separated IPs/CIDRs).
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_ip_deny(): string {
|
|
return $this->get_settings()['ip_deny'];
|
|
}
|
|
|
|
/**
|
|
* Retrieve the email notifications configuration.
|
|
*
|
|
* @since 1.3.0
|
|
*
|
|
* @return array{on_enable: bool, on_new_ip: bool, on_recovery_used: bool, on_recovery_used_admin: bool, digest_enabled: bool, digest_frequency: string}
|
|
*/
|
|
public function get_notifications(): array {
|
|
return $this->get_settings()['notifications'];
|
|
}
|
|
}
|