Classes for different types of webhooks

This commit is contained in:
southerntofu 2020-10-03 10:50:46 -04:00
parent 15000261af
commit 56fdb018fa
1 changed files with 41 additions and 21 deletions

View File

@ -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);