|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Morphatic\AutoDeploy\Origins; |
|
4
|
|
|
|
|
5
|
|
|
class Github extends AbstractOrigin implements OriginInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The name of the origin. |
|
9
|
|
|
* |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
public $name = 'Github'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Determines whether or not the Request originated from Github. |
|
16
|
|
|
* |
|
17
|
|
|
* @return bool Returns true if the request originated from Github. False otherwise. |
|
18
|
|
|
*/ |
|
19
|
18 |
|
public function isOrigin() |
|
20
|
|
|
{ |
|
21
|
|
|
// Correct IP range for Github maintained here: |
|
22
|
|
|
// https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist/ |
|
23
|
18 |
|
$hasGithubHeader = false !== strpos($this->request->header('User-Agent'), 'GitHub-Hookshot'); |
|
24
|
18 |
|
$hasGithubIp = $this->isIpInRange($this->request->server('REMOTE_ADDR'), '192.30.252.0', 22); |
|
25
|
|
|
|
|
26
|
18 |
|
return $hasGithubHeader && $hasGithubIp; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Verifies the authenticity of a webhook request from Github. |
|
31
|
|
|
* |
|
32
|
|
|
* Follows the procedure described here: https://developer.github.com/webhooks/securing/ |
|
33
|
|
|
* |
|
34
|
|
|
* @return bool Returns true if the request is authentic. False otherwise. |
|
35
|
|
|
*/ |
|
36
|
4 |
|
public function isAuthentic() |
|
37
|
|
|
{ |
|
38
|
|
|
// get the Github signature |
|
39
|
4 |
|
$xhub = $this->request->header('X-Hub-Signature') ?: 'nothing'; |
|
40
|
|
|
|
|
41
|
|
|
// reconstruct the hash on this side |
|
42
|
4 |
|
$hash = 'sha1='.hash_hmac('sha1', $this->request->getContent(), config('auto-deploy.secret')); |
|
43
|
|
|
|
|
44
|
|
|
// securely compare them |
|
45
|
4 |
|
return hash_equals($xhub, $hash); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Gets the event the triggered the webhook request. |
|
50
|
|
|
* |
|
51
|
|
|
* @return string The name of the event, e.g. push, release, create, etc. |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function event() |
|
54
|
|
|
{ |
|
55
|
2 |
|
return $this->request->header('X-GitHub-Event'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Gets the URL to be cloned from. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string The URL of the repo. |
|
62
|
|
|
*/ |
|
63
|
36 |
|
public function getRepoUrl() |
|
64
|
|
|
{ |
|
65
|
36 |
|
return $this->request->json('repository.clone_url'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets the ID of the commit that is to be cloned. |
|
70
|
|
|
* |
|
71
|
|
|
* @return string The commit ID. |
|
72
|
|
|
*/ |
|
73
|
36 |
|
public function getCommitId() |
|
74
|
|
|
{ |
|
75
|
36 |
|
return $this->request->json('after'); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|