xref: /plugin/authchained/auth.php (revision aca355cf857fb5105df4c52c8479dacaa7f2d750)
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->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        //if(!$this->cando['external']) msg("no valid authorisation system in use", -1);
234        //       echo "TESTSETEST";
235        if(is_null($this->chained_auth)) {
236            foreach($this->chained_plugins as $module) {
237                $tmp_array = $module[1]->getUserData($user);
238                if(!is_bool($tmp_array))
239                    $tmp_chk_arr =array_filter($tmp_array);
240                if(!empty($tmp_chk_arr) && $tmp_array)
241                    return $tmp_array;
242            }
243            return false;
244        } else {
245            return $this->chained_auth->getUserData($user);
246        }
247    }
248
249    /**
250    * Forwards the result of the auth plugin of the logged in user or
251    * returns null.
252    *
253    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
254    * @author  Christian Marg <marg@rz.tu-clausthal.de>
255    * @param   string     $user
256    * @param   string     $pass
257    * @param   string     $name
258    * @param   string     $mail
259    * @param   null|array $grps
260    * @return  bool|null
261    */
262    public function createUser($user, $pass, $name, $mail, $grps = null) {
263        if(!is_null($this->usermanager_auth) && $this->canDo('addUser')) {
264            return $this->usermanager_auth->createUser($user, $pass, $name, $mail, $grps);
265        } else {
266            msg("authorisation method does not allow creation of new users", -1);
267            return null;
268        }
269    }
270
271    /**
272    * Forwards the result of the auth plugin of the logged in user or
273    * returns false
274    *
275    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
276    * @author  Christian Marg <marg@rz.tu-clausthal.de>
277    * @param   string $user    nick of the user to be changed
278    * @param   array  $changes array of field/value pairs to be changed (password will be clear text)
279    * @return  bool
280    */
281    public function modifyUser($user, $changes) {
282        if(!is_null($this->usermanager_auth) && $this->canDo('UserMod') ) {
283            return $this->usermanager_auth->modifyUser($user, $changes);
284        } else {
285            msg("authorisation method does not allow modifying of user data", -1);
286            return null;
287        }
288    }
289
290    /**
291    * Forwards the result of the auth plugin of the logged in user or
292    * returns false
293    *
294    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
295    * @author  Christian Marg <marg@rz.tu-clausthal.de>
296    * @param   array  $users
297    * @return  int    number of users deleted
298    */
299    public function deleteUsers($users) {
300        if(!is_null($this->usermanager_auth) && $this->canDo('delUser') ) {
301            return $this->usermanager_auth->deleteUsers($users);
302        }else{
303            msg("authorisation method does not allow deleting of users", -1);
304            return false;
305        }
306    }
307
308    /**
309    * Forwards the result of the auth plugin of the logged in user or
310    * returns 0
311    *
312    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
313    * @author Christian Marg <marg@rz.tu-clausthal.de>
314    * @param  array $filter array of field/pattern pairs, empty array for no filter
315    * @return int
316    */
317    public function getUserCount($filter = array()) {
318        if(!is_null($this->usermanager_auth) && $this->canDo('getUserCount') ){
319            return $this->usermanager_auth->getUserCount($filter);
320        } else {
321            msg("authorisation method does not provide user counts", -1);
322            return 0;
323        }
324    }
325
326    /**
327    * Forwards the result of the auth plugin of the logged in user or
328    * returns empty array
329    *
330    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
331    * @author  Christian Marg <marg@rz.tu-clausthal.de>
332    * @param   int   $start     index of first user to be returned
333    * @param   int   $limit     max number of users to be returned
334    * @param   array $filter    array of field/pattern pairs, null for no filter
335    * @return  array list of userinfo (refer getUserData for internal userinfo details)
336    */
337    public function retrieveUsers($start = 0, $limit = -1, $filter = null) {
338        if(!is_null($this->usermanager_auth) && $this->canDo('getUsers') ) {
339            //msg("RetrieveUsers is using ".get_class($this->usermanager_auth));
340            return $this->usermanager_auth->retrieveUsers($start, $limit, $filter);
341        } else {
342            msg("authorisation method does not support mass retrievals", -1);
343            return array();
344        }
345    }
346
347    /**
348    * Forwards the result of the auth plugin of the logged in user or
349    * returns false
350    *
351    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
352    * @author  Christian Marg <marg@rz.tu-clausthal.de>
353    * @param   string $group
354    * @return  bool
355    */
356    public function addGroup($group) {
357        if(!is_null($this->usermanager_auth) && $this->canDo('addGroup') ) {
358            return $this->usermanager_auth->addGroup($group);
359        } else {
360            msg("authorisation method does not support independent group creation", -1);
361            return false;
362        }
363    }
364
365    /**
366    * Forwards the result of the auth plugin of the logged in user or
367    * returns empty array
368    *
369    * @author  Philipp Neuser <pneuser@physik.fu-berlin.de>
370    * @author  Christian Marg <marg@rz.tu-clausthal.de>
371    * @param   int $start
372    * @param   int $limit
373    * @return  array
374    */
375    public function retrieveGroups($start = 0, $limit = 0) {
376        if(!is_null($this->usermanager_auth) && $this->canDo('getGroups') ) {
377                return $this->usermanager_auth->retrieveGroups($start,$limit);
378        } else {
379            msg("authorisation method does not support group list retrieval", -1);
380            return array();
381        }
382    }
383
384    /**
385    * Forwards the result of the auth plugin of the logged in user or
386    * returns true
387    *
388    * @return bool
389    */
390    public function isCaseSensitive() {
391        if(is_null($this->chained_auth))
392            return parent::isCaseSensitive();
393        else
394            return $this->chained_auth->isCaseSensitive();
395    }
396
397    /**
398    * Sanitize a given username [OPTIONAL]
399    * Forwards the result of the auth plugin of the logged in user or
400    * returns false
401    *
402    *
403    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
404    * @author Christian Marg <marg@rz.tu-clausthal.de>
405    * @param  string $user username
406    * @return string the cleaned username
407    */
408    public function cleanUser($user) {
409        global $ACT;
410        //print_r($this->chained_auth);
411        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
412            if(!is_null($this->usermanager_auth))
413                return $this->usermanager_auth->cleanUser($user);
414        } else {
415            if(!is_null($this->chained_auth))
416                return $this->chained_auth->cleanUser($user);
417        }
418        return parent::cleanUser($user);
419    }
420
421    /**
422    * Sanitize a given groupname [OPTIONAL]
423    * Forwards the result of the auth plugin of the logged in user or
424    * returns false
425    *
426    * @author Philipp Neuser <pneuser@physik.fu-berlin.de>
427    * @author Christian Marg <marg@rz.tu-clausthal.de>
428    * @param  string $group groupname
429    * @return string the cleaned groupname
430    */
431    public function cleanGroup($group) {
432        global $ACT;
433        if ($ACT == "admin" && $_REQUEST['page']=="usermanager") {
434            if(!is_null($this->usermanager_auth))
435                return $this->usermanager_auth->cleanGroup($group);
436        } else {
437            if(!is_null($this->chained_auth))
438                return $this->chained_auth->cleanGroup($group);
439        }
440        return parent::cleanGroup($group);
441    }
442
443
444    public function useSessionCache($user) {
445        global $conf;
446        if(is_null($this->chained_auth))
447            return parent::useSessionCache($user);
448        else
449            return $this->chained_auth->useSessionCache($user);
450    }
451
452}
453