class-rest-controller.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST controller for the Jetpack Reprint exporter secret-rotation endpoint. |
| 4 | * |
| 5 | * Requires a Jetpack-signed request (WordPress.com public API proxy only). |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Reprint_Export; |
| 11 | |
| 12 | use Automattic\Jetpack\Connection\Manager; |
| 13 | use WP_REST_Controller; |
| 14 | use WP_REST_Response; |
| 15 | use WP_REST_Server; |
| 16 | |
| 17 | /** |
| 18 | * Reprint exporter REST controller. |
| 19 | */ |
| 20 | class REST_Controller extends WP_REST_Controller { |
| 21 | |
| 22 | /** |
| 23 | * The API namespace. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $namespace = 'jetpack/v4'; |
| 28 | |
| 29 | /** |
| 30 | * The REST base path. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $rest_base = 'reprint'; |
| 35 | |
| 36 | /** |
| 37 | * Registers the reprint export routes. |
| 38 | */ |
| 39 | public function register_routes() { |
| 40 | register_rest_route( |
| 41 | $this->namespace, |
| 42 | '/' . $this->rest_base . '/rotate-export-secret', |
| 43 | array( |
| 44 | array( |
| 45 | 'methods' => WP_REST_Server::CREATABLE, |
| 46 | 'callback' => array( $this, 'rotate_secret' ), |
| 47 | 'permission_callback' => array( $this, 'permission_check' ), |
| 48 | ), |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | register_rest_route( |
| 53 | $this->namespace, |
| 54 | '/' . $this->rest_base . '/enable-export', |
| 55 | array( |
| 56 | array( |
| 57 | 'methods' => WP_REST_Server::CREATABLE, |
| 58 | 'callback' => array( $this, 'enable_export' ), |
| 59 | 'permission_callback' => array( $this, 'permission_check' ), |
| 60 | ), |
| 61 | ) |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Opens the 60-minute export window without rotating the secret. |
| 67 | * |
| 68 | * Purpose-built enable endpoint: a client that already holds a valid |
| 69 | * secret can re-open a lapsed window without minting a new one. The |
| 70 | * route is only registered when the feature is available, so a 404 |
| 71 | * here doubles as the client's "is Reprint export available?" probe. |
| 72 | * |
| 73 | * @return WP_REST_Response The unix timestamp the window was opened at. |
| 74 | */ |
| 75 | public function enable_export() { |
| 76 | return new WP_REST_Response( |
| 77 | array( 'enabled_at' => Reprint_Exporter::open_export_window() ), |
| 78 | 200 |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Rotates the shared secret and opens the export window. |
| 84 | * |
| 85 | * Generates a cryptographically random 64-character hex secret, stores it |
| 86 | * in a WordPress option (autoload disabled), opens the 60-minute export |
| 87 | * window, and returns the secret. The caller uses this secret to |
| 88 | * authenticate export requests via HMAC. |
| 89 | * |
| 90 | * Rotating the secret intentionally also opens the export window so the |
| 91 | * Pressable client flow is a single round trip: rotate, then immediately |
| 92 | * stream from ?reprint-api-jetpack using HMAC. |
| 93 | * |
| 94 | * @return WP_REST_Response The new secret on success, or a 500 error. |
| 95 | */ |
| 96 | public function rotate_secret() { |
| 97 | $secret = bin2hex( random_bytes( 32 ) ); |
| 98 | |
| 99 | if ( ! update_option( Reprint_Exporter::SECRET_OPTION, $secret, false ) ) { |
| 100 | return new WP_REST_Response( |
| 101 | array( 'error' => 'Failed to persist the new secret.' ), |
| 102 | 500 |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | // Open the sliding export window so the client can stream right away. |
| 107 | Reprint_Exporter::open_export_window(); |
| 108 | |
| 109 | return new WP_REST_Response( array( 'secret' => $secret ), 200 ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Permission callback: only Jetpack-signed requests (public API proxy). |
| 114 | * |
| 115 | * @return bool |
| 116 | */ |
| 117 | public function permission_check() { |
| 118 | return method_exists( Manager::class, 'verify_xml_rpc_signature' ) |
| 119 | && ( new Manager() )->verify_xml_rpc_signature(); |
| 120 | } |
| 121 | } |
| 122 |