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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
<?php
// +---------------------------------------------------------------------------+
// This file is part of Stud.IP
// StudipAuthAbstract.php
// Abstract class, used as a template for authentication plugins
//
// Copyright (c) 2003 André Noack <noack@data-quest.de>
// Suchi & Berg GmbH <info@data-quest.de>
// +---------------------------------------------------------------------------+
// This program 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 2
// of the License, or any later version.
// +---------------------------------------------------------------------------+
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// +---------------------------------------------------------------------------+
/**
* abstract base class for authentication plugins
*
* abstract base class for authentication plugins
* to write your own authentication plugin, derive it from this class and
* implement the following abstract methods: isUsedUsername($username) and
* isAuthenticated($username, $password, $jscript)
* don't forget to call the parents constructor if you implement your own, php
* won't do that for you !
*
* @abstract
* @author André Noack <noack@data-quest.de>
* @package
*/
class StudipAuthAbstract
{
/**
* contains error message, if authentication fails
*
*
* @var string $error_msg
*/
public $error_msg;
/**
* indicates whether the authenticated user logs in for the first time
*
*
* @var bool $is_new_user
*/
public $is_new_user = false;
/**
* array of user domains to assign to each user, can be set in local.inc
*
* @access public
* @var array $user_domains
*/
public $user_domains;
/**
* associative array with mapping for database fields
*
* associative array with mapping for database fields,
* should be set in local.inc
* structure :
* array('<table name>.<field name>' => array( 'callback' => '<name of callback method used for data retrieval>',
* 'map_args' => '<arguments passed to callback method>'))
* @var array $user_data_mapping
*/
public $user_data_mapping = null;
/**
* name of the plugin
*
* name of the plugin (last part of class name) is set in the constructor
* @var string $plugin_name
*/
public $plugin_name;
/**
* text, which precedes error message for the plugin
*
*
* @var string $error_head
*/
public $error_head;
/**
* toggles display of standard login
*
*
* @var bool $show_login
*/
public $show_login;
/**
* @var $plugin_instances
*/
private static $plugin_instances;
private $config_data = [];
/**
* static method to instantiate and retrieve a reference to an object (singleton)
*
* always use this method to instantiate a plugin object, it will ensure that only one object of each
* plugin will exist
* @param string $plugin_name name of plugin, if omitted an array with all plugin objects will be returned
* @return mixed either a reference to the plugin with the passed name, or an array with references to all plugins
*/
public static function getInstance($plugin_name = false)
{
if (!is_array(self::$plugin_instances)) {
foreach ($GLOBALS['STUDIP_AUTH_PLUGIN'] as $plugin) {
$config = $GLOBALS['STUDIP_AUTH_CONFIG_' . strtoupper($plugin)];
$plugin_class = $config['plugin_class'] ?? 'StudipAuth' . $plugin;
if (empty($config['plugin_name'])) {
$config['plugin_name'] = strtolower($plugin);
}
self::$plugin_instances[strtoupper($plugin)] = new $plugin_class($config);
}
}
return ($plugin_name) ? self::$plugin_instances[strtoupper($plugin_name)]??null : self::$plugin_instances;
}
/**
* static method to check if SSO login is enabled
*
* @return bool
*/
public static function isSSOEnabled(): bool
{
self::getInstance();
foreach (self::$plugin_instances as $auth_plugin) {
if ($auth_plugin instanceof StudipAuthSSO) {
return true;
}
}
return false;
}
/**
* static method to check if standard login is enabled
*
* @return bool
*/
public static function isLoginEnabled(): bool
{
self::getInstance();
foreach (self::$plugin_instances as $auth_plugin) {
if ($auth_plugin->show_login === true) {
return true;
}
}
return false;
}
/**
* static method to check authentication in all plugins
*
* if authentication fails in one plugin, the error message is stored and the next plugin is used
* if authentication succeeds, the uid element in the returned array will contain the Stud.IP user id
*
* @param string $username the username to check
* @param string $password the password to check
* @return array structure: array('uid'=>'string <Stud.IP user id>','error'=>'string <error message>','is_new_user'=>'bool')
*/
public static function CheckAuthentication($username, $password)
{
$plugins = StudipAuthAbstract::GetInstance();
$error = false;
$uid = false;
foreach ($plugins as $object) {
// SSO plugins can't be used
if ($object instanceof StudipAuthSSO) {
continue;
}
if ($user = $object->authenticateUser($username, $password)) {
if ($user) {
$uid = $user->id;
$locked = $user['locked'];
$key = $user['validation_key'];
$checkIPRange = ($GLOBALS['ENABLE_ADMIN_IP_CHECK'] && $user['perms'] === 'admin')
|| ($GLOBALS['ENABLE_ROOT_IP_CHECK'] && $user['perms'] === 'root');
if ($user->isExpired()) {
$error .= _('Dieses Benutzerkonto ist abgelaufen.<br> Wenden Sie sich bitte an die Administration.') . '<BR>';
return ['uid' => false, 'error' => $error];
} else if ($locked) {
$error .= _('Dieser Benutzer ist gesperrt! Wenden Sie sich bitte an die Administration.') . '<BR>';
return ['uid' => false, 'error' => $error];
} else if ($key != '') {
return ['uid' => $uid, 'user' => $user, 'error' => $error, 'need_email_activation' => $uid];
} else if ($checkIPRange && !self::CheckIPRange()) {
$error .= _('Der Login in Ihren Account ist aus diesem Netzwerk nicht erlaubt.') . '<BR>';
return ['uid' => false, 'error' => $error];
}
}
return ['uid' => $uid, 'user' => $user, 'error' => $error, 'is_new_user' => $object->is_new_user];
} else {
$error .= (($object->error_head) ? ('<b>' . $object->error_head . ':</b> ') : '') . $object->error_msg . '<br>';
}
}
return ['uid' => $uid, 'error' => $error];
}
/**
* static method to check if passed username is used in external data sources
*
* all plugins are checked, the error messages are stored and returned
*
* @param string $username the username
* @return array
*/
public static function CheckUsername($username)
{
$plugins = StudipAuthAbstract::GetInstance();
$error = false;
$found = false;
foreach ($plugins as $object) {
if ($found = $object->isUsedUsername($username)) {
return ['found' => $found, 'error' => $error];
} else {
$error .= (($object->error_head) ? ('<b>' . $object->error_head . ':</b> ') : '') . $object->error_msg . '<br>';
}
}
return ['found' => $found, 'error' => $error];
}
/**
* static method to check for a mapped field
*
* this method checks in the plugin with the passed name, if the passed
* Stud.IP DB field is mapped to an external data source
*
* @param string the name of the db field must be in form '<table name>.<field name>'
* @param string the name of the plugin to check
* @return bool true if the field is mapped, else false
*/
public static function CheckField($field_name, $plugin_name)
{
if (!$plugin_name) {
return false;
}
$plugin = StudipAuthAbstract::GetInstance($plugin_name);
return (is_object($plugin) ? $plugin->isMappedField($field_name) : false);
}
/**
* static method to check if ip address belongs to allowed range
*
* @return bool true if the client ip address is within the valid range
*/
public static function CheckIPRange()
{
$ip = $_SERVER['REMOTE_ADDR'];
$version = substr_count($ip, ':') > 1 ? 'V6' : 'V4'; // valid ip v6 addresses have atleast two colons
$method = 'CheckIPRange' . $version;
if (is_array($GLOBALS['LOGIN_IP_RANGES'][$version])) {
foreach ($GLOBALS['LOGIN_IP_RANGES'][$version] as $range) {
if (self::$method($ip, $range)) {
return true;
}
}
}
return false;
}
/**
* @param $ip string IPv4 adress
* @param $range array assoc array with [start] & [end]
* @return bool
*/
public static function CheckIPRangeV4($ip, $range)
{
$ipv4 = ip2long($ip);
if ($ipv4 === false) {
return false; // invalid ip address
}
$start = ip2long($range['start']);
$end = ip2long($range['end']);
return $ipv4 >= $start && $ipv4 <= $end;
}
/**
* @param $ip string IPv6 address
* @param $range array assoc array with [start] & [end]
* @return bool
*/
public static function CheckIPRangeV6($ip, $range)
{
$ipv6 = inet_pton($ip);
if ($ipv6 === false) {
return false; // invalid ip address
}
$start = inet_pton($range['start']);
$end = inet_pton($range['end']);
return strlen($ipv6) === strlen($start)
&& $ipv6 >= $start && $ipv6 <= $end;
}
/**
* Constructor
*
* you should use StudipAuthAbstract::GetInstance($plugin_name)
* to get a reference to a plugin object. Make sure the constructor in the base class is called
* when deriving your own plugin class, it assigns the settings from local.inc as members of the plugin
* each key of the $STUDIP_AUTH_CONFIG_<plugin name> array will become a member of the object
*
* @param array $config
*/
public function __construct($config = [])
{
//get configuration array set in local inc
if (empty($config)) {
$this->plugin_name = strtolower(substr(get_class($this), 10));
$config = $GLOBALS['STUDIP_AUTH_CONFIG_' . strtoupper($this->plugin_name)];
}
//assign each key in the config array as a member of the plugin object
foreach ($config as $key => $value) {
$this->$key = $value;
}
}
/**
* authentication method
*
* this method authenticates the passed username, it is used by StudipAuthAbstract::CheckAuthentication()
* if authentication succeeds it calls StudipAuthAbstract::doDataMapping() to map data fields
* if the authenticated user logs in for the first time it calls StudipAuthAbstract::doNewUserInit() to
* initialize the new user
* @param string $username the username to check
* @param string $password the password to check
* @return string if authentication succeeds the Stud.IP user , else false
*/
public function authenticateUser($username, $password)
{
$username = $this->verifyUsername($username);
if ($this->isAuthenticated($username, $password)) {
if ($user = $this->getStudipUser($username)) {
$this->doDataMapping($user);
if ($this->is_new_user) {
$this->doNewUserInit($user);
}
$this->setUserDomains($user);
}
return $user;
} else {
return false;
}
}
/**
* method to retrieve the Stud.IP user id to a given username
*
*
* @access private
* @param string the username
* @return User the Stud.IP or false if an error occurs
*/
function getStudipUser($username)
{
$user = User::findByUsername($username);
if ($user) {
$auth_plugin = $user->auth_plugin;
if ($auth_plugin === null) {
$this->error_msg = _('Dies ist ein vorläufiger Benutzer.') . '<br>';
return false;
}
if ($auth_plugin != $this->plugin_name) {
$this->error_msg = sprintf(_('Dieser Benutzername wird bereits über %s authentifiziert!'), $auth_plugin) . '<br>';
return false;
}
return $user;
}
$new_user = new User();
$new_user->username = $username;
$new_user->perms = 'autor';
$new_user->auth_plugin = $this->plugin_name;
$new_user->preferred_language = $_SESSION['_language'];
if ($new_user->store()) {
$this->is_new_user = true;
return $new_user;
}
}
/**
* initialize a new user
*
* this method is invoked for one time, if a new user logs in ($this->is_new_user is true)
* place special treatment of new users here
*
* @access private
* @param User $user the user object
*/
function doNewUserInit($user)
{
// auto insertion of new users, according to $AUTO_INSERT_SEM[] (defined in local.inc)
AutoInsert::instance()->saveUser($user->id, $user->perms);
}
/**
* This method sets the user domains for the current user.
*
* @access private
* @param User the user object
*/
function setUserDomains($user)
{
$user_domains = $this->getUserDomains();
$uid = $user->id;
if (isset($user_domains)) {
$old_domains = UserDomain::getUserDomainsForUser($uid);
foreach ($old_domains as $domain) {
if (!in_array($domain->id, $user_domains)) {
$domain->removeUser($uid);
}
}
foreach ($user_domains as $user_domain) {
$domain = new UserDomain($user_domain);
if ($domain->isNew()) {
$domain->name = $user_domain;
$domain->store();
}
if (!in_array($domain, $old_domains)) {
$domain->addUser($uid);
}
}
}
}
/**
* Get the user domains to assign to the current user.
*/
function getUserDomains()
{
return $this->user_domains;
}
/**
* this method handles the data mapping
*
* for each entry in $this->user_data_mapping the according callback will be invoked
* the return value of the callback method is then written to the db field, which is specified
* in the key of the array
*
* @access private
* @param User the user object
* @return bool
*/
function doDataMapping($user)
{
if ($user && is_array($this->user_data_mapping)) {
foreach ($this->user_data_mapping as $key => $value) {
$callback = null;
if (method_exists($this, $value['callback'])) {
$callback = [$this, $value['callback']];
} else if (is_callable($value['callback'])) {
$callback = $value['callback'];
}
if ($callback) {
$split = explode('.', $key);
$table = $split[0];
$field = $split[1];
if ($table === 'auth_user_md5' || $table === 'user_info') {
$mapped_value = call_user_func($callback, $value['map_args']);
if (isset($mapped_value)) {
$user->setValue($field, $mapped_value);
}
} else {
call_user_func($callback, [$table, $field, $user, $value['map_args']]);
}
}
}
return $user->store();
}
return false;
}
/**
* method to check, if a given db field is mapped by the plugin
*
*
* @access private
* @param string the name of the db field (<table_name>.<field_name>)
* @return bool true if the field is mapped
*/
function isMappedField($name)
{
return isset($this->user_data_mapping[$name]);
}
/**
* method to eliminate bad characters in the given username
*
*
* @access private
* @param string the username
* @return string the username
*/
function verifyUsername($username)
{
if ($this->username_case_insensitiv) {
$username = mb_strtolower($username);
}
if ($this->bad_char_regex) {
return preg_replace($this->bad_char_regex, '', $username);
} else {
return trim($username);
}
}
/**
* method to check, if username is used
*
* abstract MUST be realized
*
* @access private
* @param string the username
* @return bool true if the username exists
*/
function isUsedUsername($username)
{
$this->error_msg = sprintf(
_('Methode %s nicht implementiert!'),
__METHOD__
);
return false;
}
/**
* method to check the authentication of a given username and a given password
*
* abstract, MUST be realized
*
* @access private
* @param string the username
* @param string the password
* @return bool true if authentication succeeds
*/
function isAuthenticated($username, $password)
{
$this->error_msg = sprintf(
_('Methode %s nicht implementiert!'),
__METHOD__
);
return false;
}
// Store dynamically set dynamically created properties in $config_data
public function __isset($offset)
{
return isset($this->config_data[$offset]);
}
public function __set($offset, $value)
{
$this->config_data[$offset] = $value;
}
public function __get($offset)
{
return $this->config_data[$offset] ?? null;
}
public function __unset($offset)
{
unset($this->config_data[$offset]);
}
}
|