data['id']); } public function testInvalidJson() { $this->expectException(\Telnyx\Exception\UnexpectedValueException::class); $payload = 'this is not valid JSON'; $keypair = self::generateKeypairSignature(0, $payload); $event = Webhook::constructEvent($payload, base64_encode($keypair['signature']), 0, base64_encode($keypair['publicKey']), 0); } public function testValidJsonAndInvalidHeader() { $this->expectException(\Telnyx\Exception\SignatureVerificationException::class); $sigHeader = 'bad_header'; Webhook::constructEvent(self::EVENT_PAYLOAD, $sigHeader, self::SECRET); } public function testMalformedHeader() { $this->expectException(\Telnyx\Exception\SignatureVerificationException::class); $this->expectExceptionMessage('Unable to extract timestamp and signatures from header'); Webhook::constructFromRequest(); } public function testTimestampTooOld() { $this->expectException(\Telnyx\Exception\SignatureVerificationException::class); $this->expectExceptionMessage('Timestamp outside the tolerance zone'); $timestamp = \time() - 15; $keypair = self::generateKeypairSignature($timestamp, self::EVENT_PAYLOAD); WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, base64_encode($keypair['signature']), $timestamp, base64_encode($keypair['publicKey']), 10); } public function testTimestampTooRecent() { $this->expectException(\Telnyx\Exception\SignatureVerificationException::class); $this->expectExceptionMessage('Timestamp outside the tolerance zone'); $timestamp = \time() + 15; $keypair = self::generateKeypairSignature($timestamp, self::EVENT_PAYLOAD); WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, base64_encode($keypair['signature']), $timestamp, base64_encode($keypair['publicKey']), 10); } public function testValidHeaderAndSignature() { $keypair = self::generateKeypairSignature(); static::assertTrue(WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, base64_encode($keypair['signature']), 0, base64_encode($keypair['publicKey']), 0)); } public function testTimestampOffButNoTolerance() { $timestamp = 12345; $keypair = self::generateKeypairSignature($timestamp, self::EVENT_PAYLOAD); static::assertTrue(WebhookSignature::verifyHeader(self::EVENT_PAYLOAD, base64_encode($keypair['signature']), $timestamp, base64_encode($keypair['publicKey']), 0)); } }