xref: /plugin/oauth/helper.php (revision 67e2b52dad11b975d8e5fd1dd930444f244aa7c4)
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        $rdurl = wl($id, array('oa' => $servicename), true, '&');
37        dbglog($rdurl);
38        $service = new $class($rdurl);
39        if(!$service->isInitialized()) {
40            msg("Failed to initialize $service authentication service. Check credentials", -1);
41            return null;
42        }
43
44        return $service;
45    }
46
47    /**
48     * List available Services
49     *
50     * @return array
51     */
52    public function listServices() {
53        $services = array();
54        $files    = glob(__DIR__.'/classes/*Adapter.php');
55
56        foreach($files as $file) {
57            $file = basename($file, 'Adapter.php');
58            if($file == 'Abstract') continue;
59            $services[] = $file;
60        }
61
62        return $services;
63    }
64
65    /**
66     * Return the configured key for the given service
67     *
68     * @param $service
69     * @return string
70     */
71    public function getKey($service) {
72        $service = strtolower($service);
73        return $this->getConf($service.'-key');
74    }
75
76    /**
77     * Return the configured secret for the given service
78     *
79     * @param $service
80     * @return string
81     */
82    public function getSecret($service) {
83        $service = strtolower($service);
84        return $this->getConf($service.'-secret');
85    }
86
87}
88
89// vim:ts=4:sw=4:et:
90