-
Notifications
You must be signed in to change notification settings - Fork 36
/
auth.php
216 lines (174 loc) · 8.22 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library of interface functions and constants for module jitsi
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
*
* All the jitsi specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod_jitsi
* @copyright 2021 Sergio Comerón Sánchez-Paniagua <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(dirname(__FILE__))).'/config.php');
require_once(dirname(dirname(dirname(__FILE__))).'/lib/moodlelib.php');
require_once(dirname(__FILE__).'/lib.php');
require_once(__DIR__ . '/api/vendor/autoload.php');
require_login();
global $DB, $CFG;
$name = optional_param('name', null, PARAM_TEXT);
$PAGE->set_context(context_system::instance());
$tokensessionkey = 'token-' . "https://www.googleapis.com/auth/youtube";
if ($name) {
if (!file_exists(__DIR__ . '/api/vendor/autoload.php')) {
throw new \Exception('Api client not found on '.$CFG->wwwroot.'/mod/jitsi/api/vendor/autoload.php');
}
$accountbyname = $DB->get_record('jitsi_record_account', ['name' => $name]);
if ($accountbyname) {
if ($accountbyname->inuse == 1 && $accountbyname->clientaccesstoken == null && $accountbyname->clientrefreshtoken == null) {
$accountbyname->inuse = 0;
$DB->update_record('jitsi_record_account', $accountbyname);
}
}
$accountinuse = $DB->get_record('jitsi_record_account', ['inuse' => 1]);
unset($_SESSION[$tokensessionkey]);
if ($accountinuse) {
$client = new Google_Client();
$client->setClientId($CFG->jitsi_oauth_id);
$client->setClientSecret($CFG->jitsi_oauth_secret);
$tokensessionkey = 'token-' . "https://www.googleapis.com/auth/youtube";
$client->setAccessToken($accountinuse->clientaccesstoken);
$t = time();
$timediff = $t - $accountinuse->tokencreated;
if ($timediff > 3599) {
$newaccesstoken = $client->fetchAccessTokenWithRefreshToken($accountinuse->clientrefreshtoken);
$accountinuse->clientaccesstoken = $newaccesstoken['access_token'];
$newrefreshaccesstoken = $client->getRefreshToken();
$accountinuse->refreshtoken = $newrefreshaccesstoken;
$accountinuse->tokencreated = time();
$DB->update_record('jitsi_record_account', $accountinuse);
}
$accountinuse->inuse = 0;
$DB->update_record('jitsi_record_account', $accountinuse);
}
$_SESSION['name'] = $name;
}
$accounttab = $DB->get_record('jitsi_record_account', ['name' => $_SESSION['name']]);
if (!$accounttab) {
$_SESSION[$tokensessionkey] = null;
}
if ($CFG->jitsi_oauth_id == null || $CFG->jitsi_oauth_secret == null) {
echo "Empty parameters 'jitsi_oauth_id' & 'jitsi_oauth_secret'";
} else {
if (!file_exists(__DIR__ . '/api/vendor/autoload.php')) {
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
}
require_once(__DIR__ . '/api/vendor/autoload.php');
$oauth2clientid = $CFG->jitsi_oauth_id;
$oauth2clientsecret = $CFG->jitsi_oauth_secret;
$client = new Google_Client();
$client->setClientId($oauth2clientid);
$client->setClientSecret($oauth2clientsecret);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setAccessType("offline");
$httparray = explode(":", $CFG->wwwroot);
$principiohttp = $httparray[0].'://';
$redirect = filter_var($principiohttp . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);
$tokensessionkey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
$paramstring = base64UrlDecode($_GET['state']);
$paramarray = explode("&", $paramstring);
$randstring = $paramarray[1];
$namestring = $paramarray[0];
$randarray = explode("=", $randstring);
$namearray = explode("=", $namestring);
$rand = $randarray[1];
$name = $namearray[1];
if (strval($_SESSION['rand']) !== strval($rand)) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION[$tokensessionkey] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION[$tokensessionkey])) {
$client->setAccessToken($_SESSION[$tokensessionkey]);
}
if ($client->getAccessToken()) {
try {
$PAGE->set_url('/mod/jitsi/auth.php');
$PAGE->set_title(format_string(get_string('accounts', 'jitsi')));
$PAGE->set_heading(format_string(get_string('accounts', 'jitsi')));
echo $OUTPUT->header();
$accesstoken = $client->getAccessToken()["access_token"];
$clientrefreshtoken = $client->getRefreshToken();
echo $OUTPUT->box(get_string('accountconnected', 'jitsi'));
$link = new moodle_url('/mod/jitsi/adminaccounts.php');
echo '<a href='.$link.'>'.get_string('back').'</a>';
$account = $DB->get_record('jitsi_record_account', ['name' => $_SESSION['name']]);
if ($account == null) {
$account = new stdClass();
$time = time();
$account->name = $_SESSION['name'];
$account->clientaccesstoken = $accesstoken;
$account->clientrefreshtoken = $clientrefreshtoken;
$account->tokencreated = $time;
$account->inuse = 1;
$DB->insert_record('jitsi_record_account', $account);
} else {
$time = time();
$account->clientaccesstoken = $accesstoken;
$account->clientrefreshtoken = $clientrefreshtoken;
$account->tokencreated = $time;
$account->inuse = 1;
$DB->update_record('jitsi_record_account', $account);
}
} catch (Google_Service_Exception $e) {
$htmlbody = sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401));
} catch (Google_Exception $e) {
$htmlbody = sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401));
}
$_SESSION[$tokensessionkey] = $client->getAccessToken();
echo $OUTPUT->footer();
} else if ($oauth2clientid == 'REPLACE_ME') {
echo "<h3>Client Credentials Required</h3>";
echo "<p>You need to set <code>\$OAUTH2_CLIENT_ID</code> and";
echo "<code>\$OAUTH2_CLIENT_ID</code> before proceeding.";
echo "<p>";
} else {
$PAGE->set_url('/mod/jitsi/auth.php');
$PAGE->set_title(format_string(get_string('accounts', 'jitsi')));
$PAGE->set_heading(format_string(get_string('accounts', 'jitsi')));
echo $OUTPUT->header();
$rand = mt_rand();
$stateparameters = 'name='.$name.'&rand='.$rand;
$state = base64UrlEncode($stateparameters);
$client->setState($state);
$_SESSION['rand'] = $rand;
$authurl = $client->createAuthUrl();
echo "<h3>Authorization Required</h3>";
echo "<p>You need to <a href=\"$authurl\">authorize access</a> before proceeding.<p>";
echo $OUTPUT->footer();
}
}