PHP Classes

PHP URL Security Manager: Build, parse, sign and encrypt URLs

Recommend this page to a friend!

  Author Author  
Picture of Smoren  Freelight
Name: Smoren Freelight <contact>
Classes: 16 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 6x


  Detailed description   Download Download .zip .tar.gz  
This class can build, parse, sign and encrypt URLs.

It can assemble the values of a URL's different parts to build the whole URL string.

The class can also take a list of parameter names and generate an additional parameter that is a signature of the values of the parameters in the list.

It can also encrypt a list of parameters and replace those parameters with a single encrypted parameter.

Details

url-security-manager

Class for building, parsing, signing, signature checking, encrypting and decrypting URLs.

Install to your project

composer require smoren/url-security-manager

Unit testing

composer install
./vendor/bin/codecept build
./vendor/bin/codecept run unit tests/unit

Demo

Signing
use Smoren\UrlSecurityManager\UrlSecurityManager;

$inputUrl = 'http://localhost:8080/test/path?p1=1&p2=2&p3=3&p4=4';
$secretKey = 'fvd76df89g7fdg89';

// Let's sign some url with our secret key to send signed request to receiver
$usmSender = UrlSecurityManager::parse($inputUrl)
    // signature will be stored as value of query param "sign"
    // only query params from array (2nd argument) will be signed
    ->setSignParams('sign', ['p1', 'p2', 'p3'])
    ->setSecretKey($secretKey) // giving secret key for signing
    ->sign(); // create signature

$signedUrl = $usmSender->stringify();
echo $signedUrl;
// http://localhost:8080/test/path?p1=1&p2=2&p3=3&p4=4&sign=5342af44ed716002a81a2872734729f5

// Now we will try to check the signature of URL:
$usmReceiver = UrlSecurityManager::parse($signedUrl)
    ->setSignParams('sign', ['p1', 'p2', 'p3'])
    ->setSecretKey($secretKey);
$usmReceiver->check(); // will be executed without WrongSignatureException

$usmReceiver
    ->setSignParams('sign', ['p1', 'p2', 'p3'])
    ->setSecretKey('123');
$usmReceiver->check(); // will throw WrongSignatureException

$usmSender = UrlSecurityManager::parse($inputUrl)
    ->setSignParams('sign') // all query params will be signed
    ->setSecretKey($secretKey)
    ->sign();
$signedUrl = $usmSender->stringify();
echo $signedUrl;
// http://localhost:8080/test/path?p1=1&p2=2&p3=3&p4=4&sign=50489186458519f9f141e616dc02af73

Encrypting/decrypting
use Smoren\UrlSecurityManager\UrlSecurityManager;

$inputUrl = 'http://localhost:8080/test/path?p1=1&p2=2&p3=3&p4=4';
$secretKey = 'fvd76df89g7fdg89';

// Let's encrypt url with our secret key to send some secret data to receiver:
$usmSender = UrlSecurityManager::parse($inputUrl)
    // encrypted string will be stored as value of query param "encrypted"
    // all query params will be encrypted
    ->setEncryptParams('encrypted') 
    ->setSecretKey($secretKey)
    ->encrypt(); // encrypting data

$encryptedUrl = $usmSender->stringify();
echo $encryptedUrl;
// someting like this:
// http://localhost:8080/test/path?encrypted=X4oxVda3u%2FD2NX...

// Now we will try to decrypt received secret data:
$usmReceiver = UrlSecurityManager::parse($encryptedUrl)
    ->setEncryptParams('encrypted')
    ->setSecretKey($secretKey)
    ->decrypt();

$decryptedUrl = $usmReceiver->stringify();
echo $decryptedUrl;
// http://localhost:8080/test/path?p1=1&p2=2&p3=3&p4=4

$usmSender->decrypt();

// encrypt only query params: p1, p2
$usmSender->setEncryptParams('encrypted', ['p1', 'p2']);
$usmSender->encrypt();

echo $usmSender->stringify();
// something like this:
// http://localhost:8080/test/path?p3=3&p4=4&encrypted=CTNdFXZDlBwYwwvQV2L8mGjQg5YydC3...

$usmSender->decrypt();
echo $usmSender->stringify();
// http://localhost:8080/test/path?p3=3&p4=4&p1=1&p2=2

Building URLs
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

Parse URL from server request
use Smoren\UrlSecurityManager\UrlSecurityManager;

$usm = UrlSecurityManager::parse();

echo $usm->stringify();
// you will see full URL of your current server request

  Classes of Smoren Freelight  >  PHP URL Security Manager  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP URL Security Manager
Base name: url-security-manager
Description: Build, parse, sign and encrypt URLs
Version: -
PHP version: 7.2
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image HTTP HTTP protocol clients, headers and cookies View top rated classes
Group folder image Algorithms Numerical and statistical algorithms View top rated classes
Group folder image Cryptography Encrypting, decrypting and hashing data View top rated classes
Group folder image Libraries Frameworks and libraries of cooperating classes View top rated classes
Group folder image PHP 7 Classes using PHP 7 specific features View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (2 files, 2 directories)
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageExceptions (3 files)
  Plain text file UrlSecurityManager.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file DecryptException.php Class Class source
  Plain text file UrlSecurityManagerException.php Class Class source
  Plain text file WrongSignatureException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageunit (1 file)
Files folder image_support (1 file, 1 directory)
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  
File Role Description
  Plain text file CommonTest.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
Files folder image_generated (1 file)
  Plain text file UnitTester.php Class Class source

  Files folder image Files  /  tests  /  _support  /  _generated  
File Role Description
  Plain text file UnitTesterActions.php Class Class source

Download Download all files: url-security-manager.tar.gz url-security-manager.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  Files folder image Files  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (2 files, 2 directories)
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageExceptions (3 files)
  Plain text file UrlSecurityManager.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file DecryptException.php Class Class source
  Plain text file UrlSecurityManagerException.php Class Class source
  Plain text file WrongSignatureException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageunit (1 file)
Files folder image_support (1 file, 1 directory)
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  
File Role Description
  Plain text file CommonTest.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
Files folder image_generated (1 file)
  Plain text file UnitTester.php Class Class source

  Files folder image Files  /  tests  /  _support  /  _generated  
File Role Description
  Plain text file UnitTesterActions.php Class Class source

Download Download all files: url-security-manager.tar.gz url-security-manager.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.