-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.php
723 lines (667 loc) · 25 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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
<?php
/**
* DokuWiki Plugin authsplit (Auth Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Pieter Hollants <[email protected]>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC'))
die();
use dokuwiki\Extension\AuthPlugin;
class auth_plugin_authsplit extends AuthPlugin {
protected $authplugins;
protected $autocreate_users;
protected $debug;
/**
* Show a debug message
*
* @param string $message The message to show
* @param int $err -1 for error, 0 for info, 1 for success
* @param int $line The line in $file that triggered the message.
* @param string $file The filename that triggered the message.
* @return void
*/
protected function _debug($message, $err, $line, $file) {
if (!$this->debug)
return;
msg($message, $err, $line, $file);
}
/**
* Constructor.
*/
public function __construct() {
parent::__construct(); // for compatibility
/* Load the config earlier than usual (we need it below) */
$this->loadConfig();
/* Load all referenced auth plugins */
foreach (array('primary', 'secondary') as $type) {
$settingName = $type.'_authplugin';
$pluginName = $this->getConf($settingName);
if (!$pluginName) {
msg(sprintf($this->getLang('nocfg'), $settingName), -1);
$this->success = false;
return;
}
if ($pluginName != 'None') {
$this->authplugins[$type] = plugin_load('auth', $pluginName);
if (!$this->authplugins[$type]) {
msg(sprintf($this->getLang('pluginload'), $pluginName), -1);
$this->success = false;
return;
}
} else {
$this->authplugins[$type] = null;
}
}
/* Create users automatically? */
$this->autocreate_users = $this->getConf('autocreate_users', null);
if ($this->autocreate_users === null) {
msg(sprintf($this->getLang('nocfg'), 'autocreate_users'), -1);
$this->success = false;
return;
}
/* Convert username's case? */
$this->username_caseconversion = $this->getConf('username_caseconversion', null);
if ($this->username_caseconversion === null) {
msg(sprintf($this->getLang('nocfg'), 'username_caseconversion'), -1);
$this->success = false;
return;
}
/* Show debug messages? */
$this->debug = $this->getConf('debug', null);
if ($this->debug === null) {
msg(sprintf($this->getLang('nocfg'), 'debug'), -1);
$this->success = false;
return;
}
/* Of course, to modify login names actually BOTH auth plugins must
support that. However, at this place we just consider the secondary
auth plugin as otherwise admins can not add user accounts there in
advance. */
$this->cando['modLogin'] = $this->authplugins['secondary']->canDo('modLogin');
/* To modify passwords, the primary auth plugin must support it */
$this->cando['modPass'] = $this->authplugins['primary']->canDo('modPass');
/* To add and delete user accounts, modify real names, email addresses,
group memberships and groups, it is sufficient for the secondary
auth plugin to support it. */
foreach (array('addUser', 'delUser', 'modName', 'modMail', 'modGroups',
'getUsers', 'getUserCount', 'getGroups') as $cap) {
$this->cando[$cap] = $this->authplugins['secondary']->canDo($cap);
}
/* The primary auth plugin determines whether we do external
authentication (eg. against a third party cookie). */
$this->cando['external'] = $this->authplugins['primary']->cando['external'];
/* Whether we can do logout or not depends on the primary auth plugin */
$this->cando['logout'] = $this->authplugins['primary']->canDo('logout');
$msg = 'authsplit:__construct(): '.
$this->authplugins['primary']->getPluginName().'/'.
$this->authplugins['secondary']->getPluginName().' '.
'combination can ';
$parts = array(
'addUser' => 'add users',
'delUser' => 'delete users',
'modLogin' => 'modify login names',
'modPass' => 'modify passwords',
'modName' => 'modify real names',
'modMail' => 'modify E-Mail addresses',
'modGroups' => 'modify groups',
'getUsers' => 'get user list',
'getUserCount' => 'get user counts',
'getGroups' => 'get groups',
'logout' => 'logout users',
);
foreach ($this->cando as $key => $value) {
if ($this->cando[$key])
$msg .= $parts[$key].', ';
}
$msg = rtrim($msg, ', ').'.';
$this->_debug($msg, 1, __LINE__, __FILE__);
$this->success = true;
}
/**
* Authenticate user (DokuWiki-style user/password authentication)
*
* This method is called if DokuWiki's internal authentication system is
* used (primary auth plugin's canDo['external'] == false) and the user
* supplied a username and a password.
*
* @param string $user the user name
* @param string $pass the clear text password
* @return bool
*/
public function checkPass($user, $pass) {
/* First validate the username and password with the primary plugin. */
if (!$this->authplugins['primary']->checkPass($user, $pass)) {
$this->_debug(
'authsplit:checkPass(): primary auth plugin\'s checkPass() '.
'failed', -1, __LINE__, __FILE__
);
return false;
}
$this->_debug(
'authsplit:checkPass(): primary auth plugin authenticated the '.
'user successfully.', 1, __LINE__, __FILE__
);
/* Then make sure that the secondary auth plugin also knows about the
user. */
return $this->_checkUserOnSecondaryAuthPlugin($user);
}
/**
* Authenticate user (external authentication)
*
* This method is called on every page load if external authentication is
* used (primary auth plugin's canDo['external'] == true).
*
* @param string $user The user name (may be empty)
* @param string $pass The clear text password (may be empty)
* @param bool $sticky Whether the cookie should not expire
* @return bool
*/
public function trustExternal($user, $pass, $sticky = false) {
global $USERINFO;
/* First delegate to the primary plugin's trustExternal() to
validate the user (by means of a password, a cookie or whatever). */
if (!$this->authplugins['primary']->trustExternal($user, $pass, $sticky)) {
$this->_debug(
'authsplit:trustExternal(): primary auth plugin\'s ' .
'trustExternal() failed', -1, __LINE__, __FILE__
);
return false;
}
/* As $user may be empty (eg. when authentication is done via a cookie)
we set it from the server environment */
$user = $_SERVER['REMOTE_USER'];
$this->_debug(
'authsplit:trustExternal(): derived user name: ' . $user,
-1, __LINE__, __FILE__
);
/* Then make sure the secondary auth plugin also knows about the
user. */
if ($this->_checkUserOnSecondaryAuthPlugin($user)) {
/* With external authentication, $USERINFO must be set on every
page load. */
$USERINFO = $this->getUserData($user, true);
return true;
}
return false;
}
/**
* Ensures that the user is known to the secondary auth plugin as well.
*
* @param string $user the user name
* @return bool
*/
public function _checkUserOnSecondaryAuthPlugin($user) {
/* User already known? */
$userinfo = $this->authplugins['secondary']->getUserData($user, false);
if ($userinfo)
return true;
$this->_debug(
'authsplit:'.__FUNCTION__.'(): secondary auth plugin\'s ' .
'getUserData() failed, seems user is yet unknown there.', -1,
__LINE__, __FILE__
);
$this->_debug(
'authsplit:'.__FUNCTION__.'(): autocreate_users is set to '.
$this->autocreate_users.'.',
$this->autocreate_users == 1 ? 1 : -1,
__LINE__, __FILE__
);
/* Make sure automatic user creation is enabled */
if (!$this->autocreate_users)
return false;
/* Make sure the secondary auth plugin can create user accounts */
if (!$this->authplugins['secondary']->cando['addUser']) {
msg(
sprintf(
$this->getLang('erraddusercap'),
$this->authplugins['secondary']->getPluginName()
),
-1
);
return false;
}
/* Since auth plugins by definition must have a getUserData()
method, we use the primary auth plugin's data to create a user
account in the secondary auth plugin. */
$params = $this->authplugins['primary']->getUserData($user, true);
if (!$params) {
msg(
sprintf(
$this->getLang('erradduserinfo'),
$this->authplugins['primary']->getPluginName()
),
-1
);
return false;
}
$this->_debug(
'authsplit:'.__FUNCTION__.'(): primary auth plugin\'s ' .
'getUserData(): '.$this->_dumpUserData($params).'.',
1, __LINE__, __FILE__
);
/* Create the new user account */
$result = $this->triggerUserMod(
'create',
array(
$user, $pass,
$params['name'], $params['mail'], $params['grps']
)
);
if ($result === false || $result === null)
{
$this->_debug(
'authsplit:'.__FUNCTION__.'(): primary auth plugin\'s '.
'getUserData() could not supply data.', -1,
__LINE__, __FILE__
);
return false;
}
if(actionOK('profile'))
{
msg($this->getLang('autocreated'), -1);
}
return true;
}
/**
* Logoff the user (useful for external authentication)
*
* @param string $user the user name
* @param string $pass the clear text password
* @return bool
*/
public function logOff() {
return $this->authplugins['primary']->logOff();
}
/**
* Log the user In (useful for external authentication)
*
* @return bool
*/
public function logIn() {
return $this->authplugins['primary']->logIn();
}
/**
* Return user info
*
* Returned info about the given user needs to contain
* at least these fields:
*
* name string full name of the user
* mail string email address of the user
* grps array list of groups the user is in
*
* @param string $user the user name
* @param bool $requireGroups whether to return user's groups as well
* @return array containing user data or false
*/
public function getUserData($user, $requireGroups = true) {
/* A user must be present in BOTH auth plugins. */
$userinfo = $this->authplugins['primary']->getUserData($user, false);
if (!$userinfo) {
$this->_debug(
'authsplit:checkPass(): primary auth plugin\'s getUserData() '.
'failed, seems user is yet unknown there.', 1,
__LINE__, __FILE__
);
return false;
}
$userinfo = $this->authplugins['secondary']->getUserData($user, $requireGroups);
if (!$userinfo) {
$this->_debug(
'authsplit:checkPass(): secondary auth plugin\'s getUserData() '.
'failed, seems user is yet unknown there.', 1,
__LINE__, __FILE__
);
return false;
}
$this->_debug(
'authsplit:getUserData(): secondary auth plugin\'s getUserData(): '.
$this->_dumpUserData($userinfo).'.', 1, __LINE__, __FILE__
);
return $userinfo;
}
/**
* Returns a string representation of user data for debugging purposes
*
* @param array $user An array with user data
* @return string
*/
protected function _dumpUserData($user) {
$msg = 'Name: "'.$user['name'].'", '.
'Mail: "'.$user['mail'].'", ' .
'Groups: ';
foreach ($user['grps'] as $grp) {
$msg .= '"'.$grp.'", ';
}
$msg = rtrim($msg, ', ');
return $msg;
}
/**
* Create a new User
*
* Returns false if the user already exists, null when an error
* occurred and true if everything went well.
*
* The new user HAS TO be added to the default group by this
* function!
*
* Set addUser capability when implemented
*
* @param string $user
* @param string $pass
* @param string $name
* @param string $mail
* @param null|array $grps
* @return bool|null
*/
public function createUser($user, $pass, $name, $mail, $grps = null) {
/* Does the user not exist yet in the primary auth plugin and does it
support creating users? */
$userinfo = $this->authplugins['primary']->getUserData($user, false);
if (!$userinfo && $this->authplugins['primary']->cando['addUser']) {
$result = $this->authplugins['primary']->createUser(
$user, $pass, $name, $email
);
if ($result === false || $result === null) {
$this->_debug(
'authsplit:createUser(): primary auth plugin\'s '.
'createUser() failed.', -1, __LINE__, __FILE__
);
return $result;
}
$this->_debug(
'authsplit:createUser(): user created in primary auth plugin.',
1, __LINE__, __FILE__
);
}
/* We need to create the user in the secondary auth plugin in any case. */
$result = $this->authplugins['secondary']->createUser(
$user, '', $name, $mail, $grps
);
if ($result === false || $result === null) {
$this->_debug(
'authsplit:createUser(): secondary auth plugin\'s '.
'createUser() failed.', -1, __LINE__, __FILE__
);
return $result;
}
$this->_debug(
'authsplit:createUser(): user created in secondary auth plugin.',
1, __LINE__, __FILE__
);
return true;
}
/**
* Modify user data
*
* Set the mod* capabilities according to the implemented features
*
* @param string $user nick of the user to be changed
* @param array $changes array of field/value pairs to be changed
* (password will be clear text)
* @return bool
*/
public function modifyUser($user, $changes) {
if (!is_array($changes) || !count($changes))
return true; // nothing to change
foreach ($changes as $field => $value) {
if ($field == 'pass') {
/* Passwords must be changed in the primary auth plugin */
$result = $this->authplugins['primary']->modifyUser(
$user,
array(
'pass' => $value
)
);
if (!$result) {
$this->_debug(
'authsplit:modifyUser(): primary auth plugin\'s '.
'modifyUser() failed.', -1, __LINE__, __FILE__
);
return false;
}
}
elseif ($field == 'grps') {
/* Groups are handled by the secondary auth plugin. */
$result = $this->authplugins['secondary']->modifyUser(
$user,
array(
'grps' => $value
)
);
if (!$result) {
$this->_debug(
'authsplit:modifyUser(): secondary auth plugin\'s '.
'modifyUser() failed.', -1, __LINE__, __FILE__
);
return false;
}
}
elseif ( ($field == 'login') || ($field == 'name') || ($field == 'mail') ) {
/* If the primary auth plugin supports the update,
we'll try it there first. */
if ($this->authplugins['primary']->canDo('mod' . ucfirst($field))) {
$result = $this->authplugins['primary']->modifyUser(
$user, array(
$field => $value
)
);
if (!$result) {
$this->_debug(
'authsplit:modifyUser(): primary auth plugin\'s '.
'modifyUser() failed.', -1, __LINE__, __FILE__
);
return false;
}
}
else {
$this->_debug(
"authsplit:modifyUser(): primary auth plugin " .
"doesn\'t support modifying $field.", 1, __LINE__, __FILE__
);
}
/* Now in the secondary auth plugin. */
$result = $this->authplugins['secondary']->modifyUser(
$user,
array(
$field => $value
)
);
if (!$result) {
$this->_debug(
'authsplit:modifyUser(): secondary auth plugin\'s '.
'modifyUser() failed.', -1, __LINE__, __FILE__
);
return false;
}
}
}
return true;
}
/**
* Delete one or more users
*
* Set delUser capability when implemented
*
* @param array $users
* @return int number of users deleted
*/
public function deleteUsers($users) {
/* We do NOT attempt to delete the user with the primary auth plugin.
Just because we don't want the account in DokuWiki any more, does not
mean that there are no other services that depend on the account's
existance in the primary auth source. */
$result = $this->authplugins['secondary']->deleteUsers($users);
if (!$result) {
$this->_debug(
'authsplit:deleteUsers(): secondary auth plugin\'s '.
'deleteUsers() failed.', -1, __LINE__, __FILE__
);
}
return $result;
}
/**
* Bulk retrieval of user data
*
* Set getUsers capability when implemented
*
* @param int $start index of first user to be returned
* @param int $limit max number of users to be returned
* @param array $filter array of field/pattern pairs, null for no filter
* @return array list of userinfo (refer to getuseraccts for internal
* userinfo details)
*/
public function retrieveUsers($start = 0, $limit = -1, $filter = null) {
/* We're always interested in the users defined in the secondary auth
plugin. */
$result = $this->authplugins['secondary']->retrieveUsers(
$start, $limit, $filter
);
if (!$result) {
$this->_debug(
'authsplit:retrieveUsers(): secondary auth plugin\'s '.
'retrieveUsers() failed.', -1, __LINE__, __FILE__
);
}
return $result;
}
/**
* Return a count of the number of user which meet $filter criteria
*
* Set getUserCount capability when implemented
*
* @param array $filter array of field/pattern pairs, empty array for no filter
* @return int
*/
public function getUserCount($filter = array()) {
/* We're always interested in the users defined in the secondary auth plugin. */
$result = $this->authplugins['secondary']->getUserCount($filter);
if (!$result) {
$this->_debug(
'authsplit:getUserCount(): secondary auth plugin\'s '.
'getUserCount() failed.', -1, __LINE__, __FILE__
);
}
return $result;
}
/**
* Define a group
*
* Set addGroup capability when implemented
*
* @param string $group
* @return bool
*/
public function addGroup($group) {
/* Groups are always defined in the secondary auth plugin. */
$result = $this->authplugins['secondary']->addGroup($group);
if (!$result) {
$this->_debug(
'authsplit:addGroup(): secondary auth plugin\'s addGroup() '.
'failed.', -1, __LINE__, __FILE__
);
}
return $result;
}
/**
* Retrieve groups
*
* Set getGroups capability when implemented
*
* @param int $start
* @param int $limit
* @return array
*/
public function retrieveGroups($start = 0, $limit = 0) {
/* Groups are always defined in the secondary auth plugin. */
$result = $this->authplugins['secondary']->retrieveGroups($start, $limit);
if (!$result) {
$this->_debug(
'authsplit:retrieveGroups(): secondary auth plugin\'s '.
'retrieveGroups() failed.', -1, __LINE__, __FILE__
);
}
return $result;
}
/**
* Return case sensitivity of the backend
*
* When your backend is caseinsensitive (eg. you can login with USER and
* user) then you need to overwrite this method and return false
*
* @return bool
*/
public function isCaseSensitive() {
/* The primary auth plugin dictates case-sensitivity of login names. */
$result = $this->authplugins['primary']->isCaseSensitive();
$status = $result ? "Yes" : "No";
$this->_debug(
'authsplit:isCaseSensitive(): primary auth plugin\'s '.
'isCaseSensitive(): '.$status.'.', 1, __LINE__, __FILE__
);
return $result;
}
/**
* Sanitize a given username
*
* This function is applied to any user name that is given to
* the backend and should also be applied to any user name within
* the backend before returning it somewhere.
*
* This should be used to enforce username restrictions.
*
* @param string $user username
* @return string the cleaned username
*/
public function cleanUser($user) {
/* The primary auth plugin dictates possible login name restrictions. */
$result = $this->authplugins['primary']->cleanUser($user);
$this->_debug(
'authsplit:cleanUser(): primary auth plugin\'s '.
'cleanUser("'.$user.'"): "'.$result.'".', 1, __LINE__, __FILE__
);
/* Apply case conversion? */
if ($this->username_caseconversion == 'To uppercase') {
$result = strtoupper($result);
$this->_debug(
'authsplit:cleanUser(): converted username to uppercase: '.
$result, 1, __LINE__, __FILE__
);
}
elseif ($this->username_caseconversion == 'To lowercase') {
$result = strtolower($result);
$this->_debug(
'authsplit:cleanUser(): converted username to lowercase: '.
$result, 1, __LINE__, __FILE__
);
}
return $result;
}
/**
* Sanitize a given groupname
*
* This function is applied to any groupname that is given to
* the backend and should also be applied to any groupname within
* the backend before returning it somewhere.
*
* This should be used to enforce groupname restrictions.
*
* Groupnames are to be passed without a leading '@' here.
*
* @param string $group groupname
* @return string the cleaned groupname
*/
public function cleanGroup($group) {
/* The secondary auth plugin dictates possible group names
restrictions. */
$result = $this->authplugins['secondary']->cleanGroup($group);
$this->_debug(
'authsplit:cleanGroup(): secondary auth plugin\'s '.
'cleanGroup("'.$group.'"): "'.$result.'".', 1, __LINE__, __FILE__
);
return $result;
}
}
// vim:ts=4:sw=4:et: