Box   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 16 3
A check() 0 6 1
A headers() 0 4 1
1
<?php
2
3
namespace Shield\Box;
4
5
use Carbon\Carbon;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
use Shield\Shield\Contracts\Service;
9
10
/**
11
 * Class Box
12
 *
13
 * @package \Shield\Box
14
 */
15
class Box implements Service
16
{
17 4
    public function verify(Request $request, Collection $config): bool
18
    {
19 4
        $rawTimestamp = (string) $request->header('BOX-DELIVERY-TIMESTAMP');
20
21 4
        $timestamp = Carbon::parse($rawTimestamp);
22
23
        // 10 Minute Tolerance
24 4
        if (Carbon::now(config('app.timezone', 'UTC'))->diffInSeconds($timestamp) > $config->get('tolerance', 600)) {
25 1
            return false;
26
        }
27
28 3
        $generated = $request->getContent() . $rawTimestamp;
29
30
        // Primary or Secondary can pass to be valid.
31 3
        return $this->check($generated, $config->get('primary'), $request->header('BOX-SIGNATURE-PRIMARY')) || $this->check($generated, $config->get('secondary'), $request->header('BOX-SIGNATURE-SECONDARY'));
32
    }
33
34 3
    public function check($generated, $key, $supplied)
35
    {
36 3
        $encoded = base64_encode(hash_hmac('sha256', $generated, $key, true));
37
38 3
        return hash_equals($encoded, $supplied);
39
    }
40
41 1
    public function headers(): array
42
    {
43 1
        return ['BOX-DELIVERY-TIMESTAMP', 'BOX-SIGNATURE-PRIMARY', 'BOX-SIGNATURE-SECONDARY'];
44
    }
45
}
46