xref: /plugin/oauth/helper.php (revision f2e164b0a85700a431df521494ba9906d2482c87)
180852c15SAndreas Gohr<?php
280852c15SAndreas Gohr/**
380852c15SAndreas Gohr * DokuWiki Plugin oauth (Helper 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
1280852c15SAndreas Gohrclass helper_plugin_oauth extends DokuWiki_Plugin {
1380852c15SAndreas Gohr
1480852c15SAndreas Gohr    /**
15f10e09e2SAndreas Gohr     * Load the needed libraries and initialize the named oAuth service
1680852c15SAndreas Gohr     *
17f10e09e2SAndreas Gohr     * @param string $servicename
1863b91737SAndreas Gohr     * @return null|\OAuth\Plugin\AbstractAdapter
1980852c15SAndreas Gohr     */
20a7a8f46aSAndreas Gohr    public function loadService(&$servicename) {
2167e2b52dSAndreas Gohr        $id = getID(); // $ID isn't set in trustExternal, yet
22f10e09e2SAndreas Gohr
23f10e09e2SAndreas Gohr        $servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename);
24f10e09e2SAndreas Gohr        if(!$servicename) return null;
25f10e09e2SAndreas Gohr
26f10e09e2SAndreas Gohr        require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
2763b91737SAndreas Gohr        require_once(__DIR__.'/classes/AbstractAdapter.php');
28f10e09e2SAndreas Gohr        require_once(__DIR__.'/classes/oAuthHTTPClient.php');
29551dc731SAndreas Gohr        require_once(__DIR__.'/classes/oAuthStorage.php');
30f10e09e2SAndreas Gohr
3163b91737SAndreas Gohr        $file = __DIR__.'/classes/'.$servicename.'Adapter.php';
32f10e09e2SAndreas Gohr        if(!file_exists($file)) return null;
33f10e09e2SAndreas Gohr        require_once($file);
3463b91737SAndreas Gohr        $class = '\\OAuth\\Plugin\\'.$servicename.'Adapter';
35f10e09e2SAndreas Gohr
3663b91737SAndreas Gohr        /** @var \OAuth\Plugin\AbstractAdapter $service */
372e94f0b8SAndreas Gohr        $service = new $class($this->redirectURI());
38f10e09e2SAndreas Gohr        if(!$service->isInitialized()) {
39f10e09e2SAndreas Gohr            msg("Failed to initialize $service authentication service. Check credentials", -1);
40f10e09e2SAndreas Gohr            return null;
41f10e09e2SAndreas Gohr        }
42f10e09e2SAndreas Gohr
43a90c044eSAndreas Gohr        // The generic service can be externally configured
44a90c044eSAndreas Gohr        if(is_a($service->oAuth, 'OAuth\\OAuth2\\Service\\Generic')) {
45a90c044eSAndreas Gohr            $service->oAuth->setAuthorizationEndpoint($this->getAuthEndpoint($servicename));
46a90c044eSAndreas Gohr            $service->oAuth->setAccessTokenEndpoint($this->getTokenEndpoint($servicename));
47a90c044eSAndreas Gohr        }
48a90c044eSAndreas Gohr
49f10e09e2SAndreas Gohr        return $service;
50f10e09e2SAndreas Gohr    }
51f10e09e2SAndreas Gohr
52a90c044eSAndreas Gohr    /**
53a90c044eSAndreas Gohr     * The redirect URI used in all oAuth requests
54a90c044eSAndreas Gohr     *
55a90c044eSAndreas Gohr     * @return string
56a90c044eSAndreas Gohr     */
572e94f0b8SAndreas Gohr    public function redirectURI() {
589683193cSMichael Große        if ($this->getConf('custom-redirectURI') !== '') {
599683193cSMichael Große            return $this->getConf('custom-redirectURI');
609683193cSMichael Große        } else {
612e94f0b8SAndreas Gohr            return DOKU_URL . DOKU_SCRIPT;
622e94f0b8SAndreas Gohr        }
639683193cSMichael Große    }
642e94f0b8SAndreas Gohr
65dfbdd519SAndreas Gohr    /**
66dfbdd519SAndreas Gohr     * List available Services
67dfbdd519SAndreas Gohr     *
683c0138dbSAndreas Gohr     * @param bool $enabledonly list only enabled services
69dfbdd519SAndreas Gohr     * @return array
70dfbdd519SAndreas Gohr     */
713c0138dbSAndreas Gohr    public function listServices($enabledonly = true) {
72dfbdd519SAndreas Gohr        $services = array();
7363b91737SAndreas Gohr        $files    = glob(__DIR__.'/classes/*Adapter.php');
74dfbdd519SAndreas Gohr
75dfbdd519SAndreas Gohr        foreach($files as $file) {
7663b91737SAndreas Gohr            $file = basename($file, 'Adapter.php');
77dfbdd519SAndreas Gohr            if($file == 'Abstract') continue;
783c0138dbSAndreas Gohr            if($enabledonly && !$this->getKey($file)) continue;
79dfbdd519SAndreas Gohr            $services[] = $file;
80dfbdd519SAndreas Gohr        }
81dfbdd519SAndreas Gohr
82dfbdd519SAndreas Gohr        return $services;
83dfbdd519SAndreas Gohr    }
84f10e09e2SAndreas Gohr
85f10e09e2SAndreas Gohr    /**
86f10e09e2SAndreas Gohr     * Return the configured key for the given service
87f10e09e2SAndreas Gohr     *
88f10e09e2SAndreas Gohr     * @param $service
89f10e09e2SAndreas Gohr     * @return string
90f10e09e2SAndreas Gohr     */
91f10e09e2SAndreas Gohr    public function getKey($service) {
92f10e09e2SAndreas Gohr        $service = strtolower($service);
93f10e09e2SAndreas Gohr        return $this->getConf($service.'-key');
94f10e09e2SAndreas Gohr    }
95f10e09e2SAndreas Gohr
96f10e09e2SAndreas Gohr    /**
97f10e09e2SAndreas Gohr     * Return the configured secret for the given service
98f10e09e2SAndreas Gohr     *
99f10e09e2SAndreas Gohr     * @param $service
100f10e09e2SAndreas Gohr     * @return string
101f10e09e2SAndreas Gohr     */
102f10e09e2SAndreas Gohr    public function getSecret($service) {
103f10e09e2SAndreas Gohr        $service = strtolower($service);
104f10e09e2SAndreas Gohr        return $this->getConf($service.'-secret');
10580852c15SAndreas Gohr    }
10680852c15SAndreas Gohr
107a90c044eSAndreas Gohr    /**
108a90c044eSAndreas Gohr     * Return the configured Authentication Endpoint URL for the given service
109a90c044eSAndreas Gohr     *
110a90c044eSAndreas Gohr     * @param $service
111a90c044eSAndreas Gohr     * @return string
112a90c044eSAndreas Gohr     */
113a90c044eSAndreas Gohr    public function getAuthEndpoint($service) {
114a90c044eSAndreas Gohr        $service = strtolower($service);
115a90c044eSAndreas Gohr        return $this->getConf($service.'-authurl');
116a90c044eSAndreas Gohr    }
117a90c044eSAndreas Gohr
118a90c044eSAndreas Gohr    /**
119a90c044eSAndreas Gohr     * Return the configured Access Token Endpoint URL for the given service
120a90c044eSAndreas Gohr     *
121a90c044eSAndreas Gohr     * @param $service
122a90c044eSAndreas Gohr     * @return string
123a90c044eSAndreas Gohr     */
124a90c044eSAndreas Gohr    public function getTokenEndpoint($service) {
125a90c044eSAndreas Gohr        $service = strtolower($service);
126a90c044eSAndreas Gohr        return $this->getConf($service.'-tokenurl');
127a90c044eSAndreas Gohr    }
128d9818adbSMichael Große
129d9818adbSMichael Große    /**
130ebf681d1SMichael Große     * @return array
131d9818adbSMichael Große     */
132ebf681d1SMichael Große    public function getValidDomains() {
1333c0c1b14SMichael Große        if ($this->getConf('mailRestriction') === '') {
1343c0c1b14SMichael Große            return array();
1353c0c1b14SMichael Große        }
136d9818adbSMichael Große        $validDomains = explode(',', trim($this->getConf('mailRestriction'), ','));
137ebf681d1SMichael Große        $validDomains = array_map('trim', $validDomains);
138d9818adbSMichael Große        return $validDomains;
139d9818adbSMichael Große    }
140d9818adbSMichael Große
141d9818adbSMichael Große    /**
142d9818adbSMichael Große     * @param string $mail
143d9818adbSMichael Große     *
144d9818adbSMichael Große     * @return bool
145d9818adbSMichael Große     */
146d9818adbSMichael Große    public function checkMail($mail) {
147d9818adbSMichael Große        $hostedDomains = $this->getValidDomains();
148d9818adbSMichael Große
149d9818adbSMichael Große        foreach ($hostedDomains as $validDomain) {
150d9818adbSMichael Große            if(substr($mail, -strlen($validDomain)) === $validDomain) {
151d9818adbSMichael Große                return true;
152d9818adbSMichael Große            }
153d9818adbSMichael Große        }
154d9818adbSMichael Große        return false;
155d9818adbSMichael Große    }
156*f2e164b0SMichael Große
157*f2e164b0SMichael Große    /**
158*f2e164b0SMichael Große     * @param array $session cookie auth session
159*f2e164b0SMichael Große     *
160*f2e164b0SMichael Große     * @return bool
161*f2e164b0SMichael Große     */
162*f2e164b0SMichael Große    public function validBrowserID ($session) {
163*f2e164b0SMichael Große        return $session['buid'] == auth_browseruid();
164*f2e164b0SMichael Große    }
165*f2e164b0SMichael Große
166*f2e164b0SMichael Große    /**
167*f2e164b0SMichael Große     * @param array $session cookie auth session
168*f2e164b0SMichael Große     *
169*f2e164b0SMichael Große     * @return bool
170*f2e164b0SMichael Große     */
171*f2e164b0SMichael Große    public function isSessionTimedOut ($session) {
172*f2e164b0SMichael Große        global $conf;
173*f2e164b0SMichael Große        return $session['time'] < time() - $conf['auth_security_timeout'];
174*f2e164b0SMichael Große    }
175*f2e164b0SMichael Große
176*f2e164b0SMichael Große    /**
177*f2e164b0SMichael Große     * @return bool
178*f2e164b0SMichael Große     */
179*f2e164b0SMichael Große    public function isGETRequest () {
180*f2e164b0SMichael Große        global $INPUT;
181*f2e164b0SMichael Große        $result = $INPUT->server->str('REQUEST_METHOD') === 'GET';
182*f2e164b0SMichael Große        return $result;
183*f2e164b0SMichael Große    }
184*f2e164b0SMichael Große
185*f2e164b0SMichael Große    /**
186*f2e164b0SMichael Große     * check if we are handling a request to doku.php. Only doku.php defines $updateVersion
187*f2e164b0SMichael Große     *
188*f2e164b0SMichael Große     * @return bool
189*f2e164b0SMichael Große     */
190*f2e164b0SMichael Große    public function isDokuPHP() {
191*f2e164b0SMichael Große        global $updateVersion;
192*f2e164b0SMichael Große        return isset($updateVersion);
193*f2e164b0SMichael Große    }
19480852c15SAndreas Gohr}
19580852c15SAndreas Gohr
19680852c15SAndreas Gohr// vim:ts=4:sw=4:et:
197