Skip to content

Commit

Permalink
bug fixed, tests upd, readme upd
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Sep 11, 2020
1 parent 3b7a5f1 commit 87850ef
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
4 changes: 2 additions & 2 deletions src/UrlSecurityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 87850ef

Please sign in to comment.