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

TravisCI::verify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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