-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.php
More file actions
138 lines (119 loc) · 4.56 KB
/
Copy pathwebhook.php
File metadata and controls
138 lines (119 loc) · 4.56 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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Inbound webhook endpoint for Integration Hub.
*
* External services POST JSON to this endpoint to push data back into Moodle.
* Authentication is via the service's own auth_token (Bearer or X-API-Key).
*
* Usage:
* POST /local/integrationhub/webhook.php?service=my_service_slug
* Headers: Authorization: Bearer <token>
* Body: {"key": "value"}
*
* @package local_integrationhub
* @copyright 2026 Integration Hub Contributors
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// This endpoint does NOT require Moodle login — auth is via service token.
define('NO_MOODLE_COOKIES', true);
define('AJAX_SCRIPT', true);
require(__DIR__ . '/../../config.php');
use local_integrationhub\service\registry as service_registry;
use local_integrationhub\webhook_handler;
header('Content-Type: application/json');
// Only accept POST.
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => get_string('webhook_method_not_allowed', 'local_integrationhub')]);
exit;
}
// 1. Get the service slug.
$serviceslug = optional_param('service', '', PARAM_ALPHANUMEXT);
if (empty($serviceslug)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => get_string('webhook_missing_service', 'local_integrationhub')]);
exit;
}
// 2. Resolve the service.
$service = service_registry::get_service($serviceslug);
if (!$service) {
http_response_code(404);
echo json_encode(['success' => false, 'error' => get_string('webhook_invalid_service', 'local_integrationhub')]);
exit;
}
if (empty($service->enabled)) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => get_string('webhook_service_disabled', 'local_integrationhub')]);
exit;
}
// 3. Authenticate — check Bearer token or X-API-Key against the service's auth_token.
$authenticated = false;
$expectedtoken = trim($service->auth_token ?? '');
if (!empty($expectedtoken)) {
// Try Authorization header.
$authheader = '';
if (function_exists('getallheaders')) {
$headers = getallheaders();
$authheader = $headers['Authorization'] ?? $headers['authorization'] ?? '';
} else if (!empty($_SERVER['HTTP_AUTHORIZATION'])) {
$authheader = $_SERVER['HTTP_AUTHORIZATION'];
}
// Check Bearer token.
if (preg_match('/^Bearer\s+(.+)$/i', $authheader, $matches)) {
if (hash_equals($expectedtoken, trim($matches[1]))) {
$authenticated = true;
}
}
// Check X-API-Key header as fallback.
if (!$authenticated) {
$apikey = $_SERVER['HTTP_X_API_KEY'] ?? '';
if (!empty($apikey) && hash_equals($expectedtoken, trim($apikey))) {
$authenticated = true;
}
}
} else {
// No token configured — allow (open webhook). Not recommended for production.
$authenticated = true;
}
if (!$authenticated) {
http_response_code(403);
echo json_encode(['success' => false, 'error' => get_string('webhook_invalid_token', 'local_integrationhub')]);
exit;
}
// 4. Read and parse the JSON body.
$rawbody = file_get_contents('php://input');
if (empty($rawbody)) {
http_response_code(400);
echo json_encode(['success' => false, 'error' => get_string('webhook_empty_body', 'local_integrationhub')]);
exit;
}
$payload = json_decode($rawbody, true);
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400);
$errormsg = get_string('webhook_invalid_json', 'local_integrationhub', json_last_error_msg());
echo json_encode(['success' => false, 'error' => $errormsg]);
exit;
}
// 5. Dispatch to shared handler.
$result = webhook_handler::handle($service, $payload, 'webhook');
if ($result['success']) {
http_response_code(200);
echo json_encode(['success' => true]);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'error' => $result['error']]);
}