Browse code

Added README.md appinfo/info.xml appinfo/signature.json lib/Controller/AuthorApiController.php and the providers directory

DoubleBastionAdmin authored on 20/08/2022 16:33:00
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,75 @@
1
+<?php
2
+
3
+namespace Telnyx;
4
+
5
+abstract class Webhook
6
+{
7
+    const DEFAULT_TOLERANCE = 300;
8
+
9
+    /**
10
+     * Alias for constructEvent().
11
+     * Returns an Event instance using the current HTTPS request. Throws an
12
+     * Exception\UnexpectedValueException if the payload is not valid JSON, and
13
+     * an Exception\SignatureVerificationException if the signature
14
+     * verification fails for any reason.
15
+     *
16
+     * @param string $public_key secret used to generate the signature
17
+     * @param int $tolerance maximum difference allowed between the header's
18
+     *  timestamp and the current time
19
+     *
20
+     * @throws Exception\UnexpectedValueException if the payload is not valid JSON,
21
+     * @throws Exception\SignatureVerificationException if the verification fails
22
+     *
23
+     * @return Event the Event instance
24
+     */
25
+    public static function constructFromRequest($public_key = '', $tolerance = self::DEFAULT_TOLERANCE)
26
+    {
27
+        if (!isset($_SERVER['HTTP_TELNYX_SIGNATURE_ED25519']) || !isset($_SERVER['HTTP_TELNYX_TIMESTAMP'])) {
28
+            throw Exception\SignatureVerificationException::factory(
29
+                'Unable to extract timestamp and signatures from header'
30
+            );
31
+        }
32
+
33
+        $payload = @file_get_contents('php://input');
34
+        $sig_header = $_SERVER['HTTP_TELNYX_SIGNATURE_ED25519'];
35
+        $timestamp_header = $_SERVER['HTTP_TELNYX_TIMESTAMP'];
36
+
37
+        return \Telnyx\Webhook::constructEvent(
38
+            $payload, $sig_header, $timestamp_header, $public_key, $tolerance
39
+        );
40
+    }
41
+
42
+    /**
43
+     * Returns an Event instance using the provided JSON payload. Throws an
44
+     * Exception\UnexpectedValueException if the payload is not valid JSON, and
45
+     * an Exception\SignatureVerificationException if the signature
46
+     * verification fails for any reason.
47
+     *
48
+     * @param string $payload the payload sent by Telnyx
49
+     * @param string $signature_header the contents of the signature header sent by Telnyx
50
+     * @param string $timestamp the timestamp from the header sent by Telnyx
51
+     * @param string $public_key secret used to generate the signature
52
+     * @param int $tolerance maximum difference allowed between the header's
53
+     *  timestamp and the current time
54
+     *
55
+     * @throws Exception\UnexpectedValueException if the payload is not valid JSON,
56
+     * @throws Exception\SignatureVerificationException if the verification fails
57
+     *
58
+     * @return Event the Event instance
59
+     */
60
+    public static function constructEvent($payload, $signature_header, $timestamp, $public_key = '', $tolerance = self::DEFAULT_TOLERANCE)
61
+    {
62
+        WebhookSignature::verifyHeader($payload, $signature_header, $timestamp, $public_key, $tolerance);
63
+
64
+        $data = \json_decode($payload, true);
65
+        $jsonError = \json_last_error();
66
+        if (null === $data && \JSON_ERROR_NONE !== $jsonError) {
67
+            $msg = "Invalid payload: {$payload} "
68
+              . "(json_last_error() was {$jsonError})";
69
+
70
+            throw new Exception\UnexpectedValueException($msg);
71
+        }
72
+
73
+        return Event::constructFrom($data);
74
+    }
75
+}