Skip to content

Commit 539b9a9

Browse files
author
mead
committed
default
1 parent ff824b1 commit 539b9a9

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/GogsWebhook/Http/Controllers/WebHookController.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,27 @@ class WebHookController extends Controller
1616
{
1717
public function handle(Request $request)
1818
{
19-
$path = config('gogs-webhook.webhook.path', base_path());
20-
// $path = base_path();
21-
$token = config('gogs-webhook.webhook.token', false);
22-
if (!$token) {
23-
exit('error request');
24-
}
25-
if ($request->header('X-Gogs-Signature', false) !== $token) {
26-
exit('error request');
19+
$path = base_path();
20+
21+
$signature = $request->header('X-Gogs-Signature', false);
22+
if (!$signature && !$this->verify_webhook($signature)) {
23+
exit('tokan is error !');
2724
}
28-
// if (empty($json['token']) || $json['token'] !== $token) {
29-
// exit('error request');
30-
// }
3125

32-
$cmd = "cd $path && git pull 2>&1";
33-
exit($cmd);
26+
$cmd = "cd $path && git pull origin master 2>&1";
3427
$status = shell_exec($cmd);
3528
exit($status);
36-
print $status;
29+
}
30+
31+
protected function verify_webhook($hmac_header)
32+
{
33+
$token = config('gogs-webhook.webhook.app_secret', false);
34+
$data = file_get_contents('php://input');
35+
if (!$token) {
36+
exit('token is not null!');
37+
}
38+
39+
$calculated_hmac = hash_hmac('sha256', $data, $token, false);
40+
return ((string)$hmac_header === (string)$calculated_hmac);
3741
}
3842
}

src/config/gogs-webhook.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
return [
1010
'webhook' => [
11-
'token' => '',
12-
'path' => '',
13-
'user' => '',
14-
'user_group'=>''
11+
'app_secret' => env('WEBHOOK_APP_SECRET', ''),
1512
]
1613
];

0 commit comments

Comments
 (0)