MDL-56905 tool_mobile: Return private tokens just after log-in
[moodle.git] / admin / tool / mobile / launch.php
blobb574849e0fa880e03a55b953762c1d349e6023d2
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 * 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');
29 require_once($CFG->libdir . '/externallib.php');
31 $serviceshortname = required_param('service', PARAM_ALPHANUMEXT);
32 $passport = required_param('passport', PARAM_RAW); // Passport send from the app to validate the response URL.
33 $urlscheme = optional_param('urlscheme', 'moodlemobile', PARAM_NOTAGS); // The URL scheme the app supports.
35 // Check web services enabled.
36 if (!$CFG->enablewebservices) {
37 throw new moodle_exception('enablewsdescription', 'webservice');
40 // Check if the plugin is properly configured.
41 $typeoflogin = get_config('tool_mobile', 'typeoflogin');
42 if ($typeoflogin != tool_mobile\api::LOGIN_VIA_BROWSER and
43 $typeoflogin != tool_mobile\api::LOGIN_VIA_EMBEDDED_BROWSER) {
44 throw new moodle_exception('pluginnotenabledorconfigured', 'tool_mobile');
47 // Check if the service exists and is enabled.
48 $service = $DB->get_record('external_services', array('shortname' => $serviceshortname, 'enabled' => 1));
49 if (empty($service)) {
50 throw new moodle_exception('servicenotavailable', 'webservice');
53 require_login(0, false);
55 // Require an active user: not guest, not suspended.
56 core_user::require_active_user($USER);
58 // Get an existing token or create a new one.
59 $timenow = time();
60 $token = external_generate_token_for_current_user($service);
61 $privatetoken = $token->privatetoken;
62 external_log_token_request($token);
64 // Don't return the private token if the user didn't just log in and a new token wasn't created.
65 if (empty($SESSION->justloggedin) and $token->timecreated < $timenow) {
66 $privatetoken = null;
69 $siteadmin = has_capability('moodle/site:config', context_system::instance(), $USER->id);
71 // Passport is generated in the mobile app, so the app opening can be validated using that variable.
72 // Passports are valid only one time, it's deleted in the app once used.
73 $siteid = md5($CFG->wwwroot . $passport);
74 $apptoken = $siteid . ':::' . $token->token;
75 if ($privatetoken and is_https() and !$siteadmin) {
76 $apptoken .= ':::' . $privatetoken;
79 $apptoken = base64_encode($apptoken);
81 // Redirect using the custom URL scheme checking first if a URL scheme is forced in the site settings.
82 $forcedurlscheme = get_config('tool_mobile', 'forcedurlscheme');
83 if (!empty($forcedurlscheme)) {
84 $urlscheme = $forcedurlscheme;
87 $location = "$urlscheme://token=$apptoken";
89 // For iOS 10 onwards, we have to simulate a user click.
90 if (core_useragent::is_ios()) {
91 $PAGE->set_context(null);
92 $PAGE->set_url('/local/mobile/launch.php', array('service' => $serviceshortname, 'passport' => $passport, 'urlscheme' => $urlscheme));
94 echo $OUTPUT->header();
95 $notice = get_string('clickheretolaunchtheapp', 'tool_mobile');
96 echo html_writer::link($location, $notice, array('id' => 'launchapp'));
97 echo html_writer::script(
98 "window.onload = function() {
99 document.getElementById('launchapp').click();
102 echo $OUTPUT->footer();
103 } else {
104 // For Android a http redirect will do fine.
105 header('Location: ' . $location);
106 die;