getLPWhitelabelInfo(); self::$whitelabel = is_array($values) ? $values : array(); } return (isset(self::$whitelabel[$key]) && is_string(self::$whitelabel[$key])) ? self::$whitelabel[$key] : $default; } # Sent as a JSON failure rather than returned as a WP_Error. These are system # states, not wrong credentials, and a WP_Error would be recorded as a failed # login against the firewall's lockout counter. public static function sendFailure($message, $data = array()) { wp_send_json_error(array_merge($data, array('message' => $message))); exit; } public static function humanWait($seconds) { if ($seconds < 90) { return sprintf('%d seconds', max(1, intval($seconds))); } return sprintf('%d minutes', intval(ceil($seconds / MINUTE_IN_SECONDS))); } public static function isEnabled($settings) { $config = $settings->getOption(self::$wp_2fa_option); return (is_array($config) && array_key_exists('enabled', $config) && $config['enabled'] === true); } public function init() { add_filter('authenticate', array($this, 'authenticate'), 25, 3); add_action('login_form', array($this, 'custom_login_form')); add_action('login_enqueue_scripts', array($this, 'enqueue_login_assets')); } public function enqueue_login_assets() { wp_enqueue_style('WPR-wp-2fa-login', plugin_dir_url(__FILE__) . 'css/login.css', array(), '1.4'); wp_enqueue_script('WPR-wp-2fa-login', plugin_dir_url(__FILE__) . 'js/login.js', array(), '1.4', true); } public function authenticate($user, $username, $password) { if (!($user instanceof WP_User)) { return $user; } if ('1' !== get_user_meta($user->ID, self::FLAG_META_KEY, true)) { return $user; } if (!self::isInteractiveLogin()) { return new WP_Error('twofa_context', self::CONTEXT_MESSAGE); } $method = get_user_meta($user->ID, self::METHOD_META_KEY, true); if ($method === 'email_otp') { return WPRWP2FAEmailOTPLogin::authenticate($user); } if ($method !== '' && $method !== 'totp') { self::sendFailure(self::CONFIG_MESSAGE); } return WPRWP2FATimeOTPLogin::authenticate($user); } private static function isInteractiveLogin() { global $pagenow; if ((defined('XMLRPC_REQUEST') && XMLRPC_REQUEST) || (defined('REST_REQUEST') && REST_REQUEST) || (defined('WP_CLI') && WP_CLI)) { return false; } return $pagenow === 'wp-login.php'; } function custom_login_form() { $tooltip_message = self::whitelabelMessage('2fa_tooltip', self::TOOLTIP_MESSAGE); $is_url = filter_var($tooltip_message, FILTER_VALIDATE_URL); $allowed_tooltip_html = array( 'a' => array( 'class' => true, 'href' => true, 'rel' => true, 'target' => true ), 'span' => array( 'class' => true, 'id' => true, 'title' => true ) ); $icon_html = ''; if ($is_url) { $tooltip_html = '' . $icon_html . ''; } else { $tooltip_html = ''; } ?>