From 56fdb018faf3a12817792fc03022ece718a3c77b Mon Sep 17 00:00:00 2001 From: southerntofu Date: Sat, 3 Oct 2020 10:50:46 -0400 Subject: [PATCH] Classes for different types of webhooks --- index.php | 62 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/index.php b/index.php index 182d2e5..fc7f220 100644 --- a/index.php +++ b/index.php @@ -93,22 +93,42 @@ function find_secret($repo_url) { return $secret; } -function find_url_gitea($array) { - $repo_url = isset($array["repository"]["html_url"]) ? - $array["repository"]["html_url"] : ""; - if (empty($repo_url)) { - error('Could not find Gitea repository URL'); +class GiteaWebhook { + function __construct($payload) { + $this->data = json_to_array($payload); + } + + function repo_url() { + $repo_url = isset($this->data["repository"]["html_url"]) ? + $this->data["repository"]["html_url"] : ""; + if (empty($repo_url)) { + error('Could not find Gitea repository URL'); + } + return $repo_url; + } + + function secret() { + return extract_header("HTTP_X_GITEA_SIGNATURE"); } - return $repo_url; } -function find_url_gitlab($array) { - $repo_url = isset($array["project"]["git_http_url"]) ? - $array["project"]["git_http_url"] : ""; - if (empty($repo_url)) { - error('Could not find Gitlab repository URL'); +class GitlabWebhook { + function __construct($payload) { + $this->data = json_to_array($payload); + } + + function repo_url() { + $repo_url = isset($this->data["project"]["git_http_url"]) ? + $this->data["project"]["git_http_url"] : ""; + if (empty($repo_url)) { + error('Could not find Gitlab repository URL'); + } + return $repo_url; + } + + function secret() { + return extract_header("HTTP_X_GITLAB_TOKEN"); } - return $repo_url; } function notify($repo) { @@ -125,10 +145,12 @@ function action() { if (!isset($_GET['action'])) { error("You need to specify an action (gitea, gitlab) like this: ?action=gitea", 404); } + + $payload = extract_payload(); + switch($_GET['action']) { case 'github': $claimed_secret = extract_header("HTTP_X_HUB_SIGNATURE"); - $payload = extract_payload(); $payload_array = json_to_array($payload); // Gitea URL is same as Github, for the moment $repo_url = find_url_gitea($payload_array); @@ -137,19 +159,17 @@ function action() { notify($repo_url); break; case 'gitea': - $claimed_secret = extract_header("HTTP_X_GITEA_SIGNATURE"); - $payload = extract_payload(); - $payload_array = json_to_array($payload); - $repo_url = find_url_gitea($payload_array); + $webhook = new GiteaWebhook($payload); + $claimed_secret = $webhook->secret(); + $repo_url = $webhook->repo_url(); $secret = find_secret($repo_url); verify_signature($payload, $secret, $claimed_secret); notify($repo_url); break; case 'gitlab': - $claimed_secret = extract_header("HTTP_X_GITLAB_TOKEN"); - $payload = extract_payload(); - $payload_array = json_to_array($payload); - $repo_url = find_url_gitlab($payload_array); + $webhook = new GitlabWebhook($payload); + $claimed_secret = $webhook->secret(); + $repo_url = $webhook->repo_url(); $secret = find_secret($repo_url); verify_token($secret, $claimed_secret); notify($repo_url);