-
Notifications
You must be signed in to change notification settings - Fork 2
/
TravisCIHandler.php
88 lines (74 loc) · 1.82 KB
/
TravisCIHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php declare(strict_types=1);
namespace NPM\ServiceWebhookHandler\Handlers;
use NPM\ServiceWebhookHandler\Utils;
class TravisCIHandler extends WebhookHandler
{
/**
* @var string
*/
public const API_HOST = 'https://api.travis-ci.org';
/**
* @var int
*/
public const API_CONFIG_CACHE_TIME = 60;
/**
* @var string
*/
protected $repo_slug;
/**
* @return string
*/
public function getRepoSlug(): string
{
return $this->repo_slug;
}
/**
* @inheritdoc
*/
protected function getVitalHeaders(): array
{
return [
'signature' => 'HTTP_SIGNATURE',
'repo_slug' => 'HTTP_TRAVIS_REPO_SLUG',
];
}
/**
* @inheritdoc
*/
protected function validateSignature(string $signature, string $payload): bool
{
if ($pubkey = $this->getTravisPubKey()) {
return 1 === openssl_verify($payload, base64_decode($signature), $pubkey);
}
return false;
}
/**
* Get the (cached) public key from the Travis CI API.
*
* @return string
*/
protected function getTravisPubKey(): string
{
$api_config = $this->getTravisApiConfig();
return $api_config['config']['notifications']['webhook']['public_key'] ?? '';
}
/**
* Get the Travis API config.
*
* @param string $api_host
*
* @return array
*/
protected function getTravisApiConfig(string $api_host = ''): array
{
if ($api_host === '') {
$api_host = self::API_HOST;
}
$api_config = Utils::fetchCacheableFile(
$api_host . '/config',
'travis-ci-api-config.json',
self::API_CONFIG_CACHE_TIME
);
return json_decode($api_config, true) ?: [];
}
}