xref: /plugin/oauth/helper.php (revision 2e94f0b8bfa63adcf4d6ecc092b35a59dde77be3)
1<?php
2/**
3 * DokuWiki Plugin oauth (Helper 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 helper_plugin_oauth extends DokuWiki_Plugin {
13
14    /**
15     * Load the needed libraries and initialize the named oAuth service
16     *
17     * @param string $servicename
18     * @return null|\OAuth\Plugin\AbstractAdapter
19     */
20    public function loadService(&$servicename) {
21        $id = getID(); // $ID isn't set in trustExternal, yet
22
23        $servicename = preg_replace('/[^a-zA-Z_]+/', '', $servicename);
24        if(!$servicename) return null;
25
26        require_once(__DIR__.'/phpoauthlib/src/OAuth/bootstrap.php');
27        require_once(__DIR__.'/classes/AbstractAdapter.php');
28        require_once(__DIR__.'/classes/oAuthHTTPClient.php');
29
30        $file = __DIR__.'/classes/'.$servicename.'Adapter.php';
31        if(!file_exists($file)) return null;
32        require_once($file);
33        $class = '\\OAuth\\Plugin\\'.$servicename.'Adapter';
34
35        /** @var \OAuth\Plugin\AbstractAdapter $service */
36        $service = new $class($this->redirectURI());
37        if(!$service->isInitialized()) {
38            msg("Failed to initialize $service authentication service. Check credentials", -1);
39            return null;
40        }
41
42        return $service;
43    }
44
45    public function redirectURI() {
46        return DOKU_URL.DOKU_SCRIPT;
47    }
48
49    /**
50     * List available Services
51     *
52     * @param bool $enabledonly list only enabled services
53     * @return array
54     */
55    public function listServices($enabledonly = true) {
56        $services = array();
57        $files    = glob(__DIR__.'/classes/*Adapter.php');
58
59        foreach($files as $file) {
60            $file = basename($file, 'Adapter.php');
61            if($file == 'Abstract') continue;
62            if($enabledonly && !$this->getKey($file)) continue;
63            $services[] = $file;
64        }
65
66        return $services;
67    }
68
69    /**
70     * Return the configured key for the given service
71     *
72     * @param $service
73     * @return string
74     */
75    public function getKey($service) {
76        $service = strtolower($service);
77        return $this->getConf($service.'-key');
78    }
79
80    /**
81     * Return the configured secret for the given service
82     *
83     * @param $service
84     * @return string
85     */
86    public function getSecret($service) {
87        $service = strtolower($service);
88        return $this->getConf($service.'-secret');
89    }
90
91}
92
93// vim:ts=4:sw=4:et:
94