MDL-68973 versions: Fix other small details
[moodle.git] / badges / oauth2callback.php
blobb3b8153f34941aa320dde2918a0f0a6aab4bc877
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 * Verify authorization callback.
20 * @package core_badges
21 * @copyright 2020 Tung Thai
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @author Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
25 require_once(__DIR__ . '/../config.php');
27 $error = optional_param('error', '', PARAM_RAW);
29 if ($error) {
30 $message = optional_param('error_description', '', PARAM_RAW);
31 if ($message) {
32 print_error($message);
33 } else {
34 print_error($error);
36 die();
39 require_login();
41 // The authorization code generated by the authorization server.
42 $code = required_param('code', PARAM_RAW);
43 $scope = required_param('scope', PARAM_RAW);
45 // The state parameter we've given (used in moodle as a redirect url).
46 $state = required_param('state', PARAM_LOCALURL);
48 $redirecturl = new moodle_url($state);
49 $params = $redirecturl->params();
51 if (isset($params['sesskey']) and confirm_sesskey($params['sesskey'])) {
52 $redirecturl->param('oauth2code', $code);
53 $redirecturl->param('scope', $scope);
54 redirect($redirecturl);
55 } else {
56 print_error('invalidsesskey');