1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* This file receives a registration request along with the registration token and returns a client_id.
|
|
|
19 |
*
|
|
|
20 |
* @copyright 2020 Claude Vervoort (Cengage), Carlos Costa, Adrian Hutchinson (Macgraw Hill)
|
|
|
21 |
* @package mod_lti
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
define('NO_DEBUG_DISPLAY', true);
|
|
|
25 |
define('NO_MOODLE_COOKIES', true);
|
|
|
26 |
|
|
|
27 |
use mod_lti\local\ltiopenid\registration_helper;
|
|
|
28 |
use mod_lti\local\ltiopenid\registration_exception;
|
|
|
29 |
|
|
|
30 |
require_once(__DIR__ . '/../../config.php');
|
|
|
31 |
require_once($CFG->dirroot . '/mod/lti/locallib.php');
|
|
|
32 |
|
|
|
33 |
$code = 200;
|
|
|
34 |
$message = '';
|
|
|
35 |
if ($_SERVER['REQUEST_METHOD'] === 'POST' or ($_SERVER['REQUEST_METHOD'] === 'GET')) {
|
|
|
36 |
$doregister = $_SERVER['REQUEST_METHOD'] === 'POST';
|
|
|
37 |
// Retrieve registration token from Bearer Authorization header.
|
|
|
38 |
$authheader = moodle\mod\lti\OAuthUtil::get_headers()['Authorization'] ?? '';
|
|
|
39 |
if (!($authheader && substr($authheader, 0, 7) == 'Bearer ')) {
|
|
|
40 |
$message = 'missing_registration_token';
|
|
|
41 |
$code = 401;
|
|
|
42 |
} else {
|
|
|
43 |
|
|
|
44 |
// Registers tool.
|
|
|
45 |
try {
|
|
|
46 |
$tokenres = registration_helper::get()->validate_registration_token(trim(substr($authheader, 7)));
|
|
|
47 |
$type = new stdClass();
|
|
|
48 |
$type->state = LTI_TOOL_STATE_PENDING;
|
|
|
49 |
if (array_key_exists('type', $tokenres)) {
|
|
|
50 |
$type = $tokenres['type'];
|
|
|
51 |
}
|
|
|
52 |
if ($doregister) {
|
|
|
53 |
$registrationpayload = json_decode(file_get_contents('php://input'), true);
|
|
|
54 |
$config = registration_helper::get()->registration_to_config($registrationpayload, $tokenres['clientid']);
|
|
|
55 |
if ($type->id) {
|
|
|
56 |
lti_update_type($type, clone $config);
|
|
|
57 |
$typeid = $type->id;
|
|
|
58 |
} else {
|
|
|
59 |
$typeid = lti_add_type($type, clone $config);
|
|
|
60 |
}
|
|
|
61 |
header('Content-Type: application/json; charset=utf-8');
|
|
|
62 |
$message = json_encode(registration_helper::get()->config_to_registration((object)$config, $typeid));
|
|
|
63 |
} else if ($type) {
|
|
|
64 |
$config = lti_get_type_config($type->id);
|
|
|
65 |
header('Content-Type: application/json; charset=utf-8');
|
|
|
66 |
$message = json_encode(registration_helper::get()->config_to_registration((object)$config, $type->id, $type));
|
|
|
67 |
} else {
|
|
|
68 |
$code = 404;
|
|
|
69 |
$message = "No registration found.";
|
|
|
70 |
}
|
|
|
71 |
} catch (registration_exception $e) {
|
|
|
72 |
$code = $e->getCode();
|
|
|
73 |
$message = $e->getMessage();
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
} else {
|
|
|
77 |
$code = 400;
|
|
|
78 |
$message = 'Unsupported operation';
|
|
|
79 |
}
|
|
|
80 |
$response = new \mod_lti\local\ltiservice\response();
|
|
|
81 |
// Set code.
|
|
|
82 |
$response->set_code($code);
|
|
|
83 |
// Set body.
|
|
|
84 |
$response->set_body($message);
|
|
|
85 |
$response->send();
|