Automatically generated installer lang files
[moodle.git] / login / token.php
blobbf6fec5d20084b1089de68d54f764505a6cd4c18
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 $reason = null;
51 $user = authenticate_user_login($username, $password, false, $reason, false);
52 if (!empty($user)) {
54 // Cannot authenticate unless maintenance access is granted.
55 $hasmaintenanceaccess = has_capability('moodle/site:maintenanceaccess', $systemcontext, $user);
56 if (!empty($CFG->maintenance_enabled) and !$hasmaintenanceaccess) {
57 throw new moodle_exception('sitemaintenance', 'admin');
60 if (isguestuser($user)) {
61 throw new moodle_exception('noguest');
63 if (empty($user->confirmed)) {
64 throw new moodle_exception('usernotconfirmed', 'moodle', '', $user->username);
66 // check credential expiry
67 $userauth = get_auth_plugin($user->auth);
68 if (!empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
69 $days2expire = $userauth->password_expire($user->username);
70 if (intval($days2expire) < 0 ) {
71 throw new moodle_exception('passwordisexpired', 'webservice');
75 // let enrol plugins deal with new enrolments if necessary
76 enrol_check_plugins($user);
78 // setup user session to check capability
79 \core\session\manager::set_user($user);
81 //check if the service exists and is enabled
82 $service = $DB->get_record('external_services', array('shortname' => $serviceshortname, 'enabled' => 1));
83 if (empty($service)) {
84 // will throw exception if no token found
85 throw new moodle_exception('servicenotavailable', 'webservice');
88 // Get an existing token or create a new one.
89 $token = external_generate_token_for_current_user($service);
90 $privatetoken = $token->privatetoken;
91 external_log_token_request($token);
93 $siteadmin = has_capability('moodle/site:config', $systemcontext, $USER->id);
95 $usertoken = new stdClass;
96 $usertoken->token = $token->token;
97 // Private token, only transmitted to https sites and non-admin users.
98 if (is_https() and !$siteadmin) {
99 $usertoken->privatetoken = $privatetoken;
100 } else {
101 $usertoken->privatetoken = null;
103 echo json_encode($usertoken);
104 } else {
105 throw new moodle_exception('invalidlogin');