MDL-76113 enrol_lti: Fix secret on lti user for LTI 2.0 consumers
[moodle.git] / enrol / lti / tool.php
blob6730d474f1c081adc4a30a30e51ea47d001d8495
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.
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/>.
17 /**
18 * The main entry point for the external system.
20 * @package enrol_lti
21 * @copyright 2016 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../../config.php');
27 $toolid = required_param('id', PARAM_INT);
29 $PAGE->set_context(context_system::instance());
30 $url = new moodle_url('/enrol/lti/tool.php');
31 $PAGE->set_url($url);
32 $PAGE->set_pagelayout('popup');
33 $PAGE->set_title(get_string('opentool', 'enrol_lti'));
35 // Get the tool.
36 $tool = \enrol_lti\helper::get_lti_tool($toolid);
38 // Check if the authentication plugin is disabled.
39 if (!is_enabled_auth('lti')) {
40 throw new \moodle_exception('pluginnotenabled', 'auth', '', get_string('pluginname', 'auth_lti'));
41 exit();
44 // Check if the enrolment plugin is disabled.
45 if (!enrol_is_enabled('lti')) {
46 throw new \moodle_exception('enrolisdisabled', 'enrol_lti');
47 exit();
50 // Check if the enrolment instance is disabled.
51 if ($tool->status != ENROL_INSTANCE_ENABLED) {
52 throw new \moodle_exception('enrolisdisabled', 'enrol_lti');
53 exit();
56 $consumerkey = required_param('oauth_consumer_key', PARAM_TEXT);
57 $ltiversion = optional_param('lti_version', null, PARAM_TEXT);
58 $messagetype = required_param('lti_message_type', PARAM_TEXT);
60 // Only accept launch requests from this endpoint.
61 if ($messagetype != "basic-lti-launch-request") {
62 throw new \moodle_exception('invalidrequest', 'enrol_lti');
63 exit();
66 // Initialise tool provider.
67 $toolprovider = new \enrol_lti\tool_provider($toolid);
69 // Special handling for LTIv1 launch requests.
70 if ($ltiversion === \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1) {
71 $dataconnector = new \enrol_lti\data_connector();
72 $consumer = new \IMSGlobal\LTI\ToolProvider\ToolConsumer($consumerkey, $dataconnector);
73 // Check if the consumer has already been registered to the enrol_lti_lti2_consumer table. Register if necessary.
74 $consumer->ltiVersion = \IMSGlobal\LTI\ToolProvider\ToolProvider::LTI_VERSION1;
75 // For LTIv1, set the tool secret as the consumer secret.
76 $consumer->secret = $tool->secret;
77 $consumer->name = optional_param('tool_consumer_instance_name', '', PARAM_TEXT);
78 $consumer->consumerName = $consumer->name;
79 $consumer->consumerGuid = optional_param('tool_consumer_instance_guid', null, PARAM_TEXT);
80 $consumer->consumerVersion = optional_param('tool_consumer_info_version', null, PARAM_TEXT);
81 $consumer->enabled = true;
82 $consumer->protected = true;
83 $consumer->save();
85 // Set consumer to tool provider.
86 $toolprovider->consumer = $consumer;
87 // Map tool consumer and published tool, if necessary.
88 $toolprovider->map_tool_to_consumer();
91 // Handle the request.
92 $toolprovider->handleRequest();
94 echo $OUTPUT->header();
95 echo $OUTPUT->footer();