xref: /plugin/authchained/auth.php (revision 1b39d8c634486789ca5a77224c76cc8d70685d29)
1<?php
2// must be run within Dokuwiki
3if(!defined('DOKU_INC')) die();
4
5/**
6* Chained authentication backend
7*
8* @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
9* @author     Philipp Neuser <pneuser@physik.fu-berlin.de>
10* @author     Christian Marg <marg@rz.tu-clausthal.de>
11*
12* Based on "Chained authentication backend"
13* by Grant Gardner <grant@lastweekend.com.au>
14* see https://www.dokuwiki.org/auth:ggauth
15*
16*/
17class auth_plugin_authchained extends DokuWiki_Auth_Plugin {
18    public $success = true;
19    //array with authentication plugins
20    protected $chained_plugins = array();
21    protected $chained_auth = NULL;
22    protected $usermanager_auth = NULL;
23
24    /**
25    * Constructor.
26    *
27    * Loads all configured plugins or the authentication plugin of the
28    * logged in user.
29    *
30    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
31    * @author  Christian Marg <marg@rz.tu-clausthal.de>
32    */
33    public function __construct() {
34        global $conf;
35        // call parent
36        #      parent::__constructor();
37
38        //check if there is already an authentication plugin selected
39        if(     isset($_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']) &&
40                !empty($_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']) ) {
41
42            //get previously selected authentication plugin
43            $this->chained_auth =& plugin_load('auth',$_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']);
44            if ( is_null($this->chained_auth) || !$this->chained_auth->success ) {
45                $this->success = false;
46            }
47        } else {
48            //get authentication plugins
49            if($this->getConf('authtypes')){
50                foreach(explode(":",$this->getConf('authtypes')) as $tmp_plugin){
51                    $tmp_class =& plugin_load('auth',$tmp_plugin);
52
53                    if ( !is_null($tmp_class) || $tmp_class->success ) {
54                        $tmp_module = array($tmp_plugin,$tmp_class);
55                        array_push($this->chained_plugins, $tmp_module);
56                    } else {
57                        msg("Problem constructing $tmp_plugin",-1);
58                        $this->success = false;
59                    }
60                }
61            } else {
62                $success = false;
63            }
64        }
65
66        // If defined, instantiate usermanager authtype.
67        // No need to check for duplicates, "plugin_load" does that for us.
68        if($this->getConf('usermanager_authtype')){
69            $this->usermanager_auth =& plugin_load('auth',$this->getConf('usermanager_authtype'));
70            if(is_null($this->usermanager_auth) || !$this->usermanager_auth->success ) {
71                    msg("Problem constructing usermanager authtype: ".$this->getConf('usermanager_authtype'),-1);
72                    $this->success = false;
73            }
74        } else {
75            $this->usermanager_auth =& $this->chained_auth;
76        }
77
78        //debug
79        //      print_r($chained_plugins);
80    }
81
82    /**
83    * Forwards the authentication to configured authplugins.
84    * Returns true, if the usermanager authtype has the capability and no user
85    * is logged in.
86    *
87    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
88    * @author  Christian Marg <marg@rz.tu-clausthal.de>
89    * @param   string $cap the capability to check
90    * @return  bool
91    */
92    public function canDo($cap) {
93        global $ACT;
94        #      print_r($cap);
95        if(is_null($this->chained_auth)) {
96            if (!is_null($this->usermanager_auth)) {
97                return $this->usermanager_auth->canDo($cap);
98            } else {
99                return parent::canDo($cap);
100            }
101        } else {
102            switch($cap) {
103                case 'Profile':
104                case 'logoff':
105                    //Depends on current user.
106                    return $this->chained_auth->canDo($cap);
107                case 'UserMod':
108                case 'addUser':
109                case 'delUser':
110                case 'getUsers':
111                case 'getUserCount':
112                case 'getGroups':
113                    //Depends on the auth for use with user manager
114                    return $this->usermanager_auth->canDo($cap);
115                case 'modPass':
116                case 'modName':
117                case 'modLogin':
118                case 'modGroups':
119                case 'modMail':
120                    /**
121                    * Use request attributes to guess whether we are in the Profile or UserManager
122                    * and return the appropriate auth capabilities
123                    */
124                    if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
125                        return $this->usermanager_auth->canDo($cap);
126                    } else {
127                        // assume we want profile info.
128                        return $this->chained_auth->canDo($cap);
129                    }
130// I don't know how to handle "external" in this context yet.
131// Is it in any way sensible to mix regular auth with external auth?
132//                case 'external':
133//                    //We are external if one of the chains is valid for external use
134//                    return $this->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']);
135                default:
136                    //Everything else (false)
137                    return parent::canDo($cap);
138            }
139            #echo "canDo $cap ".$this->chained_auth->canDo($cap)."\n";
140        }
141    }
142
143    /**
144    * Forwards the result of the auth plugin of the logged in user and
145    * unsets our session variable.
146    * @see     auth_logoff()
147    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de
148    * @author  Christian Marg <marg@rz.tu-clausthal.de>
149    */
150    public function logOff() {
151        if(!is_null($this->chained_auth))
152            $this->chained_auth->logOff();
153        unset($_SESSION[DOKU_COOKIE]['plugin']['authchained']['module']);
154    }
155
156    /**
157    * Do all authentication [ OPTIONAL ]
158    * If the current plugin is external, be external.
159    *
160    * @see     auth_login()
161    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
162    * @author  Christian Marg <marg@rz.tu-clausthal.de>
163    *
164    * @param   string  $user    Username
165    * @param   string  $pass    Cleartext Password
166    * @param   bool    $sticky  Cookie should not expire
167    * @return  bool             true on successful auth
168    */
169    public function trustExternal($user, $pass, $sticky = false) {
170        if(!is_null($this->chained_auth) && $this->chained_auth->canDo('external'))
171            $this->chained_auth->trustExternal($user, $pass, $sticky);
172    }
173
174    /**
175    * Check user+password [ MUST BE OVERRIDDEN ]
176    *
177    * Checks if the given user exists in one of the plugins and checks
178    * against the given password. The first plugin returning true becomes
179    * auth plugin of the user session.
180    *
181    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de
182    * @author  Christian Marg <marg@rz.tu-clausthal.de>
183    * @param   string $user the user name
184    * @param   string $pass the clear text password
185    * @return  bool
186    */
187    public function checkPass($user, $pass) {
188        //debug
189        //print_r($this->chained_plugins);
190        if(is_null($this->chained_auth)) {
191            foreach($this->chained_plugins as $module) {
192                if($module[1]->canDo('external')) {
193                    if($module[1]->trustExternal($user, $pass)) {
194                        $_SESSION[DOKU_COOKIE]['plugin']['authchained']['module'] = $module[0];
195                        $this->chained_auth = $module[1];
196                        return true;
197                    } else {
198                        if($module[1]->checkPass($user, $pass)) {
199                            $_SESSION[DOKU_COOKIE]['plugin']['authchained']['module'] = $module[0];
200                            $this->chained_auth = $module[1];
201                            return true;
202                        }
203                    }
204                } else {
205                    if($module[1]->checkPass($user, $pass)) {
206                        $_SESSION[DOKU_COOKIE]['plugin']['authchained']['module'] = $module[0];
207                        $this->chained_auth = $module[1];
208                        return true;
209                    }
210                }
211            }
212        } else {
213            return $this->chained_auth->checkPass($user, $pass);
214        }
215        return false;
216    }
217
218    /**
219    * Forwards the result of the auth plugin of the logged in user or
220    * checks all plugins if the users exists. The first plugin returning
221    * data is used.
222    *
223    * name string  full name of the user
224    * mail string  email addres of the user
225    * grps array   list of groups the user is in
226    *
227    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
228    * @author  Christian Marg <marg@rz.tu-clausthal.de>
229    * @param   string $user the user name
230    * @return  array containing user data or false
231    */
232    public function getUserData($user) {
233        global $ACT;
234        //if(!$this->cando['external']) msg("no valid authorisation system in use", -1);
235        //       echo "TESTSETEST";
236
237        //print_r($this->chained_auth);
238        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
239            if(!is_null($this->usermanager_auth))
240                return $this->usermanager_auth->getUserData($user);
241
242        if(is_null($this->chained_auth)) {
243            foreach($this->chained_plugins as $module) {
244                $tmp_array = $module[1]->getUserData($user);
245                if(!is_bool($tmp_array))
246                    $tmp_chk_arr =array_filter($tmp_array);
247                if(!empty($tmp_chk_arr) && $tmp_array)
248                    return $tmp_array;
249            }
250            return false;
251        } else {
252            return $this->chained_auth->getUserData($user);
253        }
254    }
255
256    /**
257    * Forwards the result of the auth plugin of the logged in user or
258    * returns null.
259    *
260    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
261    * @author  Christian Marg <marg@rz.tu-clausthal.de>
262    * @param   string     $user
263    * @param   string     $pass
264    * @param   string     $name
265    * @param   string     $mail
266    * @param   null|array $grps
267    * @return  bool|null
268    */
269    public function createUser($user, $pass, $name, $mail, $grps = null) {
270        if(!is_null($this->usermanager_auth) && $this->canDo('addUser')) {
271            return $this->usermanager_auth->createUser($user, $pass, $name, $mail, $grps);
272        } else {
273            msg("authorisation method does not allow creation of new users", -1);
274            return null;
275        }
276    }
277
278    /**
279    * Forwards the result of the auth plugin of the logged in user or
280    * returns false
281    *
282    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
283    * @author  Christian Marg <marg@rz.tu-clausthal.de>
284    * @param   string $user    nick of the user to be changed
285    * @param   array  $changes array of field/value pairs to be changed (password will be clear text)
286    * @return  bool
287    */
288    public function modifyUser($user, $changes) {
289        if(!is_null($this->usermanager_auth) && $this->canDo('UserMod') ) {
290            return $this->usermanager_auth->modifyUser($user, $changes);
291        } else {
292            msg("authorisation method does not allow modifying of user data", -1);
293            return null;
294        }
295    }
296
297    /**
298    * Forwards the result of the auth plugin of the logged in user or
299    * returns false
300    *
301    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
302    * @author  Christian Marg <marg@rz.tu-clausthal.de>
303    * @param   array  $users
304    * @return  int    number of users deleted
305    */
306    public function deleteUsers($users) {
307        if(!is_null($this->usermanager_auth) && $this->canDo('delUser') ) {
308            return $this->usermanager_auth->deleteUsers($users);
309        }else{
310            msg("authorisation method does not allow deleting of users", -1);
311            return false;
312        }
313    }
314
315    /**
316    * Forwards the result of the auth plugin of the logged in user or
317    * returns 0
318    *
319    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
320    * @author Christian Marg <marg@rz.tu-clausthal.de>
321    * @param  array $filter array of field/pattern pairs, empty array for no filter
322    * @return int
323    */
324    public function getUserCount($filter = array()) {
325        if(!is_null($this->usermanager_auth) && $this->canDo('getUserCount') ){
326            return $this->usermanager_auth->getUserCount($filter);
327        } else {
328            msg("authorisation method does not provide user counts", -1);
329            return 0;
330        }
331    }
332
333    /**
334    * Forwards the result of the auth plugin of the logged in user or
335    * returns empty array
336    *
337    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
338    * @author  Christian Marg <marg@rz.tu-clausthal.de>
339    * @param   int   $start     index of first user to be returned
340    * @param   int   $limit     max number of users to be returned
341    * @param   array $filter    array of field/pattern pairs, null for no filter
342    * @return  array list of userinfo (refer getUserData for internal userinfo details)
343    */
344    public function retrieveUsers($start = 0, $limit = -1, $filter = null) {
345        if(!is_null($this->usermanager_auth) && $this->canDo('getUsers') ) {
346            //msg("RetrieveUsers is using ".get_class($this->usermanager_auth));
347            return $this->usermanager_auth->retrieveUsers($start, $limit, $filter);
348        } else {
349            msg("authorisation method does not support mass retrievals", -1);
350            return array();
351        }
352    }
353
354    /**
355    * Forwards the result of the auth plugin of the logged in user or
356    * returns false
357    *
358    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
359    * @author  Christian Marg <marg@rz.tu-clausthal.de>
360    * @param   string $group
361    * @return  bool
362    */
363    public function addGroup($group) {
364        if(!is_null($this->usermanager_auth) && $this->canDo('addGroup') ) {
365            return $this->usermanager_auth->addGroup($group);
366        } else {
367            msg("authorisation method does not support independent group creation", -1);
368            return false;
369        }
370    }
371
372    /**
373    * Forwards the result of the auth plugin of the logged in user or
374    * returns empty array
375    *
376    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
377    * @author  Christian Marg <marg@rz.tu-clausthal.de>
378    * @param   int $start
379    * @param   int $limit
380    * @return  array
381    */
382    public function retrieveGroups($start = 0, $limit = 0) {
383        if(!is_null($this->usermanager_auth) && $this->canDo('getGroups') ) {
384                return $this->usermanager_auth->retrieveGroups($start,$limit);
385        } else {
386            msg("authorisation method does not support group list retrieval", -1);
387            return array();
388        }
389    }
390
391    /**
392    * Forwards the result of the auth plugin of the logged in user or
393    * returns true
394    *
395    * @return bool
396    */
397    public function isCaseSensitive() {
398        if(is_null($this->chained_auth))
399            return parent::isCaseSensitive();
400        else
401            return $this->chained_auth->isCaseSensitive();
402    }
403
404    /**
405    * Sanitize a given username [OPTIONAL]
406    * Forwards the result of the auth plugin of the logged in user or
407    * returns false
408    *
409    *
410    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
411    * @author Christian Marg <marg@rz.tu-clausthal.de>
412    * @param  string $user username
413    * @return string the cleaned username
414    */
415    public function cleanUser($user) {
416        global $ACT;
417        //print_r($this->chained_auth);
418        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
419            if(!is_null($this->usermanager_auth))
420                return $this->usermanager_auth->cleanUser($user);
421        } else {
422            if(!is_null($this->chained_auth))
423                return $this->chained_auth->cleanUser($user);
424        }
425        return parent::cleanUser($user);
426    }
427
428    /**
429    * Sanitize a given groupname [OPTIONAL]
430    * Forwards the result of the auth plugin of the logged in user or
431    * returns false
432    *
433    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
434    * @author Christian Marg <marg@rz.tu-clausthal.de>
435    * @param  string $group groupname
436    * @return string the cleaned groupname
437    */
438    public function cleanGroup($group) {
439        global $ACT;
440        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
441            if(!is_null($this->usermanager_auth))
442                return $this->usermanager_auth->cleanGroup($group);
443        } else {
444            if(!is_null($this->chained_auth))
445                return $this->chained_auth->cleanGroup($group);
446        }
447        return parent::cleanGroup($group);
448    }
449
450
451    public function useSessionCache($user) {
452        global $conf;
453        if(is_null($this->chained_auth))
454            return parent::useSessionCache($user);
455        else
456            return $this->chained_auth->useSessionCache($user);
457    }
458
459}
460