": an HMAC-SHA256 of "{t}.{raw body}" keyed with the * account's whsec_ signing secret. Signatures older than the tolerance, or from the future, are rejected. */ class AgentaOS_Signature { const TOLERANCE_SECONDS = 300; /** * @param string $raw_body Exact request body as received. * @param string $header Value of the X-AgentaOS-Signature header. * @param string $secret Webhook signing secret (whsec_...). * @param int|null $now Current Unix time, injectable for tests. * @return bool True when the signature is valid and fresh. */ public static function is_valid( $raw_body, $header, $secret, $now = null ) { if ( '' === (string) $secret || '' === (string) $header ) { return false; } $parts = array(); foreach ( explode( ',', $header ) as $piece ) { $pair = explode( '=', trim( $piece ), 2 ); if ( 2 === count( $pair ) ) { $parts[ $pair[0] ] = $pair[1]; } } if ( ! isset( $parts['t'], $parts['v1'] ) || ! ctype_digit( $parts['t'] ) ) { return false; } $timestamp = (int) $parts['t']; $age = ( null === $now ? time() : $now ) - $timestamp; // Allow the store's clock to run up to 10 seconds behind AgentaOS: shared hosts drift. if ( $age < -10 || $age > self::TOLERANCE_SECONDS ) { return false; } $expected = hash_hmac( 'sha256', $timestamp . '.' . $raw_body, $secret ); return hash_equals( $expected, $parts['v1'] ); } }