xref: /plugin/oauth/auth.php (revision f10e09e2973ae496de9f18a40de02b630286307d)
1<?php
2/**
3 * DokuWiki Plugin oauth (Auth Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class auth_plugin_oauth extends auth_plugin_authplain {
13
14    public function __construct() {
15        parent::__construct();
16
17
18        $this->cando['external'] = true;
19    }
20
21
22    function trustExternal($user, $pass, $sticky = false) {
23	    global $USERINFO, $ID, $INPUT;
24
25        // get form login info
26        if(!empty($user)){
27            return auth_login($user, $pass, $sticky);
28        }
29
30        if($INPUT->has('oa')) {
31            /** @var helper_plugin_oauth $hlp */
32            $hlp = plugin_load('helper', 'oauth');
33            $service = $hlp->loadService($INPUT->str('oa'));
34            if(is_null($service)) return false;
35
36
37
38            if($service->checkToken()) {
39                $uinfo = $service->getUser();
40                $this->setUserSession($uinfo);
41                return true;
42            }
43        }
44
45        return false;
46    }
47
48
49    protected function setUserSession($data) {
50        global $USERINFO;
51
52        // reopen session
53        session_start();
54
55        $USERINFO = $data;
56        $_SERVER['REMOTE_USER'] = $data['user'];
57        $_SESSION[DOKU_COOKIE]['auth']['user'] = $data['user'];
58        $_SESSION[DOKU_COOKIE]['auth']['pass'] = $data['pass'];
59        $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
60
61        // close session again
62        session_write_close();
63    }
64
65}
66
67// vim:ts=4:sw=4:et: