1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Handler; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Exception\SignatureInvalidException; |
6
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
7
|
|
|
|
8
|
|
|
class SignatureParameterHandler implements ParameterHandlerInterface |
9
|
|
|
{ |
10
|
|
|
const PARAMETER_SIGNATURE = 's'; |
11
|
|
|
const PARAMETER_BUST_CACHE = 'bc'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $key; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $hashAlgorithm; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param $key |
25
|
|
|
* @param string $hashAlgorithm |
26
|
|
|
*/ |
27
|
7 |
|
public function __construct($key, $hashAlgorithm = 'sha256') |
28
|
|
|
{ |
29
|
7 |
|
$this->key = $key; |
30
|
7 |
|
$this->hashAlgorithm = $hashAlgorithm; |
31
|
7 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param MediaInterface $media |
35
|
|
|
* @param ParameterBag $parameterBag |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
2 |
|
public function getRouteParameters(MediaInterface $media, ParameterBag $parameterBag) |
39
|
|
|
{ |
40
|
2 |
|
$parameters = $parameterBag->toArray($media); |
41
|
2 |
|
$parameters[self::PARAMETER_SIGNATURE] = $this->calculateSignature($parameters); |
42
|
|
|
|
43
|
2 |
|
return $parameters; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param MediaInterface $media |
48
|
|
|
* @param $width |
49
|
|
|
* @param $height |
50
|
|
|
* @param array $extra |
51
|
|
|
* @return ParameterBag |
52
|
|
|
* @throws SignatureInvalidException |
53
|
|
|
*/ |
54
|
2 |
|
public function getPayload(MediaInterface $media, $width, $height, array $extra = []) |
55
|
|
|
{ |
56
|
2 |
|
if (!isset($extra[self::PARAMETER_SIGNATURE])) { |
57
|
|
|
throw new SignatureInvalidException(); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
$signature = $extra[self::PARAMETER_SIGNATURE]; |
61
|
2 |
|
unset($extra[self::PARAMETER_SIGNATURE]); |
62
|
|
|
|
63
|
2 |
|
$parameters = new ParameterBag($width, $height, $extra); |
64
|
2 |
|
if (!$this->isValid($media, $parameters, $signature)) { |
65
|
|
|
throw new SignatureInvalidException(); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
return $parameters; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param MediaInterface $media |
73
|
|
|
* @param ParameterBag $parameters |
74
|
|
|
* @param $expectedSignature |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
2 |
|
private function isValid(MediaInterface $media, ParameterBag $parameters, $expectedSignature) |
78
|
|
|
{ |
79
|
2 |
|
return hash_equals($this->calculateSignature($parameters->toArray($media)), $expectedSignature); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $parameters |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
4 |
|
private function calculateSignature(array $parameters) |
87
|
|
|
{ |
88
|
4 |
|
return hash_hmac($this->hashAlgorithm, $this->key, json_encode($this->normalize($parameters))); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $parameters |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
4 |
|
private function normalize(array $parameters) |
96
|
|
|
{ |
97
|
4 |
|
if (isset($parameters[self::PARAMETER_BUST_CACHE])) { |
98
|
1 |
|
unset($parameters[self::PARAMETER_BUST_CACHE]); |
99
|
1 |
|
} |
100
|
4 |
|
ksort($parameters); |
101
|
|
|
|
102
|
4 |
|
return array_map('strval', $parameters); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|