'GET', 'callback' => array( self::class, 'respond' ), // Public by design: our backend calls it unauthenticated, and it // only confirms a challenge the caller already holds. 'permission_callback' => '__return_true', ) ); add_filter( 'rest_pre_serve_request', array( self::class, 'suppress_empty_body' ), 10, 3 ); } /** * Route callback. * * @param WP_REST_Request $request Request. */ public static function respond( $request ): WP_REST_Response { $challenge = $request->get_param( 'challenge' ); $nonce = $request->get_param( 'nonce' ); if ( ! AgentMySite_Pkce::is_token( $challenge ) || ! is_string( $nonce ) || 1 !== preg_match( self::NONCE_PATTERN, $nonce ) || ! AgentMySite_Verifier_Store::confirms_challenge( $challenge ) ) { return new WP_REST_Response( null, 404, self::headers() ); } return new WP_REST_Response( array( 'ok' => true, 'nonce' => $nonce, ), 200, self::headers() ); } /** * `rest_pre_serve_request` callback: send this route's 404 with no body. * * @param bool $served Whether the request was already served. * @param WP_REST_Response $result Result. * @param WP_REST_Request $request Request. */ public static function suppress_empty_body( $served, $result, $request ): bool { if ( $served ) { return true; } return '/' . self::REST_NAMESPACE . self::ROUTE === $request->get_route() && 404 === $result->get_status(); } /** * Response headers. * * @return array */ private static function headers(): array { return array( 'Cache-Control' => 'no-store' ); } }