robotstxt-2fa/includes/user/class-two-factor-config.php
2026-08-07 14:15:58 +00:00

303 lines
7.7 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}, app_passwords_require_2fa_creation: bool, geoip_database_path: string, geoip_country_allow: string, geoip_country_deny: string, geoip_always_challenge: string, geoip_auto_download: bool}
*/
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'];
}
/**
* Determine whether a recent 2FA verification is required before creating an Application Password.
*
* @since 1.5.0
*
* @return bool
*/
public function is_app_passwords_require_2fa_creation(): bool {
return $this->get_settings()['app_passwords_require_2fa_creation'];
}
/**
* Retrieve the GeoIP database file path.
*
* When auto-download is enabled, the managed uploads path is returned and
* the manual path setting is ignored.
*
* @since 1.5.0
*
* @return string
*/
public function get_geoip_database_path(): string {
$settings = $this->get_settings();
if ( ! empty( $settings['geoip_auto_download'] ) ) {
return \Robotstxt\TwoFA\GeoIP_Updater::get_database_path();
}
return $settings['geoip_database_path'];
}
/**
* Determine whether the auto-download GeoIP database is enabled.
*
* @since 1.5.3
*
* @return bool
*/
public function get_geoip_auto_download(): bool {
return ! empty( $this->get_settings()['geoip_auto_download'] );
}
/**
* Retrieve the GeoIP country allow list string (newline-separated ISO codes).
*
* @since 1.5.0
*
* @return string
*/
public function get_geoip_country_allow(): string {
return $this->get_settings()['geoip_country_allow'];
}
/**
* Retrieve the GeoIP country deny list string (newline-separated ISO codes).
*
* @since 1.5.0
*
* @return string
*/
public function get_geoip_country_deny(): string {
return $this->get_settings()['geoip_country_deny'];
}
/**
* Retrieve the GeoIP always-challenge country list string (newline-separated ISO codes).
*
* @since 1.5.0
*
* @return string
*/
public function get_geoip_always_challenge(): string {
return $this->get_settings()['geoip_always_challenge'];
}
}