MDL-53777 tool_mobile: New script for launching the mobile app
[moodle.git] / login / token.php
blob13ea7fcd07fa67f037717a05042f978f446d28c9
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 * Return token
19 * @package moodlecore
20 * @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 define('AJAX_SCRIPT', true);
25 define('REQUIRE_CORRECT_ACCESS', true);
26 define('NO_MOODLE_COOKIES', true);
28 require_once(__DIR__ . '/../config.php');
29 require_once($CFG->libdir . '/externallib.php');
31 // Allow CORS requests.
32 header('Access-Control-Allow-Origin: *');
34 $username = required_param('username', PARAM_USERNAME);
35 $password = required_param('password', PARAM_RAW);
36 $serviceshortname = required_param('service', PARAM_ALPHANUMEXT);
38 echo $OUTPUT->header();
40 if (!$CFG->enablewebservices) {
41 throw new moodle_exception('enablewsdescription', 'webservice');
43 $username = trim(core_text::strtolower($username));
44 if (is_restored_user($username)) {
45 throw new moodle_exception('restoredaccountresetpassword', 'webservice');
47 $user = authenticate_user_login($username, $password);
48 if (!empty($user)) {
50 // Cannot authenticate unless maintenance access is granted.
51 $hasmaintenanceaccess = has_capability('moodle/site:maintenanceaccess', context_system::instance(), $user);
52 if (!empty($CFG->maintenance_enabled) and !$hasmaintenanceaccess) {
53 throw new moodle_exception('sitemaintenance', 'admin');
56 if (isguestuser($user)) {
57 throw new moodle_exception('noguest');
59 if (empty($user->confirmed)) {
60 throw new moodle_exception('usernotconfirmed', 'moodle', '', $user->username);
62 // check credential expiry
63 $userauth = get_auth_plugin($user->auth);
64 if (!empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
65 $days2expire = $userauth->password_expire($user->username);
66 if (intval($days2expire) < 0 ) {
67 throw new moodle_exception('passwordisexpired', 'webservice');
71 // Check whether the user should be changing password.
72 if (get_user_preferences('auth_forcepasswordchange', false, $user)) {
73 if ($userauth->can_change_password()) {
74 throw new moodle_exception('forcepasswordchangenotice');
75 } else {
76 throw new moodle_exception('nopasswordchangeforced', 'auth');
80 // let enrol plugins deal with new enrolments if necessary
81 enrol_check_plugins($user);
83 // setup user session to check capability
84 \core\session\manager::set_user($user);
86 //check if the service exists and is enabled
87 $service = $DB->get_record('external_services', array('shortname' => $serviceshortname, 'enabled' => 1));
88 if (empty($service)) {
89 // will throw exception if no token found
90 throw new moodle_exception('servicenotavailable', 'webservice');
93 // Get an existing token or create a new one.
94 $token = external_generate_token_for_current_user($service);
96 // log token access
97 $DB->set_field('external_tokens', 'lastaccess', time(), array('id'=>$token->id));
99 $params = array(
100 'objectid' => $token->id,
102 $event = \core\event\webservice_token_sent::create($params);
103 $event->add_record_snapshot('external_tokens', $token);
104 $event->trigger();
106 $usertoken = new stdClass;
107 $usertoken->token = $token->token;
108 echo json_encode($usertoken);
109 } else {
110 throw new moodle_exception('invalidlogin');