Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
24 / 24 |
SignatureValidator | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
24 / 24 |
__construct( SignatureCalculatorInterface $signatureCalculator, AlgorithmExtractorInterface $algorithmExtractor, AlgorithmsProviderInterface $algorithmsProvider ) | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
isSignatureValid( $data, $signatureHeader, CredentialsInterface $credentials ) | |
100.00% |
1 / 1 |
1 | |
100.00% |
10 / 10 |
|||
extractSignature($signature) | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
extractAlgorithm($signature) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
compareSignatureStrings($firstSignature, $secondSignature) | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
<?php | |
/** | |
* @author Krzysztof Gzocha <krzysztof.gzocha@xsolve.pl> | |
*/ | |
namespace Team3\PayU\SignatureCalculator\Validator; | |
use Team3\PayU\Configuration\Credentials\CredentialsInterface; | |
use Team3\PayU\SignatureCalculator\Encoder\Algorithms\AlgorithmInterface; | |
use Team3\PayU\SignatureCalculator\Encoder\Algorithms\AlgorithmsProviderInterface; | |
use Team3\PayU\SignatureCalculator\SignatureCalculatorException; | |
use Team3\PayU\SignatureCalculator\SignatureCalculatorInterface; | |
/** | |
* {@inheritdoc} | |
* | |
* Class SignatureValidator | |
* @package Team3\PayU\SignatureCalculator\Validator | |
*/ | |
class SignatureValidator implements SignatureValidatorInterface | |
{ | |
/** | |
* @var SignatureCalculatorInterface | |
*/ | |
private $signatureCalculator; | |
/** | |
* @var AlgorithmExtractorInterface | |
*/ | |
private $algorithmExtractor; | |
/** | |
* @var AlgorithmsProviderInterface | |
*/ | |
private $algorithmsProvider; | |
/** | |
* @param SignatureCalculatorInterface $signatureCalculator | |
* @param AlgorithmExtractorInterface $algorithmExtractor | |
* @param AlgorithmsProviderInterface $algorithmsProvider | |
*/ | |
public function __construct( | |
SignatureCalculatorInterface $signatureCalculator, | |
AlgorithmExtractorInterface $algorithmExtractor, | |
AlgorithmsProviderInterface $algorithmsProvider | |
) { | |
$this->signatureCalculator = $signatureCalculator; | |
$this->algorithmExtractor = $algorithmExtractor; | |
$this->algorithmsProvider = $algorithmsProvider; | |
} | |
/** | |
* @param string $data | |
* @param string $signatureHeader | |
* @param CredentialsInterface $credentials | |
* | |
* @return bool | |
* @throws SignatureCalculatorException | |
*/ | |
public function isSignatureValid( | |
$data, | |
$signatureHeader, | |
CredentialsInterface $credentials | |
) { | |
$calculatedSignature = $this | |
->signatureCalculator | |
->calculate( | |
json_decode($data, true), | |
$credentials, | |
$this->extractAlgorithm($signatureHeader) | |
); | |
return $this->compareSignatureStrings( | |
$calculatedSignature, | |
$this->extractSignature($signatureHeader) | |
); | |
} | |
/** | |
* @param string $signature | |
* | |
* @return string | |
* @throws SignatureValidatorException | |
*/ | |
private function extractSignature($signature) | |
{ | |
$matches = []; | |
preg_match('/signature=([a-zA-Z0-9]+);/', $signature, $matches); | |
if (!array_key_exists(1, $matches)) { | |
throw new SignatureValidatorException(sprintf( | |
'Could not extract signature from string %s', | |
$signature | |
)); | |
} | |
return $matches[1]; | |
} | |
/** | |
* @param string $signature | |
* | |
* @return AlgorithmInterface | |
*/ | |
private function extractAlgorithm($signature) | |
{ | |
return $this | |
->algorithmExtractor | |
->extractAlgorithm($signature, $this->algorithmsProvider->getAlgorithms()); | |
} | |
/** | |
* @param string $firstSignature | |
* @param string $secondSignature | |
* | |
* @return bool | |
*/ | |
private function compareSignatureStrings($firstSignature, $secondSignature) | |
{ | |
return 0 === strcasecmp($firstSignature, $secondSignature); | |
} | |
} |