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 |
* LTI Advantage Initiate Dynamic Registration endpoint.
|
|
|
19 |
*
|
|
|
20 |
* https://www.imsglobal.org/spec/lti-dr/v1p0
|
|
|
21 |
*
|
|
|
22 |
* This endpoint handles the Registration Initiation Launch, in which a platform (via the user agent) sends their
|
|
|
23 |
* OpenID config URL and an optional registration token (to be used as the access token in the registration request).
|
|
|
24 |
*
|
|
|
25 |
* The code then makes the required dynamic registration calls, namely:
|
|
|
26 |
* 1. It fetches the platform's OpenID config by making a GET request to the provided OpenID config URL.
|
|
|
27 |
* 2. It then POSTS a client registration request (along with the registration token provided by the platform),
|
|
|
28 |
*
|
|
|
29 |
* Finally, the code returns to the user agent signalling a completed registration, via a HTML5 web message
|
|
|
30 |
* (postMessage). This lets the browser know the window may be closed.
|
|
|
31 |
*
|
|
|
32 |
* @package enrol_lti
|
|
|
33 |
* @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
|
|
|
37 |
use core\context\system;
|
|
|
38 |
use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
|
|
|
39 |
use enrol_lti\local\ltiadvantage\repository\context_repository;
|
|
|
40 |
use enrol_lti\local\ltiadvantage\repository\deployment_repository;
|
|
|
41 |
use enrol_lti\local\ltiadvantage\repository\resource_link_repository;
|
|
|
42 |
use enrol_lti\local\ltiadvantage\repository\user_repository;
|
|
|
43 |
use enrol_lti\local\ltiadvantage\service\application_registration_service;
|
|
|
44 |
|
|
|
45 |
require_once(__DIR__."/../../config.php");
|
|
|
46 |
global $OUTPUT, $PAGE, $CFG, $SITE;
|
|
|
47 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
48 |
|
|
|
49 |
$PAGE->set_context(context_system::instance());
|
|
|
50 |
$PAGE->set_pagelayout('popup');
|
|
|
51 |
|
|
|
52 |
// URL to the platform's OpenID configuration.
|
|
|
53 |
$openidconfigurl = required_param('openid_configuration', PARAM_URL);
|
|
|
54 |
|
|
|
55 |
// Token generated by the platform, which must be sent back in registration request. This is opaque to the tool.
|
|
|
56 |
$regtoken = optional_param('registration_token', null, PARAM_RAW);
|
|
|
57 |
|
|
|
58 |
// Moodle-specific token used to secure the dynamic registration URL.
|
|
|
59 |
$token = required_param('token', PARAM_ALPHANUM);
|
|
|
60 |
|
|
|
61 |
$appregservice = new application_registration_service(
|
|
|
62 |
new application_registration_repository(),
|
|
|
63 |
new deployment_repository(),
|
|
|
64 |
new resource_link_repository(),
|
|
|
65 |
new context_repository(),
|
|
|
66 |
new user_repository()
|
|
|
67 |
);
|
|
|
68 |
|
|
|
69 |
// Using the application registration repo, find the incomplete registration using its unique id.
|
|
|
70 |
$appregrepo = new application_registration_repository();
|
|
|
71 |
$draftreg = $appregrepo->find_by_uniqueid($token);
|
|
|
72 |
if (is_null($draftreg) || $draftreg->is_complete()) {
|
|
|
73 |
throw new moodle_exception('invalidexpiredregistrationurl', 'enrol_lti');
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
// Get the OpenID config from the platform.
|
|
|
77 |
$curl = new curl();
|
|
|
78 |
$openidconfig = $curl->get($openidconfigurl);
|
|
|
79 |
$errno = $curl->get_errno();
|
|
|
80 |
if ($errno !== 0) {
|
|
|
81 |
throw new coding_exception("Error '{$errno}' while getting OpenID config from platform: {$openidconfig}");
|
|
|
82 |
}
|
|
|
83 |
$openidconfig = json_decode($openidconfig);
|
|
|
84 |
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
85 |
throw new moodle_exception('ltiadvdynregerror:invalidopenidconfigjson', 'enrol_lti');
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$regendpoint = $openidconfig->registration_endpoint ?? null;
|
|
|
89 |
if (empty($regendpoint)) {
|
|
|
90 |
throw new coding_exception('Missing registration endpoint in OpenID configuration');
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// Build the client registration request to send to the platform.
|
|
|
94 |
$wwwrooturl = $CFG->wwwroot;
|
|
|
95 |
$parsed = parse_url($wwwrooturl);
|
|
|
96 |
$sitefullname = format_string(get_site()->fullname);
|
|
|
97 |
$scopes = [
|
|
|
98 |
'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem',
|
|
|
99 |
'https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly',
|
|
|
100 |
'https://purl.imsglobal.org/spec/lti-ags/scope/score',
|
|
|
101 |
'https://purl.imsglobal.org/spec/lti-nrps/scope/contextmembership.readonly',
|
|
|
102 |
];
|
|
|
103 |
|
|
|
104 |
$regrequest = (object) [
|
|
|
105 |
'application_type' => 'web',
|
|
|
106 |
'grant_types' => ['client_credentials', 'implicit'],
|
|
|
107 |
'response_types' => ['id_token'],
|
|
|
108 |
'initiate_login_uri' => $CFG->wwwroot . '/enrol/lti/login.php?id=' . $draftreg->get_uniqueid(),
|
|
|
109 |
'redirect_uris' => [
|
|
|
110 |
$CFG->wwwroot . '/enrol/lti/launch.php',
|
|
|
111 |
$CFG->wwwroot . '/enrol/lti/launch_deeplink.php',
|
|
|
112 |
],
|
|
|
113 |
// TODO: Consider whether to support client_name#ja syntax for multi language support - see MDL-73109.
|
|
|
114 |
'client_name' => format_string($SITE->fullname, true, ['context' => system::instance()]),
|
|
|
115 |
'jwks_uri' => $CFG->wwwroot . '/enrol/lti/jwks.php',
|
|
|
116 |
'logo_uri' => $OUTPUT->get_compact_logo_url() ? $OUTPUT->get_compact_logo_url()->out(false) : '',
|
|
|
117 |
'token_endpoint_auth_method' => 'private_key_jwt',
|
|
|
118 |
'scope' => implode(" ", $scopes),
|
|
|
119 |
'https://purl.imsglobal.org/spec/lti-tool-configuration' => [
|
|
|
120 |
'domain' => $parsed['host'],
|
|
|
121 |
'target_link_uri' => $CFG->wwwroot . '/enrol/lti/launch.php',
|
|
|
122 |
'claims' => [
|
|
|
123 |
'iss',
|
|
|
124 |
'sub',
|
|
|
125 |
'aud',
|
|
|
126 |
'given_name',
|
|
|
127 |
'family_name',
|
|
|
128 |
'email',
|
|
|
129 |
'picture',
|
|
|
130 |
],
|
|
|
131 |
'messages' => [
|
|
|
132 |
(object) [
|
|
|
133 |
'type' => 'LtiDeepLinkingRequest',
|
|
|
134 |
'allowLearner' => false,
|
|
|
135 |
'target_link_uri' => $CFG->wwwroot . '/enrol/lti/launch_deeplink.php',
|
|
|
136 |
// TODO: Consider whether to support label#ja syntax for multi language support - see MDL-73109.
|
|
|
137 |
'label' => get_string('registrationdeeplinklabel', 'enrol_lti', $sitefullname),
|
|
|
138 |
'placements' => [
|
|
|
139 |
"ContentArea"
|
|
|
140 |
],
|
|
|
141 |
],
|
|
|
142 |
(object) [
|
|
|
143 |
'type' => 'LtiResourceLinkRequest',
|
|
|
144 |
'allowLearner' => true,
|
|
|
145 |
'target_link_uri' => $CFG->wwwroot . '/enrol/lti/launch.php',
|
|
|
146 |
// TODO: Consider whether to support label#ja syntax for multi language support - see MDL-73109.
|
|
|
147 |
'label' => get_string('registrationresourcelinklabel', 'enrol_lti', $sitefullname),
|
|
|
148 |
'placements' => [
|
|
|
149 |
"ContentArea"
|
|
|
150 |
],
|
|
|
151 |
],
|
|
|
152 |
]
|
|
|
153 |
]
|
|
|
154 |
];
|
|
|
155 |
|
|
|
156 |
if (!is_null($regtoken)) {
|
|
|
157 |
$curl->setHeader(['Authorization: Bearer ' . $regtoken]);
|
|
|
158 |
}
|
|
|
159 |
$curl->setHeader('Content-Type: application/json');
|
|
|
160 |
$regrequest = json_encode($regrequest);
|
|
|
161 |
$regresponse = $curl->post($regendpoint, $regrequest);
|
|
|
162 |
$errno = $curl->get_errno();
|
|
|
163 |
if ($errno !== 0) {
|
|
|
164 |
throw new coding_exception("Error '{$errno}' while posting client registration request to client: {$regresponse}");
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
if ($regresponse) {
|
|
|
168 |
$regresponse = json_decode($regresponse);
|
|
|
169 |
if ($regresponse->client_id) {
|
|
|
170 |
$toolconfig = $regresponse->{'https://purl.imsglobal.org/spec/lti-tool-configuration'};
|
|
|
171 |
|
|
|
172 |
if ($appregrepo->find_by_platform($openidconfig->issuer, $regresponse->client_id)) {
|
|
|
173 |
throw new moodle_exception('existingregistrationerror', 'enrol_lti');
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
// Registration of the tool on the platform was successful.
|
|
|
177 |
// Now update the platform details in the registration and mark it complete.
|
|
|
178 |
$draftreg->set_accesstokenurl(new moodle_url($openidconfig->token_endpoint));
|
|
|
179 |
$draftreg->set_authenticationrequesturl(new moodle_url($openidconfig->authorization_endpoint));
|
|
|
180 |
$draftreg->set_clientid($regresponse->client_id);
|
|
|
181 |
$draftreg->set_jwksurl(new moodle_url($openidconfig->jwks_uri));
|
|
|
182 |
$draftreg->set_platformid(new moodle_url($openidconfig->issuer));
|
|
|
183 |
$draftreg->complete_registration();
|
|
|
184 |
$appreg = $appregrepo->save($draftreg);
|
|
|
185 |
|
|
|
186 |
// Deployment id is optional.
|
|
|
187 |
// If this isn't provided by the platform at this time, it must be manually set in Site admin before launches can happen.
|
|
|
188 |
if (!empty($toolconfig->deployment_id)) {
|
|
|
189 |
$deployment = $appreg->add_tool_deployment($toolconfig->deployment_id, $toolconfig->deployment_id);
|
|
|
190 |
$deploymentrepo = new deployment_repository();
|
|
|
191 |
$deploymentrepo->save($deployment);
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
echo "<script>
|
|
|
197 |
(window.opener || window.parent).postMessage({subject: 'org.imsglobal.lti.close'}, '*');
|
|
|
198 |
</script>";
|