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 * Launch page, launch the app using custom URL schemes.
20 * If the user is not logged when visiting this page, he will be redirected to the login page.
21 * Once he is logged, he will be redirected again to this page and the app launched via custom URL schemes.
23 * @package tool_mobile
24 * @copyright 2016 Juan Leyva
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once(__DIR__
. '/../../../config.php');
30 $serviceshortname = required_param('service', PARAM_ALPHANUMEXT
);
31 $passport = required_param('passport', PARAM_RAW
); // Passport send from the app to validate the response URL.
32 $urlscheme = optional_param('urlscheme', 'moodlemobile', PARAM_NOTAGS
); // The URL scheme the app supports.
33 $confirmed = optional_param('confirmed', false, PARAM_BOOL
); // If we are being redirected after user confirmation.
34 $oauthsso = optional_param('oauthsso', 0, PARAM_INT
); // Id of the OpenID issuer (for OAuth direct SSO).
36 // Validate that the urlscheme is valid.
37 if (!preg_match('/^[a-zA-Z][a-zA-Z0-9-\+\.]*$/', $urlscheme)) {
38 throw new moodle_exception('Invalid parameter: the value of urlscheme isn\'t valid. ' .
39 'It should start with a letter and can only contain letters, numbers and the characters "." "+" "-".');
42 // Check web services enabled.
43 if (!$CFG->enablewebservices
) {
44 throw new moodle_exception('enablewsdescription', 'webservice');
47 // Check if the service exists and is enabled.
48 $service = $DB->get_record('external_services', ['shortname' => $serviceshortname, 'enabled' => 1]);
49 if (empty($service)) {
50 throw new moodle_exception('servicenotavailable', 'webservice');
53 // Set a cookie indicating that there was a launch to authenticate via the site from the app.
54 $ldata = json_encode(['service' => $serviceshortname, 'passport' => $passport,
55 'urlscheme' => $urlscheme, 'confirmed' => (int) $confirmed, 'oauthsso' => $oauthsso]);
56 $expires = time() +
(15 * MINSECS
); // 15 minutes for authentication should be enough.
57 setcookie('tool_mobile_launch', $ldata, $expires, $CFG->sessioncookiepath
, $CFG->sessioncookiedomain
);
59 // We have been requested to start a SSO process via OpenID.
60 if (!empty($oauthsso) && is_enabled_auth('oauth2')) {
61 $wantsurl = new moodle_url('/admin/tool/mobile/launch.php',
62 array('service' => $serviceshortname, 'passport' => $passport, 'urlscheme' => $urlscheme, 'confirmed' => $confirmed));
63 $oauthurl = new moodle_url('/auth/oauth2/login.php',
64 array('id' => $oauthsso, 'sesskey' => sesskey(), 'wantsurl' => $wantsurl));
65 header('Location: ' . $oauthurl->out(false));
69 // Check if the plugin is properly configured.
70 $typeoflogin = get_config('tool_mobile', 'typeoflogin');
71 if (empty($SESSION->justloggedin
) &&
72 !is_enabled_auth('oauth2') &&
73 $typeoflogin != tool_mobile\api
::LOGIN_VIA_BROWSER
&&
74 $typeoflogin != tool_mobile\api
::LOGIN_VIA_EMBEDDED_BROWSER
) {
75 throw new moodle_exception('pluginnotenabledorconfigured', 'tool_mobile');
78 // If the user is using the inapp (embedded) browser, we need to set the Secure and Partitioned attributes to the session cookie.
79 if (\core_useragent
::is_moodle_app()) {
80 \core\session\utility\cookie_helper
::add_attributes_to_cookie_response_header(
81 cookiename
: "MoodleSession{$CFG->sessioncookie}",
82 attributes
: ['Secure', 'Partitioned'],
86 require_login(0, false);
88 // Require an active user: not guest, not suspended.
89 core_user
::require_active_user($USER);
92 unset($_COOKIE['tool_mobile_launch']);
93 setcookie('tool_mobile_launch', '', -1, $CFG->sessioncookiepath
);
95 // Get an existing token or create a new one.
97 $token = \core_external\util
::generate_token_for_current_user($service);
98 $privatetoken = $token->privatetoken
;
99 \core_external\util
::log_token_request($token);
101 // Don't return the private token if the user didn't just log in and a new token wasn't created.
102 if (empty($SESSION->justloggedin
) and $token->timecreated
< $timenow) {
103 $privatetoken = null;
106 $siteadmin = has_capability('moodle/site:config', context_system
::instance(), $USER->id
);
108 // Passport is generated in the mobile app, so the app opening can be validated using that variable.
109 // Passports are valid only one time, it's deleted in the app once used.
110 $siteid = md5($CFG->wwwroot
. $passport);
111 $apptoken = $siteid . ':::' . $token->token
;
112 if ($privatetoken and is_https() and !$siteadmin) {
113 $apptoken .= ':::' . $privatetoken;
116 $apptoken = base64_encode($apptoken);
118 // Redirect using the custom URL scheme checking first if a URL scheme is forced in the site settings.
119 $forcedurlscheme = get_config('tool_mobile', 'forcedurlscheme');
120 if (!empty($forcedurlscheme)) {
121 $urlscheme = $forcedurlscheme;
124 $location = "$urlscheme://token=$apptoken";
126 // For iOS 10 onwards, we have to simulate a user click.
127 // If we come from the confirmation page, we should display a nicer page.
128 $isios = core_useragent
::is_ios();
129 if ($confirmed or $isios) {
130 $PAGE->set_context(context_system
::instance());
131 $PAGE->set_heading($COURSE->fullname
);
132 $params = array('service' => $serviceshortname, 'passport' => $passport, 'urlscheme' => $urlscheme, 'confirmed' => $confirmed);
133 $PAGE->set_url("/$CFG->admin/tool/mobile/launch.php", $params);
135 echo $OUTPUT->header();
137 $confirmedstr = get_string('confirmed');
138 $PAGE->navbar
->add($confirmedstr);
139 $PAGE->set_title($confirmedstr);
140 echo $OUTPUT->notification($confirmedstr, \core\output\notification
::NOTIFY_SUCCESS
);
141 echo $OUTPUT->box_start('generalbox centerpara boxwidthnormal boxaligncenter');
142 echo $OUTPUT->single_button(new moodle_url('/course/'), get_string('courses'));
143 echo $OUTPUT->box_end();
146 $notice = get_string('clickheretolaunchtheapp', 'tool_mobile');
147 echo $OUTPUT->box(html_writer
::link($location, $notice, ['id' => 'launchapp']), 'generalbox warning centerpara');
148 echo html_writer
::script(
149 "window.onload = function() {
150 document.getElementById('launchapp').click();
153 echo $OUTPUT->footer();
155 // For Android a http redirect will do fine.
156 header('Location: ' . $location);