105 lines
2.8 KiB
PHP
105 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Smilia\Core;
|
|
|
|
use AltchaOrg\Altcha\Algorithm\Pbkdf2;
|
|
use AltchaOrg\Altcha\Altcha;
|
|
use AltchaOrg\Altcha\CreateChallengeOptions;
|
|
use AltchaOrg\Altcha\Payload;
|
|
use AltchaOrg\Altcha\VerifySolutionOptions;
|
|
use WP_REST_Response;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class Captcha {
|
|
|
|
public const ROUTE = '/booking-challenge';
|
|
|
|
private const COST = 2000;
|
|
private const COUNTER_MIN = 1000;
|
|
private const COUNTER_MAX = 2000;
|
|
private const TTL = 1200;
|
|
private const MAX_PAYLOAD = 4000;
|
|
private const USED_PREFIX = 'smilia_used_';
|
|
private const USED_KEEP = HOUR_IN_SECONDS;
|
|
|
|
public function register(): void {
|
|
add_action( 'rest_api_init', array( $this, 'register_route' ) );
|
|
}
|
|
|
|
public function register_route(): void {
|
|
register_rest_route(
|
|
Booking::ROUTE_NAMESPACE,
|
|
self::ROUTE,
|
|
array(
|
|
'methods' => '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' ) );
|
|
}
|
|
}
|