'GET', 'callback' => array( $this, 'handle' ), 'permission_callback' => '__return_true', ) ); } public function handle(): WP_REST_Response { $response = new WP_REST_Response( self::challenge(), 200 ); $response->header( 'Cache-Control', 'no-store' ); return $response; } public static function endpoint(): string { return rest_url( Booking::ROUTE_NAMESPACE . self::ROUTE ); } public static function script(): string { $path = 'build/blocks/altcha/altcha.min.js'; return plugins_url( $path, SMILIA_CORE_FILE ) . '?ver=' . (int) filemtime( SMILIA_CORE_DIR . '/' . $path ); } public static function challenge(): array { return ( new Altcha( hmacSignatureSecret: self::secret() ) )->createChallenge( new CreateChallengeOptions( algorithm: new Pbkdf2(), cost: self::COST, counter: random_int( self::COUNTER_MIN, self::COUNTER_MAX ), expiresAt: time() + self::TTL, ) )->toArray(); } public static function verify( string $raw ): bool { if ( '' === $raw || strlen( $raw ) > self::MAX_PAYLOAD ) { return false; } try { $payload = Payload::fromBase64( $raw ); $result = ( new Altcha( hmacSignatureSecret: self::secret() ) )->verifySolution( new VerifySolutionOptions( algorithm: new Pbkdf2(), payload: $payload, ) ); } catch ( \Throwable $error ) { return false; } if ( ! $result->verified || null === $payload->challenge->signature ) { return false; } return add_option( self::USED_PREFIX . hash( 'sha256', $payload->challenge->signature ), (string) time(), '', false ); } public static function cleanup(): void { global $wpdb; $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s AND CAST(option_value AS UNSIGNED) < %d", $wpdb->esc_like( self::USED_PREFIX ) . '%', time() - self::USED_KEEP ) ); wp_cache_delete( 'alloptions', 'options' ); } private static function secret(): string { return hash_hmac( 'sha256', 'smilia-captcha', wp_salt( 'auth' ) ); } }