401 ) ); self::$permission_cache[ $request ] = $result; return $result; } /** * Check the signature of a REST request. * * @param WP_REST_Request $request Request. * @return array { * @type bool $ok True when the request is authentic. * @type string $reason Failure reason for the log. * @type string $event_id X-Aha-Event-Id. * @type bool $connect True when a Connect order key signed the request. * } */ public static function verify_request( $request ) { $key_id = (string) $request->get_header( 'x-aha-key-id' ); $timestamp = (string) $request->get_header( 'x-aha-timestamp' ); $source = (string) $request->get_header( 'x-aha-source' ); $signature = (string) $request->get_header( 'x-aha-signature' ); $event_id = (string) $request->get_header( 'x-aha-event-id' ); $secret = Aha_Is_Woocommerce_Settings::resolve_secret( $key_id ); $verdict = Aha_Is_Woocommerce_Hmac::verify( array( 'secret' => $secret, 'timestamp' => $timestamp, 'source' => $source, 'signature' => $signature, 'raw_body' => (string) $request->get_body(), ) ); $verdict['event_id'] = $event_id; $verdict['connect'] = ! empty( $verdict['ok'] ) && Aha_Is_Woocommerce_Settings::is_connect_key( $key_id ); if ( ! $verdict['ok'] ) { // The key id is safe to log. The secret and the signature are not. Aha_Is_Woocommerce_Logger::warning( 'inbound_request_rejected', array( 'reason' => $verdict['reason'], 'key_id' => $key_id, 'route' => $request->get_route(), ) ); } return $verdict; } /** * Whether an event id was already handled. * * Order create does not need this: the UNIQUE key on aha_order_id gives it * permanent replay protection. This is for the other endpoints. * * @param string $event_id Event id. * @return bool */ public static function is_replay( $event_id ) { if ( '' === $event_id ) { return false; } return (bool) get_transient( self::replay_key( $event_id ) ); } /** * Remember an event id for the replay window. * * @param string $event_id Event id. * @return void */ public static function remember_event( $event_id ) { if ( '' === $event_id ) { return; } set_transient( self::replay_key( $event_id ), 1, self::REPLAY_WINDOW_SECONDS ); } /** * Transient key for an event id. * * @param string $event_id Event id. * @return string */ private static function replay_key( $event_id ) { return 'aha_is_woocommerce_event_' . md5( $event_id ); } }