|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Services\Uploader; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository as Config; |
|
6
|
|
|
|
|
7
|
|
|
class SignS3Upload |
|
8
|
|
|
{ |
|
9
|
|
|
private $bucket; |
|
10
|
|
|
|
|
11
|
|
|
private $secret; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Config |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param Config $config |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(Config $config) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->config = $config; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function fromPolicy($policy, SignUploadListener $listener, $disk = 'libraries') |
|
27
|
|
|
{ |
|
28
|
|
|
$policyObject = json_decode($policy, true); |
|
29
|
|
|
$policyJson = json_encode($policyObject); |
|
30
|
|
|
|
|
31
|
|
|
$this->bucket = $this->config->get('filesystems.disks.' . $disk . '.bucket'); |
|
32
|
|
|
$this->secret = $this->config->get('filesystems.disks.' . $disk . '.secret'); |
|
33
|
|
|
|
|
34
|
|
|
$signedPolicy = $this->signPolicy($policyJson); |
|
35
|
|
|
|
|
36
|
|
|
if ($signedPolicy) { |
|
37
|
|
|
return $listener->uploadIsSigned($signedPolicy); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $listener->uploadIsNotValid(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function signPolicy($policyJson) |
|
44
|
|
|
{ |
|
45
|
|
|
$policyObject = json_decode($policyJson, true); |
|
46
|
|
|
|
|
47
|
|
|
if ($this->isValid($policyObject)) { |
|
48
|
|
|
$encodedPolicy = base64_encode($policyJson); |
|
49
|
|
|
$signedPolicy = array( |
|
50
|
|
|
'policy' => $encodedPolicy, |
|
51
|
|
|
'signature' => $this->signV4Policy($policyObject, $encodedPolicy), |
|
52
|
|
|
); |
|
53
|
|
|
return $signedPolicy; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function isValid($policy) |
|
60
|
|
|
{ |
|
61
|
|
|
$expectedMaxSize = null; |
|
62
|
|
|
$conditions = $policy["conditions"]; |
|
63
|
|
|
$bucket = null; |
|
64
|
|
|
$parsedMaxSize = null; |
|
65
|
|
|
|
|
66
|
|
|
for ($i = 0; $i < count($conditions); ++$i) { |
|
|
|
|
|
|
67
|
|
|
$condition = $conditions[$i]; |
|
68
|
|
|
if (isset($condition["bucket"])) { |
|
69
|
|
|
$bucket = $condition["bucket"]; |
|
70
|
|
|
} else if (isset($condition[0]) && $condition[0] == "content-length-range") { |
|
71
|
|
|
$parsedMaxSize = $condition[2]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $bucket == $this->bucket && $parsedMaxSize == (string) $expectedMaxSize; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function signV4Policy($policy, $encodedPolicy) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($policy["conditions"] as $condition) { |
|
81
|
|
|
if (isset($condition["x-amz-credential"])) { |
|
82
|
|
|
$credentialCondition = $condition["x-amz-credential"]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$pattern = "/.+\/(.+)\\/(.+)\/s3\/aws4_request/"; |
|
87
|
|
|
preg_match($pattern, $credentialCondition, $matches); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$dateKey = hash_hmac('sha256', $matches[1], 'AWS4' . $this->secret, true); |
|
90
|
|
|
$dateRegionKey = hash_hmac('sha256', $matches[2], $dateKey, true); |
|
91
|
|
|
$dateRegionServiceKey = hash_hmac('sha256', 's3', $dateRegionKey, true); |
|
92
|
|
|
$signingKey = hash_hmac('sha256', 'aws4_request', $dateRegionServiceKey, true); |
|
93
|
|
|
|
|
94
|
|
|
return hash_hmac('sha256', $encodedPolicy, $signingKey); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: