From 87850ef9100607eaea66c981f7f6644748d2e914 Mon Sep 17 00:00:00 2001 From: Nikita Chistyakov Date: Fri, 11 Sep 2020 18:10:57 +0300 Subject: [PATCH] bug fixed, tests upd, readme upd --- README.md | 44 ++++++++++++++++++++++++++++++++++ src/UrlSecurityManager.php | 4 ++-- tests/unit/CommonTest.php | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e4cdcea..2821f79 100644 --- a/README.md +++ b/README.md @@ -96,4 +96,48 @@ echo $usmSender->stringify(); $usmSender->decrypt(); echo $usmSender->stringify(); // http://localhost:8080/test/path?p3=3&p4=4&p1=1&p2=2 +``` + +##### Building URLs +```php +use Smoren\UrlSecurityManager\UrlSecurityManager; + +$usm = UrlSecurityManager::create() + ->setScheme('https') + ->setHost('test.com') + ->setPort(8080) + ->setPath('/test/path') + ->setParams(['a' => 1, 'b' => 2]); + +echo $usm->stringify(); +// https://test.com:8080/test/path?a=1&b=2 + +$usm + ->setSignParams('sign') + ->setSecretKey('q1w2e3r4t5y6u7') + ->sign(); + +echo $usm->stringify(); +// 'https://test.com:8080/test/path?a=1&b=2&sign=89727a40dc08dc9f12d91b5d6e627c17' + +$usm = UrlSecurityManager::create([ + 'scheme' => 'http', + 'host' => 'test.com', + 'port' => 8080, + 'path' => '/test/path', + 'params' => ['a' => 1, 'b' => 2], +]); + +echo $usm->stringify(); +// 'http://test.com:8080/test/path?a=1&b=2&sign=89727a40dc08dc9f12d91b5d6e627c17' +``` + +##### Parse URL from server request +```php +use Smoren\UrlSecurityManager\UrlSecurityManager; + +$usm = UrlSecurityManager::parse(); + +echo $usm->stringify(); +// you will see full URL of your current server request ``` \ No newline at end of file diff --git a/src/UrlSecurityManager.php b/src/UrlSecurityManager.php index 7775cf3..233074f 100644 --- a/src/UrlSecurityManager.php +++ b/src/UrlSecurityManager.php @@ -97,9 +97,9 @@ class UrlSecurityManager public static function parse(?string $url = null): self { if($url === null) { - $port = static::getPortSubstring($_SERVER['SERVER_PORT']); + $port = static::getPortSubstring($_SERVER['REQUEST_SCHEME'], $_SERVER['SERVER_PORT']); $auth = static::getAuthSubstring($_SERVER['PHP_AUTH_USER'] ?? null, $_SERVER['PHP_AUTH_PW'] ?? null); - $url = "{$_SERVER['REQUEST_SCHEME']}://{$auth}{$_REQUEST['SERVER_NAME']}{$port}{$_SERVER['REQUEST_URI']}"; + $url = "{$_SERVER['REQUEST_SCHEME']}://{$auth}{$_SERVER['SERVER_NAME']}{$port}{$_SERVER['REQUEST_URI']}"; } $urlParsed = parse_url($url); diff --git a/tests/unit/CommonTest.php b/tests/unit/CommonTest.php index 6540d4b..b981504 100644 --- a/tests/unit/CommonTest.php +++ b/tests/unit/CommonTest.php @@ -17,6 +17,55 @@ class CommonTest extends \Codeception\Test\Unit { + /** + * @throws UrlSecurityManagerException + */ + public function testBuilding() + { + $usm = UrlSecurityManager::create() + ->setScheme('https') + ->setHost('test.com') + ->setPort(8080) + ->setPath('/test/path') + ->setParams(['a' => 1, 'b' => 2]); + + $this->assertSame($usm->stringify(), 'https://test.com:8080/test/path?a=1&b=2'); + + $usm + ->setSignParams('sign') + ->setSecretKey('q1w2e3r4t5y6u7') + ->sign(); + + $this->assertSame($usm->stringify(), 'https://test.com:8080/test/path?a=1&b=2&sign=89727a40dc08dc9f12d91b5d6e627c17'); + + $usm = UrlSecurityManager::create([ + 'scheme' => 'http', + 'host' => 'test.com', + 'port' => 8080, + 'path' => '/test/path', + 'params' => ['a' => 1, 'b' => 2], + ]); + $this->assertSame($usm->stringify(), 'http://test.com:8080/test/path?a=1&b=2'); + } + + /** + * @throws UrlSecurityManagerException + */ + public function testServerRequest() + { + global $_SERVER; + + $_SERVER = [ + 'SERVER_NAME' => 'localhost', + 'SERVER_PORT' => 8081, + 'REQUEST_SCHEME' => 'http', + 'REQUEST_URI' => '/123/index.php?req=456', + ]; + + $usm = UrlSecurityManager::parse(); + $this->assertSame($usm->stringify(), 'http://localhost:8081/123/index.php?req=456'); + } + /** * @throws UrlSecurityManagerException * @throws WrongSignatureException