-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
283 lines (235 loc) · 7.65 KB
/
Request.php
File metadata and controls
283 lines (235 loc) · 7.65 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace Webhook;
class Request {
/**
* Service parameter key names.
* @see \Webhook\Request::service()
*/
const SERVICE_PARAMS = [
'HTTP_X_HUB_SIGNATURE',
'HTTP_X_GITHUB_EVENT',
'CONTENT_TYPE',
'HTTP_X_GITHUB_DELIVERY',
'REQUEST_METHOD',
'HTTP_USER_AGENT',
];
const CONTENT_TYPES = [
'application/x-www-form-urlencoded',
'application/json',
];
/**
* @var string
* The raw payload of this delivery.
*/
private $_messageBody;
/**
* @var string
* The content-Type of the payload.
*/
private $_contentType;
/**
* @var string GitHub Event Type of this delivery.
*/
private $_gitHubEvent;
/**
* @var string
* The Hub-Signature provided along with this delivery.
*/
private $_hubSignature;
/**
* @var string
* The UUID of this delivery.
*/
private $_gitHubDelivery;
/**
* @var string
* The request method of this delivery.
*/
private $_requestMethod;
/**
* @var string
* The user-agent reported for this delivery.
*/
private $_userAgent;
/**
* @var \Webhook\Payload
*/
private $_payload;
/**
* Provides a webhook request object from specified paramaters.
* Suitable for use in conjunction with the $_SERVER array.
*
* @param string $messageBody request message body
* @param array $param assoc array of paramaters:<ul>
* <li><b>string $param['HTTP_X_HUB_SIGNATURE']</b> HTTP_X_HUB_SIGNATURE.</li>
* <li><b>string $param['HTTP_X_GITHUB_EVENT']</b> HTTP_X_GITHUB_EVENT.</li>
* <li><b>string $param['CONTENT_TYPE']</b> CONTENT_TYPE. Optional.</li>
* <li><b>string $param['HTTP_X_GITHUB_DELIVERY']</b> HTTP_X_GITHUB_DELIVERY.</li>
* <li><b>string $param['REQUEST_METHOD']</b> REQUEST_METHOD.</li>
* <li><b>string $param['HTTP_USER_AGENT']</b> HTTP_USER_AGENT.</li>
* </ul>
*
* @return \Webhook\Request
*/
public static function service(string $messageBody,array $param=null) {
$request = array_fill_keys(static::SERVICE_PARAMS, '');
if (!empty($param)) {
foreach($request as $k=>&$v) {
if (isset($param[$k])) $v=$param[$k];
}
unset($k);
unset($v);
}
return new static(
$messageBody,$request['HTTP_X_HUB_SIGNATURE'],$request['HTTP_X_GITHUB_EVENT'],
$request['CONTENT_TYPE'],$request['HTTP_X_GITHUB_DELIVERY'],
$request['REQUEST_METHOD'],$request['HTTP_USER_AGENT']
);
}
/**
* Determines if a secret matches a signature and message body.
*
* @param string $hub_secret Secret string known by the webhoook provider.
* @param string $hub_signature Hub-Signature value specified by the request.
* @param string $message_body Raw request payload.
*
* @return bool true if signature is valid, <b>bool</b> false otherwise
*/
final public static function isValidSignature(string $hub_secret, string $hub_signature, string $message_body) : bool {
list($algo, $hash) = explode('=', $hub_signature, 2) + ['', ''];
if ($hash !== hash_hmac($algo, $message_body, $hub_secret)) {
return false;
}
return true;
}
/**
* Enforces that a secret matches this request's signature and message body.
*
* @param string $hub_secret Secret string known by the webhoook provider.
*
* @return void
* @throws \Webhook\InvalidRequest
*/
public function validateSignature(string $hub_secret) {
if (!self::isValidSignature($hub_secret, $this->_hubSignature, $this->_messageBody)) {
throw new SignatureInvalidException();
}
}
/**
* @param string $messageBody
* @param string $hubSignature
* @param string $gitHubEvent
* @param string $contentType Optional.
* @param string $gitHubDelivery Optional.
* @param string $requestMethod Optional.
* @param string $userAgent Optional.
*
* @throws \Webhook\InvalidRequest if invalid value specified for hubSignature, or gitHubEvent;
*/
public function __construct(
string $messageBody,string $hubSignature,string $gitHubEvent,
string $contentType='',string $gitHubDelivery='',
string $requestMethod='',string $userAgent=''
)
{
if (empty($hubSignature)) {
throw new SignatureMissingException();
}
if (empty($gitHubEvent)) {
throw new EventMissingException();
}
$this->_messageBody = $messageBody;
$this->_contentType = $contentType;
$this->_gitHubEvent = $gitHubEvent;
$this->_hubSignature = $hubSignature;
$this->_gitHubDelivery = $gitHubDelivery;
$this->_requestMethod = $requestMethod;
$this->_userAgent = $userAgent;
if (empty($this->_contentType)) {
$this->_contentType = (new \finfo(\FILEINFO_MIME_TYPE))->buffer($this->_messageBody);
if (!in_array($this->_contentType,static::CONTENT_TYPES,true)) {
if (null!==json_decode($this->_messageBody)) {
$this->_contentType = "application/json";
} else {
throw new MessageBodyInvalidException();
}
}
} else {
if (!in_array($this->_contentType,static::CONTENT_TYPES,true)) {
throw new MessageBodyInvalidException();
}
}
$bodyObject = null;
if ($this->_contentType == 'application/json') {
$bodyObject = json_decode($this->_messageBody);
if (!is_object($bodyObject)) {
throw new MessageBodyInvalidException();
}
} elseif ($this->_contentType == 'application/x-www-form-urlencoded') {
parse_str($this->_messageBody,$bodyObject);
}
if (empty($bodyObject)) {
throw new MessageBodyInvalidException();
}
$eventSubns = str_replace(" ","",ucwords(str_replace("_"," ",$this->_gitHubEvent)))."Event";
$payload = __NAMESPACE__ . '\Payload\\'.$eventSubns;
if (class_exists($payload)) {
$this->_payload = new $payload($bodyObject);
} else {
$this->_payload = new Payload\Event($bodyObject, $this->_gitHubEvent);
}
}
/**
* @return \Webhook\Payload
*/
public function getPayload(): Payload {
return $this->_payload;
}
/**
* @return string The raw payload of this delivery.
*/
public function getMessageBody(): string {
return $this->_messageBody;
}
/**
* @return string
* The content-Type of the payload.
*/
public function getContentType(): string {
return $this->_contentType;
}
/**
* @return string GitHub Event Type of this delivery.
*/
public function getGitHubEvent(): string {
return $this->_gitHubEvent;
}
/**
* @return string
* The Hub-Signature provided along with this delivery.
*/
public function getHubSignature(): string {
return $this->_hubSignature;
}
/**
* @return string
* The UUID of this delivery.
*/
public function getGitHubDelivery(): string {
return $this->_gitHubDelivery;
}
/**
* @return string
* The request method of this delivery.
*/
public function getRequestMethod(): string {
return $this->_requestMethod;
}
/**
* @return string
* The user-agent reported for this delivery.
*/
public function getUserAgent(): string {
return $this->_userAgent;
}
}