Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=7.3|~8.0.0|~8.1.0",
"ext-curl": "*",
"ext-json": "*",
"firebase/php-jwt": "^5.4"
"firebase/php-jwt": "^5.5.1|^6.2"
},
"require-dev": {
"phpunit/phpunit": "^9.5.14",
Expand Down
17 changes: 16 additions & 1 deletion src/MessageBird/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MessageBird;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Firebase\JWT\SignatureInvalidException;
use MessageBird\Exceptions\ValidationException;
use MessageBird\Objects\SignedRequest;
Expand All @@ -14,6 +15,7 @@
use function http_build_query;
use function implode;
use function ksort;
use function PHPUnit\Framework\throwException;
use function time;

/**
Expand Down Expand Up @@ -139,7 +141,20 @@ public function validateSignature(string $signature, string $url, string $body)

JWT::$leeway = 1;
try {
$decoded = JWT::decode($signature, $this->signingKey, self::ALLOWED_ALGOS);
$headb64 = \explode('.', $signature)[0];
$headerRaw = JWT::urlsafeB64Decode($headb64);
$header = JWT::jsonDecode($headerRaw);

$key = [];
if ($header && property_exists($header, 'alg')) {
if (!in_array(strtoupper($header->alg), self::ALLOWED_ALGOS, true)) {
throw new ValidationException('Algorithm not supported');
}

$key = new Key($this->signingKey, $header->alg);
}

$decoded = JWT::decode($signature, $key);
} catch (\InvalidArgumentException | \UnexpectedValueException | SignatureInvalidException $e) {
throw new ValidationException($e->getMessage(), $e->getCode(), $e);
}
Expand Down