Skip to content

Commit a0cfaf2

Browse files
committed
Fix Webhook Signature Validation
1 parent f600cc5 commit a0cfaf2

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

src/Api/Webhook.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Chargily\ChargilyPay\Core\Abstracts\ApiClassesAbstract;
66
use Chargily\ChargilyPay\Core\Helpers\Carbon;
7+
use Chargily\ChargilyPay\Core\Helpers\HttpRequest;
78
use Chargily\ChargilyPay\Core\Helpers\Str;
89
use Chargily\ChargilyPay\Core\Interfaces\ApiClassesInterface;
910
use Chargily\ChargilyPay\Core\Traits\GuzzleHttpTrait;
@@ -19,14 +20,12 @@ final class Webhook extends ApiClassesAbstract implements ApiClassesInterface
1920
*/
2021
public function get(): ?WebhookElement
2122
{
22-
$headers = getallheaders();
23-
$signature = isset($headers['signature']) ? $headers['signature'] : "";
24-
$signature = (empty($signature) and isset($headers['Signature'])) ? $headers['Signature'] : "";
23+
$signature = HttpRequest::header("Signature") ?? "";
24+
$payload = HttpRequest::body() ?? "";
2525

26-
$payload = file_get_contents('php://input');
27-
$computed = hash_hmac('sha256', $payload, $this->credentials->secret);
28-
if (hash_equals($signature, $computed)) {
26+
$computed_signature = hash_hmac('sha256', $payload, $this->credentials->secret);
2927

28+
if (hash_equals($signature, $computed_signature)) {
3029
$event = json_decode($payload, true);
3130
return $this->newElement($event);
3231
}

src/Core/Abstracts/ElementsAbstract.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,22 @@ public function methods()
7171
{
7272
return $this->methods;
7373
}
74+
/**
75+
* Attributes To arrray
76+
*
77+
* @return string|null
78+
*/
79+
public function toArray()
80+
{
81+
return $this->all();
82+
}
83+
/**
84+
* Attributes To json
85+
*
86+
* @return string|null
87+
*/
88+
public function toJson(): ?string
89+
{
90+
return json_encode($this->toArray()) ?? null;
91+
}
7492
}

src/Core/Helpers/HttpRequest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Chargily\ChargilyPay\Core\Helpers;
4+
5+
class HttpRequest
6+
{
7+
/**
8+
* Cache headers
9+
*
10+
* @var array|null
11+
*/
12+
protected static ?array $headers = null;
13+
14+
/**
15+
* Get current request headers
16+
*
17+
* @return array
18+
*/
19+
public static function headers(): array
20+
{
21+
if (static::$headers) {
22+
return static::$headers;
23+
}
24+
$server_headers = [];
25+
foreach ($_SERVER as $key => $value) {
26+
if (Str::startsWith($key, "HTTP_")) {
27+
$header_name = Str::lower(Str::replace(['HTTP_', "_"], ['', '-'], $key));
28+
$header_name = ucwords($header_name, '-');
29+
30+
$server_headers[$header_name] = $value;
31+
}
32+
}
33+
34+
return static::$headers = $server_headers;
35+
}
36+
/**
37+
* get header
38+
*
39+
* @param string $name
40+
* @return string|null
41+
*/
42+
public static function header(string $name): ?string
43+
{
44+
$headers = self::headers();
45+
46+
return $headers[$name] ?? null;
47+
}
48+
/**
49+
* Get request data
50+
*
51+
* @return array|null
52+
*/
53+
public static function data(): array
54+
{
55+
if (!empty($_GET)) {
56+
return $_GET;
57+
}
58+
if (!empty($_POST)) {
59+
return $_POST;
60+
}
61+
return [];
62+
}
63+
/**
64+
* Body
65+
*
66+
* @return string|null
67+
*/
68+
public static function body(): ?string
69+
{
70+
$body = file_get_contents('php://input');
71+
return (!empty($body)) ? $body : null;
72+
}
73+
}

0 commit comments

Comments
 (0)