Passed
Push — fix/custom-s3-endpoints ( 684fe6 )
by Quentin
06:46
created

SignS3Upload::signV4ChunkRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 2
rs 9.9666
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) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

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:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $credentialCondition does not seem to be defined for all execution paths leading up to this point.
Loading history...
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