Completed
Pull Request — master (#5)
by
unknown
01:14
created

TravisCI   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 48
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A verify() 0 10 1
A getPublicKey() 0 18 3
A headers() 0 4 1
1
<?php
2
3
namespace Clarkeash\Shield\Services;
4
5
use Illuminate\Http\Request;
6
7
class TravisCI extends BaseService
8
{
9
    private $configSource;
10
11
    /**
12
     * Travis constructor.
13
     * @param string $configSource
14
     */
15
    public function __construct($configSource = 'https://api.travis-ci.org/config')
16
    {
17
        $this->configSource = $configSource;
18
    }
19
20
    public function verify(Request $request): bool
21
    {
22
        $signature = $request->header('Signature');
23
24
        $payload = $request->input('payload');
25
26
        $publicKey = $this->getPublicKey();
27
28
        return openssl_verify($payload, base64_decode($signature), $publicKey) === 1;
29
    }
30
31
    protected function getPublicKey()
32
    {
33
        $config = file_get_contents($this->configSource);
34
35
        if (!$config) {
36
            throw new \UnexpectedValueException("Could not fetch the content from {$this->configSource}.");
37
        }
38
39
        $travisConfig = json_decode($config);
40
41
        if (!$travisConfig) {
42
            throw new \UnexpectedValueException("Configuration fetched from {$this->configSource} is not valid JSON.");
43
        }
44
45
        $publicKey = $travisConfig->config->notifications->webhook->public_key;
46
47
        return $publicKey;
48
    }
49
50
    public function headers(): array
51
    {
52
        return ['Signature'];
53
    }
54
}
55