From 6b91ba4afeb04b6572c6ea068763aab82dcf0268 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 10:24:33 +0500 Subject: [PATCH 01/14] feat: add hmac signature verifier --- composer.json | 18 +- composer.lock | 1065 ++++++++++------- .../HmacSignatureVerifier.php | 98 ++ .../SignatureVerifierInterface.php | 18 + src/Types/Sdk/VerificationFailure.php | 25 + .../HmacSignatureVerifierTest.php | 414 +++++++ 6 files changed, 1226 insertions(+), 412 deletions(-) create mode 100644 src/SignatureVerifier/HmacSignatureVerifier.php create mode 100644 src/SignatureVerifier/SignatureVerifierInterface.php create mode 100644 src/Types/Sdk/VerificationFailure.php create mode 100644 tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php diff --git a/composer.json b/composer.json index 96ec814..1444884 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "psr/log": "^1.1.4 || ^2.0.0 || ^3.0.0", "apimatic/core-interfaces": "~0.1.5", "apimatic/jsonmapper": "^3.1.1", - "php-jsonpointer/php-jsonpointer": "^3.0.2" + "php-jsonpointer/php-jsonpointer": "^3.0.2", + "symfony/http-foundation": "^6.0" }, "require-dev": { "squizlabs/php_codesniffer": "^3.5", @@ -27,10 +28,14 @@ "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5" }, "autoload": { - "psr-4": { "Core\\": "src/" } + "psr-4": { + "Core\\": "src/" + } }, "autoload-dev": { - "psr-4": { "Core\\Tests\\": "tests/" } + "psr-4": { + "Core\\Tests\\": "tests/" + } }, "scripts": { "test": "phpunit --coverage-text", @@ -41,6 +46,9 @@ "lint-fix-test": "phpcbf --standard=phpcs-ruleset.xml tests/", "lint-src": "phpcs --standard=phpcs-ruleset.xml src/", "lint-test": "phpcs --standard=phpcs-ruleset.xml tests/", - "lint": ["@lint-src", "@lint-test"] + "lint": [ + "@lint-src", + "@lint-test" + ] } -} +} \ No newline at end of file diff --git a/composer.lock b/composer.lock index ca0a4a1..02fb3c5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d774ca521a28aa59304826a76f1a1b2e", + "content-hash": "b151dc7f5955b90ce7e5f1dc5da926c5", "packages": [ { "name": "apimatic/core-interfaces", @@ -51,16 +51,16 @@ }, { "name": "apimatic/jsonmapper", - "version": "3.1.3", + "version": "3.1.6", "source": { "type": "git", "url": "https://github.com/apimatic/jsonmapper.git", - "reference": "5fe6ee7ed1857d6fed669dde935c6c6c70b637d2" + "reference": "c6cc21bd56bfe5d5822bbd08f514be465c0b24e7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/apimatic/jsonmapper/zipball/5fe6ee7ed1857d6fed669dde935c6c6c70b637d2", - "reference": "5fe6ee7ed1857d6fed669dde935c6c6c70b637d2", + "url": "https://api.github.com/repos/apimatic/jsonmapper/zipball/c6cc21bd56bfe5d5822bbd08f514be465c0b24e7", + "reference": "c6cc21bd56bfe5d5822bbd08f514be465c0b24e7", "shasum": "" }, "require": { @@ -99,9 +99,9 @@ "support": { "email": "mehdi.jaffery@apimatic.io", "issues": "https://github.com/apimatic/jsonmapper/issues", - "source": "https://github.com/apimatic/jsonmapper/tree/3.1.3" + "source": "https://github.com/apimatic/jsonmapper/tree/3.1.6" }, - "time": "2024-03-15T06:02:44+00:00" + "time": "2024-11-28T09:15:32+00:00" }, { "name": "php-jsonpointer/php-jsonpointer", @@ -161,16 +161,16 @@ }, { "name": "psr/log", - "version": "3.0.0", + "version": "3.0.2", "source": { "type": "git", "url": "https://github.com/php-fig/log.git", - "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", - "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", "shasum": "" }, "require": { @@ -205,36 +205,357 @@ "psr-3" ], "support": { - "source": "https://github.com/php-fig/log/tree/3.0.0" + "source": "https://github.com/php-fig/log/tree/3.0.2" + }, + "time": "2024-09-11T13:17:53+00:00" + }, + { + "name": "symfony/deprecation-contracts", + "version": "v3.6.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62" }, - "time": "2021-07-14T16:46:02+00:00" + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/63afe740e99a13ba87ec199bb07bbdee937a5b62", + "reference": "63afe740e99a13ba87ec199bb07bbdee937a5b62", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/contracts", + "name": "symfony/contracts" + }, + "branch-alias": { + "dev-main": "3.6-dev" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.6.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-25T14:21:43+00:00" + }, + { + "name": "symfony/http-foundation", + "version": "v6.4.26", + "source": { + "type": "git", + "url": "https://github.com/symfony/http-foundation.git", + "reference": "369241591d92bb5dfb4c6ccd6ee94378a45b1521" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/369241591d92bb5dfb4c6ccd6ee94378a45b1521", + "reference": "369241591d92bb5dfb4c6ccd6ee94378a45b1521", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/deprecation-contracts": "^2.5|^3", + "symfony/polyfill-mbstring": "~1.1", + "symfony/polyfill-php83": "^1.27" + }, + "conflict": { + "symfony/cache": "<6.4.12|>=7.0,<7.1.5" + }, + "require-dev": { + "doctrine/dbal": "^2.13.1|^3|^4", + "predis/predis": "^1.1|^2.0", + "symfony/cache": "^6.4.12|^7.1.5", + "symfony/dependency-injection": "^5.4|^6.0|^7.0", + "symfony/expression-language": "^5.4|^6.0|^7.0", + "symfony/http-kernel": "^5.4.12|^6.0.12|^6.1.4|^7.0", + "symfony/mime": "^5.4|^6.0|^7.0", + "symfony/rate-limiter": "^5.4|^6.0|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\HttpFoundation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Defines an object-oriented layer for the HTTP specification", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/http-foundation/tree/v6.4.26" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2025-09-16T08:22:30+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.33.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493", + "reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493", + "shasum": "" + }, + "require": { + "ext-iconv": "*", + "php": ">=7.2" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.33.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-12-23T08:48:59+00:00" + }, + { + "name": "symfony/polyfill-php83", + "version": "v1.33.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php83.git", + "reference": "17f6f9a6b1735c0f163024d959f700cfbc5155e5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/17f6f9a6b1735c0f163024d959f700cfbc5155e5", + "reference": "17f6f9a6b1735c0f163024d959f700cfbc5155e5", + "shasum": "" + }, + "require": { + "php": ">=7.2" + }, + "type": "library", + "extra": { + "thanks": { + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Php83\\": "" + }, + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.3+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-php83/tree/v1.33.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2025-07-08T02:45:35+00:00" } ], "packages-dev": [ { "name": "composer/pcre", - "version": "3.1.3", + "version": "3.3.2", "source": { "type": "git", "url": "https://github.com/composer/pcre.git", - "reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8" + "reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/pcre/zipball/5b16e25a5355f1f3afdfc2f954a0a80aec4826a8", - "reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8", + "url": "https://api.github.com/repos/composer/pcre/zipball/b2bed4734f0cc156ee1fe9c0da2550420d99a21e", + "reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e", "shasum": "" }, "require": { "php": "^7.4 || ^8.0" }, + "conflict": { + "phpstan/phpstan": "<1.11.10" + }, "require-dev": { - "phpstan/phpstan": "^1.3", - "phpstan/phpstan-strict-rules": "^1.1", - "symfony/phpunit-bridge": "^5" + "phpstan/phpstan": "^1.12 || ^2", + "phpstan/phpstan-strict-rules": "^1 || ^2", + "phpunit/phpunit": "^8 || ^9" }, "type": "library", "extra": { + "phpstan": { + "includes": [ + "extension.neon" + ] + }, "branch-alias": { "dev-main": "3.x-dev" } @@ -264,7 +585,7 @@ ], "support": { "issues": "https://github.com/composer/pcre/issues", - "source": "https://github.com/composer/pcre/tree/3.1.3" + "source": "https://github.com/composer/pcre/tree/3.3.2" }, "funding": [ { @@ -280,28 +601,28 @@ "type": "tidelift" } ], - "time": "2024-03-19T10:26:25+00:00" + "time": "2024-11-12T16:29:46+00:00" }, { "name": "composer/semver", - "version": "3.4.0", + "version": "3.4.4", "source": { "type": "git", "url": "https://github.com/composer/semver.git", - "reference": "35e8d0af4486141bc745f23a29cc2091eb624a32" + "reference": "198166618906cb2de69b95d7d47e5fa8aa1b2b95" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/semver/zipball/35e8d0af4486141bc745f23a29cc2091eb624a32", - "reference": "35e8d0af4486141bc745f23a29cc2091eb624a32", + "url": "https://api.github.com/repos/composer/semver/zipball/198166618906cb2de69b95d7d47e5fa8aa1b2b95", + "reference": "198166618906cb2de69b95d7d47e5fa8aa1b2b95", "shasum": "" }, "require": { "php": "^5.3.2 || ^7.0 || ^8.0" }, "require-dev": { - "phpstan/phpstan": "^1.4", - "symfony/phpunit-bridge": "^4.2 || ^5" + "phpstan/phpstan": "^1.11", + "symfony/phpunit-bridge": "^3 || ^7" }, "type": "library", "extra": { @@ -345,7 +666,7 @@ "support": { "irc": "ircs://irc.libera.chat:6697/composer", "issues": "https://github.com/composer/semver/issues", - "source": "https://github.com/composer/semver/tree/3.4.0" + "source": "https://github.com/composer/semver/tree/3.4.4" }, "funding": [ { @@ -355,13 +676,9 @@ { "url": "https://github.com/composer", "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/composer/composer", - "type": "tidelift" } ], - "time": "2023-08-31T09:50:34+00:00" + "time": "2025-08-20T19:15:30+00:00" }, { "name": "composer/xdebug-handler", @@ -431,29 +748,30 @@ }, { "name": "doctrine/deprecations", - "version": "1.1.3", + "version": "1.1.5", "source": { "type": "git", "url": "https://github.com/doctrine/deprecations.git", - "reference": "dfbaa3c2d2e9a9df1118213f3b8b0c597bb99fab" + "reference": "459c2f5dd3d6a4633d3b5f46ee2b1c40f57d3f38" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/deprecations/zipball/dfbaa3c2d2e9a9df1118213f3b8b0c597bb99fab", - "reference": "dfbaa3c2d2e9a9df1118213f3b8b0c597bb99fab", + "url": "https://api.github.com/repos/doctrine/deprecations/zipball/459c2f5dd3d6a4633d3b5f46ee2b1c40f57d3f38", + "reference": "459c2f5dd3d6a4633d3b5f46ee2b1c40f57d3f38", "shasum": "" }, "require": { "php": "^7.1 || ^8.0" }, + "conflict": { + "phpunit/phpunit": "<=7.5 || >=13" + }, "require-dev": { - "doctrine/coding-standard": "^9", - "phpstan/phpstan": "1.4.10 || 1.10.15", - "phpstan/phpstan-phpunit": "^1.0", - "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", - "psalm/plugin-phpunit": "0.18.4", - "psr/log": "^1 || ^2 || ^3", - "vimeo/psalm": "4.30.0 || 5.12.0" + "doctrine/coding-standard": "^9 || ^12 || ^13", + "phpstan/phpstan": "1.4.10 || 2.1.11", + "phpstan/phpstan-phpunit": "^1.0 || ^2", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.6 || ^10.5 || ^11.5 || ^12", + "psr/log": "^1 || ^2 || ^3" }, "suggest": { "psr/log": "Allows logging deprecations via PSR-3 logger implementation" @@ -461,7 +779,7 @@ "type": "library", "autoload": { "psr-4": { - "Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations" + "Doctrine\\Deprecations\\": "src" } }, "notification-url": "https://packagist.org/downloads/", @@ -472,9 +790,9 @@ "homepage": "https://www.doctrine-project.org/", "support": { "issues": "https://github.com/doctrine/deprecations/issues", - "source": "https://github.com/doctrine/deprecations/tree/1.1.3" + "source": "https://github.com/doctrine/deprecations/tree/1.1.5" }, - "time": "2024-01-30T19:34:25+00:00" + "time": "2025-04-07T20:06:18+00:00" }, { "name": "doctrine/instantiator", @@ -638,16 +956,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.11.1", + "version": "1.13.4", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c" + "reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", - "reference": "7284c22080590fb39f2ffa3e9057f10a4ddd0e0c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/07d290f0c47959fd5eed98c95ee5602db07e0b6a", + "reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a", "shasum": "" }, "require": { @@ -655,11 +973,12 @@ }, "conflict": { "doctrine/collections": "<1.6.8", - "doctrine/common": "<2.13.3 || >=3,<3.2.2" + "doctrine/common": "<2.13.3 || >=3 <3.2.2" }, "require-dev": { "doctrine/collections": "^1.6.8", "doctrine/common": "^2.13.3 || ^3.2.2", + "phpspec/prophecy": "^1.10", "phpunit/phpunit": "^7.5.20 || ^8.5.23 || ^9.5.13" }, "type": "library", @@ -685,7 +1004,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.11.1" + "source": "https://github.com/myclabs/DeepCopy/tree/1.13.4" }, "funding": [ { @@ -693,20 +1012,20 @@ "type": "tidelift" } ], - "time": "2023-03-08T13:26:56+00:00" + "time": "2025-08-01T08:46:24+00:00" }, { "name": "netresearch/jsonmapper", - "version": "v4.4.1", + "version": "v4.5.0", "source": { "type": "git", "url": "https://github.com/cweiske/jsonmapper.git", - "reference": "132c75c7dd83e45353ebb9c6c9f591952995bbf0" + "reference": "8e76efb98ee8b6afc54687045e1b8dba55ac76e5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/cweiske/jsonmapper/zipball/132c75c7dd83e45353ebb9c6c9f591952995bbf0", - "reference": "132c75c7dd83e45353ebb9c6c9f591952995bbf0", + "url": "https://api.github.com/repos/cweiske/jsonmapper/zipball/8e76efb98ee8b6afc54687045e1b8dba55ac76e5", + "reference": "8e76efb98ee8b6afc54687045e1b8dba55ac76e5", "shasum": "" }, "require": { @@ -742,22 +1061,22 @@ "support": { "email": "cweiske@cweiske.de", "issues": "https://github.com/cweiske/jsonmapper/issues", - "source": "https://github.com/cweiske/jsonmapper/tree/v4.4.1" + "source": "https://github.com/cweiske/jsonmapper/tree/v4.5.0" }, - "time": "2024-01-31T06:18:54+00:00" + "time": "2024-09-08T10:13:13+00:00" }, { "name": "nikic/php-parser", - "version": "v5.0.2", + "version": "v5.6.2", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13" + "reference": "3a454ca033b9e06b63282ce19562e892747449bb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/139676794dc1e9231bf7bcd123cfc0c99182cb13", - "reference": "139676794dc1e9231bf7bcd123cfc0c99182cb13", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/3a454ca033b9e06b63282ce19562e892747449bb", + "reference": "3a454ca033b9e06b63282ce19562e892747449bb", "shasum": "" }, "require": { @@ -768,7 +1087,7 @@ }, "require-dev": { "ircmaxell/php-yacc": "^0.0.7", - "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^9.0" }, "bin": [ "bin/php-parse" @@ -776,7 +1095,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "5.0-dev" + "dev-master": "5.x-dev" } }, "autoload": { @@ -800,9 +1119,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.0.2" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.6.2" }, - "time": "2024-03-05T20:51:40+00:00" + "time": "2025-10-21T19:32:17+00:00" }, { "name": "phan/phan", @@ -1057,16 +1376,16 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "5.4.0", + "version": "5.6.3", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "298d2febfe79d03fe714eb871d5538da55205b1a" + "reference": "94f8051919d1b0369a6bcc7931d679a511c03fe9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/298d2febfe79d03fe714eb871d5538da55205b1a", - "reference": "298d2febfe79d03fe714eb871d5538da55205b1a", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/94f8051919d1b0369a6bcc7931d679a511c03fe9", + "reference": "94f8051919d1b0369a6bcc7931d679a511c03fe9", "shasum": "" }, "require": { @@ -1075,17 +1394,17 @@ "php": "^7.4 || ^8.0", "phpdocumentor/reflection-common": "^2.2", "phpdocumentor/type-resolver": "^1.7", - "phpstan/phpdoc-parser": "^1.7", + "phpstan/phpdoc-parser": "^1.7|^2.0", "webmozart/assert": "^1.9.1" }, "require-dev": { - "mockery/mockery": "~1.3.5", + "mockery/mockery": "~1.3.5 || ~1.6.0", "phpstan/extension-installer": "^1.1", "phpstan/phpstan": "^1.8", "phpstan/phpstan-mockery": "^1.1", "phpstan/phpstan-webmozart-assert": "^1.2", "phpunit/phpunit": "^9.5", - "vimeo/psalm": "^5.13" + "psalm/phar": "^5.26" }, "type": "library", "extra": { @@ -1115,29 +1434,29 @@ "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", "support": { "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", - "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.4.0" + "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.6.3" }, - "time": "2024-04-09T21:13:58+00:00" + "time": "2025-08-01T19:43:32+00:00" }, { "name": "phpdocumentor/type-resolver", - "version": "1.8.2", + "version": "1.10.0", "source": { "type": "git", "url": "https://github.com/phpDocumentor/TypeResolver.git", - "reference": "153ae662783729388a584b4361f2545e4d841e3c" + "reference": "679e3ce485b99e84c775d28e2e96fade9a7fb50a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/153ae662783729388a584b4361f2545e4d841e3c", - "reference": "153ae662783729388a584b4361f2545e4d841e3c", + "url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/679e3ce485b99e84c775d28e2e96fade9a7fb50a", + "reference": "679e3ce485b99e84c775d28e2e96fade9a7fb50a", "shasum": "" }, "require": { "doctrine/deprecations": "^1.0", "php": "^7.3 || ^8.0", "phpdocumentor/reflection-common": "^2.0", - "phpstan/phpdoc-parser": "^1.13" + "phpstan/phpdoc-parser": "^1.18|^2.0" }, "require-dev": { "ext-tokenizer": "*", @@ -1173,36 +1492,36 @@ "description": "A PSR-5 based resolver of Class names, Types and Structural Element Names", "support": { "issues": "https://github.com/phpDocumentor/TypeResolver/issues", - "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.8.2" + "source": "https://github.com/phpDocumentor/TypeResolver/tree/1.10.0" }, - "time": "2024-02-23T11:10:43+00:00" + "time": "2024-11-09T15:12:26+00:00" }, { "name": "phpstan/phpdoc-parser", - "version": "1.29.0", + "version": "2.3.0", "source": { "type": "git", "url": "https://github.com/phpstan/phpdoc-parser.git", - "reference": "536889f2b340489d328f5ffb7b02bb6b183ddedc" + "reference": "1e0cd5370df5dd2e556a36b9c62f62e555870495" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/536889f2b340489d328f5ffb7b02bb6b183ddedc", - "reference": "536889f2b340489d328f5ffb7b02bb6b183ddedc", + "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/1e0cd5370df5dd2e556a36b9c62f62e555870495", + "reference": "1e0cd5370df5dd2e556a36b9c62f62e555870495", "shasum": "" }, "require": { - "php": "^7.2 || ^8.0" + "php": "^7.4 || ^8.0" }, "require-dev": { "doctrine/annotations": "^2.0", - "nikic/php-parser": "^4.15", + "nikic/php-parser": "^5.3.0", "php-parallel-lint/php-parallel-lint": "^1.2", "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "^1.5", - "phpstan/phpstan-phpunit": "^1.1", - "phpstan/phpstan-strict-rules": "^1.0", - "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "phpstan/phpstan-strict-rules": "^2.0", + "phpunit/phpunit": "^9.6", "symfony/process": "^5.2" }, "type": "library", @@ -1220,41 +1539,41 @@ "description": "PHPDoc parser with support for nullable, intersection and generic types", "support": { "issues": "https://github.com/phpstan/phpdoc-parser/issues", - "source": "https://github.com/phpstan/phpdoc-parser/tree/1.29.0" + "source": "https://github.com/phpstan/phpdoc-parser/tree/2.3.0" }, - "time": "2024-05-06T12:04:23+00:00" + "time": "2025-08-30T15:50:23+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.31", + "version": "9.2.32", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965" + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/48c34b5d8d983006bd2adc2d0de92963b9155965", - "reference": "48c34b5d8d983006bd2adc2d0de92963b9155965", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/85402a822d1ecf1db1096959413d35e1c37cf1a5", + "reference": "85402a822d1ecf1db1096959413d35e1c37cf1a5", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.18 || ^5.0", + "nikic/php-parser": "^4.19.1 || ^5.1.0", "php": ">=7.3", - "phpunit/php-file-iterator": "^3.0.3", - "phpunit/php-text-template": "^2.0.2", - "sebastian/code-unit-reverse-lookup": "^2.0.2", - "sebastian/complexity": "^2.0", - "sebastian/environment": "^5.1.2", - "sebastian/lines-of-code": "^1.0.3", - "sebastian/version": "^3.0.1", - "theseer/tokenizer": "^1.2.0" + "phpunit/php-file-iterator": "^3.0.6", + "phpunit/php-text-template": "^2.0.4", + "sebastian/code-unit-reverse-lookup": "^2.0.3", + "sebastian/complexity": "^2.0.3", + "sebastian/environment": "^5.1.5", + "sebastian/lines-of-code": "^1.0.4", + "sebastian/version": "^3.0.2", + "theseer/tokenizer": "^1.2.3" }, "require-dev": { - "phpunit/phpunit": "^9.3" + "phpunit/phpunit": "^9.6" }, "suggest": { "ext-pcov": "PHP extension that provides line coverage", @@ -1263,7 +1582,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "9.2-dev" + "dev-main": "9.2.x-dev" } }, "autoload": { @@ -1292,7 +1611,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.31" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.32" }, "funding": [ { @@ -1300,7 +1619,7 @@ "type": "github" } ], - "time": "2024-03-02T06:37:42+00:00" + "time": "2024-08-22T04:23:01+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1545,45 +1864,45 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.19", + "version": "9.6.29", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8" + "reference": "9ecfec57835a5581bc888ea7e13b51eb55ab9dd3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a1a54a473501ef4cdeaae4e06891674114d79db8", - "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/9ecfec57835a5581bc888ea7e13b51eb55ab9dd3", + "reference": "9ecfec57835a5581bc888ea7e13b51eb55ab9dd3", "shasum": "" }, "require": { - "doctrine/instantiator": "^1.3.1 || ^2", + "doctrine/instantiator": "^1.5.0 || ^2", "ext-dom": "*", "ext-json": "*", "ext-libxml": "*", "ext-mbstring": "*", "ext-xml": "*", "ext-xmlwriter": "*", - "myclabs/deep-copy": "^1.10.1", - "phar-io/manifest": "^2.0.3", - "phar-io/version": "^3.0.2", + "myclabs/deep-copy": "^1.13.4", + "phar-io/manifest": "^2.0.4", + "phar-io/version": "^3.2.1", "php": ">=7.3", - "phpunit/php-code-coverage": "^9.2.28", - "phpunit/php-file-iterator": "^3.0.5", + "phpunit/php-code-coverage": "^9.2.32", + "phpunit/php-file-iterator": "^3.0.6", "phpunit/php-invoker": "^3.1.1", - "phpunit/php-text-template": "^2.0.3", - "phpunit/php-timer": "^5.0.2", - "sebastian/cli-parser": "^1.0.1", - "sebastian/code-unit": "^1.0.6", - "sebastian/comparator": "^4.0.8", - "sebastian/diff": "^4.0.3", - "sebastian/environment": "^5.1.3", - "sebastian/exporter": "^4.0.5", - "sebastian/global-state": "^5.0.1", - "sebastian/object-enumerator": "^4.0.3", - "sebastian/resource-operations": "^3.0.3", - "sebastian/type": "^3.2", + "phpunit/php-text-template": "^2.0.4", + "phpunit/php-timer": "^5.0.3", + "sebastian/cli-parser": "^1.0.2", + "sebastian/code-unit": "^1.0.8", + "sebastian/comparator": "^4.0.9", + "sebastian/diff": "^4.0.6", + "sebastian/environment": "^5.1.5", + "sebastian/exporter": "^4.0.8", + "sebastian/global-state": "^5.0.8", + "sebastian/object-enumerator": "^4.0.4", + "sebastian/resource-operations": "^3.0.4", + "sebastian/type": "^3.2.1", "sebastian/version": "^3.0.2" }, "suggest": { @@ -1628,7 +1947,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.19" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.29" }, "funding": [ { @@ -1639,12 +1958,20 @@ "url": "https://github.com/sebastianbergmann", "type": "github" }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, { "url": "https://tidelift.com/funding/github/packagist/phpunit/phpunit", "type": "tidelift" } ], - "time": "2024-04-05T04:35:58+00:00" + "time": "2025-09-24T06:29:11+00:00" }, { "name": "psr/container", @@ -1701,25 +2028,25 @@ }, { "name": "sabre/event", - "version": "5.1.4", + "version": "5.1.7", "source": { "type": "git", "url": "https://github.com/sabre-io/event.git", - "reference": "d7da22897125d34d7eddf7977758191c06a74497" + "reference": "86d57e305c272898ba3c28e9bd3d65d5464587c2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sabre-io/event/zipball/d7da22897125d34d7eddf7977758191c06a74497", - "reference": "d7da22897125d34d7eddf7977758191c06a74497", + "url": "https://api.github.com/repos/sabre-io/event/zipball/86d57e305c272898ba3c28e9bd3d65d5464587c2", + "reference": "86d57e305c272898ba3c28e9bd3d65d5464587c2", "shasum": "" }, "require": { "php": "^7.1 || ^8.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "~2.17.1", + "friendsofphp/php-cs-fixer": "~2.17.1||^3.63", "phpstan/phpstan": "^0.12", - "phpunit/phpunit": "^7.5 || ^8.5 || ^9.0" + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.6" }, "type": "library", "autoload": { @@ -1763,7 +2090,7 @@ "issues": "https://github.com/sabre-io/event/issues", "source": "https://github.com/fruux/sabre-event" }, - "time": "2021-11-04T06:51:17+00:00" + "time": "2024-08-27T11:23:05+00:00" }, { "name": "sebastian/cli-parser", @@ -1934,16 +2261,16 @@ }, { "name": "sebastian/comparator", - "version": "4.0.8", + "version": "4.0.9", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "fa0f136dd2334583309d32b62544682ee972b51a" + "reference": "67a2df3a62639eab2cc5906065e9805d4fd5dfc5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/fa0f136dd2334583309d32b62544682ee972b51a", - "reference": "fa0f136dd2334583309d32b62544682ee972b51a", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/67a2df3a62639eab2cc5906065e9805d4fd5dfc5", + "reference": "67a2df3a62639eab2cc5906065e9805d4fd5dfc5", "shasum": "" }, "require": { @@ -1996,15 +2323,27 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/comparator/issues", - "source": "https://github.com/sebastianbergmann/comparator/tree/4.0.8" + "source": "https://github.com/sebastianbergmann/comparator/tree/4.0.9" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/comparator", + "type": "tidelift" } ], - "time": "2022-09-14T12:41:17+00:00" + "time": "2025-08-10T06:51:50+00:00" }, { "name": "sebastian/complexity", @@ -2194,16 +2533,16 @@ }, { "name": "sebastian/exporter", - "version": "4.0.6", + "version": "4.0.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "78c00df8f170e02473b682df15bfcdacc3d32d72" + "reference": "14c6ba52f95a36c3d27c835d65efc7123c446e8c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/78c00df8f170e02473b682df15bfcdacc3d32d72", - "reference": "78c00df8f170e02473b682df15bfcdacc3d32d72", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/14c6ba52f95a36c3d27c835d65efc7123c446e8c", + "reference": "14c6ba52f95a36c3d27c835d65efc7123c446e8c", "shasum": "" }, "require": { @@ -2259,28 +2598,40 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/exporter/issues", - "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.6" + "source": "https://github.com/sebastianbergmann/exporter/tree/4.0.8" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/exporter", + "type": "tidelift" } ], - "time": "2024-03-02T06:33:00+00:00" + "time": "2025-09-24T06:03:27+00:00" }, { "name": "sebastian/global-state", - "version": "5.0.7", + "version": "5.0.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "bca7df1f32ee6fe93b4d4a9abbf69e13a4ada2c9" + "reference": "b6781316bdcd28260904e7cc18ec983d0d2ef4f6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/bca7df1f32ee6fe93b4d4a9abbf69e13a4ada2c9", - "reference": "bca7df1f32ee6fe93b4d4a9abbf69e13a4ada2c9", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/b6781316bdcd28260904e7cc18ec983d0d2ef4f6", + "reference": "b6781316bdcd28260904e7cc18ec983d0d2ef4f6", "shasum": "" }, "require": { @@ -2323,15 +2674,27 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/global-state/issues", - "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.7" + "source": "https://github.com/sebastianbergmann/global-state/tree/5.0.8" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/global-state", + "type": "tidelift" } ], - "time": "2024-03-02T06:35:11+00:00" + "time": "2025-08-10T07:10:35+00:00" }, { "name": "sebastian/lines-of-code", @@ -2504,16 +2867,16 @@ }, { "name": "sebastian/recursion-context", - "version": "4.0.5", + "version": "4.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/recursion-context.git", - "reference": "e75bd0f07204fec2a0af9b0f3cfe97d05f92efc1" + "reference": "539c6691e0623af6dc6f9c20384c120f963465a0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/e75bd0f07204fec2a0af9b0f3cfe97d05f92efc1", - "reference": "e75bd0f07204fec2a0af9b0f3cfe97d05f92efc1", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/539c6691e0623af6dc6f9c20384c120f963465a0", + "reference": "539c6691e0623af6dc6f9c20384c120f963465a0", "shasum": "" }, "require": { @@ -2555,15 +2918,27 @@ "homepage": "https://github.com/sebastianbergmann/recursion-context", "support": { "issues": "https://github.com/sebastianbergmann/recursion-context/issues", - "source": "https://github.com/sebastianbergmann/recursion-context/tree/4.0.5" + "source": "https://github.com/sebastianbergmann/recursion-context/tree/4.0.6" }, "funding": [ { "url": "https://github.com/sebastianbergmann", "type": "github" + }, + { + "url": "https://liberapay.com/sebastianbergmann", + "type": "liberapay" + }, + { + "url": "https://thanks.dev/u/gh/sebastianbergmann", + "type": "thanks_dev" + }, + { + "url": "https://tidelift.com/funding/github/packagist/sebastian/recursion-context", + "type": "tidelift" } ], - "time": "2023-02-03T06:07:39+00:00" + "time": "2025-08-10T06:57:39+00:00" }, { "name": "sebastian/resource-operations", @@ -2730,16 +3105,16 @@ }, { "name": "squizlabs/php_codesniffer", - "version": "3.9.2", + "version": "3.13.5", "source": { "type": "git", "url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git", - "reference": "aac1f6f347a5c5ac6bc98ad395007df00990f480" + "reference": "0ca86845ce43291e8f5692c7356fccf3bcf02bf4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/aac1f6f347a5c5ac6bc98ad395007df00990f480", - "reference": "aac1f6f347a5c5ac6bc98ad395007df00990f480", + "url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/0ca86845ce43291e8f5692c7356fccf3bcf02bf4", + "reference": "0ca86845ce43291e8f5692c7356fccf3bcf02bf4", "shasum": "" }, "require": { @@ -2756,11 +3131,6 @@ "bin/phpcs" ], "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.x-dev" - } - }, "notification-url": "https://packagist.org/downloads/", "license": [ "BSD-3-Clause" @@ -2804,53 +3174,57 @@ { "url": "https://opencollective.com/php_codesniffer", "type": "open_collective" + }, + { + "url": "https://thanks.dev/u/gh/phpcsstandards", + "type": "thanks_dev" } ], - "time": "2024-04-23T20:25:34+00:00" + "time": "2025-11-04T16:30:35+00:00" }, { "name": "symfony/console", - "version": "v6.4.7", + "version": "v7.3.5", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "a170e64ae10d00ba89e2acbb590dc2e54da8ad8f" + "reference": "cdb80fa5869653c83cfe1a9084a673b6daf57ea7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/a170e64ae10d00ba89e2acbb590dc2e54da8ad8f", - "reference": "a170e64ae10d00ba89e2acbb590dc2e54da8ad8f", + "url": "https://api.github.com/repos/symfony/console/zipball/cdb80fa5869653c83cfe1a9084a673b6daf57ea7", + "reference": "cdb80fa5869653c83cfe1a9084a673b6daf57ea7", "shasum": "" }, "require": { - "php": ">=8.1", + "php": ">=8.2", "symfony/deprecation-contracts": "^2.5|^3", "symfony/polyfill-mbstring": "~1.0", "symfony/service-contracts": "^2.5|^3", - "symfony/string": "^5.4|^6.0|^7.0" + "symfony/string": "^7.2" }, "conflict": { - "symfony/dependency-injection": "<5.4", - "symfony/dotenv": "<5.4", - "symfony/event-dispatcher": "<5.4", - "symfony/lock": "<5.4", - "symfony/process": "<5.4" + "symfony/dependency-injection": "<6.4", + "symfony/dotenv": "<6.4", + "symfony/event-dispatcher": "<6.4", + "symfony/lock": "<6.4", + "symfony/process": "<6.4" }, "provide": { "psr/log-implementation": "1.0|2.0|3.0" }, "require-dev": { "psr/log": "^1|^2|^3", - "symfony/config": "^5.4|^6.0|^7.0", - "symfony/dependency-injection": "^5.4|^6.0|^7.0", - "symfony/event-dispatcher": "^5.4|^6.0|^7.0", + "symfony/config": "^6.4|^7.0", + "symfony/dependency-injection": "^6.4|^7.0", + "symfony/event-dispatcher": "^6.4|^7.0", "symfony/http-foundation": "^6.4|^7.0", "symfony/http-kernel": "^6.4|^7.0", - "symfony/lock": "^5.4|^6.0|^7.0", - "symfony/messenger": "^5.4|^6.0|^7.0", - "symfony/process": "^5.4|^6.0|^7.0", - "symfony/stopwatch": "^5.4|^6.0|^7.0", - "symfony/var-dumper": "^5.4|^6.0|^7.0" + "symfony/lock": "^6.4|^7.0", + "symfony/messenger": "^6.4|^7.0", + "symfony/process": "^6.4|^7.0", + "symfony/stopwatch": "^6.4|^7.0", + "symfony/var-dumper": "^6.4|^7.0" }, "type": "library", "autoload": { @@ -2884,7 +3258,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v6.4.7" + "source": "https://github.com/symfony/console/tree/v7.3.5" }, "funding": [ { @@ -2896,70 +3270,7 @@ "type": "github" }, { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-04-18T09:22:46+00:00" - }, - { - "name": "symfony/deprecation-contracts", - "version": "v3.5.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/deprecation-contracts.git", - "reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1", - "reference": "0e0d29ce1f20deffb4ab1b016a7257c4f1e789a1", - "shasum": "" - }, - "require": { - "php": ">=8.1" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "3.5-dev" - }, - "thanks": { - "name": "symfony/contracts", - "url": "https://github.com/symfony/contracts" - } - }, - "autoload": { - "files": [ - "function.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "A generic function and convention to trigger deprecation notices", - "homepage": "https://symfony.com", - "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v3.5.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", + "url": "https://github.com/nicolas-grekas", "type": "github" }, { @@ -2967,24 +3278,24 @@ "type": "tidelift" } ], - "time": "2024-04-18T09:32:20+00:00" + "time": "2025-10-14T15:46:26+00:00" }, { "name": "symfony/polyfill-ctype", - "version": "v1.29.0", + "version": "v1.33.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4" + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/ef4d7e442ca910c4764bce785146269b30cb5fc4", - "reference": "ef4d7e442ca910c4764bce785146269b30cb5fc4", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/a3cc8b044a6ea513310cbd48ef7333b384945638", + "reference": "a3cc8b044a6ea513310cbd48ef7333b384945638", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "provide": { "ext-ctype": "*" @@ -2995,8 +3306,8 @@ "type": "library", "extra": { "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { @@ -3030,7 +3341,7 @@ "portable" ], "support": { - "source": "https://github.com/symfony/polyfill-ctype/tree/v1.29.0" + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.33.0" }, "funding": [ { @@ -3041,29 +3352,33 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2024-01-29T20:11:03+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "symfony/polyfill-intl-grapheme", - "version": "v1.29.0", + "version": "v1.33.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-grapheme.git", - "reference": "32a9da87d7b3245e09ac426c83d334ae9f06f80f" + "reference": "380872130d3a5dd3ace2f4010d95125fde5d5c70" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/32a9da87d7b3245e09ac426c83d334ae9f06f80f", - "reference": "32a9da87d7b3245e09ac426c83d334ae9f06f80f", + "url": "https://api.github.com/repos/symfony/polyfill-intl-grapheme/zipball/380872130d3a5dd3ace2f4010d95125fde5d5c70", + "reference": "380872130d3a5dd3ace2f4010d95125fde5d5c70", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "suggest": { "ext-intl": "For best performance" @@ -3071,8 +3386,8 @@ "type": "library", "extra": { "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { @@ -3108,7 +3423,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.29.0" + "source": "https://github.com/symfony/polyfill-intl-grapheme/tree/v1.33.0" }, "funding": [ { @@ -3119,29 +3434,33 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2024-01-29T20:11:03+00:00" + "time": "2025-06-27T09:58:17+00:00" }, { "name": "symfony/polyfill-intl-normalizer", - "version": "v1.29.0", + "version": "v1.33.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-intl-normalizer.git", - "reference": "bc45c394692b948b4d383a08d7753968bed9a83d" + "reference": "3833d7255cc303546435cb650316bff708a1c75c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/bc45c394692b948b4d383a08d7753968bed9a83d", - "reference": "bc45c394692b948b4d383a08d7753968bed9a83d", + "url": "https://api.github.com/repos/symfony/polyfill-intl-normalizer/zipball/3833d7255cc303546435cb650316bff708a1c75c", + "reference": "3833d7255cc303546435cb650316bff708a1c75c", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "suggest": { "ext-intl": "For best performance" @@ -3149,8 +3468,8 @@ "type": "library", "extra": { "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { @@ -3189,7 +3508,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.29.0" + "source": "https://github.com/symfony/polyfill-intl-normalizer/tree/v1.33.0" }, "funding": [ { @@ -3201,83 +3520,7 @@ "type": "github" }, { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-01-29T20:11:03+00:00" - }, - { - "name": "symfony/polyfill-mbstring", - "version": "v1.29.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9773676c8a1bb1f8d4340a62efe641cf76eda7ec", - "reference": "9773676c8a1bb1f8d4340a62efe641cf76eda7ec", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "provide": { - "ext-mbstring": "*" - }, - "suggest": { - "ext-mbstring": "For best performance" - }, - "type": "library", - "extra": { - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for the Mbstring extension", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "mbstring", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.29.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", + "url": "https://github.com/nicolas-grekas", "type": "github" }, { @@ -3285,30 +3528,30 @@ "type": "tidelift" } ], - "time": "2024-01-29T20:11:03+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "symfony/polyfill-php80", - "version": "v1.29.0", + "version": "v1.33.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "87b68208d5c1188808dd7839ee1e6c8ec3b02f1b" + "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/87b68208d5c1188808dd7839ee1e6c8ec3b02f1b", - "reference": "87b68208d5c1188808dd7839ee1e6c8ec3b02f1b", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/0cc9dd0f17f61d8131e7df6b84bd344899fe2608", + "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "type": "library", "extra": { "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { @@ -3349,7 +3592,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.29.0" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.33.0" }, "funding": [ { @@ -3360,25 +3603,29 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2024-01-29T20:11:03+00:00" + "time": "2025-01-02T08:10:11+00:00" }, { "name": "symfony/service-contracts", - "version": "v3.5.0", + "version": "v3.6.0", "source": { "type": "git", "url": "https://github.com/symfony/service-contracts.git", - "reference": "bd1d9e59a81d8fa4acdcea3f617c581f7475a80f" + "reference": "f021b05a130d35510bd6b25fe9053c2a8a15d5d4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/service-contracts/zipball/bd1d9e59a81d8fa4acdcea3f617c581f7475a80f", - "reference": "bd1d9e59a81d8fa4acdcea3f617c581f7475a80f", + "url": "https://api.github.com/repos/symfony/service-contracts/zipball/f021b05a130d35510bd6b25fe9053c2a8a15d5d4", + "reference": "f021b05a130d35510bd6b25fe9053c2a8a15d5d4", "shasum": "" }, "require": { @@ -3391,12 +3638,12 @@ }, "type": "library", "extra": { - "branch-alias": { - "dev-main": "3.5-dev" - }, "thanks": { - "name": "symfony/contracts", - "url": "https://github.com/symfony/contracts" + "url": "https://github.com/symfony/contracts", + "name": "symfony/contracts" + }, + "branch-alias": { + "dev-main": "3.6-dev" } }, "autoload": { @@ -3432,7 +3679,7 @@ "standards" ], "support": { - "source": "https://github.com/symfony/service-contracts/tree/v3.5.0" + "source": "https://github.com/symfony/service-contracts/tree/v3.6.0" }, "funding": [ { @@ -3448,20 +3695,20 @@ "type": "tidelift" } ], - "time": "2024-04-18T09:32:20+00:00" + "time": "2025-04-25T09:37:31+00:00" }, { "name": "symfony/string", - "version": "v7.0.7", + "version": "v7.3.4", "source": { "type": "git", "url": "https://github.com/symfony/string.git", - "reference": "e405b5424dc2528e02e31ba26b83a79fd4eb8f63" + "reference": "f96476035142921000338bad71e5247fbc138872" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/string/zipball/e405b5424dc2528e02e31ba26b83a79fd4eb8f63", - "reference": "e405b5424dc2528e02e31ba26b83a79fd4eb8f63", + "url": "https://api.github.com/repos/symfony/string/zipball/f96476035142921000338bad71e5247fbc138872", + "reference": "f96476035142921000338bad71e5247fbc138872", "shasum": "" }, "require": { @@ -3475,7 +3722,7 @@ "symfony/translation-contracts": "<2.5" }, "require-dev": { - "symfony/error-handler": "^6.4|^7.0", + "symfony/emoji": "^7.1", "symfony/http-client": "^6.4|^7.0", "symfony/intl": "^6.4|^7.0", "symfony/translation-contracts": "^2.5|^3.0", @@ -3518,7 +3765,7 @@ "utf8" ], "support": { - "source": "https://github.com/symfony/string/tree/v7.0.7" + "source": "https://github.com/symfony/string/tree/v7.3.4" }, "funding": [ { @@ -3529,12 +3776,16 @@ "url": "https://github.com/fabpot", "type": "github" }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2024-04-18T09:29:19+00:00" + "time": "2025-09-11T14:36:48+00:00" }, { "name": "theseer/tokenizer", @@ -3650,28 +3901,28 @@ }, { "name": "webmozart/assert", - "version": "1.11.0", + "version": "1.12.1", "source": { "type": "git", "url": "https://github.com/webmozarts/assert.git", - "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991" + "reference": "9be6926d8b485f55b9229203f962b51ed377ba68" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webmozarts/assert/zipball/11cb2199493b2f8a3b53e7f19068fc6aac760991", - "reference": "11cb2199493b2f8a3b53e7f19068fc6aac760991", + "url": "https://api.github.com/repos/webmozarts/assert/zipball/9be6926d8b485f55b9229203f962b51ed377ba68", + "reference": "9be6926d8b485f55b9229203f962b51ed377ba68", "shasum": "" }, "require": { "ext-ctype": "*", + "ext-date": "*", + "ext-filter": "*", "php": "^7.2 || ^8.0" }, - "conflict": { - "phpstan/phpstan": "<0.12.20", - "vimeo/psalm": "<4.6.1 || 4.6.2" - }, - "require-dev": { - "phpunit/phpunit": "^8.5.13" + "suggest": { + "ext-intl": "", + "ext-simplexml": "", + "ext-spl": "" }, "type": "library", "extra": { @@ -3702,9 +3953,9 @@ ], "support": { "issues": "https://github.com/webmozarts/assert/issues", - "source": "https://github.com/webmozarts/assert/tree/1.11.0" + "source": "https://github.com/webmozarts/assert/tree/1.12.1" }, - "time": "2022-06-03T18:03:27+00:00" + "time": "2025-10-29T15:56:20+00:00" } ], "aliases": [], diff --git a/src/SignatureVerifier/HmacSignatureVerifier.php b/src/SignatureVerifier/HmacSignatureVerifier.php new file mode 100644 index 0000000..6bd4562 --- /dev/null +++ b/src/SignatureVerifier/HmacSignatureVerifier.php @@ -0,0 +1,98 @@ +secretKey = $secretKey; + $this->signatureHeader = $signatureHeader; + $this->algorithm = strtolower($algorithm); + $this->encoding = strtolower($encoding); + $this->signatureValueTemplate = $signatureValueTemplate; + $this->templateResolver = $templateResolver; + } + + /** + * @param Request $request + * @return VerificationFailure | true + */ + public function verify(Request $request) + { + $receivedSignature = $request->headers->get($this->signatureHeader); + + if ($receivedSignature === null) { + return VerificationFailure::init('Missing signature header'); + } + + if ($this->templateResolver !== null) { + $signingData = call_user_func($this->templateResolver, $request); + + if ($signingData === null || $signingData === '') { + $signingData = $request->getContent(); + } + } else { + $signingData = $request->getContent(); + } + + $hash = hash_hmac($this->algorithm, $signingData, $this->secretKey, true); + $expectedSignature = $this->encodeHash($hash); + + if ($this->signatureValueTemplate !== null) { + $expectedSignature = str_replace('{digest}', $expectedSignature, $this->signatureValueTemplate); + } + + if (!hash_equals($expectedSignature, $receivedSignature)) { + return VerificationFailure::init('Signature mismatch'); + } + + return true; + } + + private function encodeHash(string $hash): string + { + if ($this->encoding === 'hex') { + return bin2hex($hash); + } + if ($this->encoding === 'base64') { + return base64_encode($hash); + } + return rtrim(strtr(base64_encode($hash), '+/', '-_'), '='); + } +} diff --git a/src/SignatureVerifier/SignatureVerifierInterface.php b/src/SignatureVerifier/SignatureVerifierInterface.php new file mode 100644 index 0000000..793fcf2 --- /dev/null +++ b/src/SignatureVerifier/SignatureVerifierInterface.php @@ -0,0 +1,18 @@ +errorMessage = $errorMessage; + } + + public static function init(string $errorMessage): VerificationFailure + { + return new VerificationFailure($errorMessage); + } + + public function getErrorMessage(): string + { + return $this->errorMessage; + } +} diff --git a/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php new file mode 100644 index 0000000..c8b5ee0 --- /dev/null +++ b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php @@ -0,0 +1,414 @@ +getMethod(); + $body = $request->getContent(); + $cookieHeader = $request->headers->get('Cookie', ''); + $xTimestampHeader = $request->headers->get('X-Timestamp', ''); + + // Parse JSON body to get customer name + $customerName = ''; + if (!empty($body)) { + $data = json_decode($body, true); + if (isset($data['customer']['name'])) { + $customerName = $data['customer']['name']; + } + } + + return "{$cookieHeader}:{$xTimestampHeader}:{$method}:{$body}:{$customerName}"; + }; + } + + private function createBaseRequest(array $headers = [], ?string $body = '{"foo":"bar"}'): Request + { + $defaultHeaders = [ + 'Content-Type' => 'application/json', + 'X-Timestamp' => '1697051234', + 'X-Signature' => '', + ]; + + $mergedHeaders = array_merge($defaultHeaders, $headers); + + $request = Request::create( + 'https://api.example.com/resource', + 'POST', + [], + [], + [], + [], + $body + ); + + foreach ($mergedHeaders as $key => $value) { + $request->headers->set($key, $value); + } + + return $request; + } + + public function testShouldThrowErrorForEmptySecretKey(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('secretKey must be a non-empty string'); + new HmacSignatureVerifier('', self::SIGNATURE_HEADER, $this->createTemplateResolver()); + } + + public function testShouldThrowErrorForEmptySignatureHeader(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('signatureHeader must be a non-empty string'); + new HmacSignatureVerifier(self::SECRET_KEY, '', $this->createTemplateResolver()); + } + + public function testShouldThrowErrorForInvalidAlgorithm(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('algorithm must be one of'); + new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'invalid' + ); + } + + public function testShouldThrowErrorForInvalidEncoding(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('encoding must be one of'); + new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + 'invalid' + ); + } + + public function testShouldFailVerificationIfSignatureHeaderIsMissing(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver() + ); + + $request = $this->createBaseRequest(['X-Signature' => null]); + $request->headers->remove('X-Signature'); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Missing signature header', $result->getErrorMessage()); + } + + public function testShouldFailVerificationIfSignatureDoesNotMatch(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver() + ); + + $request = $this->createBaseRequest(['X-Signature' => 'sha256=invalidsignature']); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Signature mismatch', $result->getErrorMessage()); + } + + public function testShouldPassVerificationForValidSignature(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldPassVerificationForValidSignatureWithoutSignatureValueTemplate(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver() + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldSupportSha512AndBase64Encoding(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'sha512', + 'base64' + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); + $digest = base64_encode($hash); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldSupportHexEncoding(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'sha512', + 'hex' + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldSupportBase64urlEncoding(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'sha512', + 'base64url' + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); + $base64 = base64_encode($hash); + $digest = rtrim(strtr($base64, '+/', '-_'), '='); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldUseCustomSignatureValueTemplate(): void + { + $customTemplate = 'sig:{digest}:end'; + + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + self::ENCODING, + $customTemplate + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, $customTemplate); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldFailIfDifferentSecretKeyIsGiven(): void + { + $verifier = new HmacSignatureVerifier( + 'different-key', + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + // Sign with original secret key + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Signature mismatch', $result->getErrorMessage()); + } + + public function testShouldUseBodyToSignIfTemplateResolverIsNotProvided(): void + { + $verifier = new HmacSignatureVerifier( + 'different-key', + self::SIGNATURE_HEADER, + null, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest(); + $body = $request->getContent(); + + // Sign with original secret key (different from verifier's key) + $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Signature mismatch', $result->getErrorMessage()); + } + + public function testShouldHandleVerificationWhenBothTemplateResolverAndBodyAreUndefined(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + null, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest([], ''); + + $signingData = ''; + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldFallbackToBodyIfTemplateResolverReturnsNull(): void + { + $templateResolver = function (Request $request): ?string { + return null; + }; + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $templateResolver, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + $request = $this->createBaseRequest(); + $body = $request->getContent(); + $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + $request->headers->set('X-Signature', $signature); + $result = $verifier->verify($request); + $this->assertTrue($result); + } + + public function testShouldFallbackToBodyIfTemplateResolverReturnsEmptyString(): void + { + $templateResolver = function (Request $request): string { + return ''; + }; + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $templateResolver, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + $request = $this->createBaseRequest(); + $body = $request->getContent(); + $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + $request->headers->set('X-Signature', $signature); + $result = $verifier->verify($request); + $this->assertTrue($result); + } +} From 3bc8f56dbc821713e4215396661f35cbb2467b60 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 11:48:24 +0500 Subject: [PATCH 02/14] fix: update symfony version --- composer.json | 2 +- composer.lock | 151 +++++++++++--------------------------------------- 2 files changed, 34 insertions(+), 119 deletions(-) diff --git a/composer.json b/composer.json index 1444884..ad34afe 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "apimatic/core-interfaces": "~0.1.5", "apimatic/jsonmapper": "^3.1.1", "php-jsonpointer/php-jsonpointer": "^3.0.2", - "symfony/http-foundation": "^6.0" + "symfony/http-foundation": "^5.4" }, "require-dev": { "squizlabs/php_codesniffer": "^3.5", diff --git a/composer.lock b/composer.lock index 02fb3c5..08f969d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b151dc7f5955b90ce7e5f1dc5da926c5", + "content-hash": "4cb27818baa5f4e10403b353e1bf589a", "packages": [ { "name": "apimatic/core-interfaces", @@ -278,36 +278,35 @@ }, { "name": "symfony/http-foundation", - "version": "v6.4.26", + "version": "v5.4.48", "source": { "type": "git", "url": "https://github.com/symfony/http-foundation.git", - "reference": "369241591d92bb5dfb4c6ccd6ee94378a45b1521" + "reference": "3f38b8af283b830e1363acd79e5bc3412d055341" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-foundation/zipball/369241591d92bb5dfb4c6ccd6ee94378a45b1521", - "reference": "369241591d92bb5dfb4c6ccd6ee94378a45b1521", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/3f38b8af283b830e1363acd79e5bc3412d055341", + "reference": "3f38b8af283b830e1363acd79e5bc3412d055341", "shasum": "" }, "require": { - "php": ">=8.1", - "symfony/deprecation-contracts": "^2.5|^3", + "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.1|^3", "symfony/polyfill-mbstring": "~1.1", - "symfony/polyfill-php83": "^1.27" - }, - "conflict": { - "symfony/cache": "<6.4.12|>=7.0,<7.1.5" + "symfony/polyfill-php80": "^1.16" }, "require-dev": { - "doctrine/dbal": "^2.13.1|^3|^4", - "predis/predis": "^1.1|^2.0", - "symfony/cache": "^6.4.12|^7.1.5", - "symfony/dependency-injection": "^5.4|^6.0|^7.0", - "symfony/expression-language": "^5.4|^6.0|^7.0", - "symfony/http-kernel": "^5.4.12|^6.0.12|^6.1.4|^7.0", - "symfony/mime": "^5.4|^6.0|^7.0", - "symfony/rate-limiter": "^5.4|^6.0|^7.0" + "predis/predis": "^1.0|^2.0", + "symfony/cache": "^4.4|^5.0|^6.0", + "symfony/dependency-injection": "^5.4|^6.0", + "symfony/expression-language": "^4.4|^5.0|^6.0", + "symfony/http-kernel": "^5.4.12|^6.0.12|^6.1.4", + "symfony/mime": "^4.4|^5.0|^6.0", + "symfony/rate-limiter": "^5.2|^6.0" + }, + "suggest": { + "symfony/mime": "To use the file extension guesser" }, "type": "library", "autoload": { @@ -335,7 +334,7 @@ "description": "Defines an object-oriented layer for the HTTP specification", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/http-foundation/tree/v6.4.26" + "source": "https://github.com/symfony/http-foundation/tree/v5.4.48" }, "funding": [ { @@ -346,16 +345,12 @@ "url": "https://github.com/fabpot", "type": "github" }, - { - "url": "https://github.com/nicolas-grekas", - "type": "github" - }, { "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", "type": "tidelift" } ], - "time": "2025-09-16T08:22:30+00:00" + "time": "2024-11-13T18:58:02+00:00" }, { "name": "symfony/polyfill-mbstring", @@ -443,17 +438,17 @@ "time": "2024-12-23T08:48:59+00:00" }, { - "name": "symfony/polyfill-php83", + "name": "symfony/polyfill-php80", "version": "v1.33.0", "source": { "type": "git", - "url": "https://github.com/symfony/polyfill-php83.git", - "reference": "17f6f9a6b1735c0f163024d959f700cfbc5155e5" + "url": "https://github.com/symfony/polyfill-php80.git", + "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php83/zipball/17f6f9a6b1735c0f163024d959f700cfbc5155e5", - "reference": "17f6f9a6b1735c0f163024d959f700cfbc5155e5", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/0cc9dd0f17f61d8131e7df6b84bd344899fe2608", + "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608", "shasum": "" }, "require": { @@ -471,7 +466,7 @@ "bootstrap.php" ], "psr-4": { - "Symfony\\Polyfill\\Php83\\": "" + "Symfony\\Polyfill\\Php80\\": "" }, "classmap": [ "Resources/stubs" @@ -482,6 +477,10 @@ "MIT" ], "authors": [ + { + "name": "Ion Bazan", + "email": "ion.bazan@gmail.com" + }, { "name": "Nicolas Grekas", "email": "p@tchwork.com" @@ -491,7 +490,7 @@ "homepage": "https://symfony.com/contributors" } ], - "description": "Symfony polyfill backporting some PHP 8.3+ features to lower PHP versions", + "description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions", "homepage": "https://symfony.com", "keywords": [ "compatibility", @@ -500,7 +499,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php83/tree/v1.33.0" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.33.0" }, "funding": [ { @@ -520,7 +519,7 @@ "type": "tidelift" } ], - "time": "2025-07-08T02:45:35+00:00" + "time": "2025-01-02T08:10:11+00:00" } ], "packages-dev": [ @@ -3530,90 +3529,6 @@ ], "time": "2024-09-09T11:45:10+00:00" }, - { - "name": "symfony/polyfill-php80", - "version": "v1.33.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/0cc9dd0f17f61d8131e7df6b84bd344899fe2608", - "reference": "0cc9dd0f17f61d8131e7df6b84bd344899fe2608", - "shasum": "" - }, - "require": { - "php": ">=7.2" - }, - "type": "library", - "extra": { - "thanks": { - "url": "https://github.com/symfony/polyfill", - "name": "symfony/polyfill" - } - }, - "autoload": { - "files": [ - "bootstrap.php" - ], - "psr-4": { - "Symfony\\Polyfill\\Php80\\": "" - }, - "classmap": [ - "Resources/stubs" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Ion Bazan", - "email": "ion.bazan@gmail.com" - }, - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.33.0" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://github.com/nicolas-grekas", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2025-01-02T08:10:11+00:00" - }, { "name": "symfony/service-contracts", "version": "v3.6.0", From 109adb9cf38dab04a7016f665e0df5be7688cc36 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 12:00:23 +0500 Subject: [PATCH 03/14] fix: indentation --- .../HmacSignatureVerifier.php | 149 ++-- .../SignatureVerifierInterface.php | 15 +- src/Types/Sdk/VerificationFailure.php | 27 +- .../HmacSignatureVerifierTest.php | 795 +++++++++--------- 4 files changed, 495 insertions(+), 491 deletions(-) diff --git a/src/SignatureVerifier/HmacSignatureVerifier.php b/src/SignatureVerifier/HmacSignatureVerifier.php index 6bd4562..0394c89 100644 --- a/src/SignatureVerifier/HmacSignatureVerifier.php +++ b/src/SignatureVerifier/HmacSignatureVerifier.php @@ -8,91 +8,92 @@ use Core\Types\Sdk\VerificationFailure; use Core\SignatureVerifier\SignatureVerifierInterface; + class HmacSignatureVerifier implements SignatureVerifierInterface { - private const VALID_ENCODINGS = ['hex', 'base64', 'base64url']; - private const VALID_ALGORITHMS = ['sha256', 'sha512']; - - private $secretKey; - private $signatureHeader; - private $algorithm; - private $encoding; - private $signatureValueTemplate; - private $templateResolver; - - public function __construct( - string $secretKey, - string $signatureHeader, - ?callable $templateResolver = null, - string $algorithm = 'sha256', - string $encoding = 'hex', - ?string $signatureValueTemplate = null - ) { - if (empty($secretKey)) { - throw new \InvalidArgumentException('secretKey must be a non-empty string'); - } - if (empty($signatureHeader)) { - throw new \InvalidArgumentException('signatureHeader must be a non-empty string'); - } - if (!in_array(strtolower($algorithm), self::VALID_ALGORITHMS, true)) { - throw new \InvalidArgumentException('algorithm must be one of: ' . implode(', ', self::VALID_ALGORITHMS)); - } - if (!in_array(strtolower($encoding), self::VALID_ENCODINGS, true)) { - throw new \InvalidArgumentException('encoding must be one of: ' . implode(', ', self::VALID_ENCODINGS)); + private const VALID_ENCODINGS = ['hex', 'base64', 'base64url']; + private const VALID_ALGORITHMS = ['sha256', 'sha512']; + + private $secretKey; + private $signatureHeader; + private $algorithm; + private $encoding; + private $signatureValueTemplate; + private $templateResolver; + + public function __construct( + string $secretKey, + string $signatureHeader, + ?callable $templateResolver = null, + string $algorithm = 'sha256', + string $encoding = 'hex', + ?string $signatureValueTemplate = null + ) { + if (empty($secretKey)) { + throw new \InvalidArgumentException('secretKey must be a non-empty string'); + } + if (empty($signatureHeader)) { + throw new \InvalidArgumentException('signatureHeader must be a non-empty string'); + } + if (!in_array(strtolower($algorithm), self::VALID_ALGORITHMS, true)) { + throw new \InvalidArgumentException('algorithm must be one of: ' . implode(', ', self::VALID_ALGORITHMS)); + } + if (!in_array(strtolower($encoding), self::VALID_ENCODINGS, true)) { + throw new \InvalidArgumentException('encoding must be one of: ' . implode(', ', self::VALID_ENCODINGS)); + } + + $this->secretKey = $secretKey; + $this->signatureHeader = $signatureHeader; + $this->algorithm = strtolower($algorithm); + $this->encoding = strtolower($encoding); + $this->signatureValueTemplate = $signatureValueTemplate; + $this->templateResolver = $templateResolver; } - $this->secretKey = $secretKey; - $this->signatureHeader = $signatureHeader; - $this->algorithm = strtolower($algorithm); - $this->encoding = strtolower($encoding); - $this->signatureValueTemplate = $signatureValueTemplate; - $this->templateResolver = $templateResolver; - } - - /** - * @param Request $request - * @return VerificationFailure | true - */ - public function verify(Request $request) - { - $receivedSignature = $request->headers->get($this->signatureHeader); - - if ($receivedSignature === null) { - return VerificationFailure::init('Missing signature header'); - } + /** + * @param Request $request + * @return VerificationFailure | true + */ + public function verify(Request $request) + { + $receivedSignature = $request->headers->get($this->signatureHeader); - if ($this->templateResolver !== null) { - $signingData = call_user_func($this->templateResolver, $request); + if ($receivedSignature === null) { + return VerificationFailure::init('Missing signature header'); + } - if ($signingData === null || $signingData === '') { - $signingData = $request->getContent(); - } - } else { - $signingData = $request->getContent(); - } + if ($this->templateResolver !== null) { + $signingData = call_user_func($this->templateResolver, $request); - $hash = hash_hmac($this->algorithm, $signingData, $this->secretKey, true); - $expectedSignature = $this->encodeHash($hash); + if ($signingData === null || $signingData === '') { + $signingData = $request->getContent(); + } + } else { + $signingData = $request->getContent(); + } - if ($this->signatureValueTemplate !== null) { - $expectedSignature = str_replace('{digest}', $expectedSignature, $this->signatureValueTemplate); - } + $hash = hash_hmac($this->algorithm, $signingData, $this->secretKey, true); + $expectedSignature = $this->encodeHash($hash); - if (!hash_equals($expectedSignature, $receivedSignature)) { - return VerificationFailure::init('Signature mismatch'); - } + if ($this->signatureValueTemplate !== null) { + $expectedSignature = str_replace('{digest}', $expectedSignature, $this->signatureValueTemplate); + } - return true; - } + if (!hash_equals($expectedSignature, $receivedSignature)) { + return VerificationFailure::init('Signature mismatch'); + } - private function encodeHash(string $hash): string - { - if ($this->encoding === 'hex') { - return bin2hex($hash); + return true; } - if ($this->encoding === 'base64') { - return base64_encode($hash); + + private function encodeHash(string $hash): string + { + if ($this->encoding === 'hex') { + return bin2hex($hash); + } + if ($this->encoding === 'base64') { + return base64_encode($hash); + } + return rtrim(strtr(base64_encode($hash), '+/', '-_'), '='); } - return rtrim(strtr(base64_encode($hash), '+/', '-_'), '='); - } } diff --git a/src/SignatureVerifier/SignatureVerifierInterface.php b/src/SignatureVerifier/SignatureVerifierInterface.php index 793fcf2..88a4cbd 100644 --- a/src/SignatureVerifier/SignatureVerifierInterface.php +++ b/src/SignatureVerifier/SignatureVerifierInterface.php @@ -6,13 +6,14 @@ use Symfony\Component\HttpFoundation\Request; + interface SignatureVerifierInterface { - /** - * Verifies the signature of a request. - * - * @param Request $request - * @return VerificationFailure | true - */ - public function verify(Request $request); + /** + * Verifies the signature of a request. + * + * @param Request $request + * @return VerificationFailure | true + */ + public function verify(Request $request); } diff --git a/src/Types/Sdk/VerificationFailure.php b/src/Types/Sdk/VerificationFailure.php index 9b253d9..1aa1547 100644 --- a/src/Types/Sdk/VerificationFailure.php +++ b/src/Types/Sdk/VerificationFailure.php @@ -4,22 +4,23 @@ namespace Core\Types\Sdk; + class VerificationFailure { - private string $errorMessage; + private string $errorMessage; - public function __construct(string $errorMessage) - { - $this->errorMessage = $errorMessage; - } + public function __construct(string $errorMessage) + { + $this->errorMessage = $errorMessage; + } - public static function init(string $errorMessage): VerificationFailure - { - return new VerificationFailure($errorMessage); - } + public static function init(string $errorMessage): VerificationFailure + { + return new VerificationFailure($errorMessage); + } - public function getErrorMessage(): string - { - return $this->errorMessage; - } + public function getErrorMessage(): string + { + return $this->errorMessage; + } } diff --git a/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php index c8b5ee0..6594d08 100644 --- a/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php +++ b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php @@ -9,406 +9,407 @@ use Core\SignatureVerifier\HmacSignatureVerifier; use Core\Types\Sdk\VerificationFailure; + class HmacSignatureVerifierTest extends TestCase { - private const SECRET_KEY = 'test_secret'; - private const SIGNATURE_HEADER = 'X-Signature'; - private const HMAC_ALGORITHM = 'sha256'; - private const ENCODING = 'hex'; - private const SIGNATURE_VALUE_TEMPLATE = 'sha256={digest}'; - - private function createTemplateResolver(): callable - { - return function (Request $request): string { - $method = $request->getMethod(); - $body = $request->getContent(); - $cookieHeader = $request->headers->get('Cookie', ''); - $xTimestampHeader = $request->headers->get('X-Timestamp', ''); - - // Parse JSON body to get customer name - $customerName = ''; - if (!empty($body)) { - $data = json_decode($body, true); - if (isset($data['customer']['name'])) { - $customerName = $data['customer']['name']; + private const SECRET_KEY = 'test_secret'; + private const SIGNATURE_HEADER = 'X-Signature'; + private const HMAC_ALGORITHM = 'sha256'; + private const ENCODING = 'hex'; + private const SIGNATURE_VALUE_TEMPLATE = 'sha256={digest}'; + + private function createTemplateResolver(): callable + { + return function (Request $request): string { + $method = $request->getMethod(); + $body = $request->getContent(); + $cookieHeader = $request->headers->get('Cookie', ''); + $xTimestampHeader = $request->headers->get('X-Timestamp', ''); + + // Parse JSON body to get customer name + $customerName = ''; + if (!empty($body)) { + $data = json_decode($body, true); + if (isset($data['customer']['name'])) { + $customerName = $data['customer']['name']; + } + } + + return "{$cookieHeader}:{$xTimestampHeader}:{$method}:{$body}:{$customerName}"; + }; + } + + private function createBaseRequest(array $headers = [], ?string $body = '{"foo":"bar"}'): Request + { + $defaultHeaders = [ + 'Content-Type' => 'application/json', + 'X-Timestamp' => '1697051234', + 'X-Signature' => '', + ]; + + $mergedHeaders = array_merge($defaultHeaders, $headers); + + $request = Request::create( + 'https://api.example.com/resource', + 'POST', + [], + [], + [], + [], + $body + ); + + foreach ($mergedHeaders as $key => $value) { + $request->headers->set($key, $value); } - } - - return "{$cookieHeader}:{$xTimestampHeader}:{$method}:{$body}:{$customerName}"; - }; - } - - private function createBaseRequest(array $headers = [], ?string $body = '{"foo":"bar"}'): Request - { - $defaultHeaders = [ - 'Content-Type' => 'application/json', - 'X-Timestamp' => '1697051234', - 'X-Signature' => '', - ]; - - $mergedHeaders = array_merge($defaultHeaders, $headers); - - $request = Request::create( - 'https://api.example.com/resource', - 'POST', - [], - [], - [], - [], - $body - ); - - foreach ($mergedHeaders as $key => $value) { - $request->headers->set($key, $value); + + return $request; + } + + public function testShouldThrowErrorForEmptySecretKey(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('secretKey must be a non-empty string'); + new HmacSignatureVerifier('', self::SIGNATURE_HEADER, $this->createTemplateResolver()); + } + + public function testShouldThrowErrorForEmptySignatureHeader(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('signatureHeader must be a non-empty string'); + new HmacSignatureVerifier(self::SECRET_KEY, '', $this->createTemplateResolver()); + } + + public function testShouldThrowErrorForInvalidAlgorithm(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('algorithm must be one of'); + new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'invalid' + ); + } + + public function testShouldThrowErrorForInvalidEncoding(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('encoding must be one of'); + new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + 'invalid' + ); } - return $request; - } - - public function testShouldThrowErrorForEmptySecretKey(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('secretKey must be a non-empty string'); - new HmacSignatureVerifier('', self::SIGNATURE_HEADER, $this->createTemplateResolver()); - } - - public function testShouldThrowErrorForEmptySignatureHeader(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('signatureHeader must be a non-empty string'); - new HmacSignatureVerifier(self::SECRET_KEY, '', $this->createTemplateResolver()); - } - - public function testShouldThrowErrorForInvalidAlgorithm(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('algorithm must be one of'); - new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - 'invalid' - ); - } - - public function testShouldThrowErrorForInvalidEncoding(): void - { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('encoding must be one of'); - new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - self::HMAC_ALGORITHM, - 'invalid' - ); - } - - public function testShouldFailVerificationIfSignatureHeaderIsMissing(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver() - ); - - $request = $this->createBaseRequest(['X-Signature' => null]); - $request->headers->remove('X-Signature'); - - $result = $verifier->verify($request); - - $this->assertInstanceOf(VerificationFailure::class, $result); - $this->assertEquals('Missing signature header', $result->getErrorMessage()); - } - - public function testShouldFailVerificationIfSignatureDoesNotMatch(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver() - ); - - $request = $this->createBaseRequest(['X-Signature' => 'sha256=invalidsignature']); - - $result = $verifier->verify($request); - - $this->assertInstanceOf(VerificationFailure::class, $result); - $this->assertEquals('Signature mismatch', $result->getErrorMessage()); - } - - public function testShouldPassVerificationForValidSignature(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - self::HMAC_ALGORITHM, - self::ENCODING, - self::SIGNATURE_VALUE_TEMPLATE - ); - - $request = $this->createBaseRequest(); - $templateResolver = $this->createTemplateResolver(); - $signingData = $templateResolver($request); - - $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); - $digest = bin2hex($hash); - $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); - - $request->headers->set('X-Signature', $signature); - - $result = $verifier->verify($request); - - $this->assertTrue($result); - } - - public function testShouldPassVerificationForValidSignatureWithoutSignatureValueTemplate(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver() - ); - - $request = $this->createBaseRequest(); - $templateResolver = $this->createTemplateResolver(); - $signingData = $templateResolver($request); - - $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); - $digest = bin2hex($hash); - - $request->headers->set('X-Signature', $digest); - - $result = $verifier->verify($request); - - $this->assertTrue($result); - } - - public function testShouldSupportSha512AndBase64Encoding(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - 'sha512', - 'base64' - ); - - $request = $this->createBaseRequest(); - $templateResolver = $this->createTemplateResolver(); - $signingData = $templateResolver($request); - - $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); - $digest = base64_encode($hash); - - $request->headers->set('X-Signature', $digest); - - $result = $verifier->verify($request); - - $this->assertTrue($result); - } - - public function testShouldSupportHexEncoding(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - 'sha512', - 'hex' - ); - - $request = $this->createBaseRequest(); - $templateResolver = $this->createTemplateResolver(); - $signingData = $templateResolver($request); - - $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); - $digest = bin2hex($hash); - - $request->headers->set('X-Signature', $digest); - - $result = $verifier->verify($request); - - $this->assertTrue($result); - } - - public function testShouldSupportBase64urlEncoding(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - 'sha512', - 'base64url' - ); - - $request = $this->createBaseRequest(); - $templateResolver = $this->createTemplateResolver(); - $signingData = $templateResolver($request); - - $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); - $base64 = base64_encode($hash); - $digest = rtrim(strtr($base64, '+/', '-_'), '='); - - $request->headers->set('X-Signature', $digest); - - $result = $verifier->verify($request); - - $this->assertTrue($result); - } - - public function testShouldUseCustomSignatureValueTemplate(): void - { - $customTemplate = 'sig:{digest}:end'; - - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - self::HMAC_ALGORITHM, - self::ENCODING, - $customTemplate - ); - - $request = $this->createBaseRequest(); - $templateResolver = $this->createTemplateResolver(); - $signingData = $templateResolver($request); - - $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); - $digest = bin2hex($hash); - $signature = str_replace('{digest}', $digest, $customTemplate); - - $request->headers->set('X-Signature', $signature); - - $result = $verifier->verify($request); - - $this->assertTrue($result); - } - - public function testShouldFailIfDifferentSecretKeyIsGiven(): void - { - $verifier = new HmacSignatureVerifier( - 'different-key', - self::SIGNATURE_HEADER, - $this->createTemplateResolver(), - self::HMAC_ALGORITHM, - self::ENCODING, - self::SIGNATURE_VALUE_TEMPLATE - ); - - $request = $this->createBaseRequest(); - $templateResolver = $this->createTemplateResolver(); - $signingData = $templateResolver($request); - - // Sign with original secret key - $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); - $digest = bin2hex($hash); - $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); - - $request->headers->set('X-Signature', $signature); - - $result = $verifier->verify($request); - - $this->assertInstanceOf(VerificationFailure::class, $result); - $this->assertEquals('Signature mismatch', $result->getErrorMessage()); - } - - public function testShouldUseBodyToSignIfTemplateResolverIsNotProvided(): void - { - $verifier = new HmacSignatureVerifier( - 'different-key', - self::SIGNATURE_HEADER, - null, - self::HMAC_ALGORITHM, - self::ENCODING, - self::SIGNATURE_VALUE_TEMPLATE - ); - - $request = $this->createBaseRequest(); - $body = $request->getContent(); - - // Sign with original secret key (different from verifier's key) - $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); - $digest = bin2hex($hash); - $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); - - $request->headers->set('X-Signature', $signature); - - $result = $verifier->verify($request); - - $this->assertInstanceOf(VerificationFailure::class, $result); - $this->assertEquals('Signature mismatch', $result->getErrorMessage()); - } - - public function testShouldHandleVerificationWhenBothTemplateResolverAndBodyAreUndefined(): void - { - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - null, - self::HMAC_ALGORITHM, - self::ENCODING, - self::SIGNATURE_VALUE_TEMPLATE - ); - - $request = $this->createBaseRequest([], ''); - - $signingData = ''; - $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); - $digest = bin2hex($hash); - $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); - - $request->headers->set('X-Signature', $signature); - - $result = $verifier->verify($request); - - $this->assertTrue($result); - } - - public function testShouldFallbackToBodyIfTemplateResolverReturnsNull(): void - { - $templateResolver = function (Request $request): ?string { - return null; - }; - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $templateResolver, - self::HMAC_ALGORITHM, - self::ENCODING, - self::SIGNATURE_VALUE_TEMPLATE - ); - $request = $this->createBaseRequest(); - $body = $request->getContent(); - $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); - $digest = bin2hex($hash); - $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); - $request->headers->set('X-Signature', $signature); - $result = $verifier->verify($request); - $this->assertTrue($result); - } - - public function testShouldFallbackToBodyIfTemplateResolverReturnsEmptyString(): void - { - $templateResolver = function (Request $request): string { - return ''; - }; - $verifier = new HmacSignatureVerifier( - self::SECRET_KEY, - self::SIGNATURE_HEADER, - $templateResolver, - self::HMAC_ALGORITHM, - self::ENCODING, - self::SIGNATURE_VALUE_TEMPLATE - ); - $request = $this->createBaseRequest(); - $body = $request->getContent(); - $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); - $digest = bin2hex($hash); - $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); - $request->headers->set('X-Signature', $signature); - $result = $verifier->verify($request); - $this->assertTrue($result); - } + public function testShouldFailVerificationIfSignatureHeaderIsMissing(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver() + ); + + $request = $this->createBaseRequest(['X-Signature' => null]); + $request->headers->remove('X-Signature'); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Missing signature header', $result->getErrorMessage()); + } + + public function testShouldFailVerificationIfSignatureDoesNotMatch(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver() + ); + + $request = $this->createBaseRequest(['X-Signature' => 'sha256=invalidsignature']); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Signature mismatch', $result->getErrorMessage()); + } + + public function testShouldPassVerificationForValidSignature(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldPassVerificationForValidSignatureWithoutSignatureValueTemplate(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver() + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldSupportSha512AndBase64Encoding(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'sha512', + 'base64' + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); + $digest = base64_encode($hash); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldSupportHexEncoding(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'sha512', + 'hex' + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldSupportBase64urlEncoding(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + 'sha512', + 'base64url' + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac('sha512', $signingData, self::SECRET_KEY, true); + $base64 = base64_encode($hash); + $digest = rtrim(strtr($base64, '+/', '-_'), '='); + + $request->headers->set('X-Signature', $digest); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldUseCustomSignatureValueTemplate(): void + { + $customTemplate = 'sig:{digest}:end'; + + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + self::ENCODING, + $customTemplate + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, $customTemplate); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldFailIfDifferentSecretKeyIsGiven(): void + { + $verifier = new HmacSignatureVerifier( + 'different-key', + self::SIGNATURE_HEADER, + $this->createTemplateResolver(), + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest(); + $templateResolver = $this->createTemplateResolver(); + $signingData = $templateResolver($request); + + // Sign with original secret key + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Signature mismatch', $result->getErrorMessage()); + } + + public function testShouldUseBodyToSignIfTemplateResolverIsNotProvided(): void + { + $verifier = new HmacSignatureVerifier( + 'different-key', + self::SIGNATURE_HEADER, + null, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest(); + $body = $request->getContent(); + + // Sign with original secret key (different from verifier's key) + $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertEquals('Signature mismatch', $result->getErrorMessage()); + } + + public function testShouldHandleVerificationWhenBothTemplateResolverAndBodyAreUndefined(): void + { + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + null, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + + $request = $this->createBaseRequest([], ''); + + $signingData = ''; + $hash = hash_hmac(self::HMAC_ALGORITHM, $signingData, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + + $request->headers->set('X-Signature', $signature); + + $result = $verifier->verify($request); + + $this->assertTrue($result); + } + + public function testShouldFallbackToBodyIfTemplateResolverReturnsNull(): void + { + $templateResolver = function (Request $request): ?string { + return null; + }; + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $templateResolver, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + $request = $this->createBaseRequest(); + $body = $request->getContent(); + $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + $request->headers->set('X-Signature', $signature); + $result = $verifier->verify($request); + $this->assertTrue($result); + } + + public function testShouldFallbackToBodyIfTemplateResolverReturnsEmptyString(): void + { + $templateResolver = function (Request $request): string { + return ''; + }; + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + self::SIGNATURE_HEADER, + $templateResolver, + self::HMAC_ALGORITHM, + self::ENCODING, + self::SIGNATURE_VALUE_TEMPLATE + ); + $request = $this->createBaseRequest(); + $body = $request->getContent(); + $hash = hash_hmac(self::HMAC_ALGORITHM, $body, self::SECRET_KEY, true); + $digest = bin2hex($hash); + $signature = str_replace('{digest}', $digest, self::SIGNATURE_VALUE_TEMPLATE); + $request->headers->set('X-Signature', $signature); + $result = $verifier->verify($request); + $this->assertTrue($result); + } } From fe65c075909e71a9a1ad2d86e73ab8a6ce057a27 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 12:09:21 +0500 Subject: [PATCH 04/14] fix formatting --- src/SignatureVerifier/HmacSignatureVerifier.php | 1 - src/SignatureVerifier/SignatureVerifierInterface.php | 1 - src/Types/Sdk/VerificationFailure.php | 1 - tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php | 1 - 4 files changed, 4 deletions(-) diff --git a/src/SignatureVerifier/HmacSignatureVerifier.php b/src/SignatureVerifier/HmacSignatureVerifier.php index 0394c89..7cc1115 100644 --- a/src/SignatureVerifier/HmacSignatureVerifier.php +++ b/src/SignatureVerifier/HmacSignatureVerifier.php @@ -8,7 +8,6 @@ use Core\Types\Sdk\VerificationFailure; use Core\SignatureVerifier\SignatureVerifierInterface; - class HmacSignatureVerifier implements SignatureVerifierInterface { private const VALID_ENCODINGS = ['hex', 'base64', 'base64url']; diff --git a/src/SignatureVerifier/SignatureVerifierInterface.php b/src/SignatureVerifier/SignatureVerifierInterface.php index 88a4cbd..613ff2f 100644 --- a/src/SignatureVerifier/SignatureVerifierInterface.php +++ b/src/SignatureVerifier/SignatureVerifierInterface.php @@ -6,7 +6,6 @@ use Symfony\Component\HttpFoundation\Request; - interface SignatureVerifierInterface { /** diff --git a/src/Types/Sdk/VerificationFailure.php b/src/Types/Sdk/VerificationFailure.php index 1aa1547..acc4b5d 100644 --- a/src/Types/Sdk/VerificationFailure.php +++ b/src/Types/Sdk/VerificationFailure.php @@ -4,7 +4,6 @@ namespace Core\Types\Sdk; - class VerificationFailure { private string $errorMessage; diff --git a/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php index 6594d08..7d0199a 100644 --- a/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php +++ b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php @@ -9,7 +9,6 @@ use Core\SignatureVerifier\HmacSignatureVerifier; use Core\Types\Sdk\VerificationFailure; - class HmacSignatureVerifierTest extends TestCase { private const SECRET_KEY = 'test_secret'; From 86814fb864b6b1c80b464fe4470f690691e3e6b9 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 12:26:36 +0500 Subject: [PATCH 05/14] fix: update phan confi --- .phan/config.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.phan/config.php b/.phan/config.php index bbba420..77c8555 100644 --- a/.phan/config.php +++ b/.phan/config.php @@ -31,7 +31,8 @@ $vendor_dir . '/apimatic/jsonmapper', $vendor_dir . '/phpunit/phpunit', $vendor_dir . '/php-jsonpointer/php-jsonpointer', - $vendor_dir . '/psr/log' + $vendor_dir . '/psr/log', + $vendor_dir . '/symfony/http-foundation' ], // A directory list that defines files that will be excluded From b6fb97c0c8f16e61717347604edf45d39b4498d5 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 12:32:29 +0500 Subject: [PATCH 06/14] fix: resolve compatibility errors --- src/SignatureVerifier/SignatureVerifierInterface.php | 3 ++- src/Types/Sdk/VerificationFailure.php | 2 +- .../SignatureVerifier/HmacSignatureVerifierTest.php | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/SignatureVerifier/SignatureVerifierInterface.php b/src/SignatureVerifier/SignatureVerifierInterface.php index 613ff2f..b5266b1 100644 --- a/src/SignatureVerifier/SignatureVerifierInterface.php +++ b/src/SignatureVerifier/SignatureVerifierInterface.php @@ -5,6 +5,7 @@ namespace Core\SignatureVerifier; use Symfony\Component\HttpFoundation\Request; +use Core\Types\Sdk\VerificationFailure; interface SignatureVerifierInterface { @@ -12,7 +13,7 @@ interface SignatureVerifierInterface * Verifies the signature of a request. * * @param Request $request - * @return VerificationFailure | true + * @return VerificationFailure|true */ public function verify(Request $request); } diff --git a/src/Types/Sdk/VerificationFailure.php b/src/Types/Sdk/VerificationFailure.php index acc4b5d..0eda741 100644 --- a/src/Types/Sdk/VerificationFailure.php +++ b/src/Types/Sdk/VerificationFailure.php @@ -6,7 +6,7 @@ class VerificationFailure { - private string $errorMessage; + private $errorMessage; public function __construct(string $errorMessage) { diff --git a/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php index 7d0199a..f3e2388 100644 --- a/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php +++ b/tests/Mocking/SignatureVerifier/HmacSignatureVerifierTest.php @@ -69,21 +69,21 @@ public function testShouldThrowErrorForEmptySecretKey(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('secretKey must be a non-empty string'); - new HmacSignatureVerifier('', self::SIGNATURE_HEADER, $this->createTemplateResolver()); + $verifier = new HmacSignatureVerifier('', self::SIGNATURE_HEADER, $this->createTemplateResolver()); } public function testShouldThrowErrorForEmptySignatureHeader(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('signatureHeader must be a non-empty string'); - new HmacSignatureVerifier(self::SECRET_KEY, '', $this->createTemplateResolver()); + $verifier = new HmacSignatureVerifier(self::SECRET_KEY, '', $this->createTemplateResolver()); } public function testShouldThrowErrorForInvalidAlgorithm(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('algorithm must be one of'); - new HmacSignatureVerifier( + $verifier = new HmacSignatureVerifier( self::SECRET_KEY, self::SIGNATURE_HEADER, $this->createTemplateResolver(), @@ -95,7 +95,7 @@ public function testShouldThrowErrorForInvalidEncoding(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('encoding must be one of'); - new HmacSignatureVerifier( + $verifier = new HmacSignatureVerifier( self::SECRET_KEY, self::SIGNATURE_HEADER, $this->createTemplateResolver(), From ae7a840ef0d20303fbcb2b03b3424e7f45aebb7e Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 13:28:33 +0500 Subject: [PATCH 07/14] feat: add signatureVerificationFailureCreator to HmacSignatureVerifier --- .../HmacSignatureVerifier.php | 7 +++++-- .../VerificationFailure.php | 7 +------ .../MockVerificationFailure.php | 15 +++++++++++++++ ...ifierTest.php => SignatureVerifierTest.php} | 18 +++++++++++------- 4 files changed, 32 insertions(+), 15 deletions(-) rename src/{Types/Sdk => SignatureVerifier}/VerificationFailure.php (63%) create mode 100644 tests/Mocking/SignatureVerifier/MockVerificationFailure.php rename tests/{Mocking/SignatureVerifier/HmacSignatureVerifierTest.php => SignatureVerifierTest.php} (95%) diff --git a/src/SignatureVerifier/HmacSignatureVerifier.php b/src/SignatureVerifier/HmacSignatureVerifier.php index 7cc1115..03c0795 100644 --- a/src/SignatureVerifier/HmacSignatureVerifier.php +++ b/src/SignatureVerifier/HmacSignatureVerifier.php @@ -14,6 +14,7 @@ class HmacSignatureVerifier implements SignatureVerifierInterface private const VALID_ALGORITHMS = ['sha256', 'sha512']; private $secretKey; + private $signatureVerificationFailureCreator; private $signatureHeader; private $algorithm; private $encoding; @@ -22,6 +23,7 @@ class HmacSignatureVerifier implements SignatureVerifierInterface public function __construct( string $secretKey, + callable $signatureVerificationFailureCreator, string $signatureHeader, ?callable $templateResolver = null, string $algorithm = 'sha256', @@ -42,6 +44,7 @@ public function __construct( } $this->secretKey = $secretKey; + $this->signatureVerificationFailureCreator = $signatureVerificationFailureCreator; $this->signatureHeader = $signatureHeader; $this->algorithm = strtolower($algorithm); $this->encoding = strtolower($encoding); @@ -58,7 +61,7 @@ public function verify(Request $request) $receivedSignature = $request->headers->get($this->signatureHeader); if ($receivedSignature === null) { - return VerificationFailure::init('Missing signature header'); + return call_user_func($this->signatureVerificationFailureCreator, 'Missing signature header'); } if ($this->templateResolver !== null) { @@ -79,7 +82,7 @@ public function verify(Request $request) } if (!hash_equals($expectedSignature, $receivedSignature)) { - return VerificationFailure::init('Signature mismatch'); + return call_user_func($this->signatureVerificationFailureCreator, 'Signature mismatch'); } return true; diff --git a/src/Types/Sdk/VerificationFailure.php b/src/SignatureVerifier/VerificationFailure.php similarity index 63% rename from src/Types/Sdk/VerificationFailure.php rename to src/SignatureVerifier/VerificationFailure.php index 0eda741..acadb0a 100644 --- a/src/Types/Sdk/VerificationFailure.php +++ b/src/SignatureVerifier/VerificationFailure.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Core\Types\Sdk; +namespace Core\SignatureVerifier; class VerificationFailure { @@ -13,11 +13,6 @@ public function __construct(string $errorMessage) $this->errorMessage = $errorMessage; } - public static function init(string $errorMessage): VerificationFailure - { - return new VerificationFailure($errorMessage); - } - public function getErrorMessage(): string { return $this->errorMessage; diff --git a/tests/Mocking/SignatureVerifier/MockVerificationFailure.php b/tests/Mocking/SignatureVerifier/MockVerificationFailure.php new file mode 100644 index 0000000..4db92f1 --- /dev/null +++ b/tests/Mocking/SignatureVerifier/MockVerificationFailure.php @@ -0,0 +1,15 @@ +verify($request); - $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertInstanceOf(MockVerificationFailure::class, $result); $this->assertEquals('Missing signature header', $result->getErrorMessage()); } @@ -133,7 +133,7 @@ public function testShouldFailVerificationIfSignatureDoesNotMatch(): void $result = $verifier->verify($request); - $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertInstanceOf(MockVerificationFailure::class, $result); $this->assertEquals('Signature mismatch', $result->getErrorMessage()); } @@ -310,7 +310,7 @@ public function testShouldFailIfDifferentSecretKeyIsGiven(): void $result = $verifier->verify($request); - $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertInstanceOf(MockVerificationFailure::class, $result); $this->assertEquals('Signature mismatch', $result->getErrorMessage()); } @@ -318,6 +318,7 @@ public function testShouldUseBodyToSignIfTemplateResolverIsNotProvided(): void { $verifier = new HmacSignatureVerifier( 'different-key', + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, null, self::HMAC_ALGORITHM, @@ -337,7 +338,7 @@ public function testShouldUseBodyToSignIfTemplateResolverIsNotProvided(): void $result = $verifier->verify($request); - $this->assertInstanceOf(VerificationFailure::class, $result); + $this->assertInstanceOf(MockVerificationFailure::class, $result); $this->assertEquals('Signature mismatch', $result->getErrorMessage()); } @@ -345,6 +346,7 @@ public function testShouldHandleVerificationWhenBothTemplateResolverAndBodyAreUn { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, null, self::HMAC_ALGORITHM, @@ -373,6 +375,7 @@ public function testShouldFallbackToBodyIfTemplateResolverReturnsNull(): void }; $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $templateResolver, self::HMAC_ALGORITHM, @@ -396,6 +399,7 @@ public function testShouldFallbackToBodyIfTemplateResolverReturnsEmptyString(): }; $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $templateResolver, self::HMAC_ALGORITHM, From b83b2c5604843b86b1b5ef57a388a37ef9d1aad6 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 14:29:23 +0500 Subject: [PATCH 08/14] fix: resolve import --- src/SignatureVerifier/HmacSignatureVerifier.php | 2 +- tests/SignatureVerifierTest.php | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/SignatureVerifier/HmacSignatureVerifier.php b/src/SignatureVerifier/HmacSignatureVerifier.php index 03c0795..2e2d2a4 100644 --- a/src/SignatureVerifier/HmacSignatureVerifier.php +++ b/src/SignatureVerifier/HmacSignatureVerifier.php @@ -5,7 +5,7 @@ namespace Core\SignatureVerifier; use Symfony\Component\HttpFoundation\Request; -use Core\Types\Sdk\VerificationFailure; +use Core\SignatureVerifier\VerificationFailure; use Core\SignatureVerifier\SignatureVerifierInterface; class HmacSignatureVerifier implements SignatureVerifierInterface diff --git a/tests/SignatureVerifierTest.php b/tests/SignatureVerifierTest.php index c2feaee..ea775ae 100644 --- a/tests/SignatureVerifierTest.php +++ b/tests/SignatureVerifierTest.php @@ -69,14 +69,14 @@ public function testShouldThrowErrorForEmptySecretKey(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('secretKey must be a non-empty string'); - $verifier = new HmacSignatureVerifier('', self::SIGNATURE_HEADER, $this->createTemplateResolver()); + $verifier = new HmacSignatureVerifier('', [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver()); } public function testShouldThrowErrorForEmptySignatureHeader(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('signatureHeader must be a non-empty string'); - $verifier = new HmacSignatureVerifier(self::SECRET_KEY, '', $this->createTemplateResolver()); + $verifier = new HmacSignatureVerifier(self::SECRET_KEY, [MockVerificationFailure::class, 'init'], '', $this->createTemplateResolver()); } public function testShouldThrowErrorForInvalidAlgorithm(): void @@ -85,6 +85,7 @@ public function testShouldThrowErrorForInvalidAlgorithm(): void $this->expectExceptionMessage('algorithm must be one of'); $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), 'invalid' @@ -97,6 +98,7 @@ public function testShouldThrowErrorForInvalidEncoding(): void $this->expectExceptionMessage('encoding must be one of'); $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), self::HMAC_ALGORITHM, @@ -108,6 +110,7 @@ public function testShouldFailVerificationIfSignatureHeaderIsMissing(): void { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver() ); @@ -125,6 +128,7 @@ public function testShouldFailVerificationIfSignatureDoesNotMatch(): void { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver() ); @@ -141,6 +145,7 @@ public function testShouldPassVerificationForValidSignature(): void { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), self::HMAC_ALGORITHM, @@ -167,6 +172,7 @@ public function testShouldPassVerificationForValidSignatureWithoutSignatureValue { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver() ); @@ -189,6 +195,7 @@ public function testShouldSupportSha512AndBase64Encoding(): void { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), 'sha512', @@ -213,6 +220,7 @@ public function testShouldSupportHexEncoding(): void { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), 'sha512', @@ -237,6 +245,7 @@ public function testShouldSupportBase64urlEncoding(): void { $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), 'sha512', @@ -264,6 +273,7 @@ public function testShouldUseCustomSignatureValueTemplate(): void $verifier = new HmacSignatureVerifier( self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), self::HMAC_ALGORITHM, @@ -290,6 +300,7 @@ public function testShouldFailIfDifferentSecretKeyIsGiven(): void { $verifier = new HmacSignatureVerifier( 'different-key', + [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver(), self::HMAC_ALGORITHM, From b0f8b9afd716b218ecb8b2ae4c5514489aae4789 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 14:33:04 +0500 Subject: [PATCH 09/14] fix: styles issues --- tests/SignatureVerifierTest.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/SignatureVerifierTest.php b/tests/SignatureVerifierTest.php index ea775ae..1e49428 100644 --- a/tests/SignatureVerifierTest.php +++ b/tests/SignatureVerifierTest.php @@ -69,14 +69,24 @@ public function testShouldThrowErrorForEmptySecretKey(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('secretKey must be a non-empty string'); - $verifier = new HmacSignatureVerifier('', [MockVerificationFailure::class, 'init'], self::SIGNATURE_HEADER, $this->createTemplateResolver()); + $verifier = new HmacSignatureVerifier( + '', + [MockVerificationFailure::class, 'init'], + self::SIGNATURE_HEADER, + $this->createTemplateResolver() + ); } public function testShouldThrowErrorForEmptySignatureHeader(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('signatureHeader must be a non-empty string'); - $verifier = new HmacSignatureVerifier(self::SECRET_KEY, [MockVerificationFailure::class, 'init'], '', $this->createTemplateResolver()); + $verifier = new HmacSignatureVerifier( + self::SECRET_KEY, + [MockVerificationFailure::class, 'init'], + '', + $this->createTemplateResolver() + ); } public function testShouldThrowErrorForInvalidAlgorithm(): void From 17a6f77cb2fc49a4ee6288ad934e8b97d7ec81fe Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 14:36:37 +0500 Subject: [PATCH 10/14] fix: resolve import --- src/SignatureVerifier/SignatureVerifierInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SignatureVerifier/SignatureVerifierInterface.php b/src/SignatureVerifier/SignatureVerifierInterface.php index b5266b1..f6e884f 100644 --- a/src/SignatureVerifier/SignatureVerifierInterface.php +++ b/src/SignatureVerifier/SignatureVerifierInterface.php @@ -5,7 +5,7 @@ namespace Core\SignatureVerifier; use Symfony\Component\HttpFoundation\Request; -use Core\Types\Sdk\VerificationFailure; +use Core\SignatureVerifier\VerificationFailure; interface SignatureVerifierInterface { From 37f772e5606f74b661fd383ddc51bae1cb6afe8f Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 19:39:17 +0500 Subject: [PATCH 11/14] feat: add getJsonPointerValue utility --- src/Response/Types/ErrorType.php | 40 +--- src/Utils/JsonPointerValue.php | 36 ++++ tests/JsonPointerValueTest.php | 307 +++++++++++++++++++++++++++++++ 3 files changed, 345 insertions(+), 38 deletions(-) create mode 100644 src/Utils/JsonPointerValue.php create mode 100644 tests/JsonPointerValueTest.php diff --git a/src/Response/Types/ErrorType.php b/src/Response/Types/ErrorType.php index 82fb5c7..ff4b218 100644 --- a/src/Response/Types/ErrorType.php +++ b/src/Response/Types/ErrorType.php @@ -5,9 +5,8 @@ namespace Core\Response\Types; use Core\Response\Context; -use Core\Utils\CoreHelper; +use Core\Utils\JsonPointerValue; use CoreInterfaces\Core\Response\ResponseInterface; -use Rs\Json\Pointer; class ErrorType { @@ -111,12 +110,10 @@ private function updateResponsePlaceholderValues( ); } - $jsonResponsePointer = $this->initializeJsonPointer($response); - $jsonPointers = $jsonPointersInTemplate[0]; for ($x = 0; $x < count($jsonPointers); $x++) { - $placeHolderValue = $this->getJsonPointerValue($jsonResponsePointer, ltrim($jsonPointers[$x], '#')); + $placeHolderValue = JsonPointerValue::getJsonPointerValue($response->getRawBody(), ltrim($jsonPointers[$x], '#')); $errorDescription = $this->addPlaceHolderValue( $errorDescription, @@ -153,37 +150,4 @@ private function addPlaceHolderValue( return str_replace($placeHolder, $value, $template); } - - /** - * @param $jsonPointer ?Pointer - * @param $pointer string - * @return mixed Json pointer value from the JSON provided. - */ - private function getJsonPointerValue(?Pointer $jsonPointer, string $pointer) - { - if ($jsonPointer == null || trim($pointer) === '') { - return ""; - } - - try { - $pointerValue = $jsonPointer->get($pointer); - - if (is_object($pointerValue)) { - return CoreHelper::serialize($pointerValue); - } - - return $pointerValue; - } catch (\Exception $ex) { - return ""; - } - } - - private function initializeJsonPointer(ResponseInterface $response): ?Pointer - { - try { - return new Pointer($response->getRawBody()); - } catch (\Exception $ex) { - return null; - } - } } diff --git a/src/Utils/JsonPointerValue.php b/src/Utils/JsonPointerValue.php new file mode 100644 index 0000000..f429bb7 --- /dev/null +++ b/src/Utils/JsonPointerValue.php @@ -0,0 +1,36 @@ +get($pointer); + + if (is_object($pointerValue)) { + return CoreHelper::serialize($pointerValue); + } + + return $pointerValue; + } catch (\Exception $ex) { + return ""; + } + } +} diff --git a/tests/JsonPointerValueTest.php b/tests/JsonPointerValueTest.php new file mode 100644 index 0000000..bd0e48a --- /dev/null +++ b/tests/JsonPointerValueTest.php @@ -0,0 +1,307 @@ +testData = json_encode([ + 'name' => 'John Doe', + 'age' => 30, + 'email' => 'john@example.com', + 'address' => [ + 'street' => '123 Main St', + 'city' => 'New York', + 'country' => 'USA' + ], + 'tags' => ['developer', 'php', 'testing'], + 'active' => true, + 'score' => 95.5, + 'metadata' => null + ]); + } + + /** + * Test getting a simple string value + */ + public function testGetSimpleStringValue() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/name'); + + $this->assertEquals('John Doe', $result); + } + + /** + * Test getting an integer value + */ + public function testGetIntegerValue() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/age'); + + $this->assertEquals(30, $result); + } + + /** + * Test getting a boolean value + */ + public function testGetBooleanValue() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/active'); + + $this->assertTrue($result); + } + + /** + * Test getting a float value + */ + public function testGetFloatValue() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/score'); + + $this->assertEquals(95.5, $result); + } + + /** + * Test getting a null value + */ + public function testGetNullValue() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/metadata'); + + $this->assertNull($result); + } + + /** + * Test getting a nested value + */ + public function testGetNestedValue() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/address/city'); + + $this->assertEquals('New York', $result); + } + + /** + * Test getting an array value + */ + public function testGetArrayValue() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/tags'); + + $this->assertIsArray($result); + $this->assertEquals(['developer', 'php', 'testing'], $result); + } + + /** + * Test getting a specific array element + */ + public function testGetArrayElement() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/tags/0'); + + $this->assertEquals('developer', $result); + } + + /** + * Test with empty pointer string + */ + public function testEmptyPointerString() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, ''); + + $this->assertEquals('', $result); + } + + /** + * Test with whitespace-only pointer string + */ + public function testWhitespaceOnlyPointerString() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, ' '); + + $this->assertEquals('', $result); + } + + /** + * Test with empty json string + */ + public function testEmptyJsonString() + { + $result = JsonPointerValue::getJsonPointerValue('', '/any'); + + $this->assertEquals('', $result); + } + + /** + * Test with whitespace-only json string + */ + public function testWhitespaceOnlyJsonString() + { + $result = JsonPointerValue::getJsonPointerValue(' ', '/any'); + + $this->assertEquals('', $result); + } + + /** + * Test with both empty json and empty pointer + */ + public function testEmptyJsonAndEmptyPointer() + { + $result = JsonPointerValue::getJsonPointerValue('', ''); + + $this->assertEquals('', $result); + } + + /** + * Test with invalid pointer that doesn't exist + */ + public function testInvalidPointerPath() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/nonexistent'); + + $this->assertEquals('', $result); + } + + /** + * Test with invalid nested pointer + */ + public function testInvalidNestedPointerPath() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/address/zipcode'); + + $this->assertEquals('', $result); + } + + /** + * Test with malformed pointer (missing leading slash) + */ + public function testMalformedPointer() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, 'name'); + + $this->assertEquals('', $result); + } + + /** + * Test getting an object value (should be serialized) + */ + public function testGetObjectValueSerialization() + { + $objectData = json_encode([ + 'user' => (object)[ + 'name' => 'Jane', + 'role' => 'admin' + ] + ]); + + $result = JsonPointerValue::getJsonPointerValue($objectData, '/user'); + $this->assertNotEmpty($result); + } + + /** + * Test with array index out of bounds + */ + public function testArrayIndexOutOfBounds() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/tags/999'); + + $this->assertEquals('', $result); + } + + /** + * Test with negative array index + */ + public function testNegativeArrayIndex() + { + $result = JsonPointerValue::getJsonPointerValue($this->testData, '/tags/-1'); + + $this->assertEquals('', $result); + } + + /** + * Test with deeply nested valid pointer + */ + public function testDeeplyNestedPointer() + { + $deepData = json_encode([ + 'level1' => [ + 'level2' => [ + 'level3' => [ + 'value' => 'deep value' + ] + ] + ] + ]); + + $result = JsonPointerValue::getJsonPointerValue($deepData, '/level1/level2/level3/value'); + + $this->assertEquals('deep value', $result); + } + + /** + * Test with special characters in pointer path + */ + public function testSpecialCharactersInPath() + { + $specialData = json_encode([ + 'key~with~tildes' => 'value1', + 'key/with/slashes' => 'value2' + ]); + + $result = JsonPointerValue::getJsonPointerValue($specialData, '/key~0with~0tildes'); + + $this->assertEquals('value1', $result); + } + + /** + * Test with zero value + */ + public function testZeroValue() + { + $zeroData = json_encode(['count' => 0]); + + $result = JsonPointerValue::getJsonPointerValue($zeroData, '/count'); + + $this->assertEquals(0, $result); + } + + /** + * Test with empty string value + */ + public function testEmptyStringValue() + { + $emptyData = json_encode(['description' => '']); + $result = JsonPointerValue::getJsonPointerValue($emptyData, '/description'); + + $this->assertEquals('', $result); + } + + /** + * Test with false boolean value + */ + public function testFalseBooleanValue() + { + $falseData = json_encode(['enabled' => false]); + $result = JsonPointerValue::getJsonPointerValue($falseData, '/enabled'); + + $this->assertFalse($result); + } + + /** + * Test that getJsonPointerValue returns empty string when Pointer throws (invalid JSON) + */ + public function testReturnsEmptyStringOnPointerException() + { + $invalidJson = '{invalid json'; + $result = JsonPointerValue::getJsonPointerValue($invalidJson, '/any'); + $this->assertSame('', $result); + } +} From 3a5dccecb3407aa5005c8ecebaf585dd95d0a378 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 19:41:32 +0500 Subject: [PATCH 12/14] fix formatting issue --- src/Response/Types/ErrorType.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Response/Types/ErrorType.php b/src/Response/Types/ErrorType.php index ff4b218..3f57669 100644 --- a/src/Response/Types/ErrorType.php +++ b/src/Response/Types/ErrorType.php @@ -113,7 +113,10 @@ private function updateResponsePlaceholderValues( $jsonPointers = $jsonPointersInTemplate[0]; for ($x = 0; $x < count($jsonPointers); $x++) { - $placeHolderValue = JsonPointerValue::getJsonPointerValue($response->getRawBody(), ltrim($jsonPointers[$x], '#')); + $placeHolderValue = JsonPointerValue::getJsonPointerValue( + $response->getRawBody(), + ltrim($jsonPointers[$x], '#') + ); $errorDescription = $this->addPlaceHolderValue( $errorDescription, From 34b2c20d4ed1463029d371750c88a84dc4ab05e2 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 19:47:16 +0500 Subject: [PATCH 13/14] refactor: fix sonarQube issue --- src/Utils/JsonPointerValue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils/JsonPointerValue.php b/src/Utils/JsonPointerValue.php index f429bb7..f865d75 100644 --- a/src/Utils/JsonPointerValue.php +++ b/src/Utils/JsonPointerValue.php @@ -25,7 +25,7 @@ public static function getJsonPointerValue(string $jsonObj, string $pointer) $pointerValue = $jsonPointer->get($pointer); if (is_object($pointerValue)) { - return CoreHelper::serialize($pointerValue); + $jsonPointer = CoreHelper::serialize($pointerValue); } return $pointerValue; From a360e29f20557aa7598701ac0e290aadf0778a28 Mon Sep 17 00:00:00 2001 From: Ayeshas09 Date: Wed, 5 Nov 2025 19:56:31 +0500 Subject: [PATCH 14/14] fix: resolve failing test --- src/Utils/JsonPointerValue.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils/JsonPointerValue.php b/src/Utils/JsonPointerValue.php index f865d75..76243c2 100644 --- a/src/Utils/JsonPointerValue.php +++ b/src/Utils/JsonPointerValue.php @@ -25,7 +25,7 @@ public static function getJsonPointerValue(string $jsonObj, string $pointer) $pointerValue = $jsonPointer->get($pointer); if (is_object($pointerValue)) { - $jsonPointer = CoreHelper::serialize($pointerValue); + $pointerValue = CoreHelper::serialize($pointerValue); } return $pointerValue;