2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This file responds to a login authentication request
21 * @copyright 2019 Stephen Vickers
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__
. '/../../config.php');
26 require_once($CFG->dirroot
. '/mod/lti/locallib.php');
28 $scope = optional_param('scope', '', PARAM_TEXT
);
29 $responsetype = optional_param('response_type', '', PARAM_TEXT
);
30 $clientid = optional_param('client_id', '', PARAM_TEXT
);
31 $redirecturi = optional_param('redirect_uri', '', PARAM_URL
);
32 $loginhint = optional_param('login_hint', '', PARAM_TEXT
);
33 $ltimessagehint = optional_param('lti_message_hint', 0, PARAM_INT
);
34 $state = optional_param('state', '', PARAM_TEXT
);
35 $responsemode = optional_param('response_mode', '', PARAM_TEXT
);
36 $nonce = optional_param('nonce', '', PARAM_TEXT
);
37 $prompt = optional_param('prompt', '', PARAM_TEXT
);
39 $ok = !empty($scope) && !empty($responsetype) && !empty($clientid) &&
40 !empty($redirecturi) && !empty($loginhint) &&
41 !empty($nonce) && !empty($SESSION->lti_message_hint
);
44 $error = 'invalid_request';
46 if ($ok && ($scope !== 'openid')) {
48 $error = 'invalid_scope';
50 if ($ok && ($responsetype !== 'id_token')) {
52 $error = 'unsupported_response_type';
55 list($courseid, $typeid, $id, $titleb64, $textb64) = explode(',', $SESSION->lti_message_hint
, 5);
56 $ok = ($id !== $ltimessagehint);
58 $error = 'invalid_request';
60 $config = lti_get_type_type_config($typeid);
61 $ok = ($clientid === $config->lti_clientid
);
63 $error = 'unauthorized_client';
67 if ($ok && ($loginhint !== $USER->id
)) {
69 $error = 'access_denied';
72 // If we're unable to load up config; we cannot trust the redirect uri for POSTing to.
74 throw new moodle_exception('invalidrequest', 'error');
76 $uris = array_map("trim", explode("\n", $config->lti_redirectionuris
));
77 if (!in_array($redirecturi, $uris)) {
78 throw new moodle_exception('invalidrequest', 'error');
82 if (isset($responsemode)) {
83 $ok = ($responsemode === 'form_post');
85 $error = 'invalid_request';
86 $desc = 'Invalid response_mode';
90 $error = 'invalid_request';
91 $desc = 'Missing response_mode';
94 if ($ok && !empty($prompt) && ($prompt !== 'none')) {
96 $error = 'invalid_request';
97 $desc = 'Invalid prompt';
101 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST
);
103 $cm = get_coursemodule_from_id('lti', $id, 0, false, MUST_EXIST
);
104 $context = context_module
::instance($cm->id
);
105 require_login($course, true, $cm);
106 require_capability('mod/lti:view', $context);
107 $lti = $DB->get_record('lti', array('id' => $cm->instance
), '*', MUST_EXIST
);
108 list($endpoint, $params) = lti_get_launch_data($lti, $nonce);
110 require_login($course);
111 $context = context_course
::instance($courseid);
112 require_capability('moodle/course:manageactivities', $context);
113 require_capability('mod/lti:addcoursetool', $context);
114 // Set the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
116 'course' => $courseid,
118 'sesskey' => sesskey()
120 $returnurl = new \
moodle_url('/mod/lti/contentitem_return.php', $returnurlparams);
121 // Prepare the request.
122 $title = base64_decode($titleb64);
123 $text = base64_decode($textb64);
124 $request = lti_build_content_item_selection_request($typeid, $course, $returnurl, $title, $text,
125 [], [], false, true, false, false, false, $nonce);
126 $endpoint = $request->url
;
127 $params = $request->params
;
130 $params['error'] = $error;
132 $params['error_description'] = $desc;
136 $params['state'] = $state;
138 unset($SESSION->lti_message_hint
);
139 $r = '<form action="' . $redirecturi . "\" name=\"ltiAuthForm\" id=\"ltiAuthForm\" " .
140 "method=\"post\" enctype=\"application/x-www-form-urlencoded\">\n";
141 if (!empty($params)) {
142 foreach ($params as $key => $value) {
143 $key = htmlspecialchars($key);
144 $value = htmlspecialchars($value);
145 $r .= " <input type=\"hidden\" name=\"{$key}\" value=\"{$value}\"/>\n";
149 $r .= "<script type=\"text/javascript\">\n" .
151 "document.ltiAuthForm.submit();\n" .