validator = $validator; $this->plaintext = extract_payload(); $this->data = json_to_array($this->plaintext); } function findId() { return base64_encode($this->repo); } function verify() { verify_claim($this->plaintext, $this->claim, $this->id, $this->validator); } } class GiteaWebhook extends JSONWebhook { function __construct() { parent::__construct("hmac-sha256"); $this->repo = $this->findRepo(); $this->id = $this->findId(); $this->claim = extract_header("HTTP_X_GITEA_SIGNATURE"); } function findRepo() { $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; } } class GitlabWebhook extends JSONWebhook { function __construct() { parent::__construct("token"); $this->repo = $this->findRepo(); $this->id = $this->findId(); $this->claim = extract_header("HTTP_X_GITLAB_TOKEN"); } function findRepo() { $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; } } class GithubWebhook extends JSONWebhook { function __construct() { parent::__construct("hmac-sha256"); $this->repo = $this->findRepo(); $this->id = $this->findId(); $this->claim = extract_header("HTTP_X_HUB_SIGNATURE"); } function findRepo() { $repo_url = isset($this->data["repository"]["html_url"]) ? $this->data["repository"]["html_url"] : ""; if (empty($repo_url)) { error('Could not find Github repository URL'); } return $repo_url; } } function action() { if (!isset($_GET['action'])) { error("You need to specify an action (gitea, gitlab) like this: ?action=gitea", 404); } switch($_GET['action']) { case 'github': $webhook = new GithubWebhook(); $webhook->verify(); break; case 'gitea': $webhook = new GiteaWebhook(); $webhook->verify(); break; case 'gitlab': $webhook = new GitlabWebhook(); $webhook->verify(); break; default: error("Unrecognized action: ".$_GET['action'], 400); } } action(); echo("OK"); // verify_secret($repo_url, $secret) // Verify a secret for the given URL. Useful when we don't have permission to read the secret. function verify_secret($repo_url, $secret) { $whck = find_whck(); $repo = escapeshellarg($repo_url); $secret = escapeshellarg($secret); $lines = []; $status = NULL; $secret = exec($forgehook." verify ".$repo." ".$secret, $lines, $status); if (($secret == NULL) or ($status != 0)) { echo "$secret"; error("Incorrect secret for \"".$repo."\": \"".$secret."\"", 403); } } ?>