action=agentlane_token t= * * Answers: * 200 {"token":"..."} fresh token bound to the caller, timestamp = now * 403 {"error":"invalid_ticket"} no ticket, malformed, not signed by this site, a * refreshed token, older than 30 days or from the future * 404 {"error":"disabled"} feature switched off (agentlane_token_refresh_after = 0) * 405 {"error":"method"} anything but POST * * POST on purpose: caches and CDNs do not store POST responses, and admin-ajax.php stays * reachable on sites that close the REST API to visitors. * * The handler is stateless: it writes no option, transient, cookie or table row, makes * no outbound request and logs nothing. The ticket check uses only the "issued" half of * the signature (Signals::verify_ticket()); the new token goes through every usual check * when the form is submitted (minimum age from its own timestamp, binding, three uses). * * @package AgentLane */ declare(strict_types=1); namespace AgentLane; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Class TokenEndpoint */ class TokenEndpoint { /** admin-ajax action name. */ const ACTION = 'agentlane_token'; /** POST field that carries the ticket. */ const FIELD_TICKET = 't'; /** Cache-Control on every answer. */ const CACHE_CONTROL = 'no-store, no-cache, must-revalidate, max-age=0, private'; /** * Hook the handler for visitors and for logged-in users. * * @return void */ public static function register(): void { add_action( 'wp_ajax_nopriv_' . self::ACTION, array( __CLASS__, 'handle' ) ); add_action( 'wp_ajax_' . self::ACTION, array( __CLASS__, 'handle' ) ); } /** * admin-ajax callback: read the request, answer, exit. * * @return void */ public static function handle(): void { $method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( sanitize_key( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) : ''; $ticket = null; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- public, stateless token exchange; the signed ticket replaces the nonce (a nonce cannot live in a cached page). if ( isset( $_POST[ self::FIELD_TICKET ] ) && is_string( $_POST[ self::FIELD_TICKET ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- see above. $ticket = sanitize_text_field( wp_unslash( $_POST[ self::FIELD_TICKET ] ) ); } $answer = self::respond( $method, $ticket ); self::send( $answer['status'], $answer['body'] ); } /** * The decision, without any output (unit-tested). * * @param string $method HTTP method, upper case. * @param string|null $ticket Value of the POST field t, null when absent. * @return array{status: int, body: array} */ public static function respond( string $method, ?string $ticket ): array { if ( Signals::refresh_after() <= 0 ) { return array( 'status' => 404, 'body' => array( 'error' => 'disabled' ), ); } if ( 'POST' !== $method ) { return array( 'status' => 405, 'body' => array( 'error' => 'method' ), ); } if ( null === $ticket || '' === $ticket ) { return self::refused(); } $check = Signals::verify_ticket( $ticket ); if ( empty( $check['ok'] ) ) { return self::refused(); } return array( 'status' => 200, 'body' => array( 'token' => Signals::create_refresh_token() ), ); } /** * One answer for every refused ticket, so the address tells nothing about why. * * @return array{status: int, body: array} */ protected static function refused(): array { return array( 'status' => 403, 'body' => array( 'error' => 'invalid_ticket' ), ); } /** * Headers that keep every cache layer away, then the JSON answer. * * @param int $status HTTP status. * @param array $body JSON body. * @return void */ protected static function send( int $status, array $body ): void { if ( ! defined( 'DONOTCACHEPAGE' ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- shared convention of page-cache plugins (WP Super Cache, W3 Total Cache, WP Rocket), not a constant of this plugin. define( 'DONOTCACHEPAGE', true ); } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's own documented hook. do_action( 'litespeed_control_set_nocache', 'agent lane token' ); nocache_headers(); if ( ! headers_sent() ) { header( 'Cache-Control: ' . self::CACHE_CONTROL ); header( 'X-Robots-Tag: noindex, nofollow' ); if ( 405 === $status ) { header( 'Allow: POST' ); } } wp_send_json( $body, $status ); } }