-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.php
executable file
·306 lines (241 loc) · 9.47 KB
/
service.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?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/>.
/**
* LTI web service endpoints
*
* @package qtype_lti
* @copyright Copyright (c) 2019 ETH Zurich
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_DEBUG_DISPLAY', true);
define('NO_MOODLE_COOKIES', true);
require_once(__DIR__ . "/../../../config.php");
require_once($CFG->dirroot . '/question/type/lti/locallib.php');
require_once($CFG->dirroot . '/question/type/lti/servicelib.php');
/**
* Handles exceptions when handling incoming LTI messages.
* Ensures that LTI always returns a XML message that can be consumed by the caller.
*
* @package qtype_lti
* @copyright Copyright 2019 ETH Zurich
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_lti_service_exception_handler {
/**
* Enable error response logging.
*
* @var bool
*/
protected $log = false;
/**
* The LTI service message ID, if known.
*
* @var string
*/
protected $id = '';
/**
* The LTI service message type, if known.
*
* @var string
*/
protected $type = 'unknownRequest';
/**
* Constructor.
*
* @param boolean $log
* Enable error response logging.
*/
public function __construct($log) {
$this->log = $log;
}
/**
* Set the LTI message ID being handled.
*
* @param string $id
*/
public function set_message_id($id) {
if (!empty($id)) {
$this->id = $id;
}
}
/**
* Set the LTI message type being handled.
*
* @param string $type
*/
public function set_message_type($type) {
if (!empty($type)) {
$this->type = $type;
}
}
/**
* Echo an exception message encapsulated in XML.
*
* @param \Exception|\Throwable $exception
* The exception that was thrown
*/
public function handle($exception) {
$message = $exception->getMessage();
// Add the exception backtrace for developers.
if (debugging('', DEBUG_DEVELOPER)) {
$message .= "\n" . format_backtrace(get_exception_info($exception)->backtrace, true);
}
// Switch to response.
$type = str_replace('Request', 'Response', $this->type);
// Build the appropriate xml.
$response = qtype_lti_get_response_xml('failure', $message, $this->id, $type);
$xml = $response->asXML();
// Log the request if necessary.
if ($this->log) {
qtype_lti_log_response($xml, $exception);
}
echo $xml;
}
}
// TODO: Switch to core oauthlib once implemented - MDL-30149.
use moodle\qtype\lti as lti;
$rawbody = file_get_contents("php://input");
$logrequests = qtype_lti_should_log_request($rawbody);
$errorhandler = new qtype_lti_service_exception_handler($logrequests);
// Register our own error handler so we can always send valid XML response.
set_exception_handler(array($errorhandler, 'handle'));
if ($logrequests) {
qtype_lti_log_request($rawbody);
}
$ltiheaders = lti\OAuthUtil::get_headers();
foreach ($ltiheaders as $name => $value) {
if ($name === 'Authorization') {
$oauthparams = lti\OAuthUtil::split_header($value);
$consumerkey = $oauthparams['oauth_consumer_key'];
break;
}
}
if (empty($consumerkey)) {
throw new Exception('Consumer key is missing, or Authorization header is not readable by the server.');
}
$returnedsecret = qtype_lti_get_shared_secrets_by_key($consumerkey);
$sharedsecret = qtype_lti_verify_message($consumerkey, $returnedsecret, $rawbody);
if ($sharedsecret === false) {
throw new Exception('Message signature not valid');
}
// TODO MDL-46023 Replace this code with a call to the new library.
// $origentity = libxml_disable_entity_loader(true);
$xml = simplexml_load_string($rawbody);
if (!$xml) {
// libxml_disable_entity_loader($origentity);
throw new Exception('Invalid XML content');
}
// libxml_disable_entity_loader($origentity);
$body = $xml->imsx_POXBody;
foreach ($body->children() as $child) {
$messagetype = $child->getName();
}
// We know more about the message, update error handler to send better errors.
$errorhandler->set_message_id(qtype_lti_parse_message_id($xml));
$errorhandler->set_message_type($messagetype);
switch ($messagetype) {
case 'replaceResultRequest':
$parsed = qtype_lti_parse_grade_replace_message($xml);
$ltiinstance = $DB->get_record('qtype_lti_options', array('id' => $parsed->ltiid));
if (!$ltiinstance) {
throw new Exception('No such tool.');
}
if (!qtype_lti_accepts_grades($ltiinstance)) {
throw new Exception('Tool does not accept grades');
}
qtype_lti_verify_sourcedid($ltiinstance, $parsed);
qtype_lti_set_session_user($parsed->username);
$gradestatus = qtype_lti_update_grade($parsed->username, $parsed->linkid, $parsed->resultid, $parsed->gradeval,
$parsed->ltiid, $parsed->mattempt);
if (!$gradestatus) {
throw new Exception('Grade replace response');
}
$responsexml = qtype_lti_get_response_xml('success', 'Grade replace response', $parsed->messageid, 'replaceResultResponse');
echo $responsexml->asXML();
break;
case 'readResultRequest':
$parsed = qtype_lti_parse_grade_read_message($xml);
$ltiinstance = $DB->get_record('qtype_lti_options', array('id' => $parsed->ltiid));
if (!$ltiinstance) {
throw new Exception('No such tool.');
}
if (!qtype_lti_accepts_grades($ltiinstance)) {
throw new Exception('Tool does not accept grades');
}
qtype_lti_verify_sourcedid($ltiinstance, $parsed);
$grade = qtype_lti_read_grade($parsed->username, $parsed->linkid, $parsed->resultid, $parsed->mattempt);
$responsexml = qtype_lti_get_response_xml('success', 'Result read', $parsed->messageid, 'readResultResponse');
$node = $responsexml->imsx_POXBody->readResultResponse;
$node = $node->addChild('result')->addChild('resultScore');
$node->addChild('language', 'en');
$node->addChild('textString', isset($grade) ? $grade : '');
echo $responsexml->asXML();
break;
case 'deleteResultRequest':
$parsed = qtype_lti_parse_grade_delete_message($xml);
$ltiinstance = $DB->get_record('qtype_lti_options', array('id' => $parsed->ltiid));
if (!$ltiinstance) {
throw new Exception('No such tool.');
}
if (!qtype_lti_accepts_grades($ltiinstance)) {
throw new Exception('Tool does not accept grades');
}
qtype_lti_verify_sourcedid($ltiinstance, $parsed);
qtype_lti_set_session_user($parsed->username);
$gradestatus = qtype_lti_delete_grade($parsed->username, $parsed->linkid, $parsed->resultid, $parsed->mattempt);
if (!$gradestatus) {
throw new Exception('Grade delete request was not successful');
}
$responsexml = qtype_lti_get_response_xml('success', 'Grade delete request', $parsed->messageid, 'deleteResultResponse');
echo $responsexml->asXML();
break;
default:
// Fire an event if we get a web service request which we don't support directly.
// This will allow others to extend the LTI services, which I expect to be a common
// use case, at least until the spec matures.
$data = new stdClass();
$data->body = $rawbody;
$data->xml = $xml;
$data->messageid = qtype_lti_parse_message_id($xml);
$data->messagetype = $messagetype;
$data->consumerkey = $consumerkey;
$data->sharedsecret = $sharedsecret;
$eventdata = array();
$eventdata['other'] = array();
$eventdata['other']['messageid'] = $data->messageid;
$eventdata['other']['messagetype'] = $messagetype;
$eventdata['other']['consumerkey'] = $consumerkey;
// Before firing the event, allow subplugins a chance to handle.
if (qtype_lti_extend_lti_services($data)) {
break;
}
// If an event handler handles the web service, it should set this global to true
// So this code knows whether to send an "operation not supported" or not.
global $ltiwebservicehandled;
$ltiwebservicehandled = false;
try {
$event = \qtype_lti\event\unknown_service_api_called::create($eventdata);
$event->set_message_data($data);
$event->trigger();
} catch (Exception $e) {
$ltiwebservicehandled = false;
}
if (!$ltiwebservicehandled) {
$responsexml = qtype_lti_get_response_xml('unsupported', 'unsupported', qtype_lti_parse_message_id($xml), $messagetype);
echo $responsexml->asXML();
}
break;
}