-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
281 lines (240 loc) · 8.83 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?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/>.
/**
* Main File for Auth
*
* @package auth_leeloo_pay_sso
* @copyright 2020 Leeloo LXP (https://leeloolxp.com)
* @author Leeloo LXP <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/authlib.php');
require_once($CFG->dirroot . '/lib/filelib.php');
/**
* Plugin to sync users to Leeloo LXP Vendor account of the Moodle Admin
*/
class auth_plugin_leeloo_pay_sso extends auth_plugin_base {
/**
* Constructor
*/
public function __construct() {
$this->authtype = 'leeloo_pay_sso';
$this->config = get_config('auth_leeloo_pay_sso');
}
/**
* Generate random string.
*
* @param int $strength is strength
* @return string $randomstring is random string
*/
public function generate_string($strength = 16) {
$input = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$inputlength = strlen($input);
$randomstring = '';
for ($i = 0; $i < $strength; $i++) {
$randomcharacter = $input[mt_rand(0, $inputlength - 1)];
$randomstring .= $randomcharacter;
}
return $randomstring;
}
/**
* Check if user authenticated
*
* @param string $user The userdata
* @param string $username The username
* @param string $password The password
* @return bool Return true
*/
public function user_authenticated_hook(&$user, $username, $password) {
setcookie('jsession_id', '', time() + (86400), "/");
setcookie('license_key', '', time() + (86400), "/");
global $CFG;
setcookie('moodleurl', $CFG->wwwroot, time() + (86400), "/");
$admins = get_admins();
$isadmin = false;
foreach ($admins as $admin) {
if ($user->id == $admin->id) {
$isadmin = true;
break;
}
}
if ($isadmin) {
return true;
}
$useremail = $user->email;
$license = $this->config->license;
global $CFG;
require_once($CFG->dirroot . '/lib/filelib.php');
$postdata = array('license_key' => $license);
$url = 'https://leeloolxp.com/api_moodle.php/?action=page_info';
$curl = new curl;
$options = array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_HEADER' => false,
'CURLOPT_POST' => count($postdata),
);
if (!$output = $curl->post($url, $postdata, $options)) {
return true;
}
$infoteamnio = json_decode($output);
if ($infoteamnio->status != 'false') {
$teamniourl = $infoteamnio->data->install_url;
} else {
return true;
}
$url = $teamniourl . '/admin/sync_moodle_course/check_user_by_email/' . base64_encode($useremail);
$curl = new curl;
$options = array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_HEADER' => false,
'CURLOPT_POST' => count($postdata),
'CURLOPT_HTTPHEADER' => array(
'Leeloolxptoken: ' . get_config('local_leeloolxpapi')->leelooapitoken . ''
)
);
if (!$output = $curl->post($url, $postdata, $options)) {
return true;
}
if ($output == '0') {
return true;
}
$url = $teamniourl . '/admin/sync_moodle_course/check_user_status_by_email/' . base64_encode($useremail);
$curl = new curl;
$options = array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_HEADER' => false,
'CURLOPT_POST' => count($postdata),
'CURLOPT_HTTPHEADER' => array(
'Leeloolxptoken: ' . get_config('local_leeloolxpapi')->leelooapitoken . ''
)
);
if (!$userstatusonteamnio = $curl->post($url, $postdata, $options)) {
return true;
}
if ($userstatusonteamnio == 0) {
return true;
}
$leeloolicense = $this->config->vendorkey;
$username = $username;
$password = $this->generate_string(8);
$useremail = $user->email;
$leeloousername = $username;
$leelooemail = $useremail;
$postdata = array(
'username' => base64_encode($leeloousername),
'password' => $password,
'email' => base64_encode($leelooemail),
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'leeloolicense' => $leeloolicense,
);
$url = 'https://leeloolxp.com/api-leeloo/post/user/register';
$curl = new curl;
$options = array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_HTTPHEADER' => array(
'Leeloolxptoken: ' . get_config('local_leeloolxpapi')->leelooapitoken . ''
)
);
if (!$response = $curl->post($url, $postdata, $options)) {
return true;
}
$postdata = array(
'username' => base64_encode($leeloousername),
'password' => $password,
'leeloolicense' => $leeloolicense,
);
$url = 'https://leeloolxp.com/api-leeloo/post/user/changepass';
$curl = new curl;
$options = array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_HTTPHEADER' => array(
'Leeloolxptoken: ' . get_config('local_leeloolxpapi')->leelooapitoken . ''
)
);
if (!$response = $curl->post($url, $postdata, $options)) {
return true;
}
$postdata = array(
'username' => base64_encode($leeloousername),
'password' => $password,
'leeloolicense' => $leeloolicense,
'siteid' => $user->id
);
$url = 'https://leeloolxp.com/api-leeloo/post/user/login';
$curl = new curl;
$options = array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_HTTPHEADER' => array(
'Leeloolxptoken: ' . get_config('local_leeloolxpapi')->leelooapitoken . ''
)
);
if (!$response = $curl->post($url, $postdata, $options)) {
return true;
}
$resposearr = json_decode($response);
if (isset($resposearr->session_id) && isset($resposearr->session_id) != '') {
global $SESSION;
$SESSION->jsession_id = $resposearr->session_id;
setcookie('jsession_id', $resposearr->session_id, time() + (86400), "/");
setcookie('license_key', $license, time() + (86400), "/");
global $DB;
$checksql = "SELECT * FROM {auth_leeloolxp_tracking_sso} WHERE userid = ?";
$ssourls = $DB->get_record_sql(
$checksql,
[$user->id]
);
$jurl = 'https://leeloolxp.com/es-frame?session_id=' . $resposearr->session_id . '&leeloolxplicense=' . $license;
if ($ssourls) {
$sql = 'UPDATE {auth_leeloolxp_tracking_sso} SET jurl = ? WHERE userid = ?';
$DB->execute(
$sql,
[$jurl, $user->id]
);
} else {
$sql = 'INSERT INTO {auth_leeloolxp_tracking_sso} (userid, jurl, leeloourl) VALUES (?, ?, ?)';
$DB->execute(
$sql,
[$user->id, $jurl, '']
);
}
} else {
setcookie('jsession_id', '', time() + (86400), "/");
setcookie('license_key', '', time() + (86400), "/");
}
return true;
}
/**
* Returns false if the user exists and the password is wrong.
*
* @param string $username is username
* @param string $password is password
* @return bool Authentication success or failure.
*/
public function user_login($username, $password) {
return false;
}
/**
* Clear cookie on logout
*
* @param object $user is userdata
*/
public function postlogout_hook($user) {
setcookie('jsession_id', '', time() + (86400), "/");
setcookie('license_key', '', time() + (86400), "/");
}
}