xref: /plugin/oauth/auth.php (revision 0aa332ffe96d59de83453551ff302435906e621b)
180852c15SAndreas Gohr<?php
280852c15SAndreas Gohr/**
380852c15SAndreas Gohr * DokuWiki Plugin oauth (Auth Component)
480852c15SAndreas Gohr *
580852c15SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
680852c15SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
780852c15SAndreas Gohr */
880852c15SAndreas Gohr
980852c15SAndreas Gohr// must be run within Dokuwiki
1080852c15SAndreas Gohrif(!defined('DOKU_INC')) die();
1180852c15SAndreas Gohr
12f10e09e2SAndreas Gohrclass auth_plugin_oauth extends auth_plugin_authplain {
1380852c15SAndreas Gohr
14f866280eSAndreas Gohr    /**
15f866280eSAndreas Gohr     * Constructor
16f866280eSAndreas Gohr     *
17f866280eSAndreas Gohr     * Sets capabilities.
18f866280eSAndreas Gohr     */
1980852c15SAndreas Gohr    public function __construct() {
20f10e09e2SAndreas Gohr        parent::__construct();
2180852c15SAndreas Gohr
22f10e09e2SAndreas Gohr        $this->cando['external'] = true;
2380852c15SAndreas Gohr    }
2480852c15SAndreas Gohr
25f866280eSAndreas Gohr    /**
26f866280eSAndreas Gohr     * Handle the login
27f866280eSAndreas Gohr     *
28f866280eSAndreas Gohr     * This either trusts the session data (if any), processes the second oAuth step or simply
29f866280eSAndreas Gohr     * executes a normal plugin against local users.
30f866280eSAndreas Gohr     *
31f866280eSAndreas Gohr     * @param string $user
32f866280eSAndreas Gohr     * @param string $pass
33f866280eSAndreas Gohr     * @param bool   $sticky
34f866280eSAndreas Gohr     * @return bool
35f866280eSAndreas Gohr     */
36f10e09e2SAndreas Gohr    function trustExternal($user, $pass, $sticky = false) {
37a7a8f46aSAndreas Gohr        global $conf;
38a7a8f46aSAndreas Gohr        global $USERINFO;
3980852c15SAndreas Gohr
402e94f0b8SAndreas Gohr        // are we in login progress?
412e94f0b8SAndreas Gohr        if(isset($_SESSION[DOKU_COOKIE]['oauth-inprogress'])) {
422e94f0b8SAndreas Gohr            $servicename = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['service'];
432e94f0b8SAndreas Gohr            $page        = $_SESSION[DOKU_COOKIE]['oauth-inprogress']['id'];
442e94f0b8SAndreas Gohr
452e94f0b8SAndreas Gohr            unset($_SESSION[DOKU_COOKIE]['oauth-inprogress']);
462e94f0b8SAndreas Gohr        }
4780852c15SAndreas Gohr
48a7a8f46aSAndreas Gohr        // check session for existing oAuth login data
49a7a8f46aSAndreas Gohr        $session = $_SESSION[DOKU_COOKIE]['auth'];
502e94f0b8SAndreas Gohr        if(!isset($servicename) && isset($session['oauth'])) {
51a7a8f46aSAndreas Gohr            $servicename = $session['oauth'];
52a7a8f46aSAndreas Gohr            // check if session data is still considered valid
53a7a8f46aSAndreas Gohr            if(($session['time'] >= time() - $conf['auth_security_timeout']) &&
54f866280eSAndreas Gohr                ($session['buid'] == auth_browseruid())
55f866280eSAndreas Gohr            ) {
5680852c15SAndreas Gohr
57a7a8f46aSAndreas Gohr                $_SERVER['REMOTE_USER'] = $session['user'];
58a7a8f46aSAndreas Gohr                $USERINFO               = $session['info'];
5980852c15SAndreas Gohr                return true;
60f10e09e2SAndreas Gohr            }
6180852c15SAndreas Gohr        }
6280852c15SAndreas Gohr
63a7a8f46aSAndreas Gohr        // either we're in oauth login or a previous log needs to be rechecked
642e94f0b8SAndreas Gohr        if(isset($servicename)) {
65a7a8f46aSAndreas Gohr            /** @var helper_plugin_oauth $hlp */
66a7a8f46aSAndreas Gohr            $hlp     = plugin_load('helper', 'oauth');
67a7a8f46aSAndreas Gohr            $service = $hlp->loadService($servicename);
68a7a8f46aSAndreas Gohr            if(is_null($service)) return false;
69a7a8f46aSAndreas Gohr
70a7a8f46aSAndreas Gohr            if($service->checkToken()) {
71213f4618SMichael Große
72213f4618SMichael Große
73a7a8f46aSAndreas Gohr                $uinfo = $service->getUser();
74f866280eSAndreas Gohr
751025aad7SAndreas Gohr                $uinfo['user'] = $this->cleanUser((string) $uinfo['user']);
761025aad7SAndreas Gohr                if(!$uinfo['name']) $uinfo['name'] = $uinfo['user'];
771025aad7SAndreas Gohr
781025aad7SAndreas Gohr                if(!$uinfo['user'] || !$uinfo['mail']) {
791025aad7SAndreas Gohr                    msg("$servicename did not provide the needed user info. Can't log you in", -1);
801025aad7SAndreas Gohr                    return false;
811025aad7SAndreas Gohr                }
821025aad7SAndreas Gohr
83f866280eSAndreas Gohr                // see if the user is known already
84f866280eSAndreas Gohr                $user = $this->getUserByEmail($uinfo['mail']);
85f866280eSAndreas Gohr                if($user) {
86f866280eSAndreas Gohr                    $sinfo = $this->getUserData($user);
873c0138dbSAndreas Gohr                    // check if the user allowed access via this service
883c0138dbSAndreas Gohr                    if(!in_array($this->cleanGroup($servicename), $sinfo['grps'])) {
893c0138dbSAndreas Gohr                        msg(sprintf($this->getLang('authnotenabled'), $servicename), -1);
903c0138dbSAndreas Gohr                        return false;
913c0138dbSAndreas Gohr                    }
92f866280eSAndreas Gohr                    $uinfo['user'] = $user;
93f866280eSAndreas Gohr                    $uinfo['name'] = $sinfo['name'];
94f866280eSAndreas Gohr                    $uinfo['grps'] = array_merge((array) $uinfo['grps'], $sinfo['grps']);
95*0aa332ffSMichael Große                } elseif (strpos($conf['disableactions'],'register') === False) {
96f866280eSAndreas Gohr                    // new user, create him - making sure the login is unique by adding a number if needed
97f866280eSAndreas Gohr                    $user  = $uinfo['user'];
98f866280eSAndreas Gohr                    $count = '';
99f866280eSAndreas Gohr                    while($this->getUserData($user . $count)) {
100f866280eSAndreas Gohr                        if($count) {
101f866280eSAndreas Gohr                            $count++;
102f866280eSAndreas Gohr                        } else {
103f866280eSAndreas Gohr                            $count = 1;
104f866280eSAndreas Gohr                        }
105f866280eSAndreas Gohr                    }
106f866280eSAndreas Gohr                    $user            = $user . $count;
107f866280eSAndreas Gohr                    $uinfo['user']   = $user;
1086047eb11SMichael Große                    $groups_on_creation = array();
1096047eb11SMichael Große                    $groups_on_creation[] = $conf['defaultgroup'];
1106047eb11SMichael Große                    $groups_on_creation[] = $this->cleanGroup($servicename); // add service as group
1116047eb11SMichael Große                    $uinfo['grps'] = array_merge((array) $uinfo['grps'], $groups_on_creation);
112f866280eSAndreas Gohr
1136c23164dSMichael Große                    $ok = $this->triggerUserMod('create',array($user, auth_pwgen($user), $uinfo['name'], $uinfo['mail'],
1146c23164dSMichael Große                                                          $groups_on_creation));
115caa5ded4SAndreas Gohr                    if(!$ok) {
116caa5ded4SAndreas Gohr                        msg('something went wrong creating your user account. please try again later.', -1);
117caa5ded4SAndreas Gohr                        return false;
118caa5ded4SAndreas Gohr                    }
119caa5ded4SAndreas Gohr
120caa5ded4SAndreas Gohr                    // send notification about the new user
121caa5ded4SAndreas Gohr                    $subscription = new Subscription();
122caa5ded4SAndreas Gohr                    $subscription->send_register($user, $uinfo['name'], $uinfo['mail']);
123*0aa332ffSMichael Große                } else {
124*0aa332ffSMichael Große                    msg('Self-Registration is currently disabled. Please ask your DokuWiki administrator to create your account manually.', -1);
125*0aa332ffSMichael Große                    return false;
126f866280eSAndreas Gohr                }
127f866280eSAndreas Gohr
128f866280eSAndreas Gohr                // set user session
129a7a8f46aSAndreas Gohr                $this->setUserSession($uinfo, $servicename);
130213f4618SMichael Große
131213f4618SMichael Große                $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode('oauth').'|'.base64_encode($servicename);
132213f4618SMichael Große                $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
133213f4618SMichael Große                $time      = $sticky ? (time() + 60 * 60 * 24 * 365) : 0;
134213f4618SMichael Große                setcookie(DOKU_COOKIE,$cookie, $time, $cookieDir, '',($conf['securecookie'] && is_ssl()), true);
135213f4618SMichael Große
1364485a349SMichael Große                if(isset($page)) {
1374485a349SMichael Große                    send_redirect(wl($page));
1384485a349SMichael Große                }
139a7a8f46aSAndreas Gohr                return true;
140213f4618SMichael Große            } else {
141213f4618SMichael Große                $this->relogin($servicename);
142a7a8f46aSAndreas Gohr            }
143a7a8f46aSAndreas Gohr
144936b9c9cSMichael Große            unset($_SESSION[DOKU_COOKIE]['auth']);
145a7a8f46aSAndreas Gohr            return false; // something went wrong during oAuth login
146213f4618SMichael Große        } elseif (isset($_COOKIE[DOKU_COOKIE])) {
147213f4618SMichael Große            global $INPUT;
148213f4618SMichael Große            //try cookie
149213f4618SMichael Große            list($cookieuser, $cookiesticky, $auth, $servicename) = explode('|', $_COOKIE[DOKU_COOKIE]);
150213f4618SMichael Große            $cookieuser = base64_decode($cookieuser, true);
151213f4618SMichael Große            $auth = base64_decode($auth, true);
152213f4618SMichael Große            $servicename = base64_decode($servicename, true);
153213f4618SMichael Große            if ($auth === 'oauth') {
154213f4618SMichael Große                $this->relogin($servicename);
155213f4618SMichael Große            }
15680852c15SAndreas Gohr        }
15780852c15SAndreas Gohr
158a7a8f46aSAndreas Gohr        // do the "normal" plain auth login via form
159a7a8f46aSAndreas Gohr        return auth_login($user, $pass, $sticky);
160a7a8f46aSAndreas Gohr    }
16180852c15SAndreas Gohr
162213f4618SMichael Große    protected function relogin($servicename) {
163213f4618SMichael Große        global $INPUT;
164213f4618SMichael Große
165213f4618SMichael Große        /** @var helper_plugin_oauth $hlp */
166213f4618SMichael Große        $hlp     = plugin_load('helper', 'oauth');
167213f4618SMichael Große        $service     = $hlp->loadService($servicename);
168213f4618SMichael Große        if(is_null($service)) return false;
169213f4618SMichael Große
170213f4618SMichael Große        // remember service in session
171213f4618SMichael Große        session_start();
172213f4618SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['service'] = $servicename;
173213f4618SMichael Große        $_SESSION[DOKU_COOKIE]['oauth-inprogress']['id']      = $INPUT->str('id');
174213f4618SMichael Große
175213f4618SMichael Große        $str_vars = array('wikitext', 'prefix', 'suffix', 'summary', 'sectok', 'target', 'range', 'rev', 'at');
176213f4618SMichael Große        foreach ($str_vars as $input_var) {
177213f4618SMichael Große            if ($INPUT->str($input_var) !== '') {
178213f4618SMichael Große                $_SESSION[DOKU_COOKIE]['oauth-done'][$input_var] = $INPUT->str($input_var);
179213f4618SMichael Große            }
180213f4618SMichael Große
181213f4618SMichael Große            if ($INPUT->post->str($input_var) !== '') {
182213f4618SMichael Große                $_SESSION[DOKU_COOKIE]['oauth-done']['post'][$input_var] = $INPUT->post->str($input_var);
183213f4618SMichael Große            }
184213f4618SMichael Große
185213f4618SMichael Große            if ($INPUT->get->str($input_var) !== '') {
186213f4618SMichael Große                $_SESSION[DOKU_COOKIE]['oauth-done']['get'][$input_var] = $INPUT->get->str($input_var);
187213f4618SMichael Große            }
188213f4618SMichael Große        }
189213f4618SMichael Große
190213f4618SMichael Große        if (is_array($INPUT->post->param('do'))) {
191213f4618SMichael Große            $doPost = key($INPUT->post->arr('do'));
192213f4618SMichael Große        } else {
193213f4618SMichael Große            $doPost = $INPUT->post->str('do');
194213f4618SMichael Große        }
195213f4618SMichael Große        $doGet = $INPUT->get->str('do');
196213f4618SMichael Große        if (!empty($doPost)) {
197213f4618SMichael Große            $_SESSION[DOKU_COOKIE]['oauth-done']['do'] = $doPost;
198213f4618SMichael Große        } elseif (!empty($doGet)) {
199213f4618SMichael Große            $_SESSION[DOKU_COOKIE]['oauth-done']['do'] = $doGet;
200213f4618SMichael Große        }
201213f4618SMichael Große
202213f4618SMichael Große        session_write_close();
203213f4618SMichael Große
204213f4618SMichael Große        $service->login();
205213f4618SMichael Große    }
206213f4618SMichael Große
207a7a8f46aSAndreas Gohr    /**
208a7a8f46aSAndreas Gohr     * @param array  $data
209a7a8f46aSAndreas Gohr     * @param string $service
210a7a8f46aSAndreas Gohr     */
211a7a8f46aSAndreas Gohr    protected function setUserSession($data, $service) {
212a7a8f46aSAndreas Gohr        global $USERINFO;
213a7a8f46aSAndreas Gohr        global $conf;
214a7a8f46aSAndreas Gohr
215a7a8f46aSAndreas Gohr        // set up groups
216a7a8f46aSAndreas Gohr        if(!is_array($data['grps'])) {
217a7a8f46aSAndreas Gohr            $data['grps'] = array();
218a7a8f46aSAndreas Gohr        }
219a7a8f46aSAndreas Gohr        $data['grps'][] = $this->cleanGroup($service);
220f866280eSAndreas Gohr        $data['grps']   = array_unique($data['grps']);
22180852c15SAndreas Gohr
222f10e09e2SAndreas Gohr        $USERINFO                               = $data;
223f10e09e2SAndreas Gohr        $_SERVER['REMOTE_USER']                 = $data['user'];
224f10e09e2SAndreas Gohr        $_SESSION[DOKU_COOKIE]['auth']['user']  = $data['user'];
225f10e09e2SAndreas Gohr        $_SESSION[DOKU_COOKIE]['auth']['pass']  = $data['pass'];
226f10e09e2SAndreas Gohr        $_SESSION[DOKU_COOKIE]['auth']['info']  = $USERINFO;
227a7a8f46aSAndreas Gohr        $_SESSION[DOKU_COOKIE]['auth']['buid']  = auth_browseruid();
228a7a8f46aSAndreas Gohr        $_SESSION[DOKU_COOKIE]['auth']['time']  = time();
229a7a8f46aSAndreas Gohr        $_SESSION[DOKU_COOKIE]['auth']['oauth'] = $service;
23080852c15SAndreas Gohr    }
23180852c15SAndreas Gohr
232f866280eSAndreas Gohr    /**
233e32c3607SAndreas Gohr     * Unset additional stuff in session on logout
234e32c3607SAndreas Gohr     */
235e32c3607SAndreas Gohr    public function logOff() {
236e32c3607SAndreas Gohr        parent::logOff();
237e32c3607SAndreas Gohr
238e32c3607SAndreas Gohr        if(isset($_SESSION[DOKU_COOKIE]['auth']['buid'])) {
239e32c3607SAndreas Gohr            unset($_SESSION[DOKU_COOKIE]['auth']['buid']);
240e32c3607SAndreas Gohr        }
241e32c3607SAndreas Gohr        if(isset($_SESSION[DOKU_COOKIE]['auth']['time'])) {
242e32c3607SAndreas Gohr            unset($_SESSION[DOKU_COOKIE]['auth']['time']);
243e32c3607SAndreas Gohr        }
244e32c3607SAndreas Gohr        if(isset($_SESSION[DOKU_COOKIE]['auth']['oauth'])) {
245e32c3607SAndreas Gohr            unset($_SESSION[DOKU_COOKIE]['auth']['oauth']);
246e32c3607SAndreas Gohr        }
247e32c3607SAndreas Gohr    }
248e32c3607SAndreas Gohr
249e32c3607SAndreas Gohr    /**
250f866280eSAndreas Gohr     * Find a user by his email address
251f866280eSAndreas Gohr     *
252f866280eSAndreas Gohr     * @param $mail
253f866280eSAndreas Gohr     * @return bool|string
254f866280eSAndreas Gohr     */
25538378fbbSAndreas Gohr    protected function getUserByEmail($mail) {
256f866280eSAndreas Gohr        if($this->users === null) $this->_loadUserData();
25738378fbbSAndreas Gohr        $mail = strtolower($mail);
258f866280eSAndreas Gohr
259f866280eSAndreas Gohr        foreach($this->users as $user => $uinfo) {
260f866280eSAndreas Gohr            if(strtolower($uinfo['mail']) == $mail) return $user;
26138378fbbSAndreas Gohr        }
26238378fbbSAndreas Gohr
263f866280eSAndreas Gohr        return false;
264f866280eSAndreas Gohr    }
26538378fbbSAndreas Gohr
266f866280eSAndreas Gohr    /**
267f866280eSAndreas Gohr     * Enhance function to check aainst duplicate emails
268f866280eSAndreas Gohr     *
269f866280eSAndreas Gohr     * @param string $user
270f866280eSAndreas Gohr     * @param string $pwd
271f866280eSAndreas Gohr     * @param string $name
272f866280eSAndreas Gohr     * @param string $mail
273f866280eSAndreas Gohr     * @param null   $grps
274f866280eSAndreas Gohr     * @return bool|null|string
275f866280eSAndreas Gohr     */
276f866280eSAndreas Gohr    public function createUser($user, $pwd, $name, $mail, $grps = null) {
277f866280eSAndreas Gohr        if($this->getUserByEmail($mail)) {
278f866280eSAndreas Gohr            msg($this->getLang('emailduplicate'), -1);
279f866280eSAndreas Gohr            return false;
280f866280eSAndreas Gohr        }
281f866280eSAndreas Gohr
28238378fbbSAndreas Gohr        return parent::createUser($user, $pwd, $name, $mail, $grps);
28338378fbbSAndreas Gohr    }
28438378fbbSAndreas Gohr
285f866280eSAndreas Gohr    /**
286f866280eSAndreas Gohr     * Enhance function to check aainst duplicate emails
287f866280eSAndreas Gohr     *
288f866280eSAndreas Gohr     * @param string $user
289f866280eSAndreas Gohr     * @param array  $changes
290f866280eSAndreas Gohr     * @return bool
291f866280eSAndreas Gohr     */
29238378fbbSAndreas Gohr    public function modifyUser($user, $changes) {
2933c0138dbSAndreas Gohr        global $conf;
2943c0138dbSAndreas Gohr
2953c0138dbSAndreas Gohr        if(isset($changes['mail'])) {
2963c0138dbSAndreas Gohr            $found = $this->getUserByEmail($changes['mail']);
2973c0138dbSAndreas Gohr            if($found != $user) {
298f866280eSAndreas Gohr                msg($this->getLang('emailduplicate'), -1);
299f866280eSAndreas Gohr                return false;
300f866280eSAndreas Gohr            }
3013c0138dbSAndreas Gohr        }
30238378fbbSAndreas Gohr
3033c0138dbSAndreas Gohr        $ok = parent::modifyUser($user, $changes);
3043c0138dbSAndreas Gohr
3053c0138dbSAndreas Gohr        // refresh session cache
3063c0138dbSAndreas Gohr        touch($conf['cachedir'] . '/sessionpurge');
3073c0138dbSAndreas Gohr
3083c0138dbSAndreas Gohr        return $ok;
30938378fbbSAndreas Gohr    }
31038378fbbSAndreas Gohr
31180852c15SAndreas Gohr}
31280852c15SAndreas Gohr
31380852c15SAndreas Gohr// vim:ts=4:sw=4:et:
314