-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallback.php
More file actions
143 lines (117 loc) · 4.37 KB
/
Callback.php
File metadata and controls
143 lines (117 loc) · 4.37 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Webhook;
class Callback {
/**
* Invokes the callback after enforcing that the Hub-Signature corresponds to the payload and Hub-Secret and
* that the <b>Payload</b> meets all applicable <b>CallbackRule</b> configurations.
*
* @param string $hubSignature Hub-Signature value specified by the request.
* @param string $rawPayload raw request payload.
* @param \Webhook\Payload $payload Payload object.
*
* @return void
* @throws \Webhook\InvalidRequestException
* @deprecated
* @see \Webhook\Callback::validatePayload()
*/
public function validateRequest(string $hubSignature,string $rawPayload, Payload $payload) {
if (!Request::isValidSignature($this->_hubSecret, $hubSignature, $rawPayload)) {
throw new InvalidRequestException("secret validation failed");
}
return $this->validatePayload($payload);
}
/**
* Invokes the callback after enforcing that the <b>Payload</b> meets all applicable <b>CallbackRule</b> configurations.
*
* @param string $hubSignature Hub-Signature value specified by the request.
* @param string $rawPayload raw request payload.
* @param \Webhook\Payload $payload Payload object.
*
* @return void
* @throws \Webhook\InvalidRequestException
*/
public function validatePayload(Payload $payload) {
if (count($this->_urlRule)) {
$found_urlRule_match=false;
foreach($this->_urlRule as $url) {
if ($payload->repository->url===$url) {
$found_urlRule_match = true;
break 1;
}
if ($payload->repository->html_url===$url) {
$found_urlRule_match = true;
break 1;
}
if ($payload->repository->git_url===$url) {
$found_urlRule_match = true;
break 1;
}
if ($payload->repository->ssh_url===$url) {
$found_urlRule_match = true;
break 1;
}
if ($payload->repository->clone_url===$url) {
$found_urlRule_match = true;
break 1;
}
if ($payload->repository->svn_url===$url) {
$found_urlRule_match = true;
break 1;
}
}
unset($url);
if (!$found_urlRule_match) throw new InvalidRequestException("failed to find a match for the payload's repository URL; expected one of '".implode(", ",$this->_urlRule)."', got instead {$payload->repository->url}");
}
if (count($this->_eventRule)) {
$found_eventRule_match=false;
foreach($this->_eventRule as $event) {
if ($payload->getEvent()===$event) {
$found_eventRule_match = true;
break 1;
}
}
if (!$found_eventRule_match) throw new InvalidRequestException("failed to find a match for the payload's GitHub-Event type");
}
call_user_func_array($this->_callback,[$payload]);
}
/**
* @var string
* Secret string known by the webhoook provider.
*/
private $_hubSecret;
/**
* @var callable
* callback invoked when Hub-Signature has is validated to secret
*/
private $_callback;
/**
* @var string[]
* Optional GitHub-Event type criterion for validation.
*/
private $_eventRule=[];
/**
* @var string[]
* Optional repo URL criterion for validation.
*/
private $_urlRule=[];
/**
*
* @param string $hubSecret Secret string known by the webhoook provider.
* @param callable $callback function to invoke when a Hub-Signature is validated.
* Callback signature: function( \Webhook\Payload $payload) {}
* @param CallbackRule ...$CallbackRule Optional. If specified, at least one of these rules must be satisfied
* in order for a request to be validated.
*/
public function __construct(string $hubSecret,callable $callback,CallbackRule ...$CallbackRule) {
$this->_hubSecret = $hubSecret;
$this->_callback = $callback;
foreach($CallbackRule as $v) {
if ($v instanceof UrlCallbackRule) {
$this->_urlRule []= (string) $v;
}
if ($v instanceof EventCallbackRule) {
$this->_eventRule []= (string) $v;
}
}
}
}