forked from vfremaux/moodle-auth_ticket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
314 lines (267 loc) · 10.8 KB
/
lib.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?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/>.
/**
* @package auth_ticket
* @category auth
* @author Valery Fremaux <valery.fremaux@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2010 ValEISTI (http://www.valeisti.fr)
* @copyright (C) 2012 onwards Valery Fremaux (http://www.mylearningfactory.com)
*
* Ticket related library
*/
defined('MOODLE_INTERNAL') || die;
/**
* Simple sending to user with return ticket.
* The return ticket allows auser receiving amail to enter immediately
* the platform being connected automatically during a hold time.
* the ticket is catched by a custom auth module that decodes generated ticket and
* let user through.
* Only recipients that have a valid Moodle account can use an access tickets.
* The ticket is only valid on the given return URL and cannot be used for going
* to another location, unless user's profile other mention.
*
* @param object $recipient
* @param object $sender
* @param string $title mail subject
* @param string $notification raw content of the mail
* @param string $notification_html html content of the mail
* @param string $url return url of the ticket
* @param string $purpose some textual comment on what the ticket was for
* @param bool $term the ticket validity duration, may be 'short', 'long' or 'persistant'.
*/
function ticket_notify($recipient, $sender, $title, $notification, $notificationhtml, $url, $purpose = '', $term = 'short') {
global $CFG;
if (!empty($url)) {
$ticket = ticket_generate($recipient, $purpose, $url, $term);
$notificationhtml = str_replace('<%%TICKET%%>', $ticket, $notificationhtml);
} else {
// Get rid of placeholder if not used.
$notification = str_replace('<%%TICKET%%>', '', $notificationhtml);
}
// Tickets only can be sent as HTML href values.
$notification = str_replace('<%%TICKET%%>', '', $notification);
// Todo send the email to user.
if ($CFG->debugsmtp) {
echo "Sending Mail Notification to " . fullname($recipient) .'<br/>'.$notificationhtml;
}
return email_to_user($recipient, $sender, $title, $notification, $notificationhtml);
}
/**
* Sends a notification message to all users having the role in the given context.
*
* Note that general form of URL to propose a return ticket encoded url is :
* %WWWROOT%/login/index.php?ticket=%TICKET%
*
* @param int $roleid id of the role to search users on
* @param object $context context in which find users with the role
* @param object $sender user identity of the sender
* @param string $title mail subject
* @param string $notification raw content of the mail
* @param string $notification_html html content of the mail
* @param string $url return url of the ticket
* @param string $purpose some textual comment on what the ticket was for
* @param bool $checksendall if true, the function returns true if all the recipients were sucessfull
* @param bool $term the ticket validity duration, may be 'short', 'long' or 'persistant'.
* @return true if at least one email could be sent or all are sent depending on $checksendall.
*/
function ticket_notifyrole($roleid, $context, $sender, $title, $notification, $notificationhtml, $url, $purpose = '',
$checksendall = false, $term = 'short') {
global $CFG, $DB;
// Get all users assigned to that role in context.
$role = $DB->get_record('role', array('id' => $roleid));
$assigns = get_users_from_role_on_context($role, $context);
$result = $checksendall;
foreach ($assigns as $assign) {
$fields = 'id, username,'.get_all_user_name_fields(true, '').', email, emailstop, mailformat';
$user = $DB->get_record('user', array('id' => $assign->userid), $fields);
$ticket = ticket_generate($user, $purpose, $url, $term);
$notification = str_replace('<%%TICKET%%>', $ticket, $notification);
$notificationhtml = str_replace('<%%TICKET%%>', $ticket, $notificationhtml);
// Todo send the email to user.
if ($CFG->debugsmtp) {
echo "Sending Mail Notification to ".fullname($user).'<br/>'.$notification;
} else {
if ($checksendall) {
$result = $result && email_to_user($user, $sender, $title, $notification, $notificationhtml);
} else {
$result = $result || email_to_user($user, $sender, $title, $notification, $notificationhtml);
}
}
}
return $result;
}
/**
* Generates a direct access ticket for this user. three generation methods are provided.
* - Internel : a weak method but does not relay on encryption libraries.
* - des : uses the Mysql DES encryption function. Will NOT work on other databases.
* - rsa : Uses Moodle MNET local key. Assumes we have initialized mnet. Care that the ticket may be
* rejected if the key changes. This will have impact on 'persistance' or 'long' term tickets.
*
* @param object $user a user object
* @param string $reason the reason of the ticket
* @param string $url the access URL the user will be redirected to after validating his return ticket.
* @param string $method the encryption algorithm, 'des' or 'rsa', or 'internal'.
* @param string $term the validity delay range in 'short', 'long', or 'persistance'.
* @return string an encrypted ticket
*/
function ticket_generate($user, $reason, $url, $method = null, $term = 'short') {
global $CFG, $DB, $SITE;
$config = get_config('auth_ticket');
if (is_null($method)) {
$method = $config->encryption;
}
if (empty($user->username)) {
return;
}
$ticket = new StdClass();
$ticket->username = $user->username;
if (!empty($reason)) {
$ticket->reason = $reason;
}
if (!empty($url)) {
$ticket->wantsurl = ''.$url; // Ensure we stringify.
}
$ticket->term = $term;
$ticket->date = time();
$keyinfo = json_encode($ticket);
if ($method == 'internal') {
$key = $config->internalseed;
if (empty($config->internalseed)) {
$key = md5($SITE->fullname);
}
while (strlen($key) < strlen($keyinfo)) {
// Pad key onto itself to get a key larger than the text.
$key .= $key;
}
$encrypted = '';
// Iterate through each character
for ($i = 0; $i < strlen($keyinfo); $i++) {
$encrypted .= $keyinfo{$i} ^ $key{$i};
}
} else if ($method == 'rsa') {
include_once($CFG->dirroot.'/mnet/lib.php');
$keypair = mnet_get_keypair();
if (!openssl_public_encrypt($keyinfo, $encrypted, $keypair['publickey'])) {
print_error("Failed making encoded ticket");
}
} else {
$pkey = '';
if (!empty($CFG->passwordsaltmain)) {
$pkey = substr(base64_encode($CFG->passwordsaltmain), 0, 16);
}
$sql = "
SELECT
HEX(AES_ENCRYPT(?, ?)) as result
";
if ($result = $DB->get_record_sql($sql, array($keyinfo, $pkey))) {
$encrypted = $result->result;
} else {
$encrypted = 'encryption error';
}
}
return base64_encode($encrypted); // Make sure we can emit this ticket through an URL.
}
/**
* Decodes a direct access ticket for this user.
*
* @param string $encrypted the received ticket
* @param string $method the decrypt method. Supports 'des' using DB internal function or 'rsa' using openssl layer.
* @return a decoded ticket object
*/
function ticket_decode($encrypted, $method = null) {
global $CFG, $DB, $SITE;
$config = get_config('auth_ticket');
if (is_null($method)) {
$method = $config->encryption;
}
$encrypted = base64_decode($encrypted);
if ($method == 'internal') {
$key = $config->internalseed;
if (empty($config->internalseed)) {
$key = md5($SITE->fullname);
}
while (strlen($key) < strlen($encrypted)) {
// Pad key onto itself to get a key larger than the text.
$key .= $key;
}
$decrypted = '';
// Iterate through each character
for ($i = 0; $i < strlen($encrypted); $i++) {
$decrypted .= $encrypted{$i} ^ $key{$i};
}
} else if ($method == 'rsa') {
// Using RSA.
include_once($CFG->dirroot.'/mnet/lib.php');
$keypair = mnet_get_keypair();
if (!openssl_private_decrypt(urldecode($encrypted), $decrypted, $keypair['privatekey'])) {
print_error('decoderror', 'auth_ticket', $method);
}
} else {
$pkey = substr(base64_encode(@$CFG->passwordsaltmain), 0, 16);
$sql = "
SELECT
AES_DECRYPT(UNHEX(?), ?) as result
";
if ($result = $DB->get_record_sql($sql, array($encrypted, $pkey))) {
$decrypted = $result->result;
} else {
$decrypted = 'encryption error';
}
}
if (!$ticket = json_decode(str_replace('/', "\\/", $decrypted))) {
print_error('ticketerror', 'auth_ticket', '', $method);
}
return $ticket;
}
/**
* checks conditions for ticket internal data validity and initiate the $USER if ticket is valid.
* @param string $ticket
* @return true if ticket is accepted.
*/
function ticket_accept($ticket, &$gotourl = null) {
global $DB;
$config = get_config('auth_ticket');
switch ($ticket->term) {
case 'short' :
if ($ticket->date < time() - $config->shortvaliditydelay) {
return false;
}
break;
case 'long' :
if ($ticket->date < time() - $config->longvaliditydelay) {
return false;
}
break;
case 'persistant' :
if ($config->persistantvaliditydelay == 0) {
return true;
}
if ($ticket->date < time() - $config->persistantvaliditydelay) {
return false;
}
assert(1);
}
if (empty($ticket->username)) {
return false;
}
if (!$user = $DB->get_record('user', array('username' => $ticket->username))) {
return false;
}
$USER = $user;
$gotourl = @$ticket->wantsurl;
return true;
}