MDL-62660 tool_dataprivacy: Add ability to expire data requests
[moodle.git] / login / token.php
bloba67b4b1c3cfc911f936996b0f2c346de63f645b8
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');
48 $systemcontext = context_system::instance();
50 $user = authenticate_user_login($username, $password);
51 if (!empty($user)) {
53 // Cannot authenticate unless maintenance access is granted.
54 $hasmaintenanceaccess = has_capability('moodle/site:maintenanceaccess', $systemcontext, $user);
55 if (!empty($CFG->maintenance_enabled) and !$hasmaintenanceaccess) {
56 throw new moodle_exception('sitemaintenance', 'admin');
59 if (isguestuser($user)) {
60 throw new moodle_exception('noguest');
62 if (empty($user->confirmed)) {
63 throw new moodle_exception('usernotconfirmed', 'moodle', '', $user->username);
65 // check credential expiry
66 $userauth = get_auth_plugin($user->auth);
67 if (!empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
68 $days2expire = $userauth->password_expire($user->username);
69 if (intval($days2expire) < 0 ) {
70 throw new moodle_exception('passwordisexpired', 'webservice');
74 // let enrol plugins deal with new enrolments if necessary
75 enrol_check_plugins($user);
77 // setup user session to check capability
78 \core\session\manager::set_user($user);
80 //check if the service exists and is enabled
81 $service = $DB->get_record('external_services', array('shortname' => $serviceshortname, 'enabled' => 1));
82 if (empty($service)) {
83 // will throw exception if no token found
84 throw new moodle_exception('servicenotavailable', 'webservice');
87 // Get an existing token or create a new one.
88 $token = external_generate_token_for_current_user($service);
89 $privatetoken = $token->privatetoken;
90 external_log_token_request($token);
92 $siteadmin = has_capability('moodle/site:config', $systemcontext, $USER->id);
94 $usertoken = new stdClass;
95 $usertoken->token = $token->token;
96 // Private token, only transmitted to https sites and non-admin users.
97 if (is_https() and !$siteadmin) {
98 $usertoken->privatetoken = $privatetoken;
99 } else {
100 $usertoken->privatetoken = null;
102 echo json_encode($usertoken);
103 } else {
104 throw new moodle_exception('invalidlogin');