xref: /plugin/oauth/helper.php (revision a90c044eb2c6bb1ec2b753d87f55d9eff96705ca)
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');
29f10e09e2SAndreas Gohr
3063b91737SAndreas Gohr        $file = __DIR__.'/classes/'.$servicename.'Adapter.php';
31f10e09e2SAndreas Gohr        if(!file_exists($file)) return null;
32f10e09e2SAndreas Gohr        require_once($file);
3363b91737SAndreas Gohr        $class = '\\OAuth\\Plugin\\'.$servicename.'Adapter';
34f10e09e2SAndreas Gohr
3563b91737SAndreas Gohr        /** @var \OAuth\Plugin\AbstractAdapter $service */
362e94f0b8SAndreas Gohr        $service = new $class($this->redirectURI());
37f10e09e2SAndreas Gohr        if(!$service->isInitialized()) {
38f10e09e2SAndreas Gohr            msg("Failed to initialize $service authentication service. Check credentials", -1);
39f10e09e2SAndreas Gohr            return null;
40f10e09e2SAndreas Gohr        }
41f10e09e2SAndreas Gohr
42*a90c044eSAndreas Gohr        // The generic service can be externally configured
43*a90c044eSAndreas Gohr        if(is_a($service->oAuth, 'OAuth\\OAuth2\\Service\\Generic')) {
44*a90c044eSAndreas Gohr            $service->oAuth->setAuthorizationEndpoint($this->getAuthEndpoint($servicename));
45*a90c044eSAndreas Gohr            $service->oAuth->setAccessTokenEndpoint($this->getTokenEndpoint($servicename));
46*a90c044eSAndreas Gohr        }
47*a90c044eSAndreas Gohr
48f10e09e2SAndreas Gohr        return $service;
49f10e09e2SAndreas Gohr    }
50f10e09e2SAndreas Gohr
51*a90c044eSAndreas Gohr    /**
52*a90c044eSAndreas Gohr     * The redirect URI used in all oAuth requests
53*a90c044eSAndreas Gohr     *
54*a90c044eSAndreas Gohr     * @return string
55*a90c044eSAndreas Gohr     */
562e94f0b8SAndreas Gohr    public function redirectURI() {
572e94f0b8SAndreas Gohr        return DOKU_URL.DOKU_SCRIPT;
582e94f0b8SAndreas Gohr    }
592e94f0b8SAndreas Gohr
60dfbdd519SAndreas Gohr    /**
61dfbdd519SAndreas Gohr     * List available Services
62dfbdd519SAndreas Gohr     *
633c0138dbSAndreas Gohr     * @param bool $enabledonly list only enabled services
64dfbdd519SAndreas Gohr     * @return array
65dfbdd519SAndreas Gohr     */
663c0138dbSAndreas Gohr    public function listServices($enabledonly = true) {
67dfbdd519SAndreas Gohr        $services = array();
6863b91737SAndreas Gohr        $files    = glob(__DIR__.'/classes/*Adapter.php');
69dfbdd519SAndreas Gohr
70dfbdd519SAndreas Gohr        foreach($files as $file) {
7163b91737SAndreas Gohr            $file = basename($file, 'Adapter.php');
72dfbdd519SAndreas Gohr            if($file == 'Abstract') continue;
733c0138dbSAndreas Gohr            if($enabledonly && !$this->getKey($file)) continue;
74dfbdd519SAndreas Gohr            $services[] = $file;
75dfbdd519SAndreas Gohr        }
76dfbdd519SAndreas Gohr
77dfbdd519SAndreas Gohr        return $services;
78dfbdd519SAndreas Gohr    }
79f10e09e2SAndreas Gohr
80f10e09e2SAndreas Gohr    /**
81f10e09e2SAndreas Gohr     * Return the configured key for the given service
82f10e09e2SAndreas Gohr     *
83f10e09e2SAndreas Gohr     * @param $service
84f10e09e2SAndreas Gohr     * @return string
85f10e09e2SAndreas Gohr     */
86f10e09e2SAndreas Gohr    public function getKey($service) {
87f10e09e2SAndreas Gohr        $service = strtolower($service);
88f10e09e2SAndreas Gohr        return $this->getConf($service.'-key');
89f10e09e2SAndreas Gohr    }
90f10e09e2SAndreas Gohr
91f10e09e2SAndreas Gohr    /**
92f10e09e2SAndreas Gohr     * Return the configured secret for the given service
93f10e09e2SAndreas Gohr     *
94f10e09e2SAndreas Gohr     * @param $service
95f10e09e2SAndreas Gohr     * @return string
96f10e09e2SAndreas Gohr     */
97f10e09e2SAndreas Gohr    public function getSecret($service) {
98f10e09e2SAndreas Gohr        $service = strtolower($service);
99f10e09e2SAndreas Gohr        return $this->getConf($service.'-secret');
10080852c15SAndreas Gohr    }
10180852c15SAndreas Gohr
102*a90c044eSAndreas Gohr    /**
103*a90c044eSAndreas Gohr     * Return the configured Authentication Endpoint URL for the given service
104*a90c044eSAndreas Gohr     *
105*a90c044eSAndreas Gohr     * @param $service
106*a90c044eSAndreas Gohr     * @return string
107*a90c044eSAndreas Gohr     */
108*a90c044eSAndreas Gohr    public function getAuthEndpoint($service) {
109*a90c044eSAndreas Gohr        $service = strtolower($service);
110*a90c044eSAndreas Gohr        return $this->getConf($service.'-authurl');
111*a90c044eSAndreas Gohr    }
112*a90c044eSAndreas Gohr
113*a90c044eSAndreas Gohr    /**
114*a90c044eSAndreas Gohr     * Return the configured Access Token Endpoint URL for the given service
115*a90c044eSAndreas Gohr     *
116*a90c044eSAndreas Gohr     * @param $service
117*a90c044eSAndreas Gohr     * @return string
118*a90c044eSAndreas Gohr     */
119*a90c044eSAndreas Gohr    public function getTokenEndpoint($service) {
120*a90c044eSAndreas Gohr        $service = strtolower($service);
121*a90c044eSAndreas Gohr        return $this->getConf($service.'-tokenurl');
122*a90c044eSAndreas Gohr    }
12380852c15SAndreas Gohr}
12480852c15SAndreas Gohr
12580852c15SAndreas Gohr// vim:ts=4:sw=4:et:
126