272 lines
6.9 KiB
PHP
272 lines
6.9 KiB
PHP
<?php
|
|
/**
|
|
* REST API controller for 2FA settings and user status.
|
|
*
|
|
* @package Robotstxt_2FA
|
|
*/
|
|
|
|
namespace Robotstxt\TwoFA\Rest;
|
|
|
|
use Robotstxt\TwoFA\Admin\Settings_Page;
|
|
use Robotstxt\TwoFA\User\OTP_Manager;
|
|
use Robotstxt\TwoFA\User\Recovery_Codes;
|
|
use Robotstxt\TwoFA\User\Two_Factor_Config;
|
|
use Robotstxt\TwoFA\User\User_Settings_Repository;
|
|
use WP_REST_Request;
|
|
use WP_REST_Response;
|
|
use WP_REST_Server;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Exposes 2FA settings and per-user status over the WordPress REST API.
|
|
*
|
|
* All endpoints require the `manage_options` capability. No secrets (hashed
|
|
* codes, OTP secrets, trust tokens) are ever exposed; only configuration and
|
|
* status metadata are returned.
|
|
*
|
|
* @since 1.6.0
|
|
*/
|
|
class Rest_Controller {
|
|
|
|
/**
|
|
* REST namespace.
|
|
*/
|
|
private const NAMESPACE = 'robotstxt-2fa/v1';
|
|
|
|
/**
|
|
* Capability required to access any endpoint.
|
|
*/
|
|
private const CAPABILITY = 'manage_options';
|
|
|
|
/**
|
|
* Global two-factor configuration.
|
|
*
|
|
* @var Two_Factor_Config
|
|
*/
|
|
private Two_Factor_Config $config;
|
|
|
|
/**
|
|
* Settings page handler (provides settings read + sanitize).
|
|
*
|
|
* @var Settings_Page
|
|
*/
|
|
private Settings_Page $settings_page;
|
|
|
|
/**
|
|
* User settings repository.
|
|
*
|
|
* @var User_Settings_Repository
|
|
*/
|
|
private User_Settings_Repository $repository;
|
|
|
|
/**
|
|
* OTP manager.
|
|
*
|
|
* @var OTP_Manager
|
|
*/
|
|
private OTP_Manager $otp_manager;
|
|
|
|
/**
|
|
* Recovery codes manager.
|
|
*
|
|
* @var Recovery_Codes
|
|
*/
|
|
private Recovery_Codes $recovery_codes;
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct() {
|
|
$this->config = new Two_Factor_Config();
|
|
$this->settings_page = new Settings_Page();
|
|
$this->repository = new User_Settings_Repository();
|
|
$this->otp_manager = new OTP_Manager();
|
|
$this->recovery_codes = new Recovery_Codes();
|
|
}
|
|
|
|
/**
|
|
* Register WordPress hooks.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register_hooks(): void {
|
|
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
|
|
}
|
|
|
|
/**
|
|
* Register the REST routes.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register_routes(): void {
|
|
register_rest_route(
|
|
self::NAMESPACE,
|
|
'/settings',
|
|
array(
|
|
array(
|
|
'methods' => WP_REST_Server::READABLE,
|
|
'callback' => array( $this, 'get_settings' ),
|
|
'permission_callback' => array( $this, 'check_permission' ),
|
|
),
|
|
array(
|
|
'methods' => WP_REST_Server::EDITABLE,
|
|
'callback' => array( $this, 'update_settings' ),
|
|
'permission_callback' => array( $this, 'check_permission' ),
|
|
),
|
|
)
|
|
);
|
|
|
|
register_rest_route(
|
|
self::NAMESPACE,
|
|
'/users',
|
|
array(
|
|
'methods' => WP_REST_Server::READABLE,
|
|
'callback' => array( $this, 'get_users' ),
|
|
'permission_callback' => array( $this, 'check_permission' ),
|
|
'args' => array(
|
|
'role' => array(
|
|
'type' => 'string',
|
|
'description' => __( 'Restrict the list to a single role slug.', 'robotstxt-2fa' ),
|
|
'sanitize_callback' => 'sanitize_key',
|
|
),
|
|
'status' => array(
|
|
'type' => 'string',
|
|
'description' => __( 'Filter by 2FA status: enabled, required, or disabled.', 'robotstxt-2fa' ),
|
|
'enum' => array( 'enabled', 'required', 'disabled' ),
|
|
'sanitize_callback' => 'sanitize_key',
|
|
),
|
|
),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Permission callback — every endpoint requires the admin capability.
|
|
*
|
|
* @return bool|\WP_Error
|
|
*/
|
|
public function check_permission() {
|
|
if ( ! current_user_can( self::CAPABILITY ) ) {
|
|
return new \WP_Error(
|
|
'robotstxt_2fa_rest_forbidden',
|
|
__( 'You are not allowed to manage 2FA settings.', 'robotstxt-2fa' ),
|
|
array( 'status' => rest_authorization_required_code() )
|
|
);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Retrieve the full 2FA settings object.
|
|
*
|
|
* @param WP_REST_Request $request Incoming request.
|
|
*
|
|
* @return WP_REST_Response
|
|
*/
|
|
public function get_settings( WP_REST_Request $request ): WP_REST_Response {
|
|
unset( $request );
|
|
|
|
return rest_ensure_response( $this->settings_page->get_settings() );
|
|
}
|
|
|
|
/**
|
|
* Update the 2FA settings from a JSON body.
|
|
*
|
|
* Reuses {@see Settings_Page::sanitize_settings()} so the REST write path
|
|
* applies the exact same validation as the admin settings form.
|
|
*
|
|
* @param WP_REST_Request $request Incoming request with a JSON settings body.
|
|
*
|
|
* @return WP_REST_Response|\WP_Error
|
|
*/
|
|
public function update_settings( WP_REST_Request $request ) {
|
|
$sanitized = $this->settings_page->sanitize_settings( $request->get_json_params() );
|
|
|
|
update_option( Settings_Page::OPTION_NAME, $sanitized, false );
|
|
|
|
if ( is_multisite() ) {
|
|
update_site_option( Settings_Page::OPTION_NAME, $sanitized );
|
|
}
|
|
|
|
return rest_ensure_response( $this->settings_page->get_settings() );
|
|
}
|
|
|
|
/**
|
|
* List users and their 2FA status, optionally filtered by role and status.
|
|
*
|
|
* @param WP_REST_Request $request Incoming request.
|
|
*
|
|
* @return WP_REST_Response
|
|
*/
|
|
public function get_users( WP_REST_Request $request ): WP_REST_Response {
|
|
$query_args = array(
|
|
'number' => -1,
|
|
'fields' => 'all',
|
|
);
|
|
|
|
$role = $request->get_param( 'role' );
|
|
|
|
if ( is_string( $role ) && '' !== $role ) {
|
|
$query_args['role'] = sanitize_key( $role );
|
|
}
|
|
|
|
$status_filter = $request->get_param( 'status' );
|
|
|
|
if ( is_string( $status_filter ) && '' === $status_filter ) {
|
|
$status_filter = '';
|
|
}
|
|
|
|
$users = get_users( $query_args );
|
|
$out = array();
|
|
|
|
foreach ( $users as $user ) {
|
|
if ( ! $user instanceof \WP_User ) {
|
|
continue;
|
|
}
|
|
|
|
$entry = $this->build_user_entry( $user );
|
|
|
|
if ( is_string( $status_filter ) && '' !== $status_filter && $entry['status'] !== $status_filter ) {
|
|
continue;
|
|
}
|
|
|
|
$out[] = $entry;
|
|
}
|
|
|
|
return rest_ensure_response( $out );
|
|
}
|
|
|
|
/**
|
|
* Build the 2FA status payload for a single user.
|
|
*
|
|
* @param \WP_User $user User to describe.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function build_user_entry( \WP_User $user ): array {
|
|
$settings = $this->repository->get_user_settings( $user->ID );
|
|
$required = $this->config->get_required_methods_for_user( $user );
|
|
$forced = ! empty( $required );
|
|
$enabled = $settings['enabled'];
|
|
|
|
return array(
|
|
'id' => $user->ID,
|
|
'login' => $user->user_login,
|
|
'email' => $user->user_email,
|
|
'roles' => $user->roles,
|
|
'enabled' => $enabled,
|
|
'forced' => $forced,
|
|
'status' => $enabled ? 'enabled' : ( $forced ? 'required' : 'disabled' ),
|
|
'methods' => $settings['methods'],
|
|
'required_methods' => $required,
|
|
'frequency' => $settings['frequency'],
|
|
'preferred_method' => $settings['preferred_method'],
|
|
'has_otp' => '' !== $this->otp_manager->get_secret( $user->ID ),
|
|
'unused_recovery_codes' => $this->recovery_codes->count_unused_codes( $user->ID ),
|
|
);
|
|
}
|
|
}
|